Skip to content

Commit

Permalink
split query operations into functions (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
slaporte committed Jun 1, 2013
1 parent fbfa3ec commit 165b4cd
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 64 deletions.
2 changes: 1 addition & 1 deletion wapiti/operations/__init__.py
Expand Up @@ -15,7 +15,7 @@
import revisions
import templates
import user

import query_operations

for op in OperationMeta._all_ops:
globals()[op.__name__] = op
Expand Down
63 changes: 0 additions & 63 deletions wapiti/operations/misc.py
Expand Up @@ -105,67 +105,6 @@ def extract_results(self, query_resp):
ret.append(page_ident)
return ret


class GetQueryPage(QueryOperation):
field_prefix = 'qp'
input_field = SingleParam('page')
fields = [StaticParam('list', 'querypage')]
output_type = QueryPageInfo
known_qps = ['Ancientpages',
'BrokenRedirects',
'Deadendpages',
'Disambiguations',
'DoubleRedirects',
'Listredirects',
'Lonelypages',
'Longpages',
'Mostcategories',
'Mostimages',
'Mostinterwikis',
'Mostlinkedcategories',
'Mostlinkedtemplates',
'Mostlinked',
'Mostrevisions',
'Fewestrevisions',
'Shortpages',
'Uncategorizedcategories',
'Uncategorizedpages',
'Uncategorizedimages',
'Uncategorizedtemplates',
'Unusedcategories',
'Unusedimages',
'Wantedcategories',
'Wantedfiles',
'Wantedpages',
'Wantedtemplates',
# 'Unwatchedpages', # requires logging in
'Unusedtemplates',
'Withoutinterwiki']

def __init__(self, qp, *a, **kw):
if qp not in self.known_qps:
raise ValueError('Unrecognized query page: %r' % qp)
super(GetQueryPage, self).__init__(qp, *a, **kw)

def extract_results(self, query_resp):
ret = []
cached = query_resp['querypage'].get('cachedtimestamp')
name = query_resp['querypage'].get('name')
for p in query_resp['querypage']['results']:
page = QueryPageInfo(p['title'],
p['ns'],
p['value'],
name,
cached)
ret.append(page)
return ret

def prepare_params(self, **kw):
params = super(GetQueryPage, self).prepare_params(**kw)
if params.get('qpcontinue'):
params['qpoffset'] = params.pop('qpcontinue')
return params

'''
If we are completionists (for action=query)
Expand Down Expand Up @@ -212,8 +151,6 @@ def prepare_params(self, **kw):
Perform a full text search
* list=tags (tg) *
List change tags
* list=usercontribs (uc) *
Get all edits by a user
* list=users (us) *
Get information about a list of users
* list=abuselog (afl) *
Expand Down
14 changes: 14 additions & 0 deletions wapiti/operations/models.py
Expand Up @@ -477,3 +477,17 @@ def __init__(self, coord, page_ident=None):
else:
self.primary = False
return


class QueryPageInfo(object):
def __init__(self,
title,
ns,
value,
querypage,
cache):
self.title = title
self.ns = ns
self.value = value
self.querypage = querypage
self.cache = cache
181 changes: 181 additions & 0 deletions wapiti/operations/query_operations.py
@@ -0,0 +1,181 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from base import QueryOperation
from params import SingleParam, StaticParam

from models import PageInfo


class GetQueryPage(QueryOperation):
field_prefix = 'gqp'
input_field = SingleParam('page')
fields = [StaticParam('generator', 'querypage'),
StaticParam('prop', 'info'),
StaticParam('inprop', 'subjectid|talkid|protection')]
output_type = PageInfo

def extract_results(self, query_resp):
ret = []
for k, pid_dict in query_resp['pages'].iteritems():
try:
page = PageInfo.from_query(pid_dict,
source=self.source)
except ValueError:
# no page_id?
continue
ret.append(page)
return ret

def prepare_params(self, **kw):
params = super(GetQueryPage, self).prepare_params(**kw)
if params.get('gqpcontinue'):
params['gqpoffset'] = params.pop('ggqpcontinue')
return params


class GetAncientPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Ancientpages')]


class GetBrokenRedirects(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'BrokenRedirects')]


class GetDeadendPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Deadendpages')]


class GetDisambiguations(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Disambiguations')]


class GetDoubleRedirects(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Doulberedirects')]


class GetListRedirects(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Listredirects')]


class GetLonelyPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Lonelypages')]


class GetLongPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Longpages')]


class GetMostCategories(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostcategories')]


class GetMostImages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostimages')]


class GetMostInterwikiLinks(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostinterwikis')]


class GetMostLinkedCategories(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostlinkedcategories')]


class GetMostLinkedTemplates(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostlinkedtemplates')]


class GetMostLinked(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostlinked')]


class GetMostRevisions(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Mostrevisions')]


class GetFewestRevisions(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Fewestrevisions')]


class GetShortPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Shortpages')]


class GetUncategorizedCategories(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Uncategorizedcategories')]


class GetUncategorizedPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Uncategorizedpages')]


class GetUncategorizedImages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Uncategorizedimages')]


class GetUncategorizedTemplates(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Uncategorizedtemplates')]


class GetUnusedCategories(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Unusedcategories')]


class GetUnusedImages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Unusedimages')]


class GetWantedCategories(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Wantedcategories')]


class GetWantedFiles(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Wantedfiles')]


class GetWantedPages(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Wantedpages')]


class GetWantedTemplates(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Wantedtemplates')]


class GetUnusedTemplates(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Unusedtemplates')]


class GetWithoutInterwikiLinks(GetQueryPage):
input_field = None
fields = GetQueryPage.fields + [StaticParam('gqppage', 'Withoutinterwiki')]

# 'Unwatchedpages' requires being logged in

0 comments on commit 165b4cd

Please sign in to comment.