44 private static $settings = [
48 'linktable' =>
'redirect',
52 'showredirects' =>
false,
61 'linktable' =>
'pagelinks',
62 'indexes' => [
'pl_namespace',
'pl_backlinks_namespace' ],
63 'from_namespace' =>
true,
64 'showredirects' =>
true,
69 'linktable' =>
'templatelinks',
70 'from_namespace' =>
true,
71 'showredirects' =>
true,
76 'linktable' =>
'imagelinks',
77 'indexes' => [
'il_to',
'il_backlinks_namespace' ],
78 'from_namespace' =>
true,
80 'exampletitle' =>
'File:Example.jpg',
81 'showredirects' =>
true,
92 parent::__construct( $query, $moduleName, self::$settings[$moduleName][
'code'] );
93 $this->linksMigration = $linksMigration;
101 $this->run( $resultPageSet );
107 private function run( ?
ApiPageSet $resultPageSet =
null ) {
110 $db = $this->
getDB();
112 $prop = array_fill_keys( $params[
'prop'],
true );
115 $titles = $pageSet->getGoodAndMissingPages();
116 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
120 foreach ( $pageSet->getSpecialPages() as $id => $title ) {
122 $map[$title->getNamespace()][$title->getDBkey()] = $id;
126 $p = $settings[
'prefix'];
127 $hasNS = !isset( $settings[
'to_namespace'] );
129 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
130 [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings[
'linktable'] );
132 $bl_namespace =
"{$p}_namespace";
133 $bl_title =
"{$p}_title";
137 $bl_namespace = $settings[
'to_namespace'];
138 $bl_title =
"{$p}_to";
140 $titles = array_filter( $titles,
static function ( $t ) use ( $bl_namespace ) {
141 return $t->getNamespace() === $bl_namespace;
143 $map = array_intersect_key( $map, [ $bl_namespace =>
true ] );
145 $bl_from =
"{$p}_from";
150 if ( $params[
'namespace'] !==
null && count( $params[
'namespace'] ) === 0 ) {
158 if ( $hasNS && count( $map ) > 1 ) {
159 $sortby[$bl_namespace] =
'int';
162 foreach ( $map as $nsTitles ) {
163 $key = array_key_first( $nsTitles );
165 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
166 $sortby[$bl_title] =
'string';
171 if ( $params[
'namespace'] !==
null ) {
172 if ( empty( $settings[
'from_namespace'] ) ) {
174 $miser_ns =
$params[
'namespace'];
176 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
179 $this->
addWhereFld(
"{$p}_from_namespace", $params[
'namespace'] );
180 if ( !empty( $settings[
'from_namespace'] )
181 && $params[
'namespace'] !==
null && count( $params[
'namespace'] ) > 1
183 $sortby[
"{$p}_from_namespace"] =
'int';
187 $sortby[$bl_from] =
'int';
190 $continueFields = array_keys( $sortby );
191 $continueTypes = array_values( $sortby );
192 if ( $params[
'continue'] !==
null ) {
194 $conds = array_combine( $continueFields, $continueValues );
195 $this->
addWhere( $db->buildComparison(
'>=', $conds ) );
199 [ $idxNoFromNS, $idxWithFromNS ] = $settings[
'indexes'] ?? [
'',
'' ];
201 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
203 $queryInfo = $this->linksMigration->getQueryInfo( $settings[
'linktable'] );
204 $this->
addTables( array_merge( [
'page' ], $queryInfo[
'tables'] ) );
207 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
208 $idxWithFromNS .=
'_target_id';
212 $this->
addTables( [ $settings[
'linktable'],
'page' ] );
214 $this->
addWhere(
"$bl_from = page_id" );
220 $this->
addFields( array_keys( $sortby ) );
221 $this->
addFields( [
'bl_namespace' => $bl_namespace,
'bl_title' => $bl_title ] );
222 if ( $resultPageSet ===
null ) {
223 $fld_pageid = isset( $prop[
'pageid'] );
224 $fld_title = isset( $prop[
'title'] );
225 $fld_redirect = isset( $prop[
'redirect'] );
228 $this->
addFieldsIf( [
'page_title',
'page_namespace' ], $fld_title );
229 $this->
addFieldsIf(
'page_is_redirect', $fld_redirect );
232 $fld_fragment = isset( $prop[
'fragment'] );
233 $this->
addFieldsIf(
'rd_fragment', $fld_fragment );
235 $this->
addFields( $resultPageSet->getPageTableFields() );
238 $this->
addFieldsIf(
'page_namespace', $miser_ns !==
null );
240 if ( $hasNS && $map ) {
243 $this->
addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
246 foreach ( $titles as $t ) {
247 if (
$t->getNamespace() == $bl_namespace ) {
248 $where[] = $db->expr( $bl_title,
'=',
$t->getDBkey() );
251 $this->
addWhere( $db->orExpr( $where ) );
254 if ( $params[
'show'] !==
null ) {
256 $show = array_fill_keys( $params[
'show'],
true );
257 if ( ( isset( $show[
'fragment'] ) && isset( $show[
'!fragment'] ) ) ||
258 ( isset( $show[
'redirect'] ) && isset( $show[
'!redirect'] ) )
262 $this->
addWhereIf( $db->expr(
'rd_fragment',
'!=',
'' ), isset( $show[
'fragment'] ) );
263 $this->
addWhereIf( [
'rd_fragment' =>
'' ], isset( $show[
'!fragment'] ) );
264 $this->
addWhereIf( [
'page_is_redirect' => 1 ], isset( $show[
'redirect'] ) );
265 $this->
addWhereIf( [
'page_is_redirect' => 0 ], isset( $show[
'!redirect'] ) );
269 $this->
addOption(
'ORDER BY', array_keys( $sortby ) );
274 if ( !empty( $settings[
'indexes'] ) ) {
276 $params[
'namespace'] !==
null &&
277 count( $params[
'namespace'] ) == 1 &&
278 !empty( $settings[
'from_namespace'] )
281 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxWithFromNS ] );
283 } elseif ( !isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
285 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxNoFromNS ] );
289 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
291 $res = $this->
select( __METHOD__ );
293 if ( $resultPageSet ===
null ) {
300 foreach ( $res as $row ) {
301 if ( ++$count > $params[
'limit'] ) {
304 $this->setContinue( $row, $sortby );
308 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
314 $id = $map[$row->bl_namespace][$row->bl_title];
319 $vals[
'pageid'] = (int)$row->page_id;
323 Title::makeTitle( $row->page_namespace, $row->page_title )
327 if ( $fld_fragment && $row->rd_fragment !==
'' ) {
328 $vals[
'fragment'] = $row->rd_fragment;
331 if ( $fld_redirect ) {
332 $vals[
'redirect'] = (bool)$row->page_is_redirect;
336 $this->setContinue( $row, $sortby );
343 foreach ( $res as $row ) {
344 if ( ++$count > $params[
'limit'] ) {
347 $this->setContinue( $row, $sortby );
351 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
356 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
358 $resultPageSet->populateFromTitles( $titles );
362 private function setContinue( $row, $sortby ) {
364 foreach ( $sortby as $field => $v ) {
365 $cont[] = $row->$field;
379 ParamValidator::PARAM_TYPE => [
383 ParamValidator::PARAM_ISMULTI =>
true,
384 ParamValidator::PARAM_DEFAULT =>
'pageid|title',
388 ParamValidator::PARAM_ISMULTI =>
true,
389 ParamValidator::PARAM_TYPE =>
'namespace',
393 ParamValidator::PARAM_DEFAULT => 10,
394 ParamValidator::PARAM_TYPE =>
'limit',
395 IntegerDef::PARAM_MIN => 1,
404 if ( empty( $settings[
'from_namespace'] ) &&
407 'api-help-param-limited-in-miser-mode',
411 if ( !empty( $settings[
'showredirects'] ) ) {
412 $ret[
'prop'][ParamValidator::PARAM_TYPE][] =
'redirect';
413 $ret[
'prop'][ParamValidator::PARAM_DEFAULT] .=
'|redirect';
415 if ( isset( $settings[
'props'] ) ) {
416 $ret[
'prop'][ParamValidator::PARAM_TYPE] = array_merge(
417 $ret[
'prop'][ParamValidator::PARAM_TYPE], $settings[
'props']
422 if ( !empty( $settings[
'showredirects'] ) ) {
423 $show[] =
'redirect';
424 $show[] =
'!redirect';
426 if ( isset( $settings[
'show'] ) ) {
427 $show = array_merge( $show, $settings[
'show'] );
431 ParamValidator::PARAM_TYPE => $show,
432 ParamValidator::PARAM_ISMULTI =>
true,
436 unset( $ret[
'show'] );
446 $title = $settings[
'exampletitle'] ?? Title::newMainPage()->getPrefixedText();
447 $etitle = rawurlencode( $title );
450 "action=query&prop={$name}&titles={$etitle}"
451 =>
"apihelp-$path-example-simple",
452 "action=query&generator={$name}&titles={$etitle}&prop=info"
453 =>
"apihelp-$path-example-generator",
459 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
464class_alias( ApiQueryBacklinksprop::class,
'ApiQueryBacklinksprop' );
This class contains a list of pages that the client has requested.
A class containing constants representing the names of configuration variables.
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()