+ $queryFunc = function ( IDatabase $dbr, $table, $fromCol ) use (
+ $conds, $target, $limit
+ ) {
+ // Read an extra row as an at-end check
+ $queryLimit = $limit + 1;
+ $on = [
+ "rd_from = $fromCol",
+ 'rd_title' => $target->getDBkey(),
+ 'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
+ ];
+ $on['rd_namespace'] = $target->getNamespace();
+ // Inner LIMIT is 2X in case of stale backlinks with wrong namespaces
+ $subQuery = $dbr->selectSQLText(
+ [ $table, 'redirect', 'page' ],
+ [ $fromCol, 'rd_from' ],
+ $conds[$table],
+ __CLASS__ . '::showIndirectLinks',
+ // Force JOIN order per T106682 to avoid large filesorts
+ [ 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit, 'STRAIGHT_JOIN' ],
+ [
+ 'page' => [ 'INNER JOIN', "$fromCol = page_id" ],
+ 'redirect' => [ 'LEFT JOIN', $on ]
+ ]
+ );
+ return $dbr->select(
+ [ 'page', 'temp_backlink_range' => "($subQuery)" ],
+ [ 'page_id', 'page_namespace', 'page_title', 'rd_from', 'page_is_redirect' ],
+ [],
+ __CLASS__ . '::showIndirectLinks',
+ [ 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ],
+ [ 'page' => [ 'INNER JOIN', "$fromCol = page_id" ] ]
+ );
+ };
+
+ if ( $fetchlinks ) {
+ $plRes = $queryFunc( $dbr, 'pagelinks', 'pl_from' );