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
12 changes: 9 additions & 3 deletions includes/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,15 @@ public function parse( $input, Parser $parser, &$reset, &$eliminate, $isParserTa
try {
$query = new Query( $this->parameters );

$foundRows = null;
$profilingContext = '';
$currentTitle = $parser->getPage();
if ( $currentTitle instanceof Title ) {
$profilingContext
= str_replace( [ '*', '/' ], '-', $currentTitle->getPrefixedDBkey() );
}
$rows = $query->buildAndSelect( $calcRows, $foundRows, $profilingContext );

$rows = $query->buildAndSelect( $calcRows, $profilingContext );

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 @@ -226,6 +227,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 @@ -269,7 +275,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 @@ -168,11 +168,10 @@
* Start a query build. Returns found rows.
*
* @param bool $calcRows
* @param ?int &$foundRows
* @param string $profilingContext Used to see the origin of a query in the profiling
* @return array|bool
*/
public function buildAndSelect( bool $calcRows = false, ?int &$foundRows = null, $profilingContext = '' ) {
public function buildAndSelect( bool $calcRows = false, $profilingContext = '' ) {

Check notice

Code scanning / Phpmd (reported by Codacy)

This pattern reports excessively long methods. Note

The method buildAndSelect() has 256 lines of code. Current threshold is set to 100. Avoid really long methods.

Check notice

Code scanning / Phpmd (reported by Codacy)

This pattern reports methods with a large number of possible paths Note

The method buildAndSelect() has an NPath complexity of 1296000. The configured NPath complexity threshold is 200.

Check notice

Code scanning / Phpmd (reported by Codacy)

This pattern reports methods with high cyclomatic complexity Note

The method buildAndSelect() has a Cyclomatic Complexity of 30. The configured cyclomatic complexity threshold is 10.

Check warning

Code scanning / Phpmd (reported by Codacy)

You can fix this problem by extracting the logic in the boolean flag into its own class or method Warning

The method buildAndSelect has a boolean flag argument $calcRows, which is a certain sign of a Single Responsibility Principle violation.
global $wgNonincludableNamespaces, $wgDebugDumpSql;

$options = [];
Expand Down Expand Up @@ -375,19 +374,17 @@
}
$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 @@ -404,8 +401,8 @@
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
Loading