46 private $linksMigration;
53 private string $bl_ns;
54 private string $bl_from;
55 private string $bl_from_ns;
56 private string $bl_table;
57 private string $bl_code;
58 private string $bl_title;
69 private $pageMap = [];
72 private $redirTitles = [];
73 private $continueStr =
null;
76 private $backlinksSettings = [
80 'linktbl' =>
'pagelinks',
81 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
86 'linktbl' =>
'templatelinks',
87 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
92 'linktbl' =>
'imagelinks',
93 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
103 $settings = $this->backlinksSettings[$moduleName];
104 $prefix = $settings[
'prefix'];
105 $code = $settings[
'code'];
106 $this->resultArr = [];
108 parent::__construct( $query, $moduleName, $code );
109 $this->bl_table = $settings[
'linktbl'];
110 $this->hasNS = $moduleName !==
'imageusage';
111 $this->linksMigration = $linksMigration;
112 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
113 [ $this->bl_ns, $this->bl_title ] = $this->linksMigration->
getTitleFields( $this->bl_table );
115 $this->bl_ns = $prefix .
'_namespace';
116 if ( $this->hasNS ) {
117 $this->bl_title = $prefix .
'_title';
119 $this->bl_title = $prefix .
'_to';
122 $this->bl_from = $prefix .
'_from';
123 $this->bl_from_ns = $prefix .
'_from_namespace';
124 $this->bl_code = $code;
125 $this->helpUrl = $settings[
'helpurl'];
137 $this->
run( $resultPageSet );
144 private function runFirstQuery( $resultPageSet =
null ) {
145 $this->
addTables( [ $this->bl_table,
'page' ] );
146 $this->
addWhere(
"{$this->bl_from}=page_id" );
147 if ( $resultPageSet ===
null ) {
148 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
150 $this->
addFields( $resultPageSet->getPageTableFields() );
152 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
154 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
155 $conds = $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle );
158 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
159 if ( $this->hasNS ) {
160 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
164 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
166 if ( count( $this->cont ) >= 2 ) {
167 $db = $this->
getDB();
168 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
169 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
170 $this->
addWhere( $db->buildComparison( $op, [
171 $this->bl_from_ns => $this->cont[0],
172 $this->bl_from => $this->cont[1],
175 $this->
addWhere( $db->buildComparison( $op, [ $this->bl_from => $this->cont[1] ] ) );
179 if ( $this->params[
'filterredir'] ==
'redirects' ) {
181 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
187 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
188 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
190 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
191 $orderBy[] = $this->bl_from_ns . $sort;
193 $orderBy[] = $this->bl_from . $sort;
194 $this->
addOption(
'ORDER BY', $orderBy );
197 $res = $this->
select( __METHOD__ );
199 if ( $resultPageSet ===
null ) {
204 foreach ( $res as $row ) {
205 if ( ++$count > $this->params[
'limit'] ) {
209 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
214 if ( count( $this->cont ) < 2 ) {
215 $this->cont[] = $row->from_ns;
216 $this->cont[] = $row->page_id;
219 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
220 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
221 if ( $row->page_is_redirect ) {
222 $this->redirTitles[] =
$t;
225 if ( $resultPageSet ===
null ) {
226 $a = [
'pageid' => (int)$row->page_id ];
228 if ( $row->page_is_redirect ) {
229 $a[
'redirect'] =
true;
232 $this->resultArr[$a[
'pageid']] = $a;
234 $resultPageSet->processDbRow( $row );
243 private function runSecondQuery( $resultPageSet =
null ) {
244 $db = $this->
getDB();
245 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
246 $queryInfo = $this->linksMigration->getQueryInfo( $this->bl_table, $this->bl_table );
247 $this->
addTables( $queryInfo[
'tables'] );
253 $this->
addJoinConds( [
'page' => [
'JOIN',
"{$this->bl_from}=page_id" ] ] );
255 if ( $resultPageSet ===
null ) {
256 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
258 $this->
addFields( $resultPageSet->getPageTableFields() );
261 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
262 if ( $this->hasNS ) {
271 foreach ( $this->redirTitles as $t ) {
272 $redirNs =
$t->getNamespace();
273 $redirDBkey =
$t->getDBkey();
274 $expr = $db->expr( $this->bl_title,
'=', $redirDBkey );
275 if ( $this->hasNS ) {
276 $expr = $expr->and( $this->bl_ns,
'=', $redirNs );
278 $titleWhere[] = $expr;
279 $allRedirNs[$redirNs] =
true;
280 $allRedirDBkey[$redirDBkey] =
true;
282 $this->
addWhere( $db->orExpr( $titleWhere ) );
283 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
285 if ( count( $this->cont ) >= 6 ) {
286 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
289 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
290 $conds[ $this->bl_ns ] = $this->cont[2];
292 if ( count( $allRedirDBkey ) > 1 ) {
293 $conds[ $this->bl_title ] = $this->cont[3];
297 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
298 $conds[ $this->bl_from_ns ] = $this->cont[4];
300 $conds[ $this->bl_from ] = $this->cont[5];
302 $this->
addWhere( $db->buildComparison( $op, $conds ) );
304 if ( $this->params[
'filterredir'] ==
'redirects' ) {
306 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
310 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
312 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
314 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
315 $orderBy[] = $this->bl_ns . $sort;
317 if ( count( $allRedirDBkey ) > 1 ) {
318 $orderBy[] = $this->bl_title . $sort;
320 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
321 $orderBy[] = $this->bl_from_ns . $sort;
323 $orderBy[] = $this->bl_from . $sort;
324 $this->
addOption(
'ORDER BY', $orderBy );
325 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
329 $res = $this->
select( __METHOD__ );
331 if ( $resultPageSet ===
null ) {
336 foreach ( $res as $row ) {
337 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
339 if ( ++$count > $this->params[
'limit'] ) {
344 $title = $row->{$this->bl_title};
345 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
346 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
351 if ( count( $this->cont ) < 6 ) {
353 $this->cont[] = $row->{$this->bl_title};
354 $this->cont[] = $row->from_ns;
355 $this->cont[] = $row->page_id;
358 if ( $resultPageSet ===
null ) {
359 $a = [
'pageid' => (int)$row->page_id ];
361 if ( $row->page_is_redirect ) {
362 $a[
'redirect'] =
true;
364 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
366 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
368 $resultPageSet->processDbRow( $row );
377 private function run( $resultPageSet =
null ) {
378 $this->params = $this->extractRequestParams(
false );
379 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
383 $result = $this->getResult();
385 if ( $this->params[
'limit'] ==
'max' ) {
386 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
387 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
389 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
390 $this,
'limit', (
int)$this->params[
'limit'], [
391 ParamValidator::PARAM_TYPE =>
'limit',
392 IntegerDef::PARAM_MIN => 1,
393 IntegerDef::PARAM_MAX => $userMax,
394 IntegerDef::PARAM_MAX2 => $botMax,
395 IntegerDef::PARAM_IGNORE_RANGE =>
true,
400 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
403 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
405 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
413 if ( $this->params[
'continue'] !==
null ) {
414 $cont = explode(
'|', $this->params[
'continue'] );
416 switch ( count( $cont ) ) {
419 $this->cont[7] = (int)$cont[7];
420 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
426 $this->cont[6] = (int)$cont[6];
427 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
433 $this->cont[2] = (int)$cont[2];
434 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
437 $this->cont[3] = $cont[3];
440 $this->cont[4] = (int)$cont[4];
441 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
444 $this->cont[5] = (int)$cont[5];
445 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
451 $this->cont[0] = (int)$cont[0];
452 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
455 $this->cont[1] = (int)$cont[1];
456 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
462 $this->dieContinueUsageIf(
true );
465 ksort( $this->cont );
468 $this->runFirstQuery( $resultPageSet );
469 if ( $this->redirect && count( $this->redirTitles ) ) {
470 $this->resetQueryParams();
471 $this->runSecondQuery( $resultPageSet );
475 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
477 if ( $resultPageSet ===
null ) {
479 $code = $this->bl_code;
480 $data = array_map(
static function ( $arr ) use ( $code ) {
481 if ( isset( $arr[
'redirlinks'] ) ) {
482 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
486 }, array_values( $this->resultArr ) );
487 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
491 ksort( $this->resultArr );
493 if ( count( $this->cont ) >= 7 ) {
494 $startAt = $this->cont[6];
496 $startAt = array_key_first( $this->resultArr );
499 foreach ( $this->resultArr as $pageID => $arr ) {
500 if ( $pageID < $startAt ) {
505 $fit = $result->addValue(
506 [
'query', $this->getModuleName() ],
507 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
509 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
515 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
516 ksort( $redirLinks );
518 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
519 $redirStartAt = $this->cont[7];
521 $redirStartAt = array_key_first( $redirLinks );
523 foreach ( $redirLinks as $key => $redir ) {
524 if ( $key < $redirStartAt ) {
528 $fit = $result->addValue(
529 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
532 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
539 $result->addIndexedTagName(
540 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
551 $result->addIndexedTagName(
552 [
'query', $this->getModuleName() ],
556 if ( $this->continueStr !==
null ) {
557 $this->setContinueEnumParameter(
'continue', $this->continueStr );
564 ParamValidator::PARAM_TYPE =>
'string',
567 ParamValidator::PARAM_TYPE =>
'integer',
573 ParamValidator::PARAM_ISMULTI =>
true,
574 ParamValidator::PARAM_TYPE =>
'namespace'
577 ParamValidator::PARAM_DEFAULT =>
'ascending',
578 ParamValidator::PARAM_TYPE => [
584 ParamValidator::PARAM_DEFAULT =>
'all',
585 ParamValidator::PARAM_TYPE => [
592 ParamValidator::PARAM_DEFAULT => 10,
593 ParamValidator::PARAM_TYPE =>
'limit',
594 IntegerDef::PARAM_MIN => 1,
599 if ( $this->getModuleName() !==
'embeddedin' ) {
600 $retval[
'redirect'] =
false;
607 $title = Title::newMainPage()->getPrefixedText();
608 $mp = rawurlencode( $title );
611 "action=query&list=backlinks&bltitle={$mp}"
612 =>
'apihelp-query+backlinks-example-simple',
613 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
614 =>
'apihelp-query+backlinks-example-generator',
617 'action=query&list=embeddedin&eititle=Template:Stub'
618 =>
'apihelp-query+embeddedin-example-simple',
619 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
620 =>
'apihelp-query+embeddedin-example-generator',
623 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
624 =>
'apihelp-query+imageusage-example-simple',
625 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
626 =>
'apihelp-query+imageusage-example-generator',
630 return $examples[$this->getModuleName()];
634 return $this->helpUrl;