42 private string $table;
43 private string $tablePrefix;
44 private string $indexTag;
46 private $fieldTitle =
'title';
48 private $dfltNamespace =
NS_MAIN;
50 private $hasNamespace =
true;
52 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->indexTag =
't';
84 $this->table =
'imagelinks';
85 $this->tablePrefix =
'il_';
86 $this->fieldTitle =
'to';
88 $this->hasNamespace =
false;
89 $this->indexTag =
'f';
93 $this->table =
'redirect';
94 $this->tablePrefix =
'rd_';
95 $this->indexTag =
'r';
97 'fragment' =>
'rd_fragment',
98 'interwiki' =>
'rd_interwiki',
105 parent::__construct( $query, $moduleName, $prefix );
106 $this->namespaceInfo = $namespaceInfo;
107 $this->genderCache = $genderCache;
108 $this->linksMigration = $linksMigration;
120 $this->run( $resultPageSet );
127 private function run( $resultPageSet =
null ) {
128 $db = $this->
getDB();
131 $pfx = $this->tablePrefix;
133 $nsField = $pfx .
'namespace';
134 $titleField = $pfx . $this->fieldTitle;
135 $linktargetReadNew =
false;
136 $targetIdColumn =
'';
137 if ( isset( $this->linksMigration::$mapping[$this->table] ) ) {
138 [ $nsField, $titleField ] = $this->linksMigration->getTitleFields( $this->table );
139 $queryInfo = $this->linksMigration->getQueryInfo( $this->table,
'linktarget',
'STRAIGHT_JOIN' );
140 $this->
addTables( $queryInfo[
'tables'] );
142 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
143 $linktargetReadNew =
true;
144 $targetIdColumn =
"{$pfx}target_id";
148 if ( $this->useIndex ) {
149 $this->
addOption(
'USE INDEX', $this->useIndex );
154 $prop = array_fill_keys( $params[
'prop'],
true );
155 $fld_ids = isset( $prop[
'ids'] );
156 $fld_title = isset( $prop[
'title'] );
157 if ( $this->hasNamespace ) {
158 $namespace =
$params[
'namespace'];
160 $namespace = $this->dfltNamespace;
163 if ( $params[
'unique'] ) {
164 $matches = array_intersect_key( $prop, $this->props + [
'ids' => 1 ] );
169 'apierror-invalidparammix-cannotusewith',
170 "{$p}prop=" . implode(
'|', array_keys(
$matches ) ),
179 if ( $this->hasNamespace ) {
183 $continue =
$params[
'continue'] !==
null;
185 $op =
$params[
'dir'] ==
'descending' ?
'<=' :
'>=';
186 if ( $params[
'unique'] ) {
188 $this->
addWhere( $db->expr( $titleField, $op, $cont[0] ) );
189 } elseif ( !$linktargetReadNew ) {
191 $this->
addWhere( $db->buildComparison( $op, [
192 $titleField => $cont[0],
193 "{$pfx}from" => $cont[1],
197 $this->
addWhere( $db->buildComparison( $op, [
198 $targetIdColumn => $cont[0],
199 "{$pfx}from" => $cont[1],
205 $from = $continue ||
$params[
'from'] ===
null ? null :
207 $to =
$params[
'to'] ===
null ? null :
211 if ( isset( $params[
'prefix'] ) ) {
216 new LikeValue( $this->
titlePartToKey( $params[
'prefix'], $namespace ), $db->anyString() )
221 $this->
addFields( [
'pl_title' => $titleField ] );
222 $this->
addFieldsIf( [
'pl_from' => $pfx .
'from' ], !$params[
'unique'] );
223 foreach ( $this->props as $name => $field ) {
224 $this->
addFieldsIf( $field, isset( $prop[$name] ) );
230 $sort = (
$params[
'dir'] ==
'descending' ?
' DESC' :
'' );
232 if ( $linktargetReadNew ) {
233 $orderBy[] = $targetIdColumn;
235 $orderBy[] = $titleField . $sort;
237 if ( !$params[
'unique'] ) {
238 $orderBy[] = $pfx .
'from' . $sort;
240 $this->
addOption(
'ORDER BY', $orderBy );
242 $res = $this->
select( __METHOD__ );
245 if ( $resultPageSet ===
null && $res->numRows() && $this->namespaceInfo->hasGenderDistinction( $namespace ) ) {
247 foreach ( $res as $row ) {
248 $users[] = $row->pl_title;
250 if ( $users !== [] ) {
251 $this->genderCache->doQuery( $users, __METHOD__ );
260 foreach ( $res as $row ) {
261 if ( ++$count > $limit ) {
264 if ( $params[
'unique'] ) {
266 } elseif ( $linktargetReadNew ) {
274 if ( $resultPageSet ===
null ) {
279 $vals[
'fromid'] = (int)$row->pl_from;
282 $title = Title::makeTitle( $namespace, $row->pl_title );
285 foreach ( $this->props as $name => $field ) {
286 if ( isset( $prop[$name] ) && $row->$field !==
null && $row->$field !==
'' ) {
287 $vals[$name] = $row->$field;
290 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
292 if ( $params[
'unique'] ) {
294 } elseif ( $linktargetReadNew ) {
301 } elseif ( $params[
'unique'] ) {
302 $titles[] = Title::makeTitle( $namespace, $row->pl_title );
304 $pageids[] = $row->pl_from;
308 if ( $resultPageSet ===
null ) {
309 $result->addIndexedTagName( [
'query', $this->getModuleName() ], $this->indexTag );
310 } elseif ( $params[
'unique'] ) {
311 $resultPageSet->populateFromTitles( $titles );
313 $resultPageSet->populateFromPageIDs( $pageids );
320 ApiBase::PARAM_HELP_MSG =>
'api-help-param-continue',
327 ParamValidator::PARAM_ISMULTI =>
true,
328 ParamValidator::PARAM_DEFAULT =>
'title',
329 ParamValidator::PARAM_TYPE => array_merge(
330 [
'ids',
'title' ], array_keys( $this->props )
332 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
335 ParamValidator::PARAM_DEFAULT => $this->dfltNamespace,
336 ParamValidator::PARAM_TYPE =>
'namespace',
340 ParamValidator::PARAM_DEFAULT => 10,
341 ParamValidator::PARAM_TYPE =>
'limit',
342 IntegerDef::PARAM_MIN => 1,
343 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
344 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
347 ParamValidator::PARAM_DEFAULT =>
'ascending',
348 ParamValidator::PARAM_TYPE => [
354 if ( !$this->hasNamespace ) {
355 unset( $allowedParams[
'namespace'] );
358 return $allowedParams;
362 $p = $this->getModulePrefix();
363 $name = $this->getModuleName();
364 $path = $this->getModulePath();
367 "action=query&list={$name}&{$p}from=B&{$p}prop=ids|title"
368 =>
"apihelp-$path-example-b",
369 "action=query&list={$name}&{$p}unique=&{$p}from=B"
370 =>
"apihelp-$path-example-unique",
371 "action=query&generator={$name}&g{$p}unique=&g{$p}from=B"
372 =>
"apihelp-$path-example-unique-generator",
373 "action=query&generator={$name}&g{$p}from=B"
374 =>
"apihelp-$path-example-generator",
379 $name = ucfirst( $this->getModuleName() );
381 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
386class_alias( ApiQueryAllLinks::class,
'ApiQueryAllLinks' );