"Catrope" posted a comment on MediaWiki.r107556.
URL: http://www.mediawiki.org/wiki/Special:Code/MediaWiki/107556#c28812

Commit summary for MediaWiki.r107556:

Use jqueryMsg wikitext parser to parse interface messages at client side. 
Support for PLURAL in javascript.

Catrope's comment:

Here's a hacky proof of concept in eval.php, I'll work on this some more in the 
morning (and hopefully I'll catch Tim then).

<source lang="php">
catrope@roanLaptop:~/mediawiki/trunk/phase3$ php maintenance/eval.php
# Use $wgParser for something so it gets unstubbed. This is a hack and probably 
not needed in a practical implementation
> $output = $wgParser->parse( "There {{PLURAL:$1|is one article|are $1 
> articles}} on this wiki.", Title::newMainPage(), new ParserOptions );

# Clone $wgParser
> $parser = clone $wgParser;

# Our own function to handle PLURAL, outputs a JSON representation of the call
> function myPlural( $parser, $text = '' ) { $forms = array_slice( 
> func_get_args(), 2 ); return '", {"func": "plural", "text": ' . 
> json_encode($text) . ', "forms": ' . json_encode($forms) . '}, "'; }

# Install the function as the hook for PLURAL, overriding the default hook
> $parser->setFunctionHook( 'plural', 'myPlural', SFH_NO_HASH );

# Run the text that we want to parse through the preprocessor (parse() will 
work too, if we want HTML)
> $output2 = $parser->preprocess( "There {{PLURAL:$1|is one article|are $1 
> articles}} on this wiki.", Title::newMainPage(), new ParserOptions );

# Wrap the output to make it proper JSON
> $json = '["' . $output2 . '"]';

# The JSON output
> echo $json
["There ", {"func": "plural", "text": "$1", "forms": ["is one article","are $1 
articles"]}, " on this wiki."]

# PHP array representation of the JSON output
> var_dump(json_decode($json, true))
array(3) {
  [0]=>
  string(6) "There "
  [1]=>
  array(3) {
    ["func"]=>
    string(6) "plural"
    ["text"]=>
    string(2) "$1"
    ["forms"]=>
    array(2) {
      [0]=>
      string(14) "is one article"
      [1]=>
      string(15) "are $1 articles"
    }
  }
  [2]=>
  string(14) " on this wiki."
}
</source>

_______________________________________________
MediaWiki-CodeReview mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-codereview

Reply via email to