40 private string $table;
41 private string $tablePrefix;
42 private string $indexTag;
43 private $fieldTitle =
'title';
44 private $dfltNamespace =
NS_MAIN;
45 private $hasNamespace =
true;
46 private $useIndex =
null;
67 switch ( $moduleName ) {
70 $this->table =
'pagelinks';
71 $this->tablePrefix =
'pl_';
72 $this->useIndex =
'pl_namespace';
73 $this->indexTag =
'l';
75 case 'alltransclusions':
77 $this->table =
'templatelinks';
78 $this->tablePrefix =
'tl_';
80 $this->useIndex =
'tl_namespace';
81 $this->indexTag =
't';
85 $this->table =
'imagelinks';
86 $this->tablePrefix =
'il_';
87 $this->fieldTitle =
'to';
89 $this->hasNamespace =
false;
90 $this->indexTag =
'f';
94 $this->table =
'redirect';
95 $this->tablePrefix =
'rd_';
96 $this->indexTag =
'r';
98 'fragment' =>
'rd_fragment',
99 'interwiki' =>
'rd_interwiki',
106 parent::__construct( $query, $moduleName, $prefix );
107 $this->namespaceInfo = $namespaceInfo;
108 $this->genderCache = $genderCache;
109 $this->linksMigration = $linksMigration;
121 $this->
run( $resultPageSet );
128 private function run( $resultPageSet =
null ) {
129 $db = $this->
getDB();
132 $pfx = $this->tablePrefix;
134 $nsField = $pfx .
'namespace';
135 $titleField = $pfx . $this->fieldTitle;
136 $linktargetReadNew =
false;
137 $targetIdColumn =
'';
138 if ( isset( $this->linksMigration::$mapping[$this->table] ) ) {
139 [ $nsField, $titleField ] = $this->linksMigration->getTitleFields( $this->table );
140 $queryInfo = $this->linksMigration->getQueryInfo( $this->table,
'linktarget',
'STRAIGHT_JOIN' );
141 $this->
addTables( $queryInfo[
'tables'] );
143 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
144 $linktargetReadNew =
true;
145 $targetIdColumn =
"{$pfx}target_id";
149 if ( $this->useIndex ) {
150 $this->
addOption(
'USE INDEX', $this->useIndex );
155 $prop = array_fill_keys(
$params[
'prop'],
true );
156 $fld_ids = isset( $prop[
'ids'] );
157 $fld_title = isset( $prop[
'title'] );
158 if ( $this->hasNamespace ) {
159 $namespace =
$params[
'namespace'];
161 $namespace = $this->dfltNamespace;
165 $matches = array_intersect_key( $prop, $this->props + [
'ids' => 1 ] );
170 'apierror-invalidparammix-cannotusewith',
171 "{$p}prop=" . implode(
'|', array_keys(
$matches ) ),
180 if ( $this->hasNamespace ) {
184 $continue =
$params[
'continue'] !==
null;
186 $op =
$params[
'dir'] ==
'descending' ?
'<=' :
'>=';
189 $this->
addWhere( $db->expr( $titleField, $op, $cont[0] ) );
190 } elseif ( !$linktargetReadNew ) {
192 $this->
addWhere( $db->buildComparison( $op, [
193 $titleField => $cont[0],
194 "{$pfx}from" => $cont[1],
198 $this->
addWhere( $db->buildComparison( $op, [
199 $targetIdColumn => $cont[0],
200 "{$pfx}from" => $cont[1],
206 $from = $continue ||
$params[
'from'] ===
null ? null :
208 $to =
$params[
'to'] ===
null ? null :
212 if ( isset(
$params[
'prefix'] ) ) {
222 $this->
addFields( [
'pl_title' => $titleField ] );
224 foreach ( $this->props as $name => $field ) {
225 $this->
addFieldsIf( $field, isset( $prop[$name] ) );
231 $sort = (
$params[
'dir'] ==
'descending' ?
' DESC' :
'' );
233 if ( $linktargetReadNew ) {
234 $orderBy[] = $targetIdColumn;
236 $orderBy[] = $titleField . $sort;
239 $orderBy[] = $pfx .
'from' . $sort;
241 $this->
addOption(
'ORDER BY', $orderBy );
243 $res = $this->
select( __METHOD__ );
246 if ( $resultPageSet ===
null && $res->numRows() && $this->namespaceInfo->hasGenderDistinction( $namespace ) ) {
248 foreach ( $res as $row ) {
249 $users[] = $row->pl_title;
251 if ( $users !== [] ) {
252 $this->genderCache->doQuery( $users, __METHOD__ );
261 foreach ( $res as $row ) {
262 if ( ++$count > $limit ) {
267 } elseif ( $linktargetReadNew ) {
275 if ( $resultPageSet ===
null ) {
280 $vals[
'fromid'] = (int)$row->pl_from;
283 $title = Title::makeTitle( $namespace, $row->pl_title );
286 foreach ( $this->props as $name => $field ) {
287 if ( isset( $prop[$name] ) && $row->$field !==
null && $row->$field !==
'' ) {
288 $vals[$name] = $row->$field;
291 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
295 } elseif ( $linktargetReadNew ) {
302 } elseif (
$params[
'unique'] ) {
303 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
305 $pageids[] = $row->pl_from;
309 if ( $resultPageSet ===
null ) {
310 $result->addIndexedTagName( [
'query', $this->getModuleName() ], $this->indexTag );
311 } elseif (
$params[
'unique'] ) {
312 $resultPageSet->populateFromTitles( $titles );
314 $resultPageSet->populateFromPageIDs( $pageids );
328 ParamValidator::PARAM_ISMULTI =>
true,
329 ParamValidator::PARAM_DEFAULT =>
'title',
330 ParamValidator::PARAM_TYPE => array_merge(
331 [
'ids',
'title' ], array_keys( $this->props )
336 ParamValidator::PARAM_DEFAULT => $this->dfltNamespace,
337 ParamValidator::PARAM_TYPE =>
'namespace',
341 ParamValidator::PARAM_DEFAULT => 10,
342 ParamValidator::PARAM_TYPE =>
'limit',
343 IntegerDef::PARAM_MIN => 1,
348 ParamValidator::PARAM_DEFAULT =>
'ascending',
349 ParamValidator::PARAM_TYPE => [
355 if ( !$this->hasNamespace ) {
356 unset( $allowedParams[
'namespace'] );
359 return $allowedParams;
363 $p = $this->getModulePrefix();
364 $name = $this->getModuleName();
365 $path = $this->getModulePath();
368 "action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
369 =>
"apihelp-$path-example-b",
370 "action=query&list={$name}&{$p}unique=&{$p}from=B"
371 =>
"apihelp-$path-example-unique",
372 "action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
373 =>
"apihelp-$path-example-unique-generator",
374 "action=query&generator={$name}&g{$p}from=B"
375 =>
"apihelp-$path-example-generator",
380 $name = ucfirst( $this->getModuleName() );
382 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
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.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
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.
Query module to enumerate links from all pages together.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName, NamespaceInfo $namespaceInfo, GenderCache $genderCache, LinksMigration $linksMigration)
getExamplesMessages()
Returns usage examples for this module.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
addFields( $value)
Add a set of fields to select to the internal array.
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)
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 ] )
titlePartToKey( $titlePart, $namespace=NS_MAIN)
Convert an input title or title prefix into a dbkey.
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.
This is the main query class.
const META_TYPE
Key for the 'type' metadata item.