120 $this->
addTables( [ $this->bl_table,
'page' ] );
121 $this->
addWhere(
"{$this->bl_from}=page_id" );
122 if ( is_null( $resultPageSet ) ) {
123 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
125 $this->
addFields( $resultPageSet->getPageTableFields() );
127 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
129 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
130 if ( $this->hasNS ) {
131 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
133 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
135 if ( count( $this->cont ) >= 2 ) {
136 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
137 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
139 "{$this->bl_from_ns} $op {$this->cont[0]} OR " .
140 "({$this->bl_from_ns} = {$this->cont[0]} AND " .
141 "{$this->bl_from} $op= {$this->cont[1]})"
144 $this->
addWhere(
"{$this->bl_from} $op= {$this->cont[1]}" );
148 if ( $this->params[
'filterredir'] ==
'redirects' ) {
150 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->
redirect ) {
156 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
157 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
159 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
160 $orderBy[] = $this->bl_from_ns .
$sort;
162 $orderBy[] = $this->bl_from .
$sort;
163 $this->
addOption(
'ORDER BY', $orderBy );
168 foreach (
$res as $row ) {
169 if ( ++$count > $this->params[
'limit'] ) {
173 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
178 if ( count( $this->cont ) < 2 ) {
179 $this->cont[] = $row->from_ns;
180 $this->cont[] = $row->page_id;
183 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
184 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
185 if ( $row->page_is_redirect ) {
186 $this->redirTitles[] =
$t;
189 if ( is_null( $resultPageSet ) ) {
190 $a = [
'pageid' => intval( $row->page_id ) ];
192 if ( $row->page_is_redirect ) {
193 $a[
'redirect'] =
true;
196 $this->resultArr[$a[
'pageid']] = $a;
198 $resultPageSet->processDbRow( $row );
208 $db = $this->
getDB();
209 $this->
addTables( [ $this->bl_table,
'page' ] );
210 $this->
addWhere(
"{$this->bl_from}=page_id" );
212 if ( is_null( $resultPageSet ) ) {
213 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
215 $this->
addFields( $resultPageSet->getPageTableFields() );
218 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
219 if ( $this->hasNS ) {
228 foreach ( $this->redirTitles as
$t ) {
229 $redirNs =
$t->getNamespace();
230 $redirDBkey =
$t->getDBkey();
231 $titleWhere[] =
"{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
232 ( $this->hasNS ?
" AND {$this->bl_ns} = {$redirNs}" :
'' );
233 $allRedirNs[$redirNs] =
true;
234 $allRedirDBkey[$redirDBkey] =
true;
237 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
239 if ( count( $this->cont ) >= 6 ) {
240 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
242 $where =
"{$this->bl_from} $op= {$this->cont[5]}";
245 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
246 $where =
"{$this->bl_from_ns} $op {$this->cont[4]} OR " .
247 "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
249 if ( count( $allRedirDBkey ) > 1 ) {
250 $title = $db->addQuotes( $this->cont[3] );
251 $where =
"{$this->bl_title} $op $title OR " .
252 "({$this->bl_title} = $title AND ($where))";
254 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
255 $where =
"{$this->bl_ns} $op {$this->cont[2]} OR " .
256 "({$this->bl_ns} = {$this->cont[2]} AND ($where))";
261 if ( $this->params[
'filterredir'] ==
'redirects' ) {
263 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
267 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
269 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
271 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
272 $orderBy[] = $this->bl_ns .
$sort;
274 if ( count( $allRedirDBkey ) > 1 ) {
275 $orderBy[] = $this->bl_title .
$sort;
277 if ( $this->params[
'namespace'] !==
null && count( $this->params[
'namespace'] ) > 1 ) {
278 $orderBy[] = $this->bl_from_ns .
$sort;
280 $orderBy[] = $this->bl_from .
$sort;
281 $this->
addOption(
'ORDER BY', $orderBy );
282 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
288 foreach (
$res as $row ) {
289 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
291 if ( ++$count > $this->params[
'limit'] ) {
296 $title = $row->{$this->bl_title};
297 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
298 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
303 if ( count( $this->cont ) < 6 ) {
305 $this->cont[] = $row->{$this->bl_title};
306 $this->cont[] = $row->from_ns;
307 $this->cont[] = $row->page_id;
310 if ( is_null( $resultPageSet ) ) {
311 $a[
'pageid'] = intval( $row->page_id );
313 if ( $row->page_is_redirect ) {
314 $a[
'redirect'] =
true;
316 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
318 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
320 $resultPageSet->processDbRow( $row );
329 private function run( $resultPageSet =
null ) {
330 $this->params = $this->extractRequestParams(
false );
331 $this->
redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
335 $result = $this->getResult();
337 if ( $this->params[
'limit'] ==
'max' ) {
338 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
339 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
341 $this->params[
'limit'] = intval( $this->params[
'limit'] );
342 $this->validateLimit(
'limit', $this->params[
'limit'], 1, $userMax, $botMax );
345 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
348 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
350 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
357 if ( $this->params[
'continue'] !==
null ) {
358 $cont = explode(
'|', $this->params[
'continue'] );
360 switch ( count( $cont ) ) {
363 $this->cont[7] = (int)$cont[7];
364 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
370 $this->cont[6] = (int)$cont[6];
371 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
377 $this->cont[2] = (int)$cont[2];
378 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
381 $this->cont[3] = $cont[3];
384 $this->cont[4] = (int)$cont[4];
385 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
388 $this->cont[5] = (int)$cont[5];
389 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
395 $this->cont[0] = (int)$cont[0];
396 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
399 $this->cont[1] = (int)$cont[1];
400 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
405 $this->dieContinueUsageIf(
true );
408 ksort( $this->cont );
411 $this->runFirstQuery( $resultPageSet );
412 if ( $this->
redirect && count( $this->redirTitles ) ) {
413 $this->resetQueryParams();
414 $this->runSecondQuery( $resultPageSet );
418 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
420 if ( is_null( $resultPageSet ) ) {
422 $code = $this->bl_code;
423 $data = array_map(
function ( $arr ) use ( $result,
$code ) {
424 if ( isset( $arr[
'redirlinks'] ) ) {
425 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
429 }, array_values( $this->resultArr ) );
430 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
434 ksort( $this->resultArr );
435 if ( count( $this->cont ) >= 7 ) {
436 $startAt = $this->cont[6];
438 reset( $this->resultArr );
439 $startAt =
key( $this->resultArr );
442 foreach ( $this->resultArr as $pageID => $arr ) {
443 if ( $pageID < $startAt ) {
448 $fit = $result->addValue(
449 [
'query', $this->getModuleName() ],
450 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
452 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
458 $redirLinks = isset( $arr[
'redirlinks'] ) ? (
array)$arr[
'redirlinks'] : [];
459 ksort( $redirLinks );
460 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
461 $redirStartAt = $this->cont[7];
463 reset( $redirLinks );
464 $redirStartAt =
key( $redirLinks );
466 foreach ( $redirLinks as $key => $redir ) {
467 if ( $key < $redirStartAt ) {
471 $fit = $result->addValue(
472 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
475 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
482 $result->addIndexedTagName(
483 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
494 $result->addIndexedTagName(
495 [
'query', $this->getModuleName() ],
499 if ( !is_null( $this->continueStr ) ) {
500 $this->setContinueEnumParameter(
'continue', $this->continueStr );