47 private $linksMigration;
53 private $bl_ns, $bl_from, $bl_from_ns, $bl_table, $bl_code, $bl_title, $hasNS;
63 private $pageMap = [];
66 private $redirTitles = [];
67 private $continueStr =
null;
70 private $backlinksSettings = [
74 'linktbl' =>
'pagelinks',
75 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
80 'linktbl' =>
'templatelinks',
81 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
86 'linktbl' =>
'imagelinks',
87 'helpurl' =>
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
97 $settings = $this->backlinksSettings[$moduleName];
98 $prefix = $settings[
'prefix'];
99 $code = $settings[
'code'];
100 $this->resultArr = [];
102 parent::__construct( $query, $moduleName, $code );
103 $this->bl_table = $settings[
'linktbl'];
104 $this->hasNS = $moduleName !==
'imageusage';
105 $this->linksMigration = $linksMigration;
106 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
107 [ $this->bl_ns, $this->bl_title ] = $this->linksMigration->
getTitleFields( $this->bl_table );
109 $this->bl_ns = $prefix .
'_namespace';
110 if ( $this->hasNS ) {
111 $this->bl_title = $prefix .
'_title';
113 $this->bl_title = $prefix .
'_to';
116 $this->bl_from = $prefix .
'_from';
117 $this->bl_from_ns = $prefix .
'_from_namespace';
118 $this->bl_code = $code;
119 $this->helpUrl = $settings[
'helpurl'];
131 $this->
run( $resultPageSet );
138 private function runFirstQuery( $resultPageSet =
null ) {
139 $this->
addTables( [ $this->bl_table,
'page' ] );
140 $this->
addWhere(
"{$this->bl_from}=page_id" );
141 if ( $resultPageSet ===
null ) {
142 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
144 $this->
addFields( $resultPageSet->getPageTableFields() );
146 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
148 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
149 $conds = $this->linksMigration->getLinksConditions( $this->bl_table, $this->rootTitle );
152 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
153 if ( $this->hasNS ) {
154 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
158 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
160 if ( count( $this->cont ) >= 2 ) {
161 $db = $this->
getDB();
162 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
163 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
164 $this->
addWhere( $db->buildComparison( $op, [
165 $this->bl_from_ns => $this->cont[0],
166 $this->bl_from => $this->cont[1],
169 $this->
addWhere( $db->buildComparison( $op, [ $this->bl_from => $this->cont[1] ] ) );
173 if ( $this->params[
'filterredir'] ==
'redirects' ) {
175 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
181 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
182 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
184 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
185 $orderBy[] = $this->bl_from_ns . $sort;
187 $orderBy[] = $this->bl_from . $sort;
188 $this->
addOption(
'ORDER BY', $orderBy );
191 $res = $this->
select( __METHOD__ );
193 if ( $resultPageSet ===
null ) {
198 foreach ( $res as $row ) {
199 if ( ++$count > $this->params[
'limit'] ) {
203 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
208 if ( count( $this->cont ) < 2 ) {
209 $this->cont[] = $row->from_ns;
210 $this->cont[] = $row->page_id;
213 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
214 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
215 if ( $row->page_is_redirect ) {
216 $this->redirTitles[] =
$t;
219 if ( $resultPageSet ===
null ) {
220 $a = [
'pageid' => (int)$row->page_id ];
222 if ( $row->page_is_redirect ) {
223 $a[
'redirect'] =
true;
226 $this->resultArr[$a[
'pageid']] = $a;
228 $resultPageSet->processDbRow( $row );
237 private function runSecondQuery( $resultPageSet =
null ) {
238 $db = $this->
getDB();
239 if ( isset( $this->linksMigration::$mapping[$this->bl_table] ) ) {
240 $queryInfo = $this->linksMigration->getQueryInfo( $this->bl_table, $this->bl_table );
241 $this->
addTables( $queryInfo[
'tables'] );
247 $this->
addJoinConds( [
'page' => [
'JOIN',
"{$this->bl_from}=page_id" ] ] );
249 if ( $resultPageSet ===
null ) {
250 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
252 $this->
addFields( $resultPageSet->getPageTableFields() );
255 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
256 if ( $this->hasNS ) {
265 foreach ( $this->redirTitles as $t ) {
266 $redirNs =
$t->getNamespace();
267 $redirDBkey =
$t->getDBkey();
268 $expr = $db->expr( $this->bl_title,
'=', $redirDBkey );
269 if ( $this->hasNS ) {
270 $expr = $expr->and( $this->bl_ns,
'=', $redirNs );
272 $titleWhere[] = $expr;
273 $allRedirNs[$redirNs] =
true;
274 $allRedirDBkey[$redirDBkey] =
true;
277 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
279 if ( count( $this->cont ) >= 6 ) {
280 $op = $this->params[
'dir'] ==
'descending' ?
'<=' :
'>=';
283 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
284 $conds[ $this->bl_ns ] = $this->cont[2];
286 if ( count( $allRedirDBkey ) > 1 ) {
287 $conds[ $this->bl_title ] = $this->cont[3];
291 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
292 $conds[ $this->bl_from_ns ] = $this->cont[4];
294 $conds[ $this->bl_from ] = $this->cont[5];
296 $this->
addWhere( $db->buildComparison( $op, $conds ) );
298 if ( $this->params[
'filterredir'] ==
'redirects' ) {
300 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
304 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
306 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
308 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
309 $orderBy[] = $this->bl_ns . $sort;
311 if ( count( $allRedirDBkey ) > 1 ) {
312 $orderBy[] = $this->bl_title . $sort;
314 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
315 $orderBy[] = $this->bl_from_ns . $sort;
317 $orderBy[] = $this->bl_from . $sort;
318 $this->
addOption(
'ORDER BY', $orderBy );
319 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
323 $res = $this->
select( __METHOD__ );
325 if ( $resultPageSet ===
null ) {
330 foreach ( $res as $row ) {
331 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
333 if ( ++$count > $this->params[
'limit'] ) {
338 $title = $row->{$this->bl_title};
339 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
340 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
345 if ( count( $this->cont ) < 6 ) {
347 $this->cont[] = $row->{$this->bl_title};
348 $this->cont[] = $row->from_ns;
349 $this->cont[] = $row->page_id;
352 if ( $resultPageSet ===
null ) {
353 $a = [
'pageid' => (int)$row->page_id ];
355 if ( $row->page_is_redirect ) {
356 $a[
'redirect'] =
true;
358 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
360 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
362 $resultPageSet->processDbRow( $row );
371 private function run( $resultPageSet =
null ) {
372 $this->params = $this->extractRequestParams(
false );
373 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
377 $result = $this->getResult();
379 if ( $this->params[
'limit'] ==
'max' ) {
380 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
381 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
383 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
384 $this,
'limit', (
int)$this->params[
'limit'], [
385 ParamValidator::PARAM_TYPE =>
'limit',
386 IntegerDef::PARAM_MIN => 1,
387 IntegerDef::PARAM_MAX => $userMax,
388 IntegerDef::PARAM_MAX2 => $botMax,
389 IntegerDef::PARAM_IGNORE_RANGE =>
true,
394 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
397 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
399 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
407 if ( $this->params[
'continue'] !==
null ) {
408 $cont = explode(
'|', $this->params[
'continue'] );
410 switch ( count( $cont ) ) {
413 $this->cont[7] = (int)$cont[7];
414 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
420 $this->cont[6] = (int)$cont[6];
421 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
427 $this->cont[2] = (int)$cont[2];
428 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
431 $this->cont[3] = $cont[3];
434 $this->cont[4] = (int)$cont[4];
435 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
438 $this->cont[5] = (int)$cont[5];
439 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
445 $this->cont[0] = (int)$cont[0];
446 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
449 $this->cont[1] = (int)$cont[1];
450 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
456 $this->dieContinueUsageIf(
true );
459 ksort( $this->cont );
462 $this->runFirstQuery( $resultPageSet );
463 if ( $this->redirect && count( $this->redirTitles ) ) {
464 $this->resetQueryParams();
465 $this->runSecondQuery( $resultPageSet );
469 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
471 if ( $resultPageSet ===
null ) {
473 $code = $this->bl_code;
474 $data = array_map(
static function ( $arr ) use ( $code ) {
475 if ( isset( $arr[
'redirlinks'] ) ) {
476 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
480 }, array_values( $this->resultArr ) );
481 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
485 ksort( $this->resultArr );
487 if ( count( $this->cont ) >= 7 ) {
488 $startAt = $this->cont[6];
490 $startAt = array_key_first( $this->resultArr );
493 foreach ( $this->resultArr as $pageID => $arr ) {
494 if ( $pageID < $startAt ) {
499 $fit = $result->addValue(
500 [
'query', $this->getModuleName() ],
501 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
503 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
509 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
510 ksort( $redirLinks );
512 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
513 $redirStartAt = $this->cont[7];
515 $redirStartAt = array_key_first( $redirLinks );
517 foreach ( $redirLinks as $key => $redir ) {
518 if ( $key < $redirStartAt ) {
522 $fit = $result->addValue(
523 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
526 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
533 $result->addIndexedTagName(
534 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
545 $result->addIndexedTagName(
546 [
'query', $this->getModuleName() ],
550 if ( $this->continueStr !==
null ) {
551 $this->setContinueEnumParameter(
'continue', $this->continueStr );
558 ParamValidator::PARAM_TYPE =>
'string',
561 ParamValidator::PARAM_TYPE =>
'integer',
567 ParamValidator::PARAM_ISMULTI =>
true,
568 ParamValidator::PARAM_TYPE =>
'namespace'
571 ParamValidator::PARAM_DEFAULT =>
'ascending',
572 ParamValidator::PARAM_TYPE => [
578 ParamValidator::PARAM_DEFAULT =>
'all',
579 ParamValidator::PARAM_TYPE => [
586 ParamValidator::PARAM_DEFAULT => 10,
587 ParamValidator::PARAM_TYPE =>
'limit',
588 IntegerDef::PARAM_MIN => 1,
593 if ( $this->getModuleName() !==
'embeddedin' ) {
594 $retval[
'redirect'] =
false;
601 $title = Title::newMainPage()->getPrefixedText();
602 $mp = rawurlencode( $title );
605 "action=query&list=backlinks&bltitle={$mp}"
606 =>
'apihelp-query+backlinks-example-simple',
607 "action=query&generator=backlinks&gbltitle={$mp}&prop=info"
608 =>
'apihelp-query+backlinks-example-generator',
611 'action=query&list=embeddedin&eititle=Template:Stub'
612 =>
'apihelp-query+embeddedin-example-simple',
613 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
614 =>
'apihelp-query+embeddedin-example-generator',
617 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
618 =>
'apihelp-query+imageusage-example-simple',
619 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
620 =>
'apihelp-query+imageusage-example-generator',
624 return $examples[$this->getModuleName()];
628 return $this->helpUrl;