41 private static $settings = [
45 'linktable' =>
'redirect',
49 'showredirects' =>
false,
58 'linktable' =>
'pagelinks',
59 'indexes' => [
'pl_namespace',
'pl_backlinks_namespace' ],
60 'from_namespace' =>
true,
61 'showredirects' =>
true,
66 'linktable' =>
'templatelinks',
67 'from_namespace' =>
true,
68 'showredirects' =>
true,
73 'linktable' =>
'imagelinks',
74 'indexes' => [
'il_to',
'il_backlinks_namespace' ],
75 'from_namespace' =>
true,
77 'exampletitle' =>
'File:Example.jpg',
78 'showredirects' =>
true,
83 private $linksMigration;
95 parent::__construct( $query, $moduleName, self::$settings[$moduleName][
'code'] );
96 $this->linksMigration = $linksMigration;
104 $this->run( $resultPageSet );
110 private function run(
ApiPageSet $resultPageSet =
null ) {
113 $db = $this->
getDB();
115 $prop = array_fill_keys( $params[
'prop'],
true );
116 $emptyString = $db->addQuotes(
'' );
119 $titles = $pageSet->getGoodAndMissingPages();
120 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
124 foreach ( $pageSet->getSpecialPages() as $id =>
$title ) {
130 $p = $settings[
'prefix'];
131 $hasNS = !isset( $settings[
'to_namespace'] );
133 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
134 list( $bl_namespace, $bl_title ) = $this->linksMigration->getTitleFields( $settings[
'linktable'] );
136 $bl_namespace =
"{$p}_namespace";
137 $bl_title =
"{$p}_title";
141 $bl_namespace = $settings[
'to_namespace'];
142 $bl_title =
"{$p}_to";
144 $titles = array_filter( $titles,
static function (
$t ) use ( $bl_namespace ) {
145 return $t->getNamespace() === $bl_namespace;
147 $map = array_intersect_key( $map, [ $bl_namespace =>
true ] );
149 $bl_from =
"{$p}_from";
154 if ( $params[
'namespace'] !==
null && count( $params[
'namespace'] ) === 0 ) {
162 if ( $hasNS && count( $map ) > 1 ) {
163 $sortby[$bl_namespace] =
'ns';
166 foreach ( $map as $nsTitles ) {
168 $key = key( $nsTitles );
169 if ( $theTitle ===
null ) {
172 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
173 $sortby[$bl_title] =
'title';
178 if ( $params[
'namespace'] !==
null ) {
179 if ( empty( $settings[
'from_namespace'] ) ) {
180 if ( $this->
getConfig()->
get( MainConfigNames::MiserMode ) ) {
181 $miser_ns = $params[
'namespace'];
183 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
186 $this->
addWhereFld(
"{$p}_from_namespace", $params[
'namespace'] );
187 if ( !empty( $settings[
'from_namespace'] )
188 && $params[
'namespace'] !==
null && count( $params[
'namespace'] ) > 1
190 $sortby[
"{$p}_from_namespace"] =
'int';
194 $sortby[$bl_from] =
'int';
197 if ( $params[
'continue'] !==
null ) {
198 $cont = explode(
'|', $params[
'continue'] );
201 $i = count( $sortby ) - 1;
202 foreach ( array_reverse( $sortby,
true ) as $field =>
$type ) {
211 $v = $db->addQuotes( $v );
215 if ( $where ===
'' ) {
216 $where =
"$field >= $v";
218 $where =
"$field > $v OR ($field = $v AND ($where))";
227 list( $idxNoFromNS, $idxWithFromNS ) = $settings[
'indexes'] ?? [
'',
'' ];
229 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
231 $queryInfo = $this->linksMigration->getQueryInfo( $settings[
'linktable'] );
232 $this->
addTables( array_merge( [
'page' ], $queryInfo[
'tables'] ) );
235 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
236 $idxWithFromNS .=
'_target_id';
240 $this->
addTables( [ $settings[
'linktable'],
'page' ] );
242 $this->
addWhere(
"$bl_from = page_id" );
245 $this->
addWhere(
"rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
248 $this->
addFields( array_keys( $sortby ) );
249 $this->
addFields( [
'bl_namespace' => $bl_namespace,
'bl_title' => $bl_title ] );
250 if ( $resultPageSet ===
null ) {
251 $fld_pageid = isset( $prop[
'pageid'] );
252 $fld_title = isset( $prop[
'title'] );
253 $fld_redirect = isset( $prop[
'redirect'] );
256 $this->
addFieldsIf( [
'page_title',
'page_namespace' ], $fld_title );
257 $this->
addFieldsIf(
'page_is_redirect', $fld_redirect );
260 $fld_fragment = isset( $prop[
'fragment'] );
261 $this->
addFieldsIf(
'rd_fragment', $fld_fragment );
263 $this->
addFields( $resultPageSet->getPageTableFields() );
266 $this->
addFieldsIf(
'page_namespace', $miser_ns !==
null );
271 $this->
addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
274 foreach ( $titles as
$t ) {
275 if (
$t->getNamespace() == $bl_namespace ) {
276 $where[] =
"$bl_title = " . $db->addQuotes(
$t->getDBkey() );
282 if ( $params[
'show'] !==
null ) {
284 $show = array_fill_keys( $params[
'show'],
true );
285 if ( isset( $show[
'fragment'] ) && isset( $show[
'!fragment'] ) ||
286 isset( $show[
'redirect'] ) && isset( $show[
'!redirect'] )
290 $this->
addWhereIf(
"rd_fragment != $emptyString", isset( $show[
'fragment'] ) );
292 "rd_fragment = $emptyString OR rd_fragment IS NULL",
293 isset( $show[
'!fragment'] )
295 $this->
addWhereIf( [
'page_is_redirect' => 1 ], isset( $show[
'redirect'] ) );
296 $this->
addWhereIf( [
'page_is_redirect' => 0 ], isset( $show[
'!redirect'] ) );
300 $this->
addOption(
'ORDER BY', array_keys( $sortby ) );
305 if ( !empty( $settings[
'indexes'] ) ) {
306 if ( $params[
'namespace'] !==
null && !empty( $settings[
'from_namespace'] ) ) {
308 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxWithFromNS ] );
310 } elseif ( !isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
312 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxNoFromNS ] );
316 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
320 if ( $resultPageSet ===
null ) {
327 foreach (
$res as $row ) {
328 if ( ++$count > $params[
'limit'] ) {
331 $this->setContinue( $row, $sortby );
335 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
341 $id = $map[$row->bl_namespace][$row->bl_title];
346 $vals[
'pageid'] = (int)$row->page_id;
350 Title::makeTitle( $row->page_namespace, $row->page_title )
354 if ( $fld_fragment && $row->rd_fragment !==
null && $row->rd_fragment !==
'' ) {
355 $vals[
'fragment'] = $row->rd_fragment;
358 if ( $fld_redirect ) {
359 $vals[
'redirect'] = (bool)$row->page_is_redirect;
363 $this->setContinue( $row, $sortby );
370 foreach (
$res as $row ) {
371 if ( ++$count > $params[
'limit'] ) {
374 $this->setContinue( $row, $sortby );
378 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
385 $resultPageSet->populateFromTitles( $titles );
389 private function setContinue( $row, $sortby ) {
391 foreach ( $sortby as $field => $v ) {
392 $cont[] = $row->$field;
406 ParamValidator::PARAM_TYPE => [
410 ParamValidator::PARAM_ISMULTI =>
true,
411 ParamValidator::PARAM_DEFAULT =>
'pageid|title',
415 ParamValidator::PARAM_ISMULTI =>
true,
416 ParamValidator::PARAM_TYPE =>
'namespace',
420 ParamValidator::PARAM_DEFAULT => 10,
421 ParamValidator::PARAM_TYPE =>
'limit',
422 IntegerDef::PARAM_MIN => 1,
431 if ( empty( $settings[
'from_namespace'] ) &&
432 $this->
getConfig()->
get( MainConfigNames::MiserMode ) ) {
434 'api-help-param-limited-in-miser-mode',
438 if ( !empty( $settings[
'showredirects'] ) ) {
439 $ret[
'prop'][ParamValidator::PARAM_TYPE][] =
'redirect';
440 $ret[
'prop'][ParamValidator::PARAM_DEFAULT] .=
'|redirect';
442 if ( isset( $settings[
'props'] ) ) {
443 $ret[
'prop'][ParamValidator::PARAM_TYPE] = array_merge(
444 $ret[
'prop'][ParamValidator::PARAM_TYPE], $settings[
'props']
449 if ( !empty( $settings[
'showredirects'] ) ) {
450 $show[] =
'redirect';
451 $show[] =
'!redirect';
453 if ( isset( $settings[
'show'] ) ) {
454 $show = array_merge( $show, $settings[
'show'] );
458 ParamValidator::PARAM_TYPE => $show,
459 ParamValidator::PARAM_ISMULTI =>
true,
463 unset( $ret[
'show'] );
473 $title = $settings[
'exampletitle'] ?? Title::newMainPage()->getPrefixedText();
474 $etitle = rawurlencode(
$title );
477 "action=query&prop={$name}&titles={$etitle}"
478 =>
"apihelp-$path-example-simple",
479 "action=query&generator={$name}&titles={$etitle}&prop=info"
480 =>
"apihelp-$path-example-generator",
486 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
const LIMIT_BIG1
Fast query, standard limit.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
getModulePath()
Get the path to this module.
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
const LIMIT_BIG2
Fast query, apihighlimits limit.
getModuleName()
Get the name of the module being executed by this instance.
This class contains a list of pages that the client has requested.
This implements prop=redirects, prop=linkshere, prop=catmembers, prop=transcludedin,...
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, $moduleName, LinksMigration $linksMigration)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getPageSet()
Get the PageSet object to work on.
This is the main query class.
A class containing constants representing the names of configuration variables.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.