48 private $linksMigration;
57 private string $bl_ns;
58 private string $bl_from;
59 private string $bl_from_ns;
60 private string $bl_table;
61 private string $bl_code;
62 private string $bl_title;
73 private $pageMap = [];
78 private $redirTitles = [];
80 private $continueStr =
null;
83 private $backlinksSettings = [
87 'linktbl' =>
'pagelinks',
88 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
93 'linktbl' =>
'templatelinks',
94 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
99 'linktbl' =>
'imagelinks',
100 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
105 $settings = $this->backlinksSettings[$moduleName];
106 $prefix = $settings[
'prefix'];
107 $code = $settings[
'code'];
108 $this->resultArr = [];
110 parent::__construct( $query, $moduleName, $code );
111 $this->bl_table = $settings[
'linktbl'];
112 $this->hasNS = $moduleName !==
'imageusage';
113 $this->linksMigration = $linksMigration;
114 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
115 [ $this->bl_ns, $this->bl_title ] = $this->linksMigration->
getTitleFields( $this->bl_table );
117 $this->bl_ns = $prefix .
'_namespace';
118 if ( $this->hasNS ) {
119 $this->bl_title = $prefix .
'_title';
121 $this->bl_title = $prefix .
'_to';
124 $this->bl_from = $prefix .
'_from';
125 $this->bl_from_ns = $prefix .
'_from_namespace';
126 $this->bl_code = $code;
127 $this->helpUrl = $settings[
'helpurl'];
139 $this->
run( $resultPageSet );
146 private function runFirstQuery( $resultPageSet =
null ) {
147 $this->
addTables( [ $this->bl_table,
'page' ] );
148 $this->
addWhere(
"{$this->bl_from}=page_id" );
149 if ( $resultPageSet ===
null ) {
150 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
152 $this->
addFields( $resultPageSet->getPageTableFields() );
154 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
156 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
157 $conds = $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle );
160 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
161 if ( $this->hasNS ) {
162 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
166 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
168 if ( count( $this->cont ) >= 2 ) {
169 $db = $this->
getDB();
170 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
171 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
172 $this->
addWhere( $db->buildComparison( $op, [
173 $this->bl_from_ns => $this->cont[0],
174 $this->bl_from => $this->cont[1],
177 $this->
addWhere( $db->buildComparison( $op, [ $this->bl_from => $this->cont[1] ] ) );
181 if ( $this->params[
'filterredir'] ==
'redirects' ) {
183 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
189 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
190 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
192 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
193 $orderBy[] = $this->bl_from_ns . $sort;
195 $orderBy[] = $this->bl_from . $sort;
196 $this->
addOption(
'ORDER BY', $orderBy );
199 $res = $this->
select( __METHOD__ );
201 if ( $resultPageSet ===
null ) {
206 foreach ( $res as $row ) {
207 if ( ++$count > $this->params[
'limit'] ) {
211 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
216 if ( count( $this->cont ) < 2 ) {
217 $this->cont[] = $row->from_ns;
218 $this->cont[] = $row->page_id;
221 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
222 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
223 if ( $row->page_is_redirect ) {
224 $this->redirTitles[] =
$t;
227 if ( $resultPageSet ===
null ) {
228 $a = [
'pageid' => (int)$row->page_id ];
230 if ( $row->page_is_redirect ) {
231 $a[
'redirect'] =
true;
234 $this->resultArr[$a[
'pageid']] = $a;
236 $resultPageSet->processDbRow( $row );
245 private function runSecondQuery( $resultPageSet =
null ) {
246 $db = $this->
getDB();
247 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
248 $queryInfo = $this->linksMigration->getQueryInfo( $this->bl_table, $this->bl_table );
249 $this->
addTables( $queryInfo[
'tables'] );
255 $this->
addJoinConds( [
'page' => [
'JOIN',
"{$this->bl_from}=page_id" ] ] );
257 if ( $resultPageSet ===
null ) {
258 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
260 $this->
addFields( $resultPageSet->getPageTableFields() );
263 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
264 if ( $this->hasNS ) {
273 foreach ( $this->redirTitles as $t ) {
274 $redirNs =
$t->getNamespace();
275 $redirDBkey =
$t->getDBkey();
276 $expr = $db->expr( $this->bl_title,
'=', $redirDBkey );
277 if ( $this->hasNS ) {
278 $expr = $expr->and( $this->bl_ns,
'=', $redirNs );
280 $titleWhere[] = $expr;
281 $allRedirNs[$redirNs] =
true;
282 $allRedirDBkey[$redirDBkey] =
true;
284 $this->
addWhere( $db->orExpr( $titleWhere ) );
285 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
287 if ( count( $this->cont ) >= 6 ) {
288 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
291 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
292 $conds[ $this->bl_ns ] = $this->cont[2];
294 if ( count( $allRedirDBkey ) > 1 ) {
295 $conds[ $this->bl_title ] = $this->cont[3];
299 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
300 $conds[ $this->bl_from_ns ] = $this->cont[4];
302 $conds[ $this->bl_from ] = $this->cont[5];
304 $this->
addWhere( $db->buildComparison( $op, $conds ) );
306 if ( $this->params[
'filterredir'] ==
'redirects' ) {
308 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
312 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
314 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
316 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
317 $orderBy[] = $this->bl_ns . $sort;
319 if ( count( $allRedirDBkey ) > 1 ) {
320 $orderBy[] = $this->bl_title . $sort;
322 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
323 $orderBy[] = $this->bl_from_ns . $sort;
325 $orderBy[] = $this->bl_from . $sort;
326 $this->
addOption(
'ORDER BY', $orderBy );
327 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
331 $res = $this->
select( __METHOD__ );
333 if ( $resultPageSet ===
null ) {
338 foreach ( $res as $row ) {
339 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
341 if ( ++$count > $this->params[
'limit'] ) {
346 $title = $row->{$this->bl_title};
347 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
348 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
353 if ( count( $this->cont ) < 6 ) {
355 $this->cont[] = $row->{$this->bl_title};
356 $this->cont[] = $row->from_ns;
357 $this->cont[] = $row->page_id;
360 if ( $resultPageSet ===
null ) {
361 $a = [
'pageid' => (int)$row->page_id ];
363 if ( $row->page_is_redirect ) {
364 $a[
'redirect'] =
true;
366 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
368 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
370 $resultPageSet->processDbRow( $row );
379 private function run( $resultPageSet =
null ) {
380 $this->params = $this->extractRequestParams(
false );
381 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
382 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
383 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
385 $result = $this->getResult();
387 if ( $this->params[
'limit'] ==
'max' ) {
388 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
389 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
391 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
392 $this,
'limit', (
int)$this->params[
'limit'], [
393 ParamValidator::PARAM_TYPE =>
'limit',
394 IntegerDef::PARAM_MIN => 1,
395 IntegerDef::PARAM_MAX => $userMax,
396 IntegerDef::PARAM_MAX2 => $botMax,
397 IntegerDef::PARAM_IGNORE_RANGE =>
true,
402 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
405 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
407 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
415 if ( $this->params[
'continue'] !==
null ) {
416 $cont = explode(
'|', $this->params[
'continue'] );
418 switch ( count( $cont ) ) {
421 $this->cont[7] = (int)$cont[7];
422 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
428 $this->cont[6] = (int)$cont[6];
429 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
435 $this->cont[2] = (int)$cont[2];
436 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
439 $this->cont[3] = $cont[3];
442 $this->cont[4] = (int)$cont[4];
443 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
446 $this->cont[5] = (int)$cont[5];
447 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
453 $this->cont[0] = (int)$cont[0];
454 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
457 $this->cont[1] = (int)$cont[1];
458 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
464 $this->dieContinueUsageIf(
true );
467 ksort( $this->cont );
470 $this->runFirstQuery( $resultPageSet );
471 if ( $this->redirect && count( $this->redirTitles ) ) {
472 $this->resetQueryParams();
473 $this->runSecondQuery( $resultPageSet );
477 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
479 if ( $resultPageSet ===
null ) {
481 $code = $this->bl_code;
482 $data = array_map(
static function ( $arr ) use ( $code ) {
483 if ( isset( $arr[
'redirlinks'] ) ) {
484 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
485 ApiResult::setIndexedTagName( $arr[
'redirlinks'], $code );
488 }, array_values( $this->resultArr ) );
489 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
493 ksort( $this->resultArr );
495 if ( count( $this->cont ) >= 7 ) {
496 $startAt = $this->cont[6];
498 $startAt = array_key_first( $this->resultArr );
501 foreach ( $this->resultArr as $pageID => $arr ) {
502 if ( $pageID < $startAt ) {
507 $fit = $result->addValue(
508 [
'query', $this->getModuleName() ],
509 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
511 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
517 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
518 ksort( $redirLinks );
520 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
521 $redirStartAt = $this->cont[7];
523 $redirStartAt = array_key_first( $redirLinks );
525 foreach ( $redirLinks as $key => $redir ) {
526 if ( $key < $redirStartAt ) {
530 $fit = $result->addValue(
531 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
534 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
541 $result->addIndexedTagName(
542 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
553 $result->addIndexedTagName(
554 [
'query', $this->getModuleName() ],
558 if ( $this->continueStr !==
null ) {
559 $this->setContinueEnumParameter(
'continue', $this->continueStr );
566 ParamValidator::PARAM_TYPE =>
'string',
569 ParamValidator::PARAM_TYPE =>
'integer',
572 ApiBase::PARAM_HELP_MSG =>
'api-help-param-continue',
575 ParamValidator::PARAM_ISMULTI =>
true,
576 ParamValidator::PARAM_TYPE =>
'namespace'
579 ParamValidator::PARAM_DEFAULT =>
'ascending',
580 ParamValidator::PARAM_TYPE => [
586 ParamValidator::PARAM_DEFAULT =>
'all',
587 ParamValidator::PARAM_TYPE => [
594 ParamValidator::PARAM_DEFAULT => 10,
595 ParamValidator::PARAM_TYPE =>
'limit',
596 IntegerDef::PARAM_MIN => 1,
597 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
598 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
601 if ( $this->getModuleName() !==
'embeddedin' ) {
602 $retval[
'redirect'] =
false;
609 $title = Title::newMainPage()->getPrefixedText();
610 $mp = rawurlencode( $title );
613 "action=query&list=backlinks&bltitle={$mp}"
614 =>
'apihelp-query+backlinks-example-simple',
615 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
616 =>
'apihelp-query+backlinks-example-generator',
619 'action=query&list=embeddedin&eititle=Template:Stub'
620 =>
'apihelp-query+embeddedin-example-simple',
621 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
622 =>
'apihelp-query+embeddedin-example-generator',
625 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
626 =>
'apihelp-query+imageusage-example-simple',
627 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
628 =>
'apihelp-query+imageusage-example-generator',
632 return $examples[$this->getModuleName()];
636 return $this->helpUrl;