Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions includes/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class Logger {

/** Buffer of debug messages. */
private array $buffer = [];
private int $debugLevel = 0;
private static ?self $instance = null;

public function addMessage( int $errorId, string ...$args ): void {
$errorLevel = (int)floor( $errorId / 1000 );
$errorMessageId = $errorId % 1000;

if ( Utils::getDebugLevel() < $errorLevel ) {
if ( $this->getDebugLevel() < $errorLevel ) {
return;
}

Expand All @@ -36,7 +38,7 @@ public function addMessage( int $errorId, string ...$args ): void {
$text ??= wfMessage( "dpl_log_$errorMessageId", $args )->text();

$version = Utils::getVersion();
$this->buffer[] = Html::element( 'p', [],
$this->buffer[$errorId] = Html::element( 'p', [],
"Extension:DynamicPageList4 (DPL4), version $version: $text"
);
}
Expand All @@ -49,4 +51,17 @@ public function getMessages( bool $clearBuffer ): array {

return $buffer;
}

public function getDebugLevel(): int {
return $this->debugLevel;
}

public function setDebugLevel( int $level ): void {
$this->debugLevel = $level;
}

public static function getInstance(): self {
self::$instance ??= new self();
return self::$instance;
}
}
5 changes: 3 additions & 2 deletions includes/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function processParameter( string $parameter, string $option ): bool {
$parameter = strtolower( $parameter );
$parameterData = $this->getData( $parameter );
if ( $parameterData === false ) {
$this->logger->addMessage( Constants::WARN_UNKNOWNPARAM, $parameter );
return false;
}

Expand Down Expand Up @@ -267,7 +268,7 @@ private function setDefaults(): void {

if ( $default !== null && !( $default === false && $isBoolean === true ) ) {
if ( $parameter === 'debug' ) {
Utils::setDebugLevel( $default );
$this->logger->setDebugLevel( $default );
}

$this->setParameter( $parameter, $default );
Expand Down Expand Up @@ -893,7 +894,7 @@ private function _debug( string $option ): bool {

$option = (int)$option;
if ( in_array( $option, $this->getData( 'debug' )['values'] ?? [], true ) ) {
Utils::setDebugLevel( $option );
$this->logger->setDebugLevel( $option );
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions includes/ParametersData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class ParametersData {

protected readonly Config $config;
protected readonly Logger $logger;

/** The level of parameters that is accesible for the user. */
private readonly int $parameterRichness;
Expand Down Expand Up @@ -1210,6 +1211,7 @@ class ParametersData {

public function __construct() {
$this->config = Config::getInstance();
$this->logger = Logger::getInstance();
$this->parameterRichness = $this->config->get( 'functionalRichness' );

if ( Utils::isLikeIntersection() ) {
Expand Down
10 changes: 7 additions & 3 deletions includes/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Parse {

public function __construct() {
$this->config = Config::getInstance();
$this->logger = new Logger();
$this->logger = Logger::getInstance();
$this->parameters = new Parameters();
$this->request = RequestContext::getMain()->getRequest();
}
Expand Down Expand Up @@ -333,6 +333,10 @@ public function parse(
);

$this->triggerEndResets( $finalOutput, $reset, $eliminate, $isParserTag, $parser );
if ( $this->logger->getMessages( clearBuffer: false ) ) {
$parser->addTrackingCategory( 'dpl-errors-tracking-category' );
}

return $finalOutput;
}

Expand Down Expand Up @@ -496,7 +500,7 @@ private function getFullOutput( int $totalResults, bool $skipHeaderFooter ): str
}

private function setHeader( string $header ): void {
if ( Utils::getDebugLevel() === 5 ) {
if ( $this->logger->getDebugLevel() === 5 ) {
$header = Html::openElement( 'pre' ) .
Html::openElement( 'nowiki' ) . $header;
}
Expand All @@ -509,7 +513,7 @@ private function getHeader(): string {
}

private function setFooter( string $footer ): void {
if ( Utils::getDebugLevel() === 5 ) {
if ( $this->logger->getDebugLevel() === 5 ) {
$footer .= Html::closeElement( 'nowiki' ) .
Html::closeElement( 'pre' );
}
Expand Down
5 changes: 4 additions & 1 deletion includes/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
class Query {

private readonly Config $config;
private readonly Logger $logger;

private readonly IReadableDatabase $dbr;
private readonly SelectQueryBuilder $queryBuilder;
private readonly UserFactory $userFactory;
Expand Down Expand Up @@ -88,6 +90,7 @@ public function __construct(
->getReplicaDatabase( group: 'vslow' );

$this->config = Config::getInstance();
$this->logger = Logger::getInstance();
$this->queryBuilder = $this->dbr->newSelectQueryBuilder();
$this->userFactory = MediaWikiServices::getInstance()->getUserFactory();
}
Expand Down Expand Up @@ -216,7 +219,7 @@ public function buildAndSelect( bool $calcRows, string $profilingContext ): arra
$query = $this->queryBuilder->getSQL();
}

if ( Utils::getDebugLevel() >= 4 && $this->config->get( MainConfigNames::DebugDumpSql ) ) {
if ( $this->logger->getDebugLevel() >= 4 && $this->config->get( MainConfigNames::DebugDumpSql ) ) {
$this->sqlQuery = $query;
}
} catch ( DBQueryError $e ) {
Expand Down
9 changes: 0 additions & 9 deletions includes/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
final class Utils {

private static bool $likeIntersection = false;
private static int $debugLevel = 0;

/** @phan-var array<mixed,mixed> */
public static array $createdLinks = [
Expand Down Expand Up @@ -44,14 +43,6 @@ public static function getVersion(): string {
return $version;
}

public static function getDebugLevel(): int {
return self::$debugLevel;
}

public static function setDebugLevel( int $level ): void {
self::$debugLevel = $level;
}

public static function isLikeIntersection(): bool {
return self::$likeIntersection;
}
Expand Down
Loading