43 private string $bl_ns;
44 private string $bl_from;
45 private string $bl_from_ns;
46 private string $bl_table;
47 private string $bl_code;
48 private string $bl_title;
50 private $virtual_domain;
61 private $pageMap = [];
66 private $redirTitles = [];
68 private $continueStr =
null;
71 private $backlinksSettings = [
75 'linktbl' =>
'pagelinks',
76 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
77 'virtualdomain' => PageLinksTable::VIRTUAL_DOMAIN,
82 'linktbl' =>
'templatelinks',
83 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
84 'virtualdomain' => TemplateLinksTable::VIRTUAL_DOMAIN,
89 'linktbl' =>
'imagelinks',
90 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
91 'virtualdomain' => ImageLinksTable::VIRTUAL_DOMAIN,
100 $settings = $this->backlinksSettings[$moduleName];
101 $prefix = $settings[
'prefix'];
102 $code = $settings[
'code'];
103 $this->resultArr = [];
105 parent::__construct( $query, $moduleName, $code );
106 $this->bl_table = $settings[
'linktbl'];
107 $this->hasNS = $moduleName !==
'imageusage';
108 $this->linksMigration = $linksMigration;
109 [ $this->bl_ns, $this->bl_title ] = $this->linksMigration->
getTitleFields( $this->bl_table );
110 $this->bl_from = $prefix .
'_from';
111 $this->bl_from_ns = $prefix .
'_from_namespace';
112 $this->bl_code = $code;
113 $this->helpUrl = $settings[
'helpurl'];
114 $this->virtual_domain = $settings[
'virtualdomain'];
128 $this->run( $resultPageSet );
135 private function runFirstQuery( $resultPageSet =
null ) {
136 $this->
addTables( [ $this->bl_table,
'page' ] );
137 $this->
addWhere(
"{$this->bl_from}=page_id" );
138 if ( $resultPageSet ===
null ) {
139 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
141 $this->
addFields( $resultPageSet->getPageTableFields() );
143 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
145 $this->
addWhere( $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle ) );
146 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
148 if ( count( $this->cont ) >= 2 ) {
149 $db = $this->
getDB();
150 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
151 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
152 $this->
addWhere( $db->buildComparison( $op, [
153 $this->bl_from_ns => $this->cont[0],
154 $this->bl_from => $this->cont[1],
157 $this->
addWhere( $db->buildComparison( $op, [ $this->bl_from => $this->cont[1] ] ) );
161 if ( $this->params[
'filterredir'] ==
'redirects' ) {
163 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
169 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
170 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
172 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
173 $orderBy[] = $this->bl_from_ns . $sort;
175 $orderBy[] = $this->bl_from . $sort;
176 $this->
addOption(
'ORDER BY', $orderBy );
180 $res = $this->
select( __METHOD__ );
183 if ( $resultPageSet ===
null ) {
188 foreach ( $res as $row ) {
189 if ( ++$count > $this->params[
'limit'] ) {
193 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
198 if ( count( $this->cont ) < 2 ) {
199 $this->cont[] = $row->from_ns;
200 $this->cont[] = $row->page_id;
203 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
205 if ( $row->page_is_redirect ) {
206 $this->redirTitles[] =
$t;
209 if ( $resultPageSet ===
null ) {
210 $a = [
'pageid' => (int)$row->page_id ];
212 if ( $row->page_is_redirect ) {
213 $a[
'redirect'] =
true;
216 $this->resultArr[$a[
'pageid']] = $a;
218 $resultPageSet->processDbRow( $row );
227 private function runSecondQuery( $resultPageSet =
null ) {
228 $db = $this->
getDB();
229 $queryInfo = $this->linksMigration->getQueryInfo( $this->bl_table, $this->bl_table );
230 $this->
addTables( [ ...$queryInfo[
'tables'],
'page' ] );
231 $this->
addJoinConds( [ ...$queryInfo[
'joins'],
'page' => [
'JOIN',
"{$this->bl_from}=page_id" ] ] );
233 if ( $resultPageSet ===
null ) {
234 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
236 $this->
addFields( $resultPageSet->getPageTableFields() );
239 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
240 if ( $this->hasNS ) {
249 foreach ( $this->redirTitles as $t ) {
250 $redirNs =
$t->getNamespace();
251 $redirDBkey =
$t->getDBkey();
252 $expr = $db->expr( $this->bl_title,
'=', $redirDBkey );
253 if ( $this->hasNS ) {
254 $expr = $expr->and( $this->bl_ns,
'=', $redirNs );
256 $titleWhere[] = $expr;
257 $allRedirNs[$redirNs] =
true;
258 $allRedirDBkey[$redirDBkey] =
true;
260 $this->
addWhere( $db->orExpr( $titleWhere ) );
261 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
263 if ( count( $this->cont ) >= 6 ) {
264 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
267 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
268 $conds[ $this->bl_ns ] = $this->cont[2];
270 if ( count( $allRedirDBkey ) > 1 ) {
271 $conds[ $this->bl_title ] = $this->cont[3];
275 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
276 $conds[ $this->bl_from_ns ] = $this->cont[4];
278 $conds[ $this->bl_from ] = $this->cont[5];
280 $this->
addWhere( $db->buildComparison( $op, $conds ) );
282 if ( $this->params[
'filterredir'] ==
'redirects' ) {
284 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
288 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
290 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
292 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
293 $orderBy[] = $this->bl_ns . $sort;
295 if ( count( $allRedirDBkey ) > 1 ) {
296 $orderBy[] = $this->bl_title . $sort;
298 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
299 $orderBy[] = $this->bl_from_ns . $sort;
301 $orderBy[] = $this->bl_from . $sort;
302 $this->
addOption(
'ORDER BY', $orderBy );
303 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
307 $res = $this->
select( __METHOD__ );
309 if ( $resultPageSet ===
null ) {
314 foreach ( $res as $row ) {
315 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
317 if ( ++$count > $this->params[
'limit'] ) {
322 $title = $row->{$this->bl_title};
323 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
324 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
329 if ( count( $this->cont ) < 6 ) {
331 $this->cont[] = $row->{$this->bl_title};
332 $this->cont[] = $row->from_ns;
333 $this->cont[] = $row->page_id;
336 if ( $resultPageSet ===
null ) {
337 $a = [
'pageid' => (int)$row->page_id ];
339 if ( $row->page_is_redirect ) {
340 $a[
'redirect'] =
true;
342 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
344 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
346 $resultPageSet->processDbRow( $row );
355 private function run( $resultPageSet =
null ) {
356 $this->params = $this->extractRequestParams(
false );
357 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
358 $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
359 $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
361 $result = $this->getResult();
363 if ( $this->params[
'limit'] ==
'max' ) {
364 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
365 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
367 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
368 $this,
'limit', (
int)$this->params[
'limit'], [
369 ParamValidator::PARAM_TYPE =>
'limit',
370 IntegerDef::PARAM_MIN => 1,
371 IntegerDef::PARAM_MAX => $userMax,
372 IntegerDef::PARAM_MAX2 => $botMax,
373 IntegerDef::PARAM_IGNORE_RANGE =>
true,
378 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
381 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
383 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
391 if ( $this->params[
'continue'] !==
null ) {
392 $cont = explode(
'|', $this->params[
'continue'] );
394 switch ( count( $cont ) ) {
397 $this->cont[7] = (int)$cont[7];
398 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
404 $this->cont[6] = (int)$cont[6];
405 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
411 $this->cont[2] = (int)$cont[2];
412 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
415 $this->cont[3] = $cont[3];
418 $this->cont[4] = (int)$cont[4];
419 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
422 $this->cont[5] = (int)$cont[5];
423 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
429 $this->cont[0] = (int)$cont[0];
430 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
433 $this->cont[1] = (int)$cont[1];
434 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
440 $this->dieContinueUsageIf(
true );
443 ksort( $this->cont );
446 $this->runFirstQuery( $resultPageSet );
447 if ( $this->redirect && count( $this->redirTitles ) ) {
448 $this->resetQueryParams();
449 $this->runSecondQuery( $resultPageSet );
453 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
455 if ( $resultPageSet ===
null ) {
457 $code = $this->bl_code;
458 $data = array_map(
static function ( $arr ) use ( $code ) {
459 if ( isset( $arr[
'redirlinks'] ) ) {
460 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
461 ApiResult::setIndexedTagName( $arr[
'redirlinks'], $code );
464 }, array_values( $this->resultArr ) );
465 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
469 ksort( $this->resultArr );
471 if ( count( $this->cont ) >= 7 ) {
472 $startAt = $this->cont[6];
474 $startAt = array_key_first( $this->resultArr );
477 foreach ( $this->resultArr as $pageID => $arr ) {
478 if ( $pageID < $startAt ) {
483 $fit = $result->addValue(
484 [
'query', $this->getModuleName() ],
485 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
487 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
493 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
494 ksort( $redirLinks );
496 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
497 $redirStartAt = $this->cont[7];
499 $redirStartAt = array_key_first( $redirLinks );
501 foreach ( $redirLinks as $key => $redir ) {
502 if ( $key < $redirStartAt ) {
506 $fit = $result->addValue(
507 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
510 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
517 $result->addIndexedTagName(
518 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
529 $result->addIndexedTagName(
530 [
'query', $this->getModuleName() ],
534 if ( $this->continueStr !==
null ) {
535 $this->setContinueEnumParameter(
'continue', $this->continueStr );
543 ParamValidator::PARAM_TYPE =>
'string',
546 ParamValidator::PARAM_TYPE =>
'integer',
549 ApiBase::PARAM_HELP_MSG =>
'api-help-param-continue',
552 ParamValidator::PARAM_ISMULTI =>
true,
553 ParamValidator::PARAM_TYPE =>
'namespace'
556 ParamValidator::PARAM_DEFAULT =>
'ascending',
557 ParamValidator::PARAM_TYPE => [
563 ParamValidator::PARAM_DEFAULT =>
'all',
564 ParamValidator::PARAM_TYPE => [
571 ParamValidator::PARAM_DEFAULT => 10,
572 ParamValidator::PARAM_TYPE =>
'limit',
573 IntegerDef::PARAM_MIN => 1,
574 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
575 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
578 if ( $this->getModuleName() !==
'embeddedin' ) {
579 $retval[
'redirect'] =
false;
587 $title = Title::newMainPage()->getPrefixedText();
588 $mp = rawurlencode( $title );
591 "action=query&list=backlinks&bltitle={$mp}"
592 =>
'apihelp-query+backlinks-example-simple',
593 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
594 =>
'apihelp-query+backlinks-example-generator',
597 'action=query&list=embeddedin&eititle=Template:Stub'
598 =>
'apihelp-query+embeddedin-example-simple',
599 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
600 =>
'apihelp-query+embeddedin-example-generator',
603 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
604 =>
'apihelp-query+imageusage-example-simple',
605 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
606 =>
'apihelp-query+imageusage-example-generator',
610 return $examples[$this->getModuleName()];
615 return $this->helpUrl;