33 private static $settings = [
37 'linktable' =>
'redirect',
41 'showredirects' =>
false,
50 'linktable' =>
'pagelinks',
51 'index' =>
'pl_backlinks_namespace_target_id',
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 'index' =>
'il_backlinks_namespace_target_id',
69 'from_namespace' =>
true,
70 'exampletitle' =>
'File:Example.jpg',
71 'showredirects' =>
true,
72 'virtualdomain' => ImageLinksTable::VIRTUAL_DOMAIN,
81 parent::__construct( $query, $moduleName, self::$settings[$moduleName][
'code'] );
90 $this->run( $resultPageSet );
96 private function run( ?
ApiPageSet $resultPageSet =
null ) {
99 $domain = $settings[
'virtualdomain'] ??
false;
101 $db = $this->
getDB();
104 $prop = array_fill_keys( $params[
'prop'],
true );
107 $titles = $pageSet->getGoodAndMissingPages();
108 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
112 foreach ( $pageSet->getSpecialPages() as $id => $title ) {
114 $map[$title->getNamespace()][$title->getDBkey()] = $id;
118 $p = $settings[
'prefix'];
120 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
121 [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings[
'linktable'] );
123 $bl_namespace =
"{$p}_namespace";
124 $bl_title =
"{$p}_title";
126 $bl_from =
"{$p}_from";
128 $hasNS = !is_int( $bl_namespace );
130 $titles = array_filter( $titles,
static function ( $t ) use ( $bl_namespace ) {
131 return $t->getNamespace() === $bl_namespace;
133 $map = array_intersect_key( $map, [ $bl_namespace =>
true ] );
139 if ( $params[
'namespace'] !==
null && count( $params[
'namespace'] ) === 0 ) {
147 if ( $hasNS && count( $map ) > 1 ) {
148 $sortby[$bl_namespace] =
'int';
151 foreach ( $map as $nsTitles ) {
152 $key = array_key_first( $nsTitles );
154 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
155 $sortby[$bl_title] =
'string';
160 if ( $params[
'namespace'] !==
null ) {
161 if ( empty( $settings[
'from_namespace'] ) ) {
163 $miser_ns =
$params[
'namespace'];
165 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
168 $this->
addWhereFld(
"{$p}_from_namespace", $params[
'namespace'] );
169 if ( !empty( $settings[
'from_namespace'] )
170 && $params[
'namespace'] !==
null && count( $params[
'namespace'] ) > 1
172 $sortby[
"{$p}_from_namespace"] =
'int';
176 $sortby[$bl_from] =
'int';
179 $continueFields = array_keys( $sortby );
180 $continueTypes = array_values( $sortby );
181 if ( $params[
'continue'] !==
null ) {
183 $conds = array_combine( $continueFields, $continueValues );
184 $this->
addWhere( $db->buildComparison(
'>=', $conds ) );
188 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
189 $queryInfo = $this->linksMigration->getQueryInfo( $settings[
'linktable'] );
190 $this->
addTables( [
'page', ...$queryInfo[
'tables'] ] );
193 $this->
addTables( [ $settings[
'linktable'],
'page' ] );
195 $this->
addWhere(
"$bl_from = page_id" );
201 $this->
addFields( array_keys( $sortby ) );
202 $this->
addFields( [
'bl_namespace' => $bl_namespace,
'bl_title' => $bl_title ] );
203 if ( $resultPageSet ===
null ) {
204 $fld_pageid = isset( $prop[
'pageid'] );
205 $fld_title = isset( $prop[
'title'] );
206 $fld_redirect = isset( $prop[
'redirect'] );
209 $this->
addFieldsIf( [
'page_title',
'page_namespace' ], $fld_title );
210 $this->
addFieldsIf(
'page_is_redirect', $fld_redirect );
213 $fld_fragment = isset( $prop[
'fragment'] );
214 $this->
addFieldsIf(
'rd_fragment', $fld_fragment );
216 $this->
addFields( $resultPageSet->getPageTableFields() );
219 $this->
addFieldsIf(
'page_namespace', $miser_ns !==
null );
221 if ( $hasNS && $map ) {
224 $this->
addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
227 foreach ( $titles as $t ) {
228 if (
$t->getNamespace() == $bl_namespace ) {
229 $where[] = $db->expr( $bl_title,
'=',
$t->getDBkey() );
232 $this->
addWhere( $db->orExpr( $where ) );
235 if ( $params[
'show'] !==
null ) {
237 $show = array_fill_keys( $params[
'show'],
true );
238 if ( ( isset( $show[
'fragment'] ) && isset( $show[
'!fragment'] ) ) ||
239 ( isset( $show[
'redirect'] ) && isset( $show[
'!redirect'] ) )
243 $this->
addWhereIf( $db->expr(
'rd_fragment',
'!=',
'' ), isset( $show[
'fragment'] ) );
244 $this->
addWhereIf( [
'rd_fragment' =>
'' ], isset( $show[
'!fragment'] ) );
245 $this->
addWhereIf( [
'page_is_redirect' => 1 ], isset( $show[
'redirect'] ) );
246 $this->
addWhereIf( [
'page_is_redirect' => 0 ], isset( $show[
'!redirect'] ) );
250 $this->
addOption(
'ORDER BY', array_keys( $sortby ) );
255 if ( array_key_exists(
'index', $settings ) &&
256 $params[
'namespace'] !==
null &&
257 count( $params[
'namespace'] ) == 1 &&
258 !empty( $settings[
'from_namespace'] )
260 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $settings[
'index'] ] );
263 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
265 $res = $this->
select( __METHOD__ );
267 if ( $resultPageSet ===
null ) {
274 foreach ( $res as $row ) {
275 if ( ++$count > $params[
'limit'] ) {
278 $this->setContinue( $row, $sortby );
282 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
288 $id = $map[$row->bl_namespace][$row->bl_title];
293 $vals[
'pageid'] = (int)$row->page_id;
302 if ( $fld_fragment && $row->rd_fragment !==
'' ) {
303 $vals[
'fragment'] = $row->rd_fragment;
306 if ( $fld_redirect ) {
307 $vals[
'redirect'] = (bool)$row->page_is_redirect;
311 $this->setContinue( $row, $sortby );
318 foreach ( $res as $row ) {
319 if ( ++$count > $params[
'limit'] ) {
322 $this->setContinue( $row, $sortby );
326 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
333 $resultPageSet->populateFromTitles( $titles );
337 private function setContinue( \stdClass $row, array $sortby ) {
339 foreach ( $sortby as $field => $v ) {
340 $cont[] = $row->$field;
356 ParamValidator::PARAM_TYPE => [
360 ParamValidator::PARAM_ISMULTI =>
true,
361 ParamValidator::PARAM_DEFAULT =>
'pageid|title',
365 ParamValidator::PARAM_ISMULTI =>
true,
366 ParamValidator::PARAM_TYPE =>
'namespace',
370 ParamValidator::PARAM_DEFAULT => 10,
371 ParamValidator::PARAM_TYPE =>
'limit',
372 IntegerDef::PARAM_MIN => 1,
381 if ( empty( $settings[
'from_namespace'] ) &&
384 'api-help-param-limited-in-miser-mode',
388 if ( !empty( $settings[
'showredirects'] ) ) {
389 $ret[
'prop'][ParamValidator::PARAM_TYPE][] =
'redirect';
390 $ret[
'prop'][ParamValidator::PARAM_DEFAULT] .=
'|redirect';
392 if ( isset( $settings[
'props'] ) ) {
393 $ret[
'prop'][ParamValidator::PARAM_TYPE] = array_merge(
394 $ret[
'prop'][ParamValidator::PARAM_TYPE], $settings[
'props']
399 if ( !empty( $settings[
'showredirects'] ) ) {
400 $show[] =
'redirect';
401 $show[] =
'!redirect';
403 if ( isset( $settings[
'show'] ) ) {
404 $show = array_merge( $show, $settings[
'show'] );
408 ParamValidator::PARAM_TYPE => $show,
409 ParamValidator::PARAM_ISMULTI =>
true,
413 unset( $ret[
'show'] );
424 $title = $settings[
'exampletitle'] ?? Title::newMainPage()->getPrefixedText();
425 $etitle = rawurlencode( $title );
428 "action=query&prop={$name}&titles={$etitle}"
429 =>
"apihelp-$path-example-simple",
430 "action=query&generator={$name}&titles={$etitle}&prop=info"
431 =>
"apihelp-$path-example-generator",
438 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
443class_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()