47 private $linksMigration;
54 private string $bl_ns;
55 private string $bl_from;
56 private string $bl_from_ns;
57 private string $bl_table;
58 private string $bl_code;
59 private string $bl_title;
70 private $pageMap = [];
73 private $redirTitles = [];
74 private $continueStr =
null;
77 private $backlinksSettings = [
81 'linktbl' =>
'pagelinks',
82 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
87 'linktbl' =>
'templatelinks',
88 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
93 'linktbl' =>
'imagelinks',
94 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
104 $settings = $this->backlinksSettings[$moduleName];
105 $prefix = $settings[
'prefix'];
106 $code = $settings[
'code'];
107 $this->resultArr = [];
109 parent::__construct( $query, $moduleName, $code );
110 $this->bl_table = $settings[
'linktbl'];
111 $this->hasNS = $moduleName !==
'imageusage';
112 $this->linksMigration = $linksMigration;
113 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
114 [ $this->bl_ns, $this->bl_title ] = $this->linksMigration->
getTitleFields( $this->bl_table );
116 $this->bl_ns = $prefix .
'_namespace';
117 if ( $this->hasNS ) {
118 $this->bl_title = $prefix .
'_title';
120 $this->bl_title = $prefix .
'_to';
123 $this->bl_from = $prefix .
'_from';
124 $this->bl_from_ns = $prefix .
'_from_namespace';
125 $this->bl_code = $code;
126 $this->helpUrl = $settings[
'helpurl'];
138 $this->
run( $resultPageSet );
145 private function runFirstQuery( $resultPageSet =
null ) {
146 $this->
addTables( [ $this->bl_table,
'page' ] );
147 $this->
addWhere(
"{$this->bl_from}=page_id" );
148 if ( $resultPageSet ===
null ) {
149 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
151 $this->
addFields( $resultPageSet->getPageTableFields() );
153 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
155 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
156 $conds = $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle );
159 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
160 if ( $this->hasNS ) {
161 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
165 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
167 if ( count( $this->cont ) >= 2 ) {
168 $db = $this->
getDB();
169 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
170 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
171 $this->
addWhere( $db->buildComparison( $op, [
172 $this->bl_from_ns => $this->cont[0],
173 $this->bl_from => $this->cont[1],
176 $this->
addWhere( $db->buildComparison( $op, [ $this->bl_from => $this->cont[1] ] ) );
180 if ( $this->params[
'filterredir'] ==
'redirects' ) {
182 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
188 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
189 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
191 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
192 $orderBy[] = $this->bl_from_ns . $sort;
194 $orderBy[] = $this->bl_from . $sort;
195 $this->
addOption(
'ORDER BY', $orderBy );
198 $res = $this->
select( __METHOD__ );
200 if ( $resultPageSet ===
null ) {
205 foreach ( $res as $row ) {
206 if ( ++$count > $this->params[
'limit'] ) {
210 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
215 if ( count( $this->cont ) < 2 ) {
216 $this->cont[] = $row->from_ns;
217 $this->cont[] = $row->page_id;
220 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
221 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
222 if ( $row->page_is_redirect ) {
223 $this->redirTitles[] =
$t;
226 if ( $resultPageSet ===
null ) {
227 $a = [
'pageid' => (int)$row->page_id ];
229 if ( $row->page_is_redirect ) {
230 $a[
'redirect'] =
true;
233 $this->resultArr[$a[
'pageid']] = $a;
235 $resultPageSet->processDbRow( $row );
244 private function runSecondQuery( $resultPageSet =
null ) {
245 $db = $this->
getDB();
246 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
247 $queryInfo = $this->linksMigration->getQueryInfo( $this->bl_table, $this->bl_table );
248 $this->
addTables( $queryInfo[
'tables'] );
254 $this->
addJoinConds( [
'page' => [
'JOIN',
"{$this->bl_from}=page_id" ] ] );
256 if ( $resultPageSet ===
null ) {
257 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
259 $this->
addFields( $resultPageSet->getPageTableFields() );
262 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
263 if ( $this->hasNS ) {
272 foreach ( $this->redirTitles as $t ) {
273 $redirNs =
$t->getNamespace();
274 $redirDBkey =
$t->getDBkey();
275 $expr = $db->expr( $this->bl_title,
'=', $redirDBkey );
276 if ( $this->hasNS ) {
277 $expr = $expr->and( $this->bl_ns,
'=', $redirNs );
279 $titleWhere[] = $expr;
280 $allRedirNs[$redirNs] =
true;
281 $allRedirDBkey[$redirDBkey] =
true;
284 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
286 if ( count( $this->cont ) >= 6 ) {
287 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
290 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
291 $conds[ $this->bl_ns ] = $this->cont[2];
293 if ( count( $allRedirDBkey ) > 1 ) {
294 $conds[ $this->bl_title ] = $this->cont[3];
298 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
299 $conds[ $this->bl_from_ns ] = $this->cont[4];
301 $conds[ $this->bl_from ] = $this->cont[5];
303 $this->
addWhere( $db->buildComparison( $op, $conds ) );
305 if ( $this->params[
'filterredir'] ==
'redirects' ) {
307 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
311 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
313 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
315 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
316 $orderBy[] = $this->bl_ns . $sort;
318 if ( count( $allRedirDBkey ) > 1 ) {
319 $orderBy[] = $this->bl_title . $sort;
321 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
322 $orderBy[] = $this->bl_from_ns . $sort;
324 $orderBy[] = $this->bl_from . $sort;
325 $this->
addOption(
'ORDER BY', $orderBy );
326 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
330 $res = $this->
select( __METHOD__ );
332 if ( $resultPageSet ===
null ) {
337 foreach ( $res as $row ) {
338 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
340 if ( ++$count > $this->params[
'limit'] ) {
345 $title = $row->{$this->bl_title};
346 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
347 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
352 if ( count( $this->cont ) < 6 ) {
354 $this->cont[] = $row->{$this->bl_title};
355 $this->cont[] = $row->from_ns;
356 $this->cont[] = $row->page_id;
359 if ( $resultPageSet ===
null ) {
360 $a = [
'pageid' => (int)$row->page_id ];
362 if ( $row->page_is_redirect ) {
363 $a[
'redirect'] =
true;
365 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
367 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
369 $resultPageSet->processDbRow( $row );
378 private function run( $resultPageSet =
null ) {
379 $this->params = $this->extractRequestParams(
false );
380 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
384 $result = $this->getResult();
386 if ( $this->params[
'limit'] ==
'max' ) {
387 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
388 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
390 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
391 $this,
'limit', (
int)$this->params[
'limit'], [
392 ParamValidator::PARAM_TYPE =>
'limit',
393 IntegerDef::PARAM_MIN => 1,
394 IntegerDef::PARAM_MAX => $userMax,
395 IntegerDef::PARAM_MAX2 => $botMax,
396 IntegerDef::PARAM_IGNORE_RANGE =>
true,
401 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
404 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
406 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
414 if ( $this->params[
'continue'] !==
null ) {
415 $cont = explode(
'|', $this->params[
'continue'] );
417 switch ( count( $cont ) ) {
420 $this->cont[7] = (int)$cont[7];
421 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
427 $this->cont[6] = (int)$cont[6];
428 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
434 $this->cont[2] = (int)$cont[2];
435 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
438 $this->cont[3] = $cont[3];
441 $this->cont[4] = (int)$cont[4];
442 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
445 $this->cont[5] = (int)$cont[5];
446 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
452 $this->cont[0] = (int)$cont[0];
453 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
456 $this->cont[1] = (int)$cont[1];
457 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
463 $this->dieContinueUsageIf(
true );
466 ksort( $this->cont );
469 $this->runFirstQuery( $resultPageSet );
470 if ( $this->redirect && count( $this->redirTitles ) ) {
471 $this->resetQueryParams();
472 $this->runSecondQuery( $resultPageSet );
476 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
478 if ( $resultPageSet ===
null ) {
480 $code = $this->bl_code;
481 $data = array_map(
static function ( $arr ) use ( $code ) {
482 if ( isset( $arr[
'redirlinks'] ) ) {
483 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
487 }, array_values( $this->resultArr ) );
488 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
492 ksort( $this->resultArr );
494 if ( count( $this->cont ) >= 7 ) {
495 $startAt = $this->cont[6];
497 $startAt = array_key_first( $this->resultArr );
500 foreach ( $this->resultArr as $pageID => $arr ) {
501 if ( $pageID < $startAt ) {
506 $fit = $result->addValue(
507 [
'query', $this->getModuleName() ],
508 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
510 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
516 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
517 ksort( $redirLinks );
519 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
520 $redirStartAt = $this->cont[7];
522 $redirStartAt = array_key_first( $redirLinks );
524 foreach ( $redirLinks as $key => $redir ) {
525 if ( $key < $redirStartAt ) {
529 $fit = $result->addValue(
530 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
533 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
540 $result->addIndexedTagName(
541 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
552 $result->addIndexedTagName(
553 [
'query', $this->getModuleName() ],
557 if ( $this->continueStr !==
null ) {
558 $this->setContinueEnumParameter(
'continue', $this->continueStr );
565 ParamValidator::PARAM_TYPE =>
'string',
568 ParamValidator::PARAM_TYPE =>
'integer',
574 ParamValidator::PARAM_ISMULTI =>
true,
575 ParamValidator::PARAM_TYPE =>
'namespace'
578 ParamValidator::PARAM_DEFAULT =>
'ascending',
579 ParamValidator::PARAM_TYPE => [
585 ParamValidator::PARAM_DEFAULT =>
'all',
586 ParamValidator::PARAM_TYPE => [
593 ParamValidator::PARAM_DEFAULT => 10,
594 ParamValidator::PARAM_TYPE =>
'limit',
595 IntegerDef::PARAM_MIN => 1,
600 if ( $this->getModuleName() !==
'embeddedin' ) {
601 $retval[
'redirect'] =
false;
608 $title = Title::newMainPage()->getPrefixedText();
609 $mp = rawurlencode( $title );
612 "action=query&list=backlinks&bltitle={$mp}"
613 =>
'apihelp-query+backlinks-example-simple',
614 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
615 =>
'apihelp-query+backlinks-example-generator',
618 'action=query&list=embeddedin&eititle=Template:Stub'
619 =>
'apihelp-query+embeddedin-example-simple',
620 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
621 =>
'apihelp-query+embeddedin-example-generator',
624 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
625 =>
'apihelp-query+imageusage-example-simple',
626 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
627 =>
'apihelp-query+imageusage-example-generator',
631 return $examples[$this->getModuleName()];
635 return $this->helpUrl;