Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 216
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryBacklinksprop
0.00% covered (danger)
0.00%
0 / 216
0.00% covered (danger)
0.00%
0 / 9
4830
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 141
0.00% covered (danger)
0.00%
0 / 1
2862
 setContinue
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
72
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * API module to handle links table back-queries
4 *
5 * Copyright © 2014 Wikimedia Foundation and contributors
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @since 1.24
24 */
25
26use MediaWiki\Linker\LinksMigration;
27use MediaWiki\MainConfigNames;
28use MediaWiki\Title\Title;
29use Wikimedia\ParamValidator\ParamValidator;
30use Wikimedia\ParamValidator\TypeDef\IntegerDef;
31use Wikimedia\Rdbms\OrExpressionGroup;
32
33/**
34 * This implements prop=redirects, prop=linkshere, prop=catmembers,
35 * prop=transcludedin, and prop=fileusage
36 *
37 * @ingroup API
38 * @since 1.24
39 */
40class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
41
42    /** @var array Data for the various modules implemented by this class */
43    private static $settings = [
44        'redirects' => [
45            'code' => 'rd',
46            'prefix' => 'rd',
47            'linktable' => 'redirect',
48            'props' => [
49                'fragment',
50            ],
51            'showredirects' => false,
52            'show' => [
53                'fragment',
54                '!fragment',
55            ],
56        ],
57        'linkshere' => [
58            'code' => 'lh',
59            'prefix' => 'pl',
60            'linktable' => 'pagelinks',
61            'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
62            'from_namespace' => true,
63            'showredirects' => true,
64        ],
65        'transcludedin' => [
66            'code' => 'ti',
67            'prefix' => 'tl',
68            'linktable' => 'templatelinks',
69            'from_namespace' => true,
70            'showredirects' => true,
71        ],
72        'fileusage' => [
73            'code' => 'fu',
74            'prefix' => 'il',
75            'linktable' => 'imagelinks',
76            'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
77            'from_namespace' => true,
78            'to_namespace' => NS_FILE,
79            'exampletitle' => 'File:Example.jpg',
80            'showredirects' => true,
81        ],
82    ];
83
84    private LinksMigration $linksMigration;
85
86    /**
87     * @param ApiQuery $query
88     * @param string $moduleName
89     * @param LinksMigration $linksMigration
90     */
91    public function __construct(
92        ApiQuery $query,
93        $moduleName,
94        LinksMigration $linksMigration
95    ) {
96        parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
97        $this->linksMigration = $linksMigration;
98    }
99
100    public function execute() {
101        $this->run();
102    }
103
104    public function executeGenerator( $resultPageSet ) {
105        $this->run( $resultPageSet );
106    }
107
108    /**
109     * @param ApiPageSet|null $resultPageSet
110     */
111    private function run( ApiPageSet $resultPageSet = null ) {
112        $settings = self::$settings[$this->getModuleName()];
113
114        $db = $this->getDB();
115        $params = $this->extractRequestParams();
116        $prop = array_fill_keys( $params['prop'], true );
117
118        $pageSet = $this->getPageSet();
119        $titles = $pageSet->getGoodAndMissingPages();
120        $map = $pageSet->getGoodAndMissingTitlesByNamespace();
121
122        // Add in special pages, they can theoretically have backlinks too.
123        // (although currently they only do for prop=redirects)
124        foreach ( $pageSet->getSpecialPages() as $id => $title ) {
125            $titles[] = $title;
126            $map[$title->getNamespace()][$title->getDBkey()] = $id;
127        }
128
129        // Determine our fields to query on
130        $p = $settings['prefix'];
131        $hasNS = !isset( $settings['to_namespace'] );
132        if ( $hasNS ) {
133            if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
134                [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings['linktable'] );
135            } else {
136                $bl_namespace = "{$p}_namespace";
137                $bl_title = "{$p}_title";
138            }
139        } else {
140            // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
141            $bl_namespace = $settings['to_namespace'];
142            $bl_title = "{$p}_to";
143
144            $titles = array_filter( $titles, static function ( $t ) use ( $bl_namespace ) {
145                return $t->getNamespace() === $bl_namespace;
146            } );
147            $map = array_intersect_key( $map, [ $bl_namespace => true ] );
148        }
149        $bl_from = "{$p}_from";
150
151        if ( !$titles ) {
152            return; // nothing to do
153        }
154        if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
155            return; // nothing to do
156        }
157
158        // Figure out what we're sorting by, and add associated WHERE clauses.
159        // MySQL's query planner screws up if we include a field in ORDER BY
160        // when it's constant in WHERE, so we have to test that for each field.
161        $sortby = [];
162        if ( $hasNS && count( $map ) > 1 ) {
163            $sortby[$bl_namespace] = 'int';
164        }
165        $theTitle = null;
166        foreach ( $map as $nsTitles ) {
167            $key = array_key_first( $nsTitles );
168            $theTitle ??= $key;
169            if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
170                $sortby[$bl_title] = 'string';
171                break;
172            }
173        }
174        $miser_ns = null;
175        if ( $params['namespace'] !== null ) {
176            if ( empty( $settings['from_namespace'] ) ) {
177                if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
178                    $miser_ns = $params['namespace'];
179                } else {
180                    $this->addWhereFld( 'page_namespace', $params['namespace'] );
181                }
182            } else {
183                $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
184                if ( !empty( $settings['from_namespace'] )
185                    && $params['namespace'] !== null && count( $params['namespace'] ) > 1
186                ) {
187                    $sortby["{$p}_from_namespace"] = 'int';
188                }
189            }
190        }
191        $sortby[$bl_from] = 'int';
192
193        // Now use the $sortby to figure out the continuation
194        $continueFields = array_keys( $sortby );
195        $continueTypes = array_values( $sortby );
196        if ( $params['continue'] !== null ) {
197            $continueValues = $this->parseContinueParamOrDie( $params['continue'], $continueTypes );
198            $conds = array_combine( $continueFields, $continueValues );
199            $this->addWhere( $db->buildComparison( '>=', $conds ) );
200        }
201
202        // Populate the rest of the query
203        [ $idxNoFromNS, $idxWithFromNS ] = $settings['indexes'] ?? [ '', '' ];
204        // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
205        if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
206            // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
207            $queryInfo = $this->linksMigration->getQueryInfo( $settings['linktable'] );
208            $this->addTables( array_merge( [ 'page' ], $queryInfo['tables'] ) );
209            $this->addJoinConds( $queryInfo['joins'] );
210            // TODO: Move to links migration
211            if ( in_array( 'linktarget', $queryInfo['tables'] ) ) {
212                $idxWithFromNS .= '_target_id';
213            }
214        } else {
215            // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
216            $this->addTables( [ $settings['linktable'], 'page' ] );
217        }
218        $this->addWhere( "$bl_from = page_id" );
219
220        if ( $this->getModuleName() === 'redirects' ) {
221            $this->addWhereFld( 'rd_interwiki', '' );
222        }
223
224        $this->addFields( array_keys( $sortby ) );
225        $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
226        if ( $resultPageSet === null ) {
227            $fld_pageid = isset( $prop['pageid'] );
228            $fld_title = isset( $prop['title'] );
229            $fld_redirect = isset( $prop['redirect'] );
230
231            $this->addFieldsIf( 'page_id', $fld_pageid );
232            $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
233            $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
234
235            // prop=redirects
236            $fld_fragment = isset( $prop['fragment'] );
237            $this->addFieldsIf( 'rd_fragment', $fld_fragment );
238        } else {
239            $this->addFields( $resultPageSet->getPageTableFields() );
240        }
241
242        $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
243
244        if ( $hasNS && $map ) {
245            // Can't use LinkBatch because it throws away Special titles.
246            // And we already have the needed data structure anyway.
247            $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
248        } else {
249            $where = [];
250            foreach ( $titles as $t ) {
251                if ( $t->getNamespace() == $bl_namespace ) {
252                    $where[] = $db->expr( $bl_title, '=', $t->getDBkey() );
253                }
254            }
255            $this->addWhere( new OrExpressionGroup( ...$where ) );
256        }
257
258        if ( $params['show'] !== null ) {
259            // prop=redirects only
260            $show = array_fill_keys( $params['show'], true );
261            if ( ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) ||
262                ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
263            ) {
264                $this->dieWithError( 'apierror-show' );
265            }
266            $this->addWhereIf( $db->expr( 'rd_fragment', '!=', '' ), isset( $show['fragment'] ) );
267            $this->addWhereIf( [ 'rd_fragment' => '' ], isset( $show['!fragment'] ) );
268            $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
269            $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
270        }
271
272        // Override any ORDER BY from above with what we calculated earlier.
273        $this->addOption( 'ORDER BY', array_keys( $sortby ) );
274
275        // MySQL's optimizer chokes if we have too many values in "$bl_title IN
276        // (...)" and chooses the wrong index, so specify the correct index to
277        // use for the query. See T139056 for details.
278        if ( !empty( $settings['indexes'] ) ) {
279            if (
280                $params['namespace'] !== null &&
281                count( $params['namespace'] ) == 1 &&
282                !empty( $settings['from_namespace'] )
283            ) {
284                // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
285                $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
286                // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
287            } elseif ( !isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
288                // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
289                $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
290            }
291        }
292
293        $this->addOption( 'LIMIT', $params['limit'] + 1 );
294
295        $res = $this->select( __METHOD__ );
296
297        if ( $resultPageSet === null ) {
298            // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
299            if ( $fld_title ) {
300                $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
301            }
302
303            $count = 0;
304            foreach ( $res as $row ) {
305                if ( ++$count > $params['limit'] ) {
306                    // We've reached the one extra which shows that
307                    // there are additional pages to be had. Stop here...
308                    $this->setContinue( $row, $sortby );
309                    break;
310                }
311
312                if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
313                    // Miser mode namespace check
314                    continue;
315                }
316
317                // Get the ID of the current page
318                $id = $map[$row->bl_namespace][$row->bl_title];
319
320                $vals = [];
321                // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
322                if ( $fld_pageid ) {
323                    $vals['pageid'] = (int)$row->page_id;
324                }
325                if ( $fld_title ) {
326                    ApiQueryBase::addTitleInfo( $vals,
327                        Title::makeTitle( $row->page_namespace, $row->page_title )
328                    );
329                }
330                // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
331                if ( $fld_fragment && $row->rd_fragment !== '' ) {
332                    $vals['fragment'] = $row->rd_fragment;
333                }
334                // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
335                if ( $fld_redirect ) {
336                    $vals['redirect'] = (bool)$row->page_is_redirect;
337                }
338                $fit = $this->addPageSubItem( $id, $vals );
339                if ( !$fit ) {
340                    $this->setContinue( $row, $sortby );
341                    break;
342                }
343            }
344        } else {
345            $titles = [];
346            $count = 0;
347            foreach ( $res as $row ) {
348                if ( ++$count > $params['limit'] ) {
349                    // We've reached the one extra which shows that
350                    // there are additional pages to be had. Stop here...
351                    $this->setContinue( $row, $sortby );
352                    break;
353                }
354
355                if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
356                    // Miser mode namespace check
357                    continue;
358                }
359
360                $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
361            }
362            $resultPageSet->populateFromTitles( $titles );
363        }
364    }
365
366    private function setContinue( $row, $sortby ) {
367        $cont = [];
368        foreach ( $sortby as $field => $v ) {
369            $cont[] = $row->$field;
370        }
371        $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
372    }
373
374    public function getCacheMode( $params ) {
375        return 'public';
376    }
377
378    public function getAllowedParams() {
379        $settings = self::$settings[$this->getModuleName()];
380
381        $ret = [
382            'prop' => [
383                ParamValidator::PARAM_TYPE => [
384                    'pageid',
385                    'title',
386                ],
387                ParamValidator::PARAM_ISMULTI => true,
388                ParamValidator::PARAM_DEFAULT => 'pageid|title',
389                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
390            ],
391            'namespace' => [
392                ParamValidator::PARAM_ISMULTI => true,
393                ParamValidator::PARAM_TYPE => 'namespace',
394            ],
395            'show' => null, // Will be filled/removed below
396            'limit' => [
397                ParamValidator::PARAM_DEFAULT => 10,
398                ParamValidator::PARAM_TYPE => 'limit',
399                IntegerDef::PARAM_MIN => 1,
400                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
401                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
402            ],
403            'continue' => [
404                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
405            ],
406        ];
407
408        if ( empty( $settings['from_namespace'] ) &&
409        $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
410            $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
411                'api-help-param-limited-in-miser-mode',
412            ];
413        }
414
415        if ( !empty( $settings['showredirects'] ) ) {
416            $ret['prop'][ParamValidator::PARAM_TYPE][] = 'redirect';
417            $ret['prop'][ParamValidator::PARAM_DEFAULT] .= '|redirect';
418        }
419        if ( isset( $settings['props'] ) ) {
420            $ret['prop'][ParamValidator::PARAM_TYPE] = array_merge(
421                $ret['prop'][ParamValidator::PARAM_TYPE], $settings['props']
422            );
423        }
424
425        $show = [];
426        if ( !empty( $settings['showredirects'] ) ) {
427            $show[] = 'redirect';
428            $show[] = '!redirect';
429        }
430        if ( isset( $settings['show'] ) ) {
431            $show = array_merge( $show, $settings['show'] );
432        }
433        if ( $show ) {
434            $ret['show'] = [
435                ParamValidator::PARAM_TYPE => $show,
436                ParamValidator::PARAM_ISMULTI => true,
437                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
438            ];
439        } else {
440            unset( $ret['show'] );
441        }
442
443        return $ret;
444    }
445
446    protected function getExamplesMessages() {
447        $settings = self::$settings[$this->getModuleName()];
448        $name = $this->getModuleName();
449        $path = $this->getModulePath();
450        $title = $settings['exampletitle'] ?? Title::newMainPage()->getPrefixedText();
451        $etitle = rawurlencode( $title );
452
453        return [
454            "action=query&prop={$name}&titles={$etitle}"
455                => "apihelp-$path-example-simple",
456            "action=query&generator={$name}&titles={$etitle}&prop=info"
457                => "apihelp-$path-example-generator",
458        ];
459    }
460
461    public function getHelpUrls() {
462        $name = ucfirst( $this->getModuleName() );
463        return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
464    }
465}