33 private static $settings = [
37 'linktable' =>
'redirect',
41 'showredirects' =>
false,
50 'linktable' =>
'pagelinks',
51 'indexes' => [
'pl_namespace',
'pl_backlinks_namespace' ],
52 'from_namespace' =>
true,
53 'showredirects' =>
true,
54 'virtualdomain' => PageLinksTable::VIRTUAL_DOMAIN,
59 'linktable' =>
'templatelinks',
60 'from_namespace' =>
true,
61 'showredirects' =>
true,
62 'virtualdomain' => TemplateLinksTable::VIRTUAL_DOMAIN,
67 'linktable' =>
'imagelinks',
68 'indexes' => [
'il_to',
'il_backlinks_namespace' ],
69 'from_namespace' =>
true,
70 'exampletitle' =>
'File:Example.jpg',
71 'showredirects' =>
true,
72 'virtualdomain' => ImageLinksTable::VIRTUAL_DOMAIN,
83 parent::__construct( $query, $moduleName, self::$settings[$moduleName][
'code'] );
84 $this->linksMigration = $linksMigration;
93 $this->run( $resultPageSet );
99 private function run( ?
ApiPageSet $resultPageSet =
null ) {
102 $domain = $settings[
'virtualdomain'] ??
false;
104 $db = $this->
getDB();
107 $prop = array_fill_keys( $params[
'prop'],
true );
110 $titles = $pageSet->getGoodAndMissingPages();
111 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
115 foreach ( $pageSet->getSpecialPages() as $id => $title ) {
117 $map[$title->getNamespace()][$title->getDBkey()] = $id;
121 $p = $settings[
'prefix'];
123 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
124 [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings[
'linktable'] );
126 $bl_namespace =
"{$p}_namespace";
127 $bl_title =
"{$p}_title";
129 $bl_from =
"{$p}_from";
131 $hasNS = !is_int( $bl_namespace );
133 $titles = array_filter( $titles,
static function ( $t ) use ( $bl_namespace ) {
134 return $t->getNamespace() === $bl_namespace;
136 $map = array_intersect_key( $map, [ $bl_namespace =>
true ] );
142 if ( $params[
'namespace'] !==
null && count( $params[
'namespace'] ) === 0 ) {
150 if ( $hasNS && count( $map ) > 1 ) {
151 $sortby[$bl_namespace] =
'int';
154 foreach ( $map as $nsTitles ) {
155 $key = array_key_first( $nsTitles );
157 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
158 $sortby[$bl_title] =
'string';
163 if ( $params[
'namespace'] !==
null ) {
164 if ( empty( $settings[
'from_namespace'] ) ) {
166 $miser_ns =
$params[
'namespace'];
168 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
171 $this->
addWhereFld(
"{$p}_from_namespace", $params[
'namespace'] );
172 if ( !empty( $settings[
'from_namespace'] )
173 && $params[
'namespace'] !==
null && count( $params[
'namespace'] ) > 1
175 $sortby[
"{$p}_from_namespace"] =
'int';
179 $sortby[$bl_from] =
'int';
182 $continueFields = array_keys( $sortby );
183 $continueTypes = array_values( $sortby );
184 if ( $params[
'continue'] !==
null ) {
186 $conds = array_combine( $continueFields, $continueValues );
187 $this->
addWhere( $db->buildComparison(
'>=', $conds ) );
191 [ $idxNoFromNS, $idxWithFromNS ] = $settings[
'indexes'] ?? [
'',
'' ];
193 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
195 $queryInfo = $this->linksMigration->getQueryInfo( $settings[
'linktable'] );
196 $this->
addTables( [
'page', ...$queryInfo[
'tables'] ] );
199 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
200 $idxWithFromNS .=
'_target_id';
204 $this->
addTables( [ $settings[
'linktable'],
'page' ] );
206 $this->
addWhere(
"$bl_from = page_id" );
212 $this->
addFields( array_keys( $sortby ) );
213 $this->
addFields( [
'bl_namespace' => $bl_namespace,
'bl_title' => $bl_title ] );
214 if ( $resultPageSet ===
null ) {
215 $fld_pageid = isset( $prop[
'pageid'] );
216 $fld_title = isset( $prop[
'title'] );
217 $fld_redirect = isset( $prop[
'redirect'] );
220 $this->
addFieldsIf( [
'page_title',
'page_namespace' ], $fld_title );
221 $this->
addFieldsIf(
'page_is_redirect', $fld_redirect );
224 $fld_fragment = isset( $prop[
'fragment'] );
225 $this->
addFieldsIf(
'rd_fragment', $fld_fragment );
227 $this->
addFields( $resultPageSet->getPageTableFields() );
230 $this->
addFieldsIf(
'page_namespace', $miser_ns !==
null );
232 if ( $hasNS && $map ) {
235 $this->
addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
238 foreach ( $titles as $t ) {
239 if (
$t->getNamespace() == $bl_namespace ) {
240 $where[] = $db->expr( $bl_title,
'=',
$t->getDBkey() );
243 $this->
addWhere( $db->orExpr( $where ) );
246 if ( $params[
'show'] !==
null ) {
248 $show = array_fill_keys( $params[
'show'],
true );
249 if ( ( isset( $show[
'fragment'] ) && isset( $show[
'!fragment'] ) ) ||
250 ( isset( $show[
'redirect'] ) && isset( $show[
'!redirect'] ) )
254 $this->
addWhereIf( $db->expr(
'rd_fragment',
'!=',
'' ), isset( $show[
'fragment'] ) );
255 $this->
addWhereIf( [
'rd_fragment' =>
'' ], isset( $show[
'!fragment'] ) );
256 $this->
addWhereIf( [
'page_is_redirect' => 1 ], isset( $show[
'redirect'] ) );
257 $this->
addWhereIf( [
'page_is_redirect' => 0 ], isset( $show[
'!redirect'] ) );
261 $this->
addOption(
'ORDER BY', array_keys( $sortby ) );
266 if ( !empty( $settings[
'indexes'] ) ) {
268 $params[
'namespace'] !==
null &&
269 count( $params[
'namespace'] ) == 1 &&
270 !empty( $settings[
'from_namespace'] )
273 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxWithFromNS ] );
275 } elseif ( !isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
277 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxNoFromNS ] );
281 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
283 $res = $this->
select( __METHOD__ );
285 if ( $resultPageSet ===
null ) {
292 foreach ( $res as $row ) {
293 if ( ++$count > $params[
'limit'] ) {
296 $this->setContinue( $row, $sortby );
300 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
306 $id = $map[$row->bl_namespace][$row->bl_title];
311 $vals[
'pageid'] = (int)$row->page_id;
319 if ( $fld_fragment && $row->rd_fragment !==
'' ) {
320 $vals[
'fragment'] = $row->rd_fragment;
323 if ( $fld_redirect ) {
324 $vals[
'redirect'] = (bool)$row->page_is_redirect;
328 $this->setContinue( $row, $sortby );
335 foreach ( $res as $row ) {
336 if ( ++$count > $params[
'limit'] ) {
339 $this->setContinue( $row, $sortby );
343 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
350 $resultPageSet->populateFromTitles( $titles );
354 private function setContinue( \stdClass $row, array $sortby ) {
356 foreach ( $sortby as $field => $v ) {
357 $cont[] = $row->$field;
373 ParamValidator::PARAM_TYPE => [
377 ParamValidator::PARAM_ISMULTI =>
true,
378 ParamValidator::PARAM_DEFAULT =>
'pageid|title',
382 ParamValidator::PARAM_ISMULTI =>
true,
383 ParamValidator::PARAM_TYPE =>
'namespace',
387 ParamValidator::PARAM_DEFAULT => 10,
388 ParamValidator::PARAM_TYPE =>
'limit',
389 IntegerDef::PARAM_MIN => 1,
398 if ( empty( $settings[
'from_namespace'] ) &&
401 'api-help-param-limited-in-miser-mode',
405 if ( !empty( $settings[
'showredirects'] ) ) {
406 $ret[
'prop'][ParamValidator::PARAM_TYPE][] =
'redirect';
407 $ret[
'prop'][ParamValidator::PARAM_DEFAULT] .=
'|redirect';
409 if ( isset( $settings[
'props'] ) ) {
410 $ret[
'prop'][ParamValidator::PARAM_TYPE] = array_merge(
411 $ret[
'prop'][ParamValidator::PARAM_TYPE], $settings[
'props']
416 if ( !empty( $settings[
'showredirects'] ) ) {
417 $show[] =
'redirect';
418 $show[] =
'!redirect';
420 if ( isset( $settings[
'show'] ) ) {
421 $show = array_merge( $show, $settings[
'show'] );
425 ParamValidator::PARAM_TYPE => $show,
426 ParamValidator::PARAM_ISMULTI =>
true,
430 unset( $ret[
'show'] );
441 $title = $settings[
'exampletitle'] ?? Title::newMainPage()->getPrefixedText();
442 $etitle = rawurlencode( $title );
445 "action=query&prop={$name}&titles={$etitle}"
446 =>
"apihelp-$path-example-simple",
447 "action=query&generator={$name}&titles={$etitle}&prop=info"
448 =>
"apihelp-$path-example-generator",
455 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
460class_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()