MediaWiki REL1_27
ApiQueryBacklinksprop.php
Go to the documentation of this file.
1<?php
36
37 // Data for the various modules implemented by this class
38 private static $settings = [
39 'redirects' => [
40 'code' => 'rd',
41 'prefix' => 'rd',
42 'linktable' => 'redirect',
43 'props' => [
44 'fragment',
45 ],
46 'showredirects' => false,
47 'show' => [
48 'fragment',
49 '!fragment',
50 ],
51 ],
52 'linkshere' => [
53 'code' => 'lh',
54 'prefix' => 'pl',
55 'linktable' => 'pagelinks',
56 'from_namespace' => true,
57 'showredirects' => true,
58 ],
59 'transcludedin' => [
60 'code' => 'ti',
61 'prefix' => 'tl',
62 'linktable' => 'templatelinks',
63 'from_namespace' => true,
64 'showredirects' => true,
65 ],
66 'fileusage' => [
67 'code' => 'fu',
68 'prefix' => 'il',
69 'linktable' => 'imagelinks',
70 'from_namespace' => true,
71 'to_namespace' => NS_FILE,
72 'exampletitle' => 'File:Example.jpg',
73 'showredirects' => true,
74 ],
75 ];
76
77 public function __construct( ApiQuery $query, $moduleName ) {
78 parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
79 }
80
81 public function execute() {
82 $this->run();
83 }
84
85 public function executeGenerator( $resultPageSet ) {
86 $this->run( $resultPageSet );
87 }
88
92 private function run( ApiPageSet $resultPageSet = null ) {
93 $settings = self::$settings[$this->getModuleName()];
94
95 $db = $this->getDB();
97 $prop = array_flip( $params['prop'] );
98 $emptyString = $db->addQuotes( '' );
99
100 $pageSet = $this->getPageSet();
101 $titles = $pageSet->getGoodAndMissingTitles();
102 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
103
104 // Determine our fields to query on
105 $p = $settings['prefix'];
106 $hasNS = !isset( $settings['to_namespace'] );
107 if ( $hasNS ) {
108 $bl_namespace = "{$p}_namespace";
109 $bl_title = "{$p}_title";
110 } else {
111 $bl_namespace = $settings['to_namespace'];
112 $bl_title = "{$p}_to";
113
114 $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
115 return $t->getNamespace() === $bl_namespace;
116 } );
117 $map = array_intersect_key( $map, [ $bl_namespace => true ] );
118 }
119 $bl_from = "{$p}_from";
120
121 if ( !$titles ) {
122 return; // nothing to do
123 }
124
125 // Figure out what we're sorting by, and add associated WHERE clauses.
126 // MySQL's query planner screws up if we include a field in ORDER BY
127 // when it's constant in WHERE, so we have to test that for each field.
128 $sortby = [];
129 if ( $hasNS && count( $map ) > 1 ) {
130 $sortby[$bl_namespace] = 'ns';
131 }
132 $theTitle = null;
133 foreach ( $map as $nsTitles ) {
134 reset( $nsTitles );
135 $key = key( $nsTitles );
136 if ( $theTitle === null ) {
137 $theTitle = $key;
138 }
139 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
140 $sortby[$bl_title] = 'title';
141 break;
142 }
143 }
144 $miser_ns = null;
145 if ( $params['namespace'] !== null ) {
146 if ( empty( $settings['from_namespace'] ) ) {
147 if ( $this->getConfig()->get( 'MiserMode' ) ) {
148 $miser_ns = $params['namespace'];
149 } else {
150 $this->addWhereFld( 'page_namespace', $params['namespace'] );
151 }
152 } else {
153 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
154 if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) {
155 $sortby["{$p}_from_namespace"] = 'int';
156 }
157 }
158 }
159 $sortby[$bl_from] = 'int';
160
161 // Now use the $sortby to figure out the continuation
162 if ( !is_null( $params['continue'] ) ) {
163 $cont = explode( '|', $params['continue'] );
164 $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
165 $where = '';
166 $i = count( $sortby ) - 1;
167 foreach ( array_reverse( $sortby, true ) as $field => $type ) {
168 $v = $cont[$i];
169 switch ( $type ) {
170 case 'ns':
171 case 'int':
172 $v = (int)$v;
173 $this->dieContinueUsageIf( $v != $cont[$i] );
174 break;
175 default:
176 $v = $db->addQuotes( $v );
177 break;
178 }
179
180 if ( $where === '' ) {
181 $where = "$field >= $v";
182 } else {
183 $where = "$field > $v OR ($field = $v AND ($where))";
184 }
185
186 $i--;
187 }
188 $this->addWhere( $where );
189 }
190
191 // Populate the rest of the query
192 $this->addTables( [ $settings['linktable'], 'page' ] );
193 $this->addWhere( "$bl_from = page_id" );
194
195 if ( $this->getModuleName() === 'redirects' ) {
196 $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
197 }
198
199 $this->addFields( array_keys( $sortby ) );
200 $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
201 if ( is_null( $resultPageSet ) ) {
202 $fld_pageid = isset( $prop['pageid'] );
203 $fld_title = isset( $prop['title'] );
204 $fld_redirect = isset( $prop['redirect'] );
205
206 $this->addFieldsIf( 'page_id', $fld_pageid );
207 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
208 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
209
210 // prop=redirects
211 $fld_fragment = isset( $prop['fragment'] );
212 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
213 } else {
214 $this->addFields( $resultPageSet->getPageTableFields() );
215 }
216
217 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
218
219 if ( $hasNS ) {
220 $lb = new LinkBatch( $titles );
221 $this->addWhere( $lb->constructSet( $p, $db ) );
222 } else {
223 $where = [];
224 foreach ( $titles as $t ) {
225 if ( $t->getNamespace() == $bl_namespace ) {
226 $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
227 }
228 }
229 $this->addWhere( $db->makeList( $where, LIST_OR ) );
230 }
231
232 if ( $params['show'] !== null ) {
233 // prop=redirects only
234 $show = array_flip( $params['show'] );
235 if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
236 isset( $show['redirect'] ) && isset( $show['!redirect'] )
237 ) {
238 $this->dieUsageMsg( 'show' );
239 }
240 $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
241 $this->addWhereIf(
242 "rd_fragment = $emptyString OR rd_fragment IS NULL",
243 isset( $show['!fragment'] )
244 );
245 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
246 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
247 }
248
249 // Override any ORDER BY from above with what we calculated earlier.
250 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
251
252 $this->addOption( 'LIMIT', $params['limit'] + 1 );
253
254 $res = $this->select( __METHOD__ );
255
256 if ( is_null( $resultPageSet ) ) {
257 $count = 0;
258 foreach ( $res as $row ) {
259 if ( ++$count > $params['limit'] ) {
260 // We've reached the one extra which shows that
261 // there are additional pages to be had. Stop here...
262 $this->setContinue( $row, $sortby );
263 break;
264 }
265
266 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
267 // Miser mode namespace check
268 continue;
269 }
270
271 // Get the ID of the current page
272 $id = $map[$row->bl_namespace][$row->bl_title];
273
274 $vals = [];
275 if ( $fld_pageid ) {
276 $vals['pageid'] = (int)$row->page_id;
277 }
278 if ( $fld_title ) {
280 Title::makeTitle( $row->page_namespace, $row->page_title )
281 );
282 }
283 if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
284 $vals['fragment'] = $row->rd_fragment;
285 }
286 if ( $fld_redirect ) {
287 $vals['redirect'] = (bool)$row->page_is_redirect;
288 }
289 $fit = $this->addPageSubItem( $id, $vals );
290 if ( !$fit ) {
291 $this->setContinue( $row, $sortby );
292 break;
293 }
294 }
295 } else {
296 $titles = [];
297 $count = 0;
298 foreach ( $res as $row ) {
299 if ( ++$count > $params['limit'] ) {
300 // We've reached the one extra which shows that
301 // there are additional pages to be had. Stop here...
302 $this->setContinue( $row, $sortby );
303 break;
304 }
305 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
306 }
307 $resultPageSet->populateFromTitles( $titles );
308 }
309 }
310
311 private function setContinue( $row, $sortby ) {
312 $cont = [];
313 foreach ( $sortby as $field => $v ) {
314 $cont[] = $row->$field;
315 }
316 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
317 }
318
319 public function getCacheMode( $params ) {
320 return 'public';
321 }
322
323 public function getAllowedParams() {
324 $settings = self::$settings[$this->getModuleName()];
325
326 $ret = [
327 'prop' => [
329 'pageid',
330 'title',
331 ],
333 ApiBase::PARAM_DFLT => 'pageid|title',
335 ],
336 'namespace' => [
338 ApiBase::PARAM_TYPE => 'namespace',
339 ],
340 'show' => null, // Will be filled/removed below
341 'limit' => [
343 ApiBase::PARAM_TYPE => 'limit',
347 ],
348 'continue' => [
349 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
350 ],
351 ];
352
353 if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
354 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
355 'api-help-param-limited-in-miser-mode',
356 ];
357 }
358
359 if ( !empty( $settings['showredirects'] ) ) {
360 $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
361 $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
362 }
363 if ( isset( $settings['props'] ) ) {
364 $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
365 $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
366 );
367 }
368
369 $show = [];
370 if ( !empty( $settings['showredirects'] ) ) {
371 $show[] = 'redirect';
372 $show[] = '!redirect';
373 }
374 if ( isset( $settings['show'] ) ) {
375 $show = array_merge( $show, $settings['show'] );
376 }
377 if ( $show ) {
378 $ret['show'] = [
379 ApiBase::PARAM_TYPE => $show,
381 ];
382 } else {
383 unset( $ret['show'] );
384 }
385
386 return $ret;
387 }
388
389 protected function getExamplesMessages() {
390 $settings = self::$settings[$this->getModuleName()];
391 $name = $this->getModuleName();
392 $path = $this->getModulePath();
393 $title = isset( $settings['exampletitle'] ) ? $settings['exampletitle'] : 'Main Page';
394 $etitle = rawurlencode( $title );
395
396 return [
397 "action=query&prop={$name}&titles={$etitle}"
398 => "apihelp-$path-example-simple",
399 "action=query&generator={$name}&titles={$etitle}&prop=info"
400 => "apihelp-$path-example-generator",
401 ];
402 }
403
404 public function getHelpUrls() {
405 $name = ucfirst( $this->getModuleName() );
406 return "https://www.mediawiki.org/wiki/API:{$name}";
407 }
408}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$i
Definition Parser.php:1694
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition ApiBase.php:97
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:91
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition ApiBase.php:2181
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:88
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:50
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:132
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:685
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition ApiBase.php:157
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition ApiBase.php:2144
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:100
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:184
getModulePath()
Get the path to this module.
Definition ApiBase.php:528
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:125
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:186
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:464
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:53
This class contains a list of pages that the client has requested.
This implements prop=redirects, prop=linkshere, prop=catmembers, prop=transcludedin,...
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
run(ApiPageSet $resultPageSet=null)
getExamplesMessages()
Returns usage examples for this module.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
__construct(ApiQuery $query, $moduleName)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getPageSet()
Get the PageSet object to work on.
This is the main query class.
Definition ApiQuery.php:38
getConfig()
Get the Config object.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:31
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition Title.php:524
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition design.txt:26
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_FILE
Definition Defines.php:76
const LIST_OR
Definition Defines.php:197
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1810
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition hooks.txt:2413
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:944
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:314
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1458
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition linkcache.txt:17
$params