r64182 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64181‎ | r64182 | r64183 >
Date:20:34, 25 March 2010
Author:happy-melon
Status:ok
Tags:
Comment:
Work on Message.php:
* Just one function for adding parameters, not three.
* Implement an options framework: one extra widget (get-message-not-from-db) already in use in wfMsgNoDB(), others will probably follow, so set up something to keep them clean.
* Lay the groundwork for supporting __tostring() when MW stops supporting PHP < 5.2.0
* Fix various fatal and non-fatal errors
* Make use of r64178 to clean out any dependency on the wfMsg* functions.
Modified paths:
  • /trunk/phase3/includes/Message.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Message.php
@@ -1,5 +1,4 @@
22 <?php
3 -
43 /**
54 * OBS!!! *EXPERIMENTAL* This class is still under discussion.
65 *
@@ -11,34 +10,37 @@
1211 *
1312 * Examples:
1413 * Fetching a message text for interface message
15 - * $button = Xml::button( Message::key( 'submit' )->text() );
 14+ * $button = Xml::button( Message::key( 'submit' )->text() );
1615 * Messages can have parameters:
17 - * Message::key( 'welcome-to' )->param( $wgSitename )->text(); // {{GRAMMAR}} and friends work correctly
18 - * Message::key( 'are-friends' )->params( $user, $friend );
19 - * Message::key( 'bad-message' )->rawParam( '<script>...</script>' )->escaped()
 16+ * Message::key( 'welcome-to' )->params( $wgSitename )->text();
 17+ * {{GRAMMAR}} and friends work correctly
 18+ * Message::key( 'are-friends' )->params( $user, $friend );
 19+ * Message::key( 'bad-message' )->rawParams( '<script>...</script>' )->escaped()
2020 * Sometimes the message text ends up in the database, so content language is needed.
21 - * Message::key( 'file-log' )->params( $user, $filename )->inContentLanguage()->text()
 21+ * Message::key( 'file-log' )->params( $user, $filename )->inContentLanguage()->text()
2222 * Checking if message exists:
23 - * Message::key( 'mysterious-message' )->exists()
 23+ * Message::key( 'mysterious-message' )->exists()
2424 * If you want to use a different language:
25 - * Message::key( 'email-header' )->language( $user->getOption( 'language' ) )->plain()
 25+ * Message::key( 'email-header' )->language( $user->getOption( 'language' ) )->plain()
 26+ * Note that you cannot parse the text except in the content or interface
 27+ * languages
2628 *
2729 *
2830 * Comparison with old wfMsg* functions:
2931 *
3032 * Use full parsing.
31 - * Would correspond to wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
32 - * $parsed = Message::key( 'key' )->param( 'apple' )->parse();
 33+ * wfMsgExt( 'key', array( 'parseinline' ), 'apple' );
 34+ * === Message::key( 'key' )->params( 'apple' )->parse();
3335 * Parseinline is used because it is more useful when pre-building html.
3436 * In normal use it is better to use OutputPage::(add|wrap)WikiMsg.
3537 *
3638 * Places where html cannot be used. {{-transformation is done.
37 - * Would correspond to wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
38 - * $plain = Message::key( 'key' )->params( 'apple', 'pear' )->text();
 39+ * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' );
 40+ * === Message::key( 'key' )->params( 'apple', 'pear' )->text();
3941 *
4042 * Shortcut for escaping the message too, similar to wfMsgHTML, but
4143 * parameters are not replaced after escaping by default.
42 - * $escaped = Message::key( 'key' )->rawParam( 'apple' )->escaped();
 44+ * $escaped = Message::key( 'key' )->rawParams( 'apple' )->escaped();
4345 *
4446 * TODO:
4547 * * test, can we have tests?
@@ -53,11 +55,13 @@
5456 * means the current interface language, false content language.
5557 */
5658 protected $interface = true;
 59+
5760 /**
5861 * In which language to get this message. Overrides the $interface
5962 * variable.
6063 */
6164 protected $language = null;
 65+
6266 /**
6367 * The message key.
6468 */
@@ -67,14 +71,39 @@
6872 * List of parameters which will be substituted into the message.
6973 */
7074 protected $parameters = array();
 75+
 76+ /**
 77+ * Some situations need exotic combinations of options to the
 78+ * underlying Language modules; which can be specified here.
 79+ * Dependencies:
 80+ * 'parse' implies 'transform', 'escape'
 81+ */
 82+ protected $options = array(
 83+ # Don't wrap the output in a block-level element
 84+ 'inline' => true,
 85+ # Expand {{ constructs
 86+ 'transform' => true,
 87+ # Output will be safe HTML
 88+ 'escape' => true,
 89+ # Parse the text with the full parser
 90+ 'parse' => true,
 91+ # Access the database when getting the message text
 92+ 'usedb' => true,
 93+ );
