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 - 2016, Stephan Gambke |
9
|
|
|
* @license GNU General Public License, version 2 (or any later version) |
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
|
|
|
use Html; |
32
|
|
|
use Parser; |
33
|
|
|
use ParserOptions; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This class holds messages (errors, warnings, notices) for Lingo |
37
|
|
|
* |
38
|
|
|
* Contains a static function to initiate the parsing. |
39
|
|
|
* |
40
|
|
|
* @ingroup Lingo |
41
|
|
|
*/ |
42
|
|
|
class MessageLog { |
43
|
|
|
|
44
|
|
|
private $mMessages = array(); |
45
|
|
|
private $mParser = null; |
46
|
|
|
|
47
|
|
|
const MESSAGE_ERROR = 1; |
48
|
|
|
const MESSAGE_WARNING = 2; |
49
|
|
|
const MESSAGE_NOTICE = 3; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $message |
53
|
|
|
* @param int $severity |
54
|
|
|
*/ |
55
|
|
|
public function addMessage( $message, $severity = self::MESSAGE_NOTICE ) { |
56
|
|
|
$this->mMessages[] = array( $message, $severity ); |
57
|
|
|
|
58
|
|
|
// log errors and warnings in debug log |
59
|
|
|
if ( $severity == self::MESSAGE_WARNING || |
60
|
|
|
$severity == self::MESSAGE_ERROR |
61
|
|
|
) { |
62
|
|
|
wfDebug( $message ); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $message |
68
|
|
|
*/ |
69
|
|
|
public function addError( $message ) { |
70
|
|
|
$this->mMessages[] = array( $message, self::MESSAGE_ERROR ); |
71
|
|
|
wfDebug( "Error: $message\n" ); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $message |
76
|
|
|
*/ |
77
|
|
|
public function addWarning( $message ) { |
78
|
|
|
$this->mMessages[] = array( $message, self::MESSAGE_WARNING ); |
79
|
|
|
wfDebug( "Warning: $message\n" ); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $message |
84
|
|
|
*/ |
85
|
|
|
public function addNotice( $message ) { |
86
|
|
|
$this->mMessages[] = array( $message, self::MESSAGE_NOTICE ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $severity |
91
|
|
|
* @param null $header |
92
|
|
|
* @return null|string |
93
|
|
|
*/ |
94
|
|
|
public function getMessagesFormatted( $severity = self::MESSAGE_WARNING, $header = null ) { |
95
|
|
|
global $wgTitle, $wgUser; |
|
|
|
|
96
|
|
|
|
97
|
|
|
$ret = ''; |
98
|
|
|
|
99
|
|
|
foreach ( $this->mMessages as $message ) { |
100
|
|
|
if ( $message[ 1 ] <= $severity ) { |
101
|
|
|
$ret .= '* ' . $message[ 0 ] . "\n"; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ( $ret != '' ) { |
106
|
|
|
if ( !$this->mParser ) { |
107
|
|
|
$parser = new Parser(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ( $header == null ) { |
111
|
|
|
$header = ''; |
112
|
|
|
} elseif ( $header != '' ) { |
113
|
|
|
$header = Html::rawElement( 'div', array( 'class' => 'heading' ), $header ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$ret = Html::rawElement( 'div', array( 'class' => 'messages' ), |
117
|
|
|
$header . "\n" . |
118
|
|
|
$ret |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
// FIXME: Variable 'parser' might have not been defined |
|
|
|
|
122
|
|
|
// FIXME: $parser->parse returns ParserOutput, not String |
|
|
|
|
123
|
|
|
$ret = $parser->parse( $ret, $wgTitle, ParserOptions::newFromUser( $wgUser ) ); |
|
|
|
|
124
|
|
|
} else { |
125
|
|
|
// FIXME: Should probably return '' (and throw an error if necessary) |
|
|
|
|
126
|
|
|
$ret = null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $ret; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.