From 64cf6c59761782775b7859c33924a1564425467e Mon Sep 17 00:00:00 2001 From: Robert Vogel <1201528+osnard@users.noreply.github.com> Date: Fri, 24 Jun 2022 18:24:49 +0200 Subject: [PATCH] Fix profiling context calculation (#174) `ParserFactory::getParser` will create a completely new `Parser` instance. Based on the context, this instance may have not set a `Title` object. Thus accessing `Parser::getTitle` without checking for the return value may break the code. Issue: https://github.com/Universal-Omega/DynamicPageList3/issues/170 Co-authored-by: rvogel --- includes/Parse.php | 8 +++++++- includes/Query.php | 11 ++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/includes/Parse.php b/includes/Parse.php index b6775a3..58b579f 100644 --- a/includes/Parse.php +++ b/includes/Parse.php @@ -202,7 +202,13 @@ class Parse { $query = new Query( $this->parameters ); $foundRows = null; - $rows = $query->buildAndSelect( $calcRows, $foundRows ); + $profilingContext = ''; + $currentTitle= $parser->getTitle(); + if ( $currentTitle instanceof Title ) { + $profilingContext + = str_replace( [ '*', '/' ], '-', $currentTitle->getPrefixedDBkey() ); + } + $rows = $query->buildAndSelect( $calcRows, $foundRows, $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. diff --git a/includes/Query.php b/includes/Query.php index 879782c..24901d4 100644 --- a/includes/Query.php +++ b/includes/Query.php @@ -167,9 +167,10 @@ class Query { * * @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 ) { + public function buildAndSelect( bool $calcRows = false, ?int &$foundRows = null, $profilingContext = '' ) { global $wgNonincludableNamespaces, $wgDebugDumpSql; $options = []; @@ -363,10 +364,10 @@ class Query { $options['MAX_EXECUTION_TIME'] = $maxQueryTime; } - $parser = MediaWikiServices::getInstance()->getParser(); - $pageName = str_replace( [ '*', '/' ], '-', $parser->getTitle()->getPrefixedDBkey() ); - - $qname = __METHOD__ . ' - ' . $pageName; + $qname = __METHOD__; + if ( !empty( $profilingContext ) ) { + $qname .= ' - ' . $profilingContext; + } $where = $this->where; $join = $this->join; $db = $this->dbr;