7194
7295 /**
7396 * Constructor.
7497 * @param $key String: message key
7598 * @return Message: $this
7699 */
77 - public function __construct( $key ) {
 100+ public function __construct( $key, $params=array(), $options=array() ) {
78101 $this->key = $key;
 102+ if( $params ){
 103+ $this->params( $params );
 104+ }
 105+ if( $options ){
 106+ $this->options( $options );
 107+ }
79108 }
80109
81110 /**
@@ -91,44 +120,42 @@
92121
93122 /**
94123 * Adds parameters to the parameter list of this message.
95 - * @param $value String: parameter
 124+ * @params Vargars: parameters as Strings
96125 * @return Message: $this
97126 */
98 - public function param( $value ) {
99 - $this->parameters[] = $value;
100 - return $this;
101 - }
102 -
103 - /**
104 - * Adds parameters to the parameter list of this message.
105 - * @params Vargars: parameters
106 - * @return Message: $this
107 - */
108127 public function params( /*...*/ ) {
109 - $this->paramList( func_get_args() );
 128+ $this->parameters += array_values( func_get_args() );
110129 return $this;
111130 }
112131
113132 /**
114 - * Adds a list of parameters to the parameter list of this message.
115 - * @param $value Array: list of parameters, array keys will be ignored.
 133+ * Add parameters that are substituted after parsing or escaping.
 134+ * In other words the parsing process cannot access the contents
 135+ * of this type of parameter, and you need to make sure it is
 136+ * sanitized beforehand. The parser will see "$n", instead.
 137+ * @param $value Varargs: raw parameters as Strings
116138 * @return Message: $this
117139 */
118 - public function paramList( array $values ) {
119 - $this->parameters += array_values( $values );
 140+ public function rawParams( /*...*/ ) {
 141+ $params = func_get_args();
 142+ foreach( $params as $param ){
 143+ $this->parameters[] = array( 'raw' => $param );
 144+ }
120145 return $this;
121146 }
122 -
 147+
123148 /**
124 - * Adds a parameters that is substituted after parsing or escaping.
125 - * In other words the parsing process cannot access the contents
126 - * of this type parameter, and you need to make sure it is
127 - * sanitized beforehand.
128 - * @param $value String: raw parameter
129 - * @return Message: $this
 149+ * Set some of the individual options, if you need to use some
 150+ * funky combination of them.
 151+ * @param $options Array $option => $value
 152+ * @return Message $this
130153 */
131 - public function rawParam( $value ) {
132 - $this->parameters[] = array( 'raw' => $value );
 154+ public function options( array $options ){
 155+ foreach( $options as $key => $value ){
 156+ if( in_array( $key, $this->options ) ){
 157+ $this->options[$key] = (bool)$value;
 158+ }
 159+ }
133160 return $this;
134161 }
135162
@@ -139,11 +166,16 @@
140167 * @param $lang Mixed: langauge code or language object.
141168 * @return Message: $this
142169 */
143 - public function language( Language $lang ) {
144 - if ( is_string( $lang ) ) {
 170+ public function language( $lang ) {
 171+ if( $lang instanceof Language ){
 172+ $this->language = $lang;
 173+ } elseif ( is_string( $lang ) ) {
145174 $this->language = Language::factory( $lang );
146175 } else {
147 - $this->language = $lang;
 176+ $type = gettype( $lang );
 177+ throw new MWException( "Message::langauge() must be "
 178+ . "passed a String or Language object; $type given"
 179+ );
148180 }
149181 $this->interface = false;
150182 return $this;
@@ -161,27 +193,75 @@
162194
163195 /**
164196 * Returns the message parsed from wikitext to HTML.
 197+ * TODO: in PHP >= 5.2.0, we can make this a magic method,
 198+ * and then we can do, eg:
 199+ * $foo = Message::get($key);
 200+ * $string = "<abbr>$foo</abbr>";
 201+ * But we shouldn't implement that while MediaWiki still supports
 202+ * PHP < 5.2; or people will start using it...
165203 * @return String: HTML
166204 */
167 - public function parse() {
168 - $string = $this->parseAsBlock( $string );
169 - $m = array();
170 - if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
171 - $string = $m[1];
 205+ public function toString() {
 206+ $string = $this->getMessageText();
 207+
 208+ # Replace parameters before text parsing
 209+ $string = $this->replaceParameters( $string, 'before' );
 210+
 211+ # Maybe transform using the full parser
 212+ if( $this->options['parse'] ){
 213+ $string = $this->parseText( $string );
 214+ } else {
 215+
 216+ # Transform {{ constructs
 217+ if( $this->options['transform'] ){
 218+ $string = $this->transformText( $string );
 219+ }
 220+
 221+ # Sanitise
 222+ if( $this->options['escape'] ){
 223+ # FIXME: Sanitizer method here?
 224+ $string = htmlspecialchars( $string );
 225+ }
172226 }
 227+
 228+ # Strip the block element
 229+ if( !$this->options['inline'] ){
 230+ $m = array();
 231+ if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
 232+ $string = $m[1];
 233+ }
 234+ }
 235+
 236+ # Raw parameter replacement
 237+ $string = $this->replaceParameters( $string, 'after' );
 238+
173239 return $string;
174240 }
 241+
 242+ public function __tostring(){ return $this->toString(); }
 243+
 244+ /**
 245+ * Fully parse the text from wikitext to HTML
 246+ * @return String parsed HTML
 247+ */
 248+ public function parse(){
 249+ $this->options( array(
 250+ 'parse' => true,
 251+ ));
 252+ return $this->tostring();
 253+ }
175254
176255 /**
177256 * Returns the message text. {{-transformation is done.
178257 * @return String: Unescaped message text.
179258 */
180259 public function text() {
181 - $string = $this->getMessageText();
182 - $string = $this->replaceParameters( 'before' );
183 - $string = $this->transformText( $string );
184 - $string = $this->replaceParameters( 'after' );
185 - return $string;
 260+ $this->options( array(
 261+ 'parse' => false,
 262+ 'transform' => true,
 263+ 'escape' => false,
 264+ ));
 265+ return $this->tostring();
186266 }
187267
188268 /**
@@ -189,10 +269,13 @@
190270 * @return String: Unescaped untransformed message text.
191271 */
192272 public function plain() {
193 - $string = $this->getMessageText();
194 - $string = $this->replaceParameters( 'before' );
195 - $string = $this->replaceParameters( 'after' );
196 - return $string;
 273+ $this->options( array(
 274+ 'parse' => false,
 275+ 'transform' => false,
 276+ 'escape' => false,
 277+ 'inline' => false,
 278+ ));
 279+ return $this->tostring();
197280 }
198281
199282 /**
@@ -200,11 +283,11 @@
201284 * @return String: HTML
202285 */
203286 public function parseAsBlock() {
204 - $string = $this->getMessageText();
205 - $string = $this->replaceParameters( 'before' );
206 - $string = $this->parseText( $string );
207 - $string = $this->replaceParameters( 'after' );
208 - return $string;
 287+ $this->options( array(
 288+ 'parse' => true,
 289+ 'inline' => true,
 290+ ));
 291+ return $this->tostring();
209292 }
210293
211294 /**
@@ -213,12 +296,13 @@
214297 * @return String: Escaped message text.
215298 */
216299 public function escaped() {
217 - $string = $this->getMessageText();
218 - $string = $this->replaceParameters( 'before' );
219 - $string = $this->transformText( $string );
220 - $string = htmlspecialchars( $string );
221 - $string = $this->replaceParameters( 'after' );
222 - return $string;
 300+ $this->options( array(
 301+ 'parse' => false,
 302+ 'transform' => true,
 303+ 'escape' => true,
 304+ 'inline' => false,
 305+ ));
 306+ return $this->tostring();
223307 }
224308
225309 /**
@@ -226,18 +310,24 @@
227311 * @return Bool: true if it is and false if not.
228312 */
229313 public function exists() {
230 - return !wfEmptyMsg( $this->key, $this->getMessageText() );
 314+ global $wgMessageCache;
 315+ return $wgMessageCache->get(
 316+ $this->key,
 317+ $this->options['usedb'],
 318+ $this->language
 319+ ) !== false;
231320 }
232321
233322 /**
234323 * Substitutes any paramaters into the message text.
 324+ * @param $message String, the message text
235325 * @param $type String: either before or after
236326 * @return String
237327 */
238 - protected function replaceParameters( $type = 'before' ) {
 328+ protected function replaceParameters( $message, $type = 'before' ) {
239329 $replacementKeys = array();
240 - foreach( $args as $n => $param ) {
241 - if ( $type === 'before' && !isset( $param['raw'] ) ) {
 330+ foreach( $this->parameters as $n => $param ) {
 331+ if ( $type === 'before' && !is_array( $param ) ) {
242332 $replacementKeys['$' . ($n + 1)] = $param;
243333 } elseif ( $type === 'after' && isset( $param['raw'] ) ) {
244334 $replacementKeys['$' . ($n + 1)] = $param['raw'];
@@ -255,10 +345,10 @@
256346 protected function parseText( $string ) {
257347 global $wgOut;
258348 if ( $this->language !== null ) {
259 - // FIXME: remove this limitation
 349+ # FIXME: remove this limitation
260350 throw new MWException( 'Can only parse in interface or content language' );
261351 }
262 - return $wgOut->parse( $string, /*linestart*/true, $this->interface() );
 352+ return $wgOut->parse( $string, /*linestart*/true, $this->interface );
263353 }
264354
265355 /**
@@ -277,7 +367,10 @@
278368 */
279369 protected function getMessageText() {
280370 global $wgMessageCache;
281 - return $wgMessageCache->get( $this->key, /*DB*/true, $this->language );
 371+ $message = $wgMessageCache->get( $this->key, $this->options['usedb'], $this->language );
 372+ return $message === false
 373+ ? '&lt;' . htmlspecialchars( $this->key ) . '&gt;'
 374+ : $message;
282375 }
283376
284377 }
\ No newline at end of file

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r64178Make MessageCache::get() return false if the requested message does not exist...happy-melon20:21, 25 March 2010

Status & tagging log