Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/python # -*- coding: utf-8 -*-
""" Format the source as an appropriate wiki link.
Requires the target page to be on the same wiki unless it is a sparql harvest. Links to Wikidata are always prefixed.
@param source: the source value from the SQL table @param harvest_type: the type of harvest from which the source was extracted. E.g. "sparql" @param label: Optional label to use for the link """ except ValueError: raise
""" Retrieve the wikipage and site from a page or entity url. """ '|'.join(supported_sites))
""" Retrieve the wikipage and site from the source field.
Note that the returned site tuple may not be a valid pywikibot site. E.g. commons is ('wikimedia', 'commons') rather than ('commons', 'commons').
@param source: the source value from the SQL table @param harvest_type: the type of harvest from which the source was extracted, e.g. "sparql". """ except AttributeError: raise ValueError( 'Could not find source list ({0})'.format(source)) else: '|'.join(supported_sites)) except AttributeError: raise ValueError( 'Could not find source list ({0})'.format(source))
""" Save the content to the page on a given site or store it locally.
Whether the pages are outputted locally (and where to) is controlled by the HERITAGE_LOCAL_WRITE_PATH environment variable.
@param page: the pywikibot.Page to which the content should be written @param content: the content to store @param summary: the edit summary to save the content with @param minorEdit: if the edit should be marked as minor (defaults to True) """ pywikibot.warning( 'Could not save page {0} because it is not a Page ' 'instance.'.format(page))
except (OtherPageSaveError, PageSaveRelatedError): pywikibot.warning( 'Could not save page {0} ({1})'.format(page, summary)) else: bytes(local_path, encoding='utf-8'), page_to_filename(page))
"""Return the template link, as it would appear on the source site.""" template_site = pywikibot.Site(lang, project) template_page = pywikibot.Page( template_site, 'Template:{0}'.format(template_name)) return template_page.title( as_link=True, with_ns=False, insite=source_site)
""" Create a standardised filename for a page.
The name takes the form [site][namespace]pagename.wiki where '/', ':' and " " has been replaced by '_'. Namespace 0 is given as just '_'.
@param page: the pywikibot.Page for which to generate a filename. """ site=page.site, ns=namespace_str, page=pagename_str)
"""Invert and convert cursor of a query requesting title and sort key.
This assumes a request to the categorylinks table where only two values are selected for and where cl_sortkey or cl_sortkey_prefix is the second one.
As cl_sortkey and cl_sortkey_prefix may contain partial characters, any characters which cannot be decoded must be replaced.
@param cursor: the pymysql.connect.cursor for the executed query @return: dict """ result = {} while True: try: row = cursor.fetchone() (page_title, sort_key) = row if not isinstance(sort_key, str): sort_key = str(sort_key, encoding='utf-8', errors='replace') if not isinstance(page_title, str): page_title = str(page_title, encoding='utf-8') result[sort_key] = page_title except TypeError: break
return result
""" Attempt to get a monument id from a category sort key.
Candidate ids are compared to a list of known ids.
@param sort_key: a category sort key or sort key prefix @param known_ids: a list of known ids, or a dict where the keys are known ids. @return: unicode|None """ return None
# ensure there are no remaining encoding issues sort_key = str(sort_key, encoding='utf-8', errors='replace') # Just want the first line # Remove leading and trailing spaces
# Now try some variants until we have a hit
# Only remove leading zero's if we don't have a hit. # Only remove leading underscores if we don't have a hit. # Only all uppercase if we don't have a hit.
# Return None if no match has been found
""" A wikitext header embedding a local subpage or linking to central one.
@param central_page: the page name, including interwiki prefix @param subpage: the name of the subpage to embed, if it exists. Defaults to 'header'. """ # percentage formatting to avoid having to escape all curly brackets data = {'central_page': central_page, 'subpage': subpage} return ( '{{#ifexist:{{FULLPAGENAME}}/%(subpage)s' '|{{/%(subpage)s}}' '|For information on how to use this report and how to localise ' 'these instructions visit ' '[[%(central_page)s]]. }}\n' % data)
anchor='#How_can_I_localise_the_instructions?'): """ A wikitext message describing that no outstanding tasks are left.
The message transcludes a local subpage if one exists or displays a default text with a link to information on how to translate it.
@param central_page: the page name, including interwiki prefix and anchor @param type: the type of results the page would have displayed @param subpage: the name of the subpage to embed, if it exists. Defaults to 'done'. @param anchor: a particular anchor on the central page to point the link to, including hash symbol. Defaults to '#How_can_I_localise_the_instructions?'. """ # percentage formatting to avoid having to escape all curly brackets data = {'central_page': central_page, 'type': type, 'subpage': subpage, 'anchor': anchor} return ( '\n{{#ifexist:{{FULLPAGENAME}}/%(subpage)s' '|{{/%(subpage)s}}' '|There are no %(type)s left. Great work! ' '<small>([[%(central_page)s%(anchor)s|{{int:translate}}]])</small>.' '}}\n' % data)
""" A wikitext table header row.
@param columns: OrderedDict of the desired columns in the format OrderedDict({name: is_numeric}). Where is_numeric indicates that the column shuld be sorted as numbers. """ else:
""" A wikitext table bottom row, where empty values are grey and others bold.
@param num_columns: the number of columns @param values: a dict with column numbers and their values. Numbering of columns starting from 0. """ else: |