Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeller-wikia committed Mar 30, 2021
1 parent 2248ca0 commit 2a6a429
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions includes/specials/SpecialWhatlinkshere.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ function execute( $par ) {
$opts->add( 'target', '' );
$opts->add( 'namespace', '', FormOptions::INTNULL );
$opts->add( 'limit', $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
$opts->add( 'from', 0 );
$opts->add( 'back', 0 );
$opts->add( 'offset', 0 );
$opts->add( 'dir', 'next' );
$opts->add( 'hideredirs', false );
$opts->add( 'hidetrans', false );
$opts->add( 'hidelinks', false );
Expand Down Expand Up @@ -93,19 +93,19 @@ function execute( $par ) {
0,
$this->target,
$opts->getValue( 'limit' ),
$opts->getValue( 'from' ),
$opts->getValue( 'back' )
$opts->getValue( 'offset' ),
$opts->getValue( 'dir' )
);
}

/**
* @param int $level Recursion level
* @param Title $target Target title
* @param int $limit Number of entries to display
* @param int $from Display from this article ID (default: 0)
* @param int $back Display from this article ID at backwards scrolling (default: 0)
* @param int $offset Display from this article ID (default: 0)
* @param string $dir Display from this article ID at backwards scrolling (default: 0)
*/
function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
function showIndirectLinks( $level, $target, $limit, $offset = 0, $dir = 'next' ) {
$out = $this->getOutput();
$dbr = wfGetDB( DB_REPLICA );

Expand Down Expand Up @@ -138,20 +138,28 @@ function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
$conds['imagelinks'][] = "il_from_namespace $nsComparison";
}

if ( $from ) {
$conds['templatelinks'][] = "tl_from >= $from";
$conds['pagelinks'][] = "pl_from >= $from";
$conds['imagelinks'][] = "il_from >= $from";
if ( $offset ) {
if ( $dir === 'prev' ) {
$conds['templatelinks'][] = "tl_from < $offset";
$conds['pagelinks'][] = "pl_from < $offset";
$conds['imagelinks'][] = "il_from < $offset";
} else {
$conds['templatelinks'][] = "tl_from > $offset";
$conds['pagelinks'][] = "pl_from > $offset";
$conds['imagelinks'][] = "il_from > $offset";
}
}

$sortDirection = $dir === 'prev' ? ' DESC ' : ' ASC ';

if ( $hideredirs ) {
$conds['pagelinks']['rd_from'] = null;
} elseif ( $hidelinks ) {
$conds['pagelinks'][] = 'rd_from is NOT NULL';
}

$queryFunc = function ( IDatabase $dbr, $table, $fromCol ) use (
$conds, $target, $limit
$conds, $target, $limit, $sortDirection
) {
// Read an extra row as an at-end check
$queryLimit = $limit + 1;
Expand All @@ -168,7 +176,7 @@ function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
$conds[$table],
__CLASS__ . '::showIndirectLinks',
// Force JOIN order per T106682 to avoid large filesorts
[ 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit, 'STRAIGHT_JOIN' ],
[ 'ORDER BY' => $fromCol . $sortDirection, 'LIMIT' => 2 * $queryLimit, 'STRAIGHT_JOIN' ],
[
'page' => [ 'JOIN', "$fromCol = page_id" ],
'redirect' => [ 'LEFT JOIN', $on ]
Expand All @@ -179,7 +187,7 @@ function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
[ 'page_id', 'page_namespace', 'page_title', 'rd_from', 'page_is_redirect' ],
[],
__CLASS__ . '::showIndirectLinks',
[ 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ],
[ 'ORDER BY' => 'page_id' . $sortDirection, 'LIMIT' => $queryLimit ],
[ 'page' => [ 'JOIN', "$fromCol = page_id" ] ]
);
};
Expand Down Expand Up @@ -257,19 +265,26 @@ function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {

$numRows = count( $rows );

// Work out the start and end IDs, for prev/next links
if ( $numRows > $limit ) {
// More rows available after these ones
// Get the ID from the last row in the result set
$nextId = $rows[$limit]->page_id;
// Remove undisplayed rows
$rows = array_slice( $rows, 0, $limit );
$nextId = false;
$prevId = false;
if ( $dir === 'prev' ) {
if ( $numRows > $limit ) {
// More rows available after these ones
// Get the ID from the last row in the result set
$prevId = $rows[1]->page_id;
$nextId = $rows[$limit]->page_id;
// Remove undisplayed rows
$rows = array_slice( $rows, 1, $limit );
} else {
$nextId = $rows[$numRows - 1]->page_id;
}
} else {
// No more rows after
$nextId = false;
$prevId = $offset ? $rows[0]->page_id : false;
if ( $numRows > $limit ) {
$nextId = $rows[$limit - 1]->page_id;
$rows = array_slice( $rows, 0, $limit );
}
}
$prevId = $from;

// use LinkBatch to make sure, that all required data (associated with Titles)
// is loaded in one query
$lb = new LinkBatch();
Expand Down Expand Up @@ -456,11 +471,11 @@ function getPrevNext( $prevId, $nextId ) {
unset( $changed['target'] ); // Already in the request title

if ( $prevId != 0 ) {
$overrides = [ 'from' => $this->opts->getValue( 'back' ) ];
$overrides = [ 'dir' => 'prev', 'offset' => $prevId ];
$prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
}
if ( $nextId != 0 ) {
$overrides = [ 'from' => $nextId, 'back' => $prevId ];
$overrides = [ 'dir' => 'next', 'offset' => $nextId ];
$next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
}

Expand All @@ -480,8 +495,6 @@ function getPrevNext( $prevId, $nextId ) {
function whatlinkshereForm() {
// We get nicer value from the title object
$this->opts->consumeValue( 'target' );
// Reset these for new requests
$this->opts->consumeValues( [ 'back', 'from' ] );

$target = $this->target ? $this->target->getPrefixedText() : '';
$namespace = $this->opts->consumeValue( 'namespace' );
Expand Down

0 comments on commit 2a6a429

Please sign in to comment.