.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.mediawiki Namespace Reference

Functions

 request (query, params)
 
 response (resp)
 

Variables

logging logger .Logger
 
dict about
 
list categories = ['general']
 
bool paging = True
 
int number_of_results = 5
 
str search_type = 'nearmatch'
 
bool srenablerewrites = True
 
str srsort = 'relevance'
 
str srprop = 'sectiontitle|snippet|timestamp|categorysnippet'
 
str base_url = 'https://{language}.wikipedia.org/'
 
str api_path = 'w/api.php'
 
str timestamp_format = '%Y-%m-%dT%H:%M:%SZ'
 

Detailed Description

The MediaWiki engine is a *generic* engine to **query** Wikimedia wikis by
the `MediaWiki Action API`_.  For a `query action`_ all Wikimedia wikis have
endpoints that follow this pattern::

    https://{base_url}/w/api.php?action=query&list=search&format=json

.. note::

   In its actual state, this engine is implemented to parse JSON result
   (`format=json`_) from a search query (`list=search`_).  If you need other
   ``action`` and ``list`` types ask SearXNG developers to extend the
   implementation according to your needs.

.. _MediaWiki Action API: https://www.mediawiki.org/wiki/API:Main_page
.. _query action: https://www.mediawiki.org/w/api.php?action=help&modules=query
.. _`list=search`: https://www.mediawiki.org/w/api.php?action=help&modules=query%2Bsearch
.. _`format=json`: https://www.mediawiki.org/w/api.php?action=help&modules=json

Configuration
=============

Request:

- :py:obj:`base_url`
- :py:obj:`search_type`
- :py:obj:`srenablerewrites`
- :py:obj:`srsort`
- :py:obj:`srprop`

Implementations
===============

Function Documentation

◆ request()

searx.engines.mediawiki.request ( query,
params )

Definition at line 113 of file mediawiki.py.

113def request(query, params):
114
115 # write search-language back to params, required in response
116
117 if params['language'] == 'all':
118 params['language'] = 'en'
119 else:
120 params['language'] = params['language'].split('-')[0]
121
122 api_url = f"{base_url.rstrip('/')}/{api_path}?".format(language=params['language'])
123 offset = (params['pageno'] - 1) * number_of_results
124
125 args = {
126 'action': 'query',
127 'list': 'search',
128 'format': 'json',
129 'srsearch': query,
130 'sroffset': offset,
131 'srlimit': number_of_results,
132 'srwhat': search_type,
133 'srprop': srprop,
134 'srsort': srsort,
135 }
136 if srenablerewrites:
137 args['srenablerewrites'] = '1'
138
139 params['url'] = api_url + urlencode(args)
140 return params
141
142
143# get response from search-request

References searx.format.

◆ response()

searx.engines.mediawiki.response ( resp)

Definition at line 144 of file mediawiki.py.

144def response(resp):
145
146 results = []
147 search_results = resp.json()
148
149 # return empty array if there are no results
150 if not search_results.get('query', {}).get('search'):
151 return []
152
153 for result in search_results['query']['search']:
154
155 if result.get('snippet', '').startswith('#REDIRECT'):
156 continue
157
158 title = result['title']
159 sectiontitle = result.get('sectiontitle')
160 content = html_to_text(result.get('snippet', ''))
161 metadata = html_to_text(result.get('categorysnippet', ''))
162 timestamp = result.get('timestamp')
163
164 url = (
165 base_url.format(language=resp.search_params['language']) + 'wiki/' + quote(title.replace(' ', '_').encode())
166 )
167 if sectiontitle:
168 # in case of sectiontitle create a link to the section in the wiki page
169 url += '#' + quote(sectiontitle.replace(' ', '_').encode())
170 title += ' / ' + sectiontitle
171
172 item = {'url': url, 'title': title, 'content': content, 'metadata': metadata}
173
174 if timestamp:
175 item['publishedDate'] = datetime.strptime(timestamp, timestamp_format)
176
177 results.append(item)
178
179 # return results
180 return results

Variable Documentation

◆ about

dict searx.engines.mediawiki.about
Initial value:
1= {
2 "website": None,
3 "wikidata_id": None,
4 "official_api_documentation": 'https://www.mediawiki.org/w/api.php?action=help&modules=query',
5 "use_official_api": True,
6 "require_api_key": False,
7 "results": 'JSON',
8}

Definition at line 52 of file mediawiki.py.

◆ api_path

str searx.engines.mediawiki.api_path = 'w/api.php'

Definition at line 103 of file mediawiki.py.

◆ base_url

str searx.engines.mediawiki.base_url = 'https://{language}.wikipedia.org/'

Definition at line 96 of file mediawiki.py.

◆ categories

list searx.engines.mediawiki.categories = ['general']

Definition at line 62 of file mediawiki.py.

◆ logger

logging searx.engines.mediawiki.logger .Logger

Definition at line 47 of file mediawiki.py.

◆ number_of_results

int searx.engines.mediawiki.number_of_results = 5

Definition at line 64 of file mediawiki.py.

◆ paging

bool searx.engines.mediawiki.paging = True

Definition at line 63 of file mediawiki.py.

◆ search_type

str searx.engines.mediawiki.search_type = 'nearmatch'

Definition at line 66 of file mediawiki.py.

◆ srenablerewrites

bool searx.engines.mediawiki.srenablerewrites = True

Definition at line 73 of file mediawiki.py.

◆ srprop

str searx.engines.mediawiki.srprop = 'sectiontitle|snippet|timestamp|categorysnippet'

Definition at line 90 of file mediawiki.py.

◆ srsort

str searx.engines.mediawiki.srsort = 'relevance'

Definition at line 81 of file mediawiki.py.

◆ timestamp_format

str searx.engines.mediawiki.timestamp_format = '%Y-%m-%dT%H:%M:%SZ'

Definition at line 109 of file mediawiki.py.