Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix %TOTALPAGES% caching #175

Merged
merged 10 commits into from
Feb 8, 2025
Merged
10 changes: 7 additions & 3 deletions includes/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ public function parse( $input, Parser $parser, &$reset, &$eliminate, $isParserTa
try {
$query = new Query( $this->parameters );

$foundRows = null;
$rows = $query->buildAndSelect( $calcRows, $foundRows );
$rows = $query->buildAndSelect( $calcRows );
if ( $rows === false ) {
// This error path is very fast (We exit immediately if poolcounter is full)
// Thus it should be safe to try again in ~5 minutes.
Expand All @@ -217,6 +216,11 @@ public function parse( $input, Parser $parser, &$reset, &$eliminate, $isParserTa
return $this->getFullOutput();
}

if ( isset( $rows['count'] ) ) {
$foundRows = $rows['count'];
unset( $rows['count'] );
}
Universal-Omega marked this conversation as resolved.
Show resolved Hide resolved

$numRows = count( $rows );
$articles = $this->processQueryResults( $rows, $parser );

Expand Down Expand Up @@ -260,7 +264,7 @@ public function parse( $input, Parser $parser, &$reset, &$eliminate, $isParserTa
}

// $this->addOutput($lister->format($articles));
if ( $foundRows === null ) {
if ( !isset( $foundRows ) ) {
// Get row count after calling format() otherwise the count will be inaccurate.
$foundRows = $lister->getRowCount();
}
Expand Down
21 changes: 9 additions & 12 deletions includes/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ public function __construct( Parameters $parameters ) {
* Start a query build. Returns found rows.
*
* @param bool $calcRows
* @param ?int &$foundRows
* @return array|bool
*/
public function buildAndSelect( bool $calcRows = false, ?int &$foundRows = null ) {
public function buildAndSelect( bool $calcRows = false ) {
global $wgNonincludableNamespaces, $wgDebugDumpSql;

$options = [];
Expand Down Expand Up @@ -369,19 +368,17 @@ public function buildAndSelect( bool $calcRows = false, ?int &$foundRows = null
$qname = __METHOD__ . ' - ' . $pageName;
$where = $this->where;
$join = $this->join;
$db = $this->dbr;
$dbr = $this->dbr;

$doQuery = static function () use ( $qname, $db, $tables, $fields, $where, $options, $join, $calcRows, &$foundRows ) {
$res = $db->select( $tables, $fields, $where, $qname, $options, $join );
$doQuery = static function () use ( $qname, $dbr, $tables, $fields, $where, $options, $join, $calcRows ) {
$res = $dbr->select( $tables, $fields, $where, $qname, $options, $join );
$res = iterator_to_array( $res );

if ( $calcRows ) {
$calcRowsResult = $db->query( 'SELECT FOUND_ROWS() AS count;', $qname );
$total = $calcRowsResult->fetchRow();

$foundRows = (int)$total['count'];
$res['count'] = $dbr->selectField( $tables, 'FOUND_ROWS()', '', $qname );
}

return iterator_to_array( $res );
return $res;
};

$poolCounterKey = 'nowait:dpl3-query:' . WikiMap::getCurrentWikiId();
Expand All @@ -398,8 +395,8 @@ public function buildAndSelect( bool $calcRows = false, ?int &$foundRows = null
return $cache->getWithSetCallback(
$cache->makeKey( 'DPL3Query', hash( 'sha256', $query ) ),
$queryCacheTime,
static function ( $oldVal, &$ttl, &$setOpts ) use ( $worker, $db ){
$setOpts += Database::getCacheSetOptions( $db );
static function ( $oldVal, &$ttl, &$setOpts ) use ( $worker, $dbr ){
$setOpts += Database::getCacheSetOptions( $dbr );

Check warning

Code scanning / Phpmd (reported by Codacy)

Static access leads to hard to test code Warning

Avoid using static access to class '\Wikimedia\Rdbms\Database' in method 'buildAndSelect'.
$res = $worker->execute();
if ( $res === false ) {
// Do not cache errors.
Expand Down