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; |
||
33 | use Parser; |
||
34 | use ParserOptions; |
||
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 $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 |
||
62 | ) { |
||
63 | wfDebug( $message ); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param $message |
||
69 | */ |
||
70 | public function addError( $message ) { |
||
71 | $this->mMessages[] = [ $message, self::MESSAGE_ERROR ]; |
||
72 | wfDebug( "Error: $message\n" ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param $message |
||
77 | */ |
||
78 | public function addWarning( $message ) { |
||
79 | $this->mMessages[] = [ $message, self::MESSAGE_WARNING ]; |
||
80 | wfDebug( "Warning: $message\n" ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param $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 |
||
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 |
||
123 | // FIXME: $parser->parse returns ParserOutput, not String |
||
124 | $ret = $parser->parse( $ret, $wgTitle, ParserOptions::newFromUser( $wgUser ) ); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
125 | } else { |
||
126 | // FIXME: Should probably return '' (and throw an error if necessary) |
||
127 | $ret = null; |
||
128 | } |
||
129 | |||
130 | return $ret; |
||
131 | } |
||
132 | |||
133 | } |
||
134 |