124 $this->
addTables( [ $this->bl_table,
'page' ] );
125 $this->
addWhere(
"{$this->bl_from}=page_id" );
126 if ( is_null( $resultPageSet ) ) {
127 $this->
addFields( [
'page_id',
'page_title',
'page_namespace' ] );
129 $this->
addFields( $resultPageSet->getPageTableFields() );
131 $this->
addFields( [
'page_is_redirect',
'from_ns' =>
'page_namespace' ] );
133 $this->
addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
134 if ( $this->hasNS ) {
135 $this->
addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
137 $this->
addWhereFld( $this->bl_from_ns, $this->params[
'namespace'] );
139 if ( count( $this->cont ) >= 2 ) {
140 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
141 if ( count( $this->params[
'namespace'] ) > 1 ) {
143 "{$this->bl_from_ns} $op {$this->cont[0]} OR " .
144 "({$this->bl_from_ns} = {$this->cont[0]} AND " .
145 "{$this->bl_from} $op= {$this->cont[1]})"
148 $this->
addWhere(
"{$this->bl_from} $op= {$this->cont[1]}" );
152 if ( $this->params[
'filterredir'] ==
'redirects' ) {
154 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' && !$this->
redirect ) {
160 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
161 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
163 if ( count( $this->params[
'namespace'] ) > 1 ) {
164 $orderBy[] = $this->bl_from_ns .
$sort;
166 $orderBy[] = $this->bl_from .
$sort;
167 $this->
addOption(
'ORDER BY', $orderBy );
172 foreach (
$res as $row ) {
173 if ( ++$count > $this->params[
'limit'] ) {
177 $this->continueStr =
"{$row->from_ns}|{$row->page_id}";
182 if ( count( $this->cont ) < 2 ) {
183 $this->cont[] = $row->from_ns;
184 $this->cont[] = $row->page_id;
187 $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
188 $t = Title::makeTitle( $row->page_namespace, $row->page_title );
189 if ( $row->page_is_redirect ) {
190 $this->redirTitles[] =
$t;
193 if ( is_null( $resultPageSet ) ) {
194 $a = [
'pageid' => intval( $row->page_id ) ];
196 if ( $row->page_is_redirect ) {
197 $a[
'redirect'] =
true;
200 $this->resultArr[$a[
'pageid']] = $a;
202 $resultPageSet->processDbRow( $row );
212 $db = $this->
getDB();
213 $this->
addTables( [
'page', $this->bl_table ] );
214 $this->
addWhere(
"{$this->bl_from}=page_id" );
216 if ( is_null( $resultPageSet ) ) {
217 $this->
addFields( [
'page_id',
'page_title',
'page_namespace',
'page_is_redirect' ] );
219 $this->
addFields( $resultPageSet->getPageTableFields() );
222 $this->
addFields( [ $this->bl_title,
'from_ns' =>
'page_namespace' ] );
223 if ( $this->hasNS ) {
232 foreach ( $this->redirTitles as
$t ) {
233 $redirNs =
$t->getNamespace();
234 $redirDBkey =
$t->getDBkey();
235 $titleWhere[] =
"{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
236 ( $this->hasNS ?
" AND {$this->bl_ns} = {$redirNs}" :
'' );
237 $allRedirNs[$redirNs] =
true;
238 $allRedirDBkey[$redirDBkey] =
true;
241 $this->
addWhereFld(
'page_namespace', $this->params[
'namespace'] );
243 if ( count( $this->cont ) >= 6 ) {
244 $op = $this->params[
'dir'] ==
'descending' ?
'<' :
'>';
246 $where =
"{$this->bl_from} $op= {$this->cont[5]}";
249 if ( count( $this->params[
'namespace'] ) > 1 ) {
250 $where =
"{$this->bl_from_ns} $op {$this->cont[4]} OR " .
251 "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
253 if ( count( $allRedirDBkey ) > 1 ) {
254 $title = $db->addQuotes( $this->cont[3] );
255 $where =
"{$this->bl_title} $op $title OR " .
256 "({$this->bl_title} = $title AND ($where))";
258 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
259 $where =
"{$this->bl_ns} $op {$this->cont[2]} OR " .
260 "({$this->bl_ns} = {$this->cont[2]} AND ($where))";
265 if ( $this->params[
'filterredir'] ==
'redirects' ) {
267 } elseif ( $this->params[
'filterredir'] ==
'nonredirects' ) {
271 $this->
addOption(
'LIMIT', $this->params[
'limit'] + 1 );
273 $sort = ( $this->params[
'dir'] ==
'descending' ?
' DESC' :
'' );
275 if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
276 $orderBy[] = $this->bl_ns .
$sort;
278 if ( count( $allRedirDBkey ) > 1 ) {
279 $orderBy[] = $this->bl_title .
$sort;
281 if ( count( $this->params[
'namespace'] ) > 1 ) {
282 $orderBy[] = $this->bl_from_ns .
$sort;
284 $orderBy[] = $this->bl_from .
$sort;
285 $this->
addOption(
'ORDER BY', $orderBy );
286 $this->
addOption(
'USE INDEX', [
'page' =>
'PRIMARY' ] );
290 foreach (
$res as $row ) {
291 $ns = $this->hasNS ? $row->{$this->bl_ns} :
NS_FILE;
293 if ( ++$count > $this->params[
'limit'] ) {
298 $title = $row->{$this->bl_title};
299 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 2 ) ) .
300 "|$ns|$title|{$row->from_ns}|{$row->page_id}";
305 if ( count( $this->cont ) < 6 ) {
307 $this->cont[] = $row->{$this->bl_title};
308 $this->cont[] = $row->from_ns;
309 $this->cont[] = $row->page_id;
312 if ( is_null( $resultPageSet ) ) {
313 $a[
'pageid'] = intval( $row->page_id );
315 if ( $row->page_is_redirect ) {
316 $a[
'redirect'] =
true;
318 $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
320 $this->resultArr[$parentID][
'redirlinks'][$row->page_id] = $a;
322 $resultPageSet->processDbRow( $row );
331 private function run( $resultPageSet =
null ) {
332 $this->params = $this->extractRequestParams(
false );
333 $this->
redirect = isset( $this->params[
'redirect'] ) && $this->params[
'redirect'];
337 $result = $this->getResult();
339 if ( $this->params[
'limit'] ==
'max' ) {
340 $this->params[
'limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
341 $result->addParsedLimit( $this->getModuleName(), $this->params[
'limit'] );
343 $this->params[
'limit'] = intval( $this->params[
'limit'] );
344 $this->validateLimit(
'limit', $this->params[
'limit'], 1, $userMax, $botMax );
347 $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
350 if ( !$this->hasNS && $this->rootTitle->getNamespace() !==
NS_FILE ) {
352 [
'apierror-imageusage-badtitle', $this->getModuleName() ],
359 if ( $this->params[
'continue'] !==
null ) {
360 $cont = explode(
'|', $this->params[
'continue'] );
362 switch ( count( $cont ) ) {
365 $this->cont[7] = (int)$cont[7];
366 $this->dieContinueUsageIf( $cont[7] !== (
string)$this->cont[7] );
372 $this->cont[6] = (int)$cont[6];
373 $this->dieContinueUsageIf( $cont[6] !== (
string)$this->cont[6] );
379 $this->cont[2] = (int)$cont[2];
380 $this->dieContinueUsageIf( $cont[2] !== (
string)$this->cont[2] );
383 $this->cont[3] = $cont[3];
386 $this->cont[4] = (int)$cont[4];
387 $this->dieContinueUsageIf( $cont[4] !== (
string)$this->cont[4] );
390 $this->cont[5] = (int)$cont[5];
391 $this->dieContinueUsageIf( $cont[5] !== (
string)$this->cont[5] );
397 $this->cont[0] = (int)$cont[0];
398 $this->dieContinueUsageIf( $cont[0] !== (
string)$this->cont[0] );
401 $this->cont[1] = (int)$cont[1];
402 $this->dieContinueUsageIf( $cont[1] !== (
string)$this->cont[1] );
407 $this->dieContinueUsageIf(
true );
410 ksort( $this->cont );
413 $this->runFirstQuery( $resultPageSet );
414 if ( $this->
redirect && count( $this->redirTitles ) ) {
415 $this->resetQueryParams();
416 $this->runSecondQuery( $resultPageSet );
420 $this->cont += [ 0, 0, 0,
'', 0, 0, 0 ];
422 if ( is_null( $resultPageSet ) ) {
424 $code = $this->bl_code;
425 $data = array_map(
function ( $arr ) use ( $result,
$code ) {
426 if ( isset( $arr[
'redirlinks'] ) ) {
427 $arr[
'redirlinks'] = array_values( $arr[
'redirlinks'] );
431 }, array_values( $this->resultArr ) );
432 $fit = $result->addValue(
'query', $this->getModuleName(), $data );
436 ksort( $this->resultArr );
437 if ( count( $this->cont ) >= 7 ) {
438 $startAt = $this->cont[6];
440 reset( $this->resultArr );
441 $startAt =
key( $this->resultArr );
444 foreach ( $this->resultArr as $pageID => $arr ) {
445 if ( $pageID < $startAt ) {
450 $fit = $result->addValue(
451 [
'query', $this->getModuleName() ],
452 $idx, array_diff_key( $arr, [
'redirlinks' =>
'' ] ) );
454 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
460 $redirLinks = isset( $arr[
'redirlinks'] ) ? (
array)$arr[
'redirlinks'] : [];
461 ksort( $redirLinks );
462 if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
463 $redirStartAt = $this->cont[7];
465 reset( $redirLinks );
466 $redirStartAt =
key( $redirLinks );
468 foreach ( $redirLinks as $key => $redir ) {
469 if ( $key < $redirStartAt ) {
473 $fit = $result->addValue(
474 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
477 $this->continueStr = implode(
'|', array_slice( $this->cont, 0, 6 ) ) .
484 $result->addIndexedTagName(
485 [
'query', $this->getModuleName(), $idx,
'redirlinks' ],
496 $result->addIndexedTagName(
497 [
'query', $this->getModuleName() ],
501 if ( !is_null( $this->continueStr ) ) {
502 $this->setContinueEnumParameter(
'continue', $this->continueStr );