Issues (130)

src/MessageLog.php (8 issues)

1
<?php
2
3
/**
4
 * File holding the Lingo\MessageLog class.
5
 *
6
 * This file is part of the MediaWiki extension Lingo.
7
 *
8
 * @copyright 2011 - 2018, Stephan Gambke
9
 * @license GPL-2.0-or-later
10
 *
11
 * The Lingo extension is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by the Free
13
 * Software Foundation; either version 2 of the License, or (at your option) any
14
 * later version.
15
 *
16
 * The Lingo extension is distributed in the hope that it will be useful, but
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU General Public License along
22
 * with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 * @author Stephan Gambke
25
 *
26
 * @file
27
 * @ingroup Lingo
28
 */
29
30
namespace Lingo;
31
32
use Html;
0 ignored issues
show
The type Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
use Parser;
0 ignored issues
show
The type Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use ParserOptions;
0 ignored issues
show
The type ParserOptions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
36
/**
37
 * This class holds messages (errors, warnings, notices) for Lingo
38
 *
39
 * Contains a static function to initiate the parsing.
40
 *
41
 * @ingroup Lingo
42
 */
43
class MessageLog {
44
45
	private $mMessages = [];
46
	private $mParser = null;
47
48
	const MESSAGE_ERROR = 1;
49
	const MESSAGE_WARNING = 2;
50
	const MESSAGE_NOTICE = 3;
51
52
	/**
53
	 * @param string $message
54
	 * @param int $severity
55
	 */
56
	public function addMessage( $message, $severity = self::MESSAGE_NOTICE ) {
57
		$this->mMessages[] = [ $message, $severity ];
58
59
		// log errors and warnings in debug log
60
		if ( $severity == self::MESSAGE_WARNING ||
61
			$severity == self::MESSAGE_ERROR
0 ignored issues
show
Operator == prohibited; use === instead
Loading history...
62
		) {
63
			wfDebug( $message );
64
		}
65
	}
66
67
	/**
68
	 * @param string $message
69
	 */
70
	public function addError( $message ) {
71
		$this->mMessages[] = [ $message, self::MESSAGE_ERROR ];
72
		wfDebug( "Error: $message\n" );
73
	}
74
75
	/**
76
	 * @param string $message
77
	 */
78
	public function addWarning( $message ) {
79
		$this->mMessages[] = [ $message, self::MESSAGE_WARNING ];
80
		wfDebug( "Warning: $message\n" );
81
	}
82
83
	/**
84
	 * @param string $message
85
	 */
86
	public function addNotice( $message ) {
87
		$this->mMessages[] = [ $message, self::MESSAGE_NOTICE ];
88
	}
89
90
	/**
91
	 * @param int $severity
92
	 * @param null $header
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $header is correct as it would always require null to be passed?
Loading history...
93
	 * @return null|string
94
	 */
95
	public function getMessagesFormatted( $severity = self::MESSAGE_WARNING, $header = null ) {
96
		global $wgTitle, $wgUser;
97
98
		$ret = '';
99
100
		foreach ( $this->mMessages as $message ) {
101
			if ( $message[ 1 ] <= $severity ) {
102
				$ret .= '* ' . $message[ 0 ] . "\n";
103
			}
104
		}
105
106
		if ( $ret != '' ) {
107
			if ( !$this->mParser ) {
108
				$parser = new Parser();
109
			}
110
111
			if ( $header == null ) {
112
				$header = '';
113
			} elseif ( $header != '' ) {
114
				$header = Html::rawElement( 'div', [ 'class' => 'heading' ], $header );
115
			}
116
117
			$ret = Html::rawElement( 'div', [ 'class' => 'messages' ],
118
				$header . "\n" .
119
				$ret
120
			);
121
122
			// FIXME: Variable 'parser' might have not been defined
0 ignored issues
show
Comment refers to a FIXME task "Variable 'parser' might have not been defined"
Loading history...
123
			// FIXME: $parser->parse returns ParserOutput, not String
0 ignored issues
show
Comment refers to a FIXME task "$parser->parse returns ParserOutput, not String"
Loading history...
124
			$ret = $parser->parse( $ret, $wgTitle, ParserOptions::newFromUser( $wgUser ) );
125
		} else {
126
			// FIXME: Should probably return '' (and throw an error if necessary)
0 ignored issues
show
Comment refers to a FIXME task "Should probably return '' (and throw an error if necessary"
Loading history...
127
			$ret = null;
128
		}
129
130
		return $ret;
131
	}
132
133
}
134