DynamicPageList3/includes/Logger.php

69 lines
1.8 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
2021-10-01 22:52:30 +00:00
namespace MediaWiki\Extension\DynamicPageList3;
2020-11-22 20:00:48 +00:00
class Logger {
/**
* Buffer of debug messages.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
private $buffer = [];
2021-10-01 22:52:30 +00:00
public function addMessage() {
2020-11-22 20:00:48 +00:00
$args = func_get_args();
2021-02-22 23:48:01 +00:00
$args = array_map( 'htmlspecialchars', $args );
2021-10-01 22:52:30 +00:00
call_user_func_array( [ $this, 'msg' ], $args );
2020-11-22 20:00:48 +00:00
}
/**
* Return the buffer of messages.
*
2021-10-01 22:52:30 +00:00
* @param bool $clearBuffer
* @return array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public function getMessages( $clearBuffer = true ) {
2020-11-22 20:00:48 +00:00
$buffer = $this->buffer;
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $clearBuffer === true ) {
2020-11-22 20:00:48 +00:00
$this->buffer = [];
}
2021-10-01 22:52:30 +00:00
2020-11-22 20:00:48 +00:00
return $buffer;
}
/**
* Get a message, with optional parameters
* Parameters from user input must be escaped for HTML *before* passing to this function
*/
public function msg() {
$args = func_get_args();
2021-02-22 23:48:01 +00:00
$errorId = array_shift( $args );
$errorLevel = floor( $errorId / 1000 );
2020-11-22 20:00:48 +00:00
$errorMessageId = $errorId % 1000;
2021-10-01 22:52:30 +00:00
if ( Hooks::getDebugLevel() >= $errorLevel ) {
if ( Hooks::isLikeIntersection() ) {
if ( $errorId == Hooks::FATAL_TOOMANYCATS ) {
2021-02-22 23:48:01 +00:00
$text = wfMessage( 'intersection_toomanycats', $args )->text();
} elseif ( $errorId == Hooks::FATAL_TOOFEWCATS ) {
2021-02-22 23:48:01 +00:00
$text = wfMessage( 'intersection_toofewcats', $args )->text();
} elseif ( $errorId == Hooks::WARN_NORESULTS ) {
2021-02-22 23:48:01 +00:00
$text = wfMessage( 'intersection_noresults', $args )->text();
} elseif ( $errorId == Hooks::FATAL_NOSELECTION ) {
2021-02-22 23:48:01 +00:00
$text = wfMessage( 'intersection_noincludecats', $args )->text();
} elseif ( $errorId == Hooks::FATAL_POOLCOUNTER ) {
$text = wfMessage( 'intersection_pcerror', $args )->text();
2020-11-22 20:00:48 +00:00
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( empty( $text ) ) {
$text = wfMessage( 'dpl_log_' . $errorMessageId, $args )->text();
2020-11-22 20:00:48 +00:00
}
2021-10-01 22:52:30 +00:00
$this->buffer[] = '<p>Extension:DynamicPageList3 (DPL3), version ' . Hooks::getVersion() . ': ' . $text . '</p>';
2020-11-22 20:00:48 +00:00
}
}
}