From 89feddba98500962ae0a82be7235e345daee62f1 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 13 Sep 2014 19:13:13 -0500 Subject: [PATCH] Some minor tweaks, plus documentation for API parameters. --- app.fcgi | 6 +- copyvios/checker.py | 14 ++-- static/api.css | 9 ++- templates/api.mako | 148 +++++++++++++++++++++++++++++++++++++++++- templates/support/footer.mako | 2 +- 5 files changed, 170 insertions(+), 9 deletions(-) diff --git a/app.fcgi b/app.fcgi index 3327910..d78099d 100755 --- a/app.fcgi +++ b/app.fcgi @@ -87,8 +87,12 @@ def settings(): "default_lang": default.lang, "default_project": default.project} return render_template("settings.mako", **kwargs) -@app.route("/api.json") +@app.route("/api") def api(): + return render_template("api.mako", help=True) + +@app.route("/api.json") +def api_json(): if not request.args: return render_template("api.mako", help=True) diff --git a/copyvios/checker.py b/copyvios/checker.py index e961890..4869733 100644 --- a/copyvios/checker.py +++ b/copyvios/checker.py @@ -17,6 +17,9 @@ __all__ = ["do_check", "T_POSSIBLE", "T_SUSPECT"] T_POSSIBLE = 0.4 T_SUSPECT = 0.75 +def _coerce_bool(val): + return val and val not in ("0", "false") + def do_check(query=None): if not query: query = Query() @@ -32,7 +35,7 @@ def do_check(query=None): if query.submitted: query.site = get_site(query) if query.site: - _get_results(query, follow=query.noredirect is None) + _get_results(query, follow=not _coerce_bool(query.noredirect)) return query def _get_results(query, follow=True): @@ -59,14 +62,15 @@ def _get_results(query, follow=True): query.action = "compare" if query.url else "search" if query.action == "search": conn = get_cache_db() - use_engine = 0 if query.use_engine == "0" else 1 - use_links = 0 if query.use_links == "0" else 1 + use_engine = 0 if query.use_engine in ("0", "false") else 1 + use_links = 0 if query.use_links in ("0", "false") else 1 if not use_engine and not use_links: query.error = "no search method" return mode = "{0}:{1}:".format(use_engine, use_links) - if not query.nocache: - query.result = _get_cached_results(page, conn, mode, query.noskip) + if not _coerce_bool(query.nocache): + query.result = _get_cached_results( + page, conn, mode, _coerce_bool(query.noskip)) if not query.result: try: query.result = page.copyvio_check( diff --git a/static/api.css b/static/api.css index 2d6858d..a1079e5 100644 --- a/static/api.css +++ b/static/api.css @@ -1,3 +1,10 @@ -.code { +span.code { + font-family: monospace; +} + +table.parameters { +} + +table.parameters td:first-child { font-family: monospace; } diff --git a/templates/api.mako b/templates/api.mako index 589c8e6..e4a4140 100644 --- a/templates/api.mako +++ b/templates/api.mako @@ -1,3 +1,7 @@ +<%def name="walk_json(obj)"> + + ${obj | h} + @@ -8,13 +12,155 @@ % if help:
+

Copyvio Detector API

This is the first version of the API for Earwig's Copyvio Detector. It works, but some bugs might still need to be ironed out, so please report any if you see them.

+

Requests

+

The API responds to GET requests made to https://tools.wmflabs.org/copyvios/api.json. Parameters are described in the tables below:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Always
ParameterValuesRequired?Description
actioncompare, search, sitesYesThe API will do URL comparisons in compare mode, run full copyvio checks in search mode, and list all known site languages and projects in sites mode.
formatjson, jsonfmNo (default: json)The default output format is JSON. jsonfm mode produces the same output, but renders it as a formatted HTML document for debugging.
versionintegerNo (default: 1)Currently, the API only has one version. You can skip this parameter, but it is recommended to include it for forward compatibility.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
compare Mode
ParameterValuesRequired?Description
projectstringYesThe project name of the site the page lives on. Examples are wikipedia and wiktionary. A list of acceptable values can be retrieved using action=sites.
langstringYesThe language name of the site the page lives on. Examples are en and de. A list of acceptable values can be retrieved using action=sites.
titlestringYes (either title or oldid)The title of the page or article to make a comparison against. Namespace must be included if the page isn't in the mainspace.
oldidintegerYes (either title or oldid)The revision ID (also called oldid) of the page revision to make a comparison against. If both a title and oldid are given, the oldid will be used.
urlstringYesThe URL of the suspected violation source that will be compared to the page.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
search Mode
ParameterValuesRequired?Description
projectstringYesThe project name of the site the page lives on. Examples are wikipedia and wiktionary. A list of acceptable values can be retrieved using action=sites.
langstringYesThe language name of the site the page lives on. Examples are en and de. A list of acceptable values can be retrieved using action=sites.
titlestringYes (either title or oldid)The title of the page or article to make a check against. Namespace must be included if the page isn't in the mainspace.
oldidintegerYes (either title or oldid)The revision ID (also called oldid) of the page revision to make a check against. If both a title and oldid are given, the oldid will be used.
use_enginebooleanNo (default: true)Whether to use a search engine (Yahoo! BOSS) as a source of URLs to compare against the page.
use_linksbooleanNo (default: true)Whether to compare the page against external links found in its wikitext.
nocachebooleanNo (default: false)Whether to bypass search results cached from previous checks. It is recommended that you don't pass this option unless a user specifically asks for it.
noredirectbooleanNo (default: false)Whether to avoid following redirects if the given page is a redirect.
noskipbooleanNo (default: false)If a suspected source is found during a check to have a sufficiently high confidence value, the check will end prematurely, and other pending URLs will be skipped. Passing this option will prevent this behavior, resulting in complete (but more time-consuming) checks.
+

Responses

+

+

Example

+

% endif % if result:

You are using jsonfm output mode, which renders JSON data as a formatted HTML document. This is intended for testing and debugging only.

- + ${walk_json(result)}
% endif diff --git a/templates/support/footer.mako b/templates/support/footer.mako index b648ad7..275ac3f 100644 --- a/templates/support/footer.mako +++ b/templates/support/footer.mako @@ -2,7 +2,7 @@