129 $this->
addTables( [ $this->bl_table,
'page' ] );
130 $this->
addWhere(
"{$this->bl_from}=page_id" );
131 if ( $resultPageSet ===
null ) {
132 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
134 $this->
addFields( $resultPageSet->getPageTableFields() );
136 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
138 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
139 if ( $this->hasNS ) {
140 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
142 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
144 if ( count( $this->cont ) >= 2 ) {
145 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
146 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
148 "{$this->bl_from_ns} $op {$this->cont[0]} OR " .
149 "({$this->bl_from_ns} = {$this->cont[0]} AND " .
150 "{$this->bl_from} $op= {$this->cont[1]})"
153 $this->
addWhere(
"{$this->bl_from} $op= {$this->cont[1]}" );
157 if ( $this->params[
'filterredir'] ==
'redirects' ) {
159 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->redirect ) {
165 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
166 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
168 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
169 $orderBy[] = $this->bl_from_ns . $sort;
171 $orderBy[] = $this->bl_from . $sort;
172 $this->
addOption(
'ORDER BY', $orderBy );
177 if ( $resultPageSet ===
null ) {
182 foreach (
$res as $row ) {
183 if ( ++$count > $this->params[
'limit'] ) {
187 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
192 if ( count( $this->cont ) < 2 ) {
193 $this->cont[] = $row->from_ns;
194 $this->cont[] = $row->page_id;
197 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
198 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
199 if ( $row->page_is_redirect ) {
200 $this->redirTitles[] =
$t;
203 if ( $resultPageSet ===
null ) {
204 $a = [
'pageid' => (int)$row->page_id ];
206 if ( $row->page_is_redirect ) {
207 $a[
'redirect'] =
true;
210 $this->resultArr[$a[
'pageid']] = $a;
212 $resultPageSet->processDbRow( $row );
222 $db = $this->
getDB();
223 $this->
addTables( [ $this->bl_table,
'page' ] );
224 $this->
addWhere(
"{$this->bl_from}=page_id" );
226 if ( $resultPageSet ===
null ) {
227 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
229 $this->
addFields( $resultPageSet->getPageTableFields() );
232 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
233 if ( $this->hasNS ) {
242 foreach ( $this->redirTitles as
$t ) {
243 $redirNs =
$t->getNamespace();
244 $redirDBkey =
$t->getDBkey();
245 $titleWhere[] =
"{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
246 ( $this->hasNS ?
" AND {$this->bl_ns} = {$redirNs}" :
'' );
247 $allRedirNs[$redirNs] =
true;
248 $allRedirDBkey[$redirDBkey] =
true;
251 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
253 if ( count( $this->cont ) >= 6 ) {
254 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
256 $where =
"{$this->bl_from} $op= {$this->cont[5]}";
259 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
260 $where =
"{$this->bl_from_ns} $op {$this->cont[4]} OR " .
261 "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
263 if ( count( $allRedirDBkey ) > 1 ) {
264 $title = $db->addQuotes( $this->cont[3] );
265 $where =
"{$this->bl_title} $op $title OR " .
266 "({$this->bl_title} = $title AND ($where))";
268 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
269 $where =
"{$this->bl_ns} $op {$this->cont[2]} OR " .
270 "({$this->bl_ns} = {$this->cont[2]} AND ($where))";
275 if ( $this->params[
'filterredir'] ==
'redirects' ) {
277 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
281 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
283 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
285 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
286 $orderBy[] = $this->bl_ns . $sort;
288 if ( count( $allRedirDBkey ) > 1 ) {
289 $orderBy[] = $this->bl_title . $sort;
291 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
292 $orderBy[] = $this->bl_from_ns . $sort;
294 $orderBy[] = $this->bl_from . $sort;
295 $this->
addOption(
'ORDER BY', $orderBy );
296 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
302 if ( $resultPageSet ===
null ) {
307 foreach (
$res as $row ) {
308 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
310 if ( ++$count > $this->params[
'limit'] ) {
315 $title = $row->{$this->bl_title};
316 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
317 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
322 if ( count( $this->cont ) < 6 ) {
324 $this->cont[] = $row->{$this->bl_title};
325 $this->cont[] = $row->from_ns;
326 $this->cont[] = $row->page_id;
329 if ( $resultPageSet ===
null ) {
330 $a = [
'pageid' => (int)$row->page_id ];
332 if ( $row->page_is_redirect ) {
333 $a[
'redirect'] =
true;
335 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
337 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
339 $resultPageSet->processDbRow( $row );
348 private function run( $resultPageSet =
null ) {
349 $this->params = $this->extractRequestParams(
false );
350 $this->redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
354 $result = $this->getResult();
356 if ( $this->params[
'limit'] ==
'max' ) {
357 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
358 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
360 $this->params[
'limit'] = $this->getMain()->getParamValidator()->validateValue(
361 $this,
'limit', (
int)$this->params[
'limit'], [
362 ParamValidator::PARAM_TYPE =>
'limit',
363 IntegerDef::PARAM_MIN => 1,
364 IntegerDef::PARAM_MAX => $userMax,
365 IntegerDef::PARAM_MAX2 => $botMax,
366 IntegerDef::PARAM_IGNORE_RANGE =>
true,
371 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
374 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
376 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
383 if ( $this->params[
'continue'] !==
null ) {
384 $cont = explode(
'|', $this->params[
'continue'] );
386 switch ( count( $cont ) ) {
389 $this->cont[7] = (int)$cont[7];
390 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
396 $this->cont[6] = (int)$cont[6];
397 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
403 $this->cont[2] = (int)$cont[2];
404 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
407 $this->cont[3] = $cont[3];
410 $this->cont[4] = (int)$cont[4];
411 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
414 $this->cont[5] = (int)$cont[5];
415 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
421 $this->cont[0] = (int)$cont[0];
422 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
425 $this->cont[1] = (int)$cont[1];
426 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
432 $this->dieContinueUsageIf(
true );
435 ksort( $this->cont );
438 $this->runFirstQuery( $resultPageSet );
439 if ( $this->redirect && count( $this->redirTitles ) ) {
440 $this->resetQueryParams();
441 $this->runSecondQuery( $resultPageSet );
445 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
447 if ( $resultPageSet ===
null ) {
449 $code = $this->bl_code;
450 $data = array_map(
function ( $arr ) use ( $code ) {
451 if ( isset( $arr[
'redirlinks'] ) ) {
452 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
453 ApiResult::setIndexedTagName( $arr[
'redirlinks'], $code );
456 }, array_values( $this->resultArr ) );
457 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
461 ksort( $this->resultArr );
463 if ( count( $this->cont ) >= 7 ) {
464 $startAt = $this->cont[6];
466 reset( $this->resultArr );
467 $startAt = key( $this->resultArr );
470 foreach ( $this->resultArr as $pageID => $arr ) {
471 if ( $pageID < $startAt ) {
476 $fit = $result->addValue(
477 [
'query', $this->getModuleName() ],
478 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
480 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
486 $redirLinks = isset( $arr[
'redirlinks'] ) ? (array)$arr[
'redirlinks'] : [];
487 ksort( $redirLinks );
489 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
490 $redirStartAt = $this->cont[7];
492 reset( $redirLinks );
493 $redirStartAt = key( $redirLinks );
495 foreach ( $redirLinks as $key => $redir ) {
496 if ( $key < $redirStartAt ) {
500 $fit = $result->addValue(
501 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
504 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
511 $result->addIndexedTagName(
512 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
523 $result->addIndexedTagName(
524 [
'query', $this->getModuleName() ],
528 if ( $this->continueStr !==
null ) {
529 $this->setContinueEnumParameter(
'continue', $this->continueStr );