40 private $table, $tablePrefix, $indexTag;
41 private $fieldTitle =
'title';
42 private $dfltNamespace =
NS_MAIN;
43 private $hasNamespace =
true;
44 private $useIndex =
null;
65 switch ( $moduleName ) {
68 $this->table =
'pagelinks';
69 $this->tablePrefix =
'pl_';
70 $this->useIndex =
'pl_namespace';
71 $this->indexTag =
'l';
73 case 'alltransclusions':
75 $this->table =
'templatelinks';
76 $this->tablePrefix =
'tl_';
78 $this->useIndex =
'tl_namespace';
79 $this->indexTag =
't';
83 $this->table =
'imagelinks';
84 $this->tablePrefix =
'il_';
85 $this->fieldTitle =
'to';
87 $this->hasNamespace =
false;
88 $this->indexTag =
'f';
92 $this->table =
'redirect';
93 $this->tablePrefix =
'rd_';
94 $this->indexTag =
'r';
96 'fragment' =>
'rd_fragment',
97 'interwiki' =>
'rd_interwiki',
104 parent::__construct( $query, $moduleName, $prefix );
105 $this->namespaceInfo = $namespaceInfo;
106 $this->genderCache = $genderCache;
107 $this->linksMigration = $linksMigration;
119 $this->
run( $resultPageSet );
126 private function run( $resultPageSet =
null ) {
127 $db = $this->
getDB();
130 $pfx = $this->tablePrefix;
132 $nsField = $pfx .
'namespace';
133 $titleField = $pfx . $this->fieldTitle;
134 $linktargetReadNew =
false;
135 $targetIdColumn =
'';
136 if ( isset( $this->linksMigration::$mapping[$this->table] ) ) {
137 [ $nsField, $titleField ] = $this->linksMigration->getTitleFields( $this->table );
138 $queryInfo = $this->linksMigration->getQueryInfo( $this->table,
'linktarget',
'STRAIGHT_JOIN' );
139 $this->
addTables( $queryInfo[
'tables'] );
141 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
142 $linktargetReadNew =
true;
143 $targetIdColumn =
"{$pfx}target_id";
147 if ( $this->useIndex ) {
148 $this->
addOption(
'USE INDEX', $this->useIndex );
153 $prop = array_fill_keys(
$params[
'prop'],
true );
154 $fld_ids = isset( $prop[
'ids'] );
155 $fld_title = isset( $prop[
'title'] );
156 if ( $this->hasNamespace ) {
157 $namespace =
$params[
'namespace'];
159 $namespace = $this->dfltNamespace;
163 $matches = array_intersect_key( $prop, $this->props + [
'ids' => 1 ] );
168 'apierror-invalidparammix-cannotusewith',
169 "{$p}prop=" . implode(
'|', array_keys(
$matches ) ),
178 if ( $this->hasNamespace ) {
182 $continue =
$params[
'continue'] !==
null;
184 $op =
$params[
'dir'] ==
'descending' ?
'<=' :
'>=';
187 $this->
addWhere( $db->expr( $titleField, $op, $cont[0] ) );
188 } elseif ( !$linktargetReadNew ) {
190 $this->
addWhere( $db->buildComparison( $op, [
191 $titleField => $cont[0],
192 "{$pfx}from" => $cont[1],
196 $this->
addWhere( $db->buildComparison( $op, [
197 $targetIdColumn => $cont[0],
198 "{$pfx}from" => $cont[1],
204 $from = $continue ||
$params[
'from'] ===
null ? null :
206 $to =
$params[
'to'] ===
null ? null :
210 if ( isset(
$params[
'prefix'] ) ) {
220 $this->
addFields( [
'pl_title' => $titleField ] );
222 foreach ( $this->props as $name => $field ) {
223 $this->
addFieldsIf( $field, isset( $prop[$name] ) );
229 $sort = (
$params[
'dir'] ==
'descending' ?
' DESC' :
'' );
231 if ( $linktargetReadNew ) {
232 $orderBy[] = $targetIdColumn;
234 $orderBy[] = $titleField . $sort;
237 $orderBy[] = $pfx .
'from' . $sort;
239 $this->
addOption(
'ORDER BY', $orderBy );
241 $res = $this->
select( __METHOD__ );
244 if ( $resultPageSet ===
null && $res->numRows() && $this->namespaceInfo->hasGenderDistinction( $namespace ) ) {
246 foreach ( $res as $row ) {
247 $users[] = $row->pl_title;
249 if ( $users !== [] ) {
250 $this->genderCache->doQuery( $users, __METHOD__ );
259 foreach ( $res as $row ) {
260 if ( ++$count > $limit ) {
265 } elseif ( $linktargetReadNew ) {
273 if ( $resultPageSet ===
null ) {
278 $vals[
'fromid'] = (int)$row->pl_from;
281 $title = Title::makeTitle( $namespace, $row->pl_title );
284 foreach ( $this->props as $name => $field ) {
285 if ( isset( $prop[$name] ) && $row->$field !==
null && $row->$field !==
'' ) {
286 $vals[$name] = $row->$field;
289 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
293 } elseif ( $linktargetReadNew ) {
300 } elseif (
$params[
'unique'] ) {
301 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
303 $pageids[] = $row->pl_from;
307 if ( $resultPageSet ===
null ) {
308 $result->addIndexedTagName( [
'query', $this->getModuleName() ], $this->indexTag );
309 } elseif (
$params[
'unique'] ) {
310 $resultPageSet->populateFromTitles( $titles );
312 $resultPageSet->populateFromPageIDs( $pageids );
326 ParamValidator::PARAM_ISMULTI =>
true,
327 ParamValidator::PARAM_DEFAULT =>
'title',
328 ParamValidator::PARAM_TYPE => array_merge(
329 [
'ids',
'title' ], array_keys( $this->props )
334 ParamValidator::PARAM_DEFAULT => $this->dfltNamespace,
335 ParamValidator::PARAM_TYPE =>
'namespace',
339 ParamValidator::PARAM_DEFAULT => 10,
340 ParamValidator::PARAM_TYPE =>
'limit',
341 IntegerDef::PARAM_MIN => 1,
346 ParamValidator::PARAM_DEFAULT =>
'ascending',
347 ParamValidator::PARAM_TYPE => [
353 if ( !$this->hasNamespace ) {
354 unset( $allowedParams[
'namespace'] );
357 return $allowedParams;
361 $p = $this->getModulePrefix();
362 $name = $this->getModuleName();
363 $path = $this->getModulePath();
366 "action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
367 =>
"apihelp-$path-example-b",
368 "action=query&list={$name}&{$p}unique=&{$p}from=B"
369 =>
"apihelp-$path-example-unique",
370 "action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
371 =>
"apihelp-$path-example-unique-generator",
372 "action=query&generator={$name}&g{$p}from=B"
373 =>
"apihelp-$path-example-generator",
378 $name = ucfirst( $this->getModuleName() );
380 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.