41 private string $bl_ns;
42 private string $bl_from;
43 private string $bl_from_ns;
44 private string $bl_table;
45 private string $bl_code;
46 private string $bl_title;
48 private $virtual_domain;
59 private $pageMap = [];
64 private $redirTitles = [];
66 private $continueStr =
null;
69 private $backlinksSettings = [
73 'linktbl' =>
'pagelinks',
74 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
75 'virtualdomain' => PageLinksTable::VIRTUAL_DOMAIN,
80 'linktbl' =>
'templatelinks',
81 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
82 'virtualdomain' => TemplateLinksTable::VIRTUAL_DOMAIN,
87 'linktbl' =>
'imagelinks',
88 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
89 'virtualdomain' => ImageLinksTable::VIRTUAL_DOMAIN,
98 $settings = $this->backlinksSettings[$moduleName];
99 $prefix = $settings[
'prefix'];
100 $code = $settings[
'code'];
101 $this->resultArr = [];
103 parent::__construct( $query, $moduleName, $code );
104 $this->bl_table = $settings[
'linktbl'];
105 $this->hasNS = $moduleName !==
'imageusage';
106 [ $this->bl_ns, $this->bl_title ] = $this->linksMigration->getTitleFields( $this->bl_table );
107 $this->bl_from = $prefix .
'_from';
108 $this->bl_from_ns = $prefix .
'_from_namespace';
109 $this->bl_code = $code;
110 $this->helpUrl = $settings[
'helpurl'];
111 $this->virtual_domain = $settings[
'virtualdomain'];
125 $this->run( $resultPageSet );
132 private function runFirstQuery( $resultPageSet =
null ) {
133 $this->
addTables( [ $this->bl_table,
'page' ] );
134 $this->
addWhere(
"{$this->bl_from}=page_id" );
135 if ( $resultPageSet ===
null ) {
136 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
138 $this->
addFields( $resultPageSet->getPageTableFields() );
140 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
142 $this->
addWhere( $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle ) );
143 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
145 if ( count( $this->cont ) >= 2 ) {
146 $db = $this->
getDB();
147 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
148 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
149 $this->
addWhere( $db->buildComparison( $op, [
150 $this->bl_from_ns => $this->cont[0],
151 $this->bl_from => $this->cont[1],
154 $this->
addWhere( $db->buildComparison( $op, [ $this->bl_from => $this->cont[1] ] ) );
158 if ( $this->params[
'filterredir'] ==
'redirects' ) {
160 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
166 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
167 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
169 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
170 $orderBy[] = $this->bl_from_ns . $sort;
172 $orderBy[] = $this->bl_from . $sort;
173 $this->
addOption(
'ORDER BY', $orderBy );
177 $res = $this->
select( __METHOD__ );
180 if ( $resultPageSet ===
null ) {
185 foreach ( $res as $row ) {
186 if ( ++$count > $this->params[
'limit'] ) {
190 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
195 if ( count( $this->cont ) < 2 ) {
196 $this->cont[] = $row->from_ns;
197 $this->cont[] = $row->page_id;
200 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
202 if ( $row->page_is_redirect ) {
203 $this->redirTitles[] =
$t;
206 if ( $resultPageSet ===
null ) {
207 $a = [
'pageid' => (int)$row->page_id ];
209 if ( $row->page_is_redirect ) {
210 $a[
'redirect'] =
true;
213 $this->resultArr[$a[
'pageid']] = $a;
215 $resultPageSet->processDbRow( $row );
224 private function runSecondQuery( $resultPageSet =
null ) {
225 $db = $this->
getDB();
226 $queryInfo = $this->linksMigration->getQueryInfo( $this->bl_table, $this->bl_table );
227 $this->
addTables( [ ...$queryInfo[
'tables'],
'page' ] );
228 $this->
addJoinConds( [ ...$queryInfo[
'joins'],
'page' => [
'JOIN',
"{$this->bl_from}=page_id" ] ] );
230 if ( $resultPageSet ===
null ) {
231 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
233 $this->
addFields( $resultPageSet->getPageTableFields() );
236 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
237 if ( $this->hasNS ) {
246 foreach ( $this->redirTitles as $t ) {
247 $redirNs =
$t->getNamespace();
248 $redirDBkey =
$t->getDBkey();
249 $expr = $db->expr( $this->bl_title,
'=', $redirDBkey );
250 if ( $this->hasNS ) {
251 $expr = $expr->and( $this->bl_ns,
'=', $redirNs );
253 $titleWhere[] = $expr;
254 $allRedirNs[$redirNs] =
true;
255 $allRedirDBkey[$redirDBkey] =
true;
257 $this->
addWhere( $db->orExpr( $titleWhere ) );
258 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
260 if ( count( $this->cont ) >= 6 ) {
261 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
264 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
265 $conds[ $this->bl_ns ] = $this->cont[2];
267 if ( count( $allRedirDBkey ) > 1 ) {
268 $conds[ $this->bl_title ] = $this->cont[3];
272 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
273 $conds[ $this->bl_from_ns ] = $this->cont[4];
275 $conds[ $this->bl_from ] = $this->cont[5];
277 $this->
addWhere( $db->buildComparison( $op, $conds ) );
279 if ( $this->params[
'filterredir'] ==
'redirects' ) {
281 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
285 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
287 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
289 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
290 $orderBy[] = $this->bl_ns . $sort;
292 if ( count( $allRedirDBkey ) > 1 ) {
293 $orderBy[] = $this->bl_title . $sort;
295 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
296 $orderBy[] = $this->bl_from_ns . $sort;
298 $orderBy[] = $this->bl_from . $sort;
299 $this->
addOption(
'ORDER BY', $orderBy );
300 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
304 $res = $this->
select( __METHOD__ );
306 if ( $resultPageSet ===
null ) {
311 foreach ( $res as $row ) {
312 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
314 if ( ++$count > $this->params[
'limit'] ) {
319 $title = $row->{$this->bl_title};
320 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
321 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
326 if ( count( $this->cont ) < 6 ) {
328 $this->cont[] = $row->{$this->bl_title};
329 $this->cont[] = $row->from_ns;
330 $this->cont[] = $row->page_id;
333 if ( $resultPageSet ===
null ) {
334 $a = [
'pageid' => (int)$row->page_id ];
336 if ( $row->page_is_redirect ) {
337 $a[
'redirect'] =
true;
339 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
341 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
343 $resultPageSet->processDbRow( $row );
352 private function run( $resultPageSet =
null ) {
353 $this->params = $this->extractRequestParams(
false );
354 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
355 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
356 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
358 $result = $this->getResult();
360 if ( $this->params[
'limit'] ==
'max' ) {
361 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
362 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
364 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
365 $this,
'limit', (
int)$this->params[
'limit'], [
366 ParamValidator::PARAM_TYPE =>
'limit',
367 IntegerDef::PARAM_MIN => 1,
368 IntegerDef::PARAM_MAX => $userMax,
369 IntegerDef::PARAM_MAX2 => $botMax,
370 IntegerDef::PARAM_IGNORE_RANGE =>
true,
375 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
378 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
380 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
388 if ( $this->params[
'continue'] !==
null ) {
389 $cont = explode(
'|', $this->params[
'continue'] );
391 switch ( count( $cont ) ) {
394 $this->cont[7] = (int)$cont[7];
395 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
401 $this->cont[6] = (int)$cont[6];
402 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
408 $this->cont[2] = (int)$cont[2];
409 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
412 $this->cont[3] = $cont[3];
415 $this->cont[4] = (int)$cont[4];
416 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
419 $this->cont[5] = (int)$cont[5];
420 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
426 $this->cont[0] = (int)$cont[0];
427 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
430 $this->cont[1] = (int)$cont[1];
431 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
437 $this->dieContinueUsageIf(
true );
440 ksort( $this->cont );
443 $this->runFirstQuery( $resultPageSet );
444 if ( $this->redirect && count( $this->redirTitles ) ) {
445 $this->resetQueryParams();
446 $this->runSecondQuery( $resultPageSet );
450 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
452 if ( $resultPageSet ===
null ) {
454 $code = $this->bl_code;
455 $data = array_map(
static function ( $arr ) use ( $code ) {
456 if ( isset( $arr[
'redirlinks'] ) ) {
457 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
458 ApiResult::setIndexedTagName( $arr[
'redirlinks'], $code );
461 }, array_values( $this->resultArr ) );
462 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
466 ksort( $this->resultArr );
468 if ( count( $this->cont ) >= 7 ) {
469 $startAt = $this->cont[6];
471 $startAt = array_key_first( $this->resultArr );
474 foreach ( $this->resultArr as $pageID => $arr ) {
475 if ( $pageID < $startAt ) {
480 $fit = $result->addValue(
481 [
'query', $this->getModuleName() ],
482 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
484 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
490 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
491 ksort( $redirLinks );
493 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
494 $redirStartAt = $this->cont[7];
496 $redirStartAt = array_key_first( $redirLinks );
498 foreach ( $redirLinks as $key => $redir ) {
499 if ( $key < $redirStartAt ) {
503 $fit = $result->addValue(
504 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
507 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
514 $result->addIndexedTagName(
515 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
526 $result->addIndexedTagName(
527 [
'query', $this->getModuleName() ],
531 if ( $this->continueStr !==
null ) {
532 $this->setContinueEnumParameter(
'continue', $this->continueStr );
540 ParamValidator::PARAM_TYPE =>
'string',
543 ParamValidator::PARAM_TYPE =>
'integer',
546 ApiBase::PARAM_HELP_MSG =>
'api-help-param-continue',
549 ParamValidator::PARAM_ISMULTI =>
true,
550 ParamValidator::PARAM_TYPE =>
'namespace'
553 ParamValidator::PARAM_DEFAULT =>
'ascending',
554 ParamValidator::PARAM_TYPE => [
560 ParamValidator::PARAM_DEFAULT =>
'all',
561 ParamValidator::PARAM_TYPE => [
568 ParamValidator::PARAM_DEFAULT => 10,
569 ParamValidator::PARAM_TYPE =>
'limit',
570 IntegerDef::PARAM_MIN => 1,
571 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
572 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
575 if ( $this->getModuleName() !==
'embeddedin' ) {
576 $retval[
'redirect'] =
false;
584 $title = Title::newMainPage()->getPrefixedText();
585 $mp = rawurlencode( $title );
588 "action=query&list=backlinks&bltitle={$mp}"
589 =>
'apihelp-query+backlinks-example-simple',
590 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
591 =>
'apihelp-query+backlinks-example-generator',
594 'action=query&list=embeddedin&eititle=Template:Stub'
595 =>
'apihelp-query+embeddedin-example-simple',
596 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
597 =>
'apihelp-query+embeddedin-example-generator',
600 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
601 =>
'apihelp-query+imageusage-example-simple',
602 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
603 =>
'apihelp-query+imageusage-example-generator',
607 return $examples[$this->getModuleName()];
612 return $this->helpUrl;