45 private $linksMigration;
51 private $bl_ns, $bl_from, $bl_from_ns, $bl_table, $bl_code, $bl_title, $hasNS;
61 private $pageMap = [];
64 private $redirTitles = [];
65 private $continueStr =
null;
68 private $backlinksSettings = [
72 'linktbl' =>
'pagelinks',
73 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
78 'linktbl' =>
'templatelinks',
79 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
84 'linktbl' =>
'imagelinks',
85 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
95 $settings = $this->backlinksSettings[$moduleName];
96 $prefix = $settings[
'prefix'];
97 $code = $settings[
'code'];
98 $this->resultArr = [];
100 parent::__construct( $query, $moduleName, $code );
101 $this->bl_table = $settings[
'linktbl'];
102 $this->hasNS = $moduleName !==
'imageusage';
103 $this->linksMigration = $linksMigration;
104 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
105 list( $this->bl_ns, $this->bl_title ) = $this->linksMigration->
getTitleFields( $this->bl_table );
107 $this->bl_ns = $prefix .
'_namespace';
108 if ( $this->hasNS ) {
109 $this->bl_title = $prefix .
'_title';
111 $this->bl_title = $prefix .
'_to';
114 $this->bl_from = $prefix .
'_from';
115 $this->bl_from_ns = $prefix .
'_from_namespace';
116 $this->bl_code = $code;
117 $this->helpUrl = $settings[
'helpurl'];
129 $this->run( $resultPageSet );
136 private function runFirstQuery( $resultPageSet =
null ) {
137 $this->
addTables( [ $this->bl_table,
'page' ] );
138 $this->
addWhere(
"{$this->bl_from}=page_id" );
139 if ( $resultPageSet ===
null ) {
140 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
142 $this->
addFields( $resultPageSet->getPageTableFields() );
144 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
146 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
147 $conds = $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle );
150 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
151 if ( $this->hasNS ) {
152 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
156 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
158 if ( count( $this->cont ) >= 2 ) {
159 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
160 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
162 "{$this->bl_from_ns} $op {$this->cont[0]} OR " .
163 "({$this->bl_from_ns} = {$this->cont[0]} AND " .
164 "{$this->bl_from} $op= {$this->cont[1]})"
167 $this->
addWhere(
"{$this->bl_from} $op= {$this->cont[1]}" );
171 if ( $this->params[
'filterredir'] ==
'redirects' ) {
173 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
179 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
180 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
182 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
183 $orderBy[] = $this->bl_from_ns . $sort;
185 $orderBy[] = $this->bl_from . $sort;
186 $this->
addOption(
'ORDER BY', $orderBy );
191 if ( $resultPageSet ===
null ) {
196 foreach (
$res as $row ) {
197 if ( ++$count > $this->params[
'limit'] ) {
201 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
206 if ( count( $this->cont ) < 2 ) {
207 $this->cont[] = $row->from_ns;
208 $this->cont[] = $row->page_id;
211 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
213 if ( $row->page_is_redirect ) {
214 $this->redirTitles[] =
$t;
217 if ( $resultPageSet ===
null ) {
218 $a = [
'pageid' => (int)$row->page_id ];
220 if ( $row->page_is_redirect ) {
221 $a[
'redirect'] =
true;
224 $this->resultArr[$a[
'pageid']] = $a;
226 $resultPageSet->processDbRow( $row );
237 private function runSecondQuery( $resultPageSet =
null ) {
238 $db = $this->
getDB();
239 $this->
addTables( [ $this->bl_table,
'page' ] );
240 $this->
addWhere(
"{$this->bl_from}=page_id" );
242 if ( $resultPageSet ===
null ) {
243 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
245 $this->
addFields( $resultPageSet->getPageTableFields() );
248 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
249 if ( $this->hasNS ) {
258 foreach ( $this->redirTitles as
$t ) {
259 $redirNs =
$t->getNamespace();
260 $redirDBkey =
$t->getDBkey();
261 $titleWhere[] =
"{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
262 ( $this->hasNS ?
" AND {$this->bl_ns} = {$redirNs}" :
'' );
263 $allRedirNs[$redirNs] =
true;
264 $allRedirDBkey[$redirDBkey] =
true;
267 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
269 if ( count( $this->cont ) >= 6 ) {
270 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
272 $where =
"{$this->bl_from} $op= {$this->cont[5]}";
275 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
276 $where =
"{$this->bl_from_ns} $op {$this->cont[4]} OR " .
277 "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
279 if ( count( $allRedirDBkey ) > 1 ) {
280 $title = $db->addQuotes( $this->cont[3] );
281 $where =
"{$this->bl_title} $op $title OR " .
282 "({$this->bl_title} = $title AND ($where))";
284 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
285 $where =
"{$this->bl_ns} $op {$this->cont[2]} OR " .
286 "({$this->bl_ns} = {$this->cont[2]} AND ($where))";
291 if ( $this->params[
'filterredir'] ==
'redirects' ) {
293 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
297 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
299 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
301 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
302 $orderBy[] = $this->bl_ns . $sort;
304 if ( count( $allRedirDBkey ) > 1 ) {
305 $orderBy[] = $this->bl_title . $sort;
307 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
308 $orderBy[] = $this->bl_from_ns . $sort;
310 $orderBy[] = $this->bl_from . $sort;
311 $this->
addOption(
'ORDER BY', $orderBy );
312 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
318 if ( $resultPageSet ===
null ) {
323 foreach (
$res as $row ) {
324 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
326 if ( ++$count > $this->params[
'limit'] ) {
331 $title = $row->{$this->bl_title};
332 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
333 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
338 if ( count( $this->cont ) < 6 ) {
340 $this->cont[] = $row->{$this->bl_title};
341 $this->cont[] = $row->from_ns;
342 $this->cont[] = $row->page_id;
345 if ( $resultPageSet ===
null ) {
346 $a = [
'pageid' => (int)$row->page_id ];
348 if ( $row->page_is_redirect ) {
349 $a[
'redirect'] =
true;
351 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
353 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
355 $resultPageSet->processDbRow( $row );
364 private function run( $resultPageSet =
null ) {
365 $this->params = $this->extractRequestParams(
false );
366 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
370 $result = $this->getResult();
372 if ( $this->params[
'limit'] ==
'max' ) {
373 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
374 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
376 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
377 $this,
'limit', (
int)$this->params[
'limit'], [
378 ParamValidator::PARAM_TYPE =>
'limit',
379 IntegerDef::PARAM_MIN => 1,
380 IntegerDef::PARAM_MAX => $userMax,
381 IntegerDef::PARAM_MAX2 => $botMax,
382 IntegerDef::PARAM_IGNORE_RANGE =>
true,
387 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
390 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
392 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
399 if ( $this->params[
'continue'] !==
null ) {
400 $cont = explode(
'|', $this->params[
'continue'] );
402 switch ( count( $cont ) ) {
405 $this->cont[7] = (int)$cont[7];
406 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
412 $this->cont[6] = (int)$cont[6];
413 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
419 $this->cont[2] = (int)$cont[2];
420 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
423 $this->cont[3] = $cont[3];
426 $this->cont[4] = (int)$cont[4];
427 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
430 $this->cont[5] = (int)$cont[5];
431 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
437 $this->cont[0] = (int)$cont[0];
438 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
441 $this->cont[1] = (int)$cont[1];
442 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
448 $this->dieContinueUsageIf(
true );
451 ksort( $this->cont );
454 $this->runFirstQuery( $resultPageSet );
455 if ( $this->redirect && count( $this->redirTitles ) ) {
456 $this->resetQueryParams();
457 $this->runSecondQuery( $resultPageSet );
461 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
463 if ( $resultPageSet ===
null ) {
465 $code = $this->bl_code;
466 $data = array_map(
static function ( $arr ) use ( $code ) {
467 if ( isset( $arr[
'redirlinks'] ) ) {
468 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
472 }, array_values( $this->resultArr ) );
473 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
477 ksort( $this->resultArr );
479 if ( count( $this->cont ) >= 7 ) {
480 $startAt = $this->cont[6];
482 reset( $this->resultArr );
483 $startAt = key( $this->resultArr );
486 foreach ( $this->resultArr as $pageID => $arr ) {
487 if ( $pageID < $startAt ) {
492 $fit = $result->addValue(
493 [
'query', $this->getModuleName() ],
494 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
496 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
502 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
503 ksort( $redirLinks );
505 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
506 $redirStartAt = $this->cont[7];
508 reset( $redirLinks );
509 $redirStartAt = key( $redirLinks );
511 foreach ( $redirLinks as $key => $redir ) {
512 if ( $key < $redirStartAt ) {
516 $fit = $result->addValue(
517 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
520 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
527 $result->addIndexedTagName(
528 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
539 $result->addIndexedTagName(
540 [
'query', $this->getModuleName() ],
544 if ( $this->continueStr !==
null ) {
545 $this->setContinueEnumParameter(
'continue', $this->continueStr );
552 ParamValidator::PARAM_TYPE =>
'string',
555 ParamValidator::PARAM_TYPE =>
'integer',
561 ParamValidator::PARAM_ISMULTI =>
true,
562 ParamValidator::PARAM_TYPE =>
'namespace'
565 ParamValidator::PARAM_DEFAULT =>
'ascending',
566 ParamValidator::PARAM_TYPE => [
572 ParamValidator::PARAM_DEFAULT =>
'all',
573 ParamValidator::PARAM_TYPE => [
580 ParamValidator::PARAM_DEFAULT => 10,
581 ParamValidator::PARAM_TYPE =>
'limit',
582 IntegerDef::PARAM_MIN => 1,
587 if ( $this->getModuleName() !==
'embeddedin' ) {
588 $retval[
'redirect'] =
false;
595 $title = Title::newMainPage()->getPrefixedText();
596 $mp = rawurlencode(
$title );
599 "action=query&list=backlinks&bltitle={$mp}"
600 =>
'apihelp-query+backlinks-example-simple',
601 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
602 =>
'apihelp-query+backlinks-example-generator',
605 'action=query&list=embeddedin&eititle=Template:Stub'
606 =>
'apihelp-query+embeddedin-example-simple',
607 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
608 =>
'apihelp-query+embeddedin-example-generator',
611 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
612 =>
'apihelp-query+imageusage-example-simple',
613 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
614 =>
'apihelp-query+imageusage-example-generator',
618 return $examples[$this->getModuleName()];
622 return $this->helpUrl;