42 private static $settings = [
46 'linktable' =>
'redirect',
50 'showredirects' =>
false,
59 'linktable' =>
'pagelinks',
60 'indexes' => [
'pl_namespace',
'pl_backlinks_namespace' ],
61 'from_namespace' =>
true,
62 'showredirects' =>
true,
67 'linktable' =>
'templatelinks',
68 'from_namespace' =>
true,
69 'showredirects' =>
true,
74 'linktable' =>
'imagelinks',
75 'indexes' => [
'il_to',
'il_backlinks_namespace' ],
76 'from_namespace' =>
true,
78 'exampletitle' =>
'File:Example.jpg',
79 'showredirects' =>
true,
84 private $linksMigration;
96 parent::__construct( $query, $moduleName, self::$settings[$moduleName][
'code'] );
97 $this->linksMigration = $linksMigration;
105 $this->run( $resultPageSet );
111 private function run(
ApiPageSet $resultPageSet =
null ) {
114 $db = $this->
getDB();
116 $prop = array_fill_keys( $params[
'prop'],
true );
117 $emptyString = $db->addQuotes(
'' );
120 $titles = $pageSet->getGoodAndMissingPages();
121 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
125 foreach ( $pageSet->getSpecialPages() as $id =>
$title ) {
131 $p = $settings[
'prefix'];
132 $hasNS = !isset( $settings[
'to_namespace'] );
134 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
135 [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings[
'linktable'] );
137 $bl_namespace =
"{$p}_namespace";
138 $bl_title =
"{$p}_title";
142 $bl_namespace = $settings[
'to_namespace'];
143 $bl_title =
"{$p}_to";
145 $titles = array_filter( $titles,
static function (
$t ) use ( $bl_namespace ) {
146 return $t->getNamespace() === $bl_namespace;
148 $map = array_intersect_key( $map, [ $bl_namespace =>
true ] );
150 $bl_from =
"{$p}_from";
155 if ( $params[
'namespace'] !==
null && count( $params[
'namespace'] ) === 0 ) {
163 if ( $hasNS && count( $map ) > 1 ) {
164 $sortby[$bl_namespace] =
'int';
167 foreach ( $map as $nsTitles ) {
168 $key = array_key_first( $nsTitles );
170 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
171 $sortby[$bl_title] =
'string';
176 if ( $params[
'namespace'] !==
null ) {
177 if ( empty( $settings[
'from_namespace'] ) ) {
178 if ( $this->
getConfig()->
get( MainConfigNames::MiserMode ) ) {
179 $miser_ns = $params[
'namespace'];
181 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
184 $this->
addWhereFld(
"{$p}_from_namespace", $params[
'namespace'] );
185 if ( !empty( $settings[
'from_namespace'] )
186 && $params[
'namespace'] !==
null && count( $params[
'namespace'] ) > 1
188 $sortby[
"{$p}_from_namespace"] =
'int';
192 $sortby[$bl_from] =
'int';
195 $continueFields = array_keys( $sortby );
196 $continueTypes = array_values( $sortby );
197 if ( $params[
'continue'] !==
null ) {
199 $conds = array_combine( $continueFields, $continueValues );
200 $this->
addWhere( $db->buildComparison(
'>=', $conds ) );
204 [ $idxNoFromNS, $idxWithFromNS ] = $settings[
'indexes'] ?? [
'',
'' ];
206 if ( isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
208 $queryInfo = $this->linksMigration->getQueryInfo( $settings[
'linktable'] );
209 $this->
addTables( array_merge( [
'page' ], $queryInfo[
'tables'] ) );
212 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
213 $idxWithFromNS .=
'_target_id';
217 $this->
addTables( [ $settings[
'linktable'],
'page' ] );
219 $this->
addWhere(
"$bl_from = page_id" );
222 $this->
addWhere(
"rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
225 $this->
addFields( array_keys( $sortby ) );
226 $this->
addFields( [
'bl_namespace' => $bl_namespace,
'bl_title' => $bl_title ] );
227 if ( $resultPageSet ===
null ) {
228 $fld_pageid = isset( $prop[
'pageid'] );
229 $fld_title = isset( $prop[
'title'] );
230 $fld_redirect = isset( $prop[
'redirect'] );
233 $this->
addFieldsIf( [
'page_title',
'page_namespace' ], $fld_title );
234 $this->
addFieldsIf(
'page_is_redirect', $fld_redirect );
237 $fld_fragment = isset( $prop[
'fragment'] );
238 $this->
addFieldsIf(
'rd_fragment', $fld_fragment );
240 $this->
addFields( $resultPageSet->getPageTableFields() );
243 $this->
addFieldsIf(
'page_namespace', $miser_ns !==
null );
248 $this->
addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
251 foreach ( $titles as
$t ) {
252 if (
$t->getNamespace() == $bl_namespace ) {
253 $where[] =
"$bl_title = " . $db->addQuotes(
$t->getDBkey() );
259 if ( $params[
'show'] !==
null ) {
261 $show = array_fill_keys( $params[
'show'],
true );
262 if ( isset( $show[
'fragment'] ) && isset( $show[
'!fragment'] ) ||
263 isset( $show[
'redirect'] ) && isset( $show[
'!redirect'] )
267 $this->
addWhereIf(
"rd_fragment != $emptyString", isset( $show[
'fragment'] ) );
269 "rd_fragment = $emptyString OR rd_fragment IS NULL",
270 isset( $show[
'!fragment'] )
272 $this->
addWhereIf( [
'page_is_redirect' => 1 ], isset( $show[
'redirect'] ) );
273 $this->
addWhereIf( [
'page_is_redirect' => 0 ], isset( $show[
'!redirect'] ) );
277 $this->
addOption(
'ORDER BY', array_keys( $sortby ) );
282 if ( !empty( $settings[
'indexes'] ) ) {
283 if ( $params[
'namespace'] !==
null && !empty( $settings[
'from_namespace'] ) ) {
285 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxWithFromNS ] );
287 } elseif ( !isset( $this->linksMigration::$mapping[$settings[
'linktable']] ) ) {
289 $this->
addOption(
'USE INDEX', [ $settings[
'linktable'] => $idxNoFromNS ] );
293 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
297 if ( $resultPageSet ===
null ) {
304 foreach (
$res as $row ) {
305 if ( ++$count > $params[
'limit'] ) {
308 $this->setContinue( $row, $sortby );
312 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
318 $id = $map[$row->bl_namespace][$row->bl_title];
323 $vals[
'pageid'] = (int)$row->page_id;
327 Title::makeTitle( $row->page_namespace, $row->page_title )
331 if ( $fld_fragment && $row->rd_fragment !==
null && $row->rd_fragment !==
'' ) {
332 $vals[
'fragment'] = $row->rd_fragment;
335 if ( $fld_redirect ) {
336 $vals[
'redirect'] = (bool)$row->page_is_redirect;
340 $this->setContinue( $row, $sortby );
347 foreach (
$res as $row ) {
348 if ( ++$count > $params[
'limit'] ) {
351 $this->setContinue( $row, $sortby );
355 if ( $miser_ns !==
null && !in_array( $row->page_namespace, $miser_ns ) ) {
360 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
362 $resultPageSet->populateFromTitles( $titles );
366 private function setContinue( $row, $sortby ) {
368 foreach ( $sortby as $field => $v ) {
369 $cont[] = $row->$field;
383 ParamValidator::PARAM_TYPE => [
387 ParamValidator::PARAM_ISMULTI =>
true,
388 ParamValidator::PARAM_DEFAULT =>
'pageid|title',
392 ParamValidator::PARAM_ISMULTI =>
true,
393 ParamValidator::PARAM_TYPE =>
'namespace',
397 ParamValidator::PARAM_DEFAULT => 10,
398 ParamValidator::PARAM_TYPE =>
'limit',
399 IntegerDef::PARAM_MIN => 1,
408 if ( empty( $settings[
'from_namespace'] ) &&
409 $this->
getConfig()->
get( MainConfigNames::MiserMode ) ) {
411 'api-help-param-limited-in-miser-mode',
415 if ( !empty( $settings[
'showredirects'] ) ) {
416 $ret[
'prop'][ParamValidator::PARAM_TYPE][] =
'redirect';
417 $ret[
'prop'][ParamValidator::PARAM_DEFAULT] .=
'|redirect';
419 if ( isset( $settings[
'props'] ) ) {
420 $ret[
'prop'][ParamValidator::PARAM_TYPE] = array_merge(
421 $ret[
'prop'][ParamValidator::PARAM_TYPE], $settings[
'props']
426 if ( !empty( $settings[
'showredirects'] ) ) {
427 $show[] =
'redirect';
428 $show[] =
'!redirect';
430 if ( isset( $settings[
'show'] ) ) {
431 $show = array_merge( $show, $settings[
'show'] );
435 ParamValidator::PARAM_TYPE => $show,
436 ParamValidator::PARAM_ISMULTI =>
true,
440 unset( $ret[
'show'] );
450 $title = $settings[
'exampletitle'] ?? Title::newMainPage()->getPrefixedText();
451 $etitle = rawurlencode(
$title );
454 "action=query&prop={$name}&titles={$etitle}"
455 =>
"apihelp-$path-example-simple",
456 "action=query&generator={$name}&titles={$etitle}&prop=info"
457 =>
"apihelp-$path-example-generator",
463 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
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.