r64675 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64674‎ | r64675 | r64676 >
Date:22:17, 6 April 2010
Author:than4213
Status:deferred
Tags:
Comment:
Use the new Parser in preSaveTransform
Modified paths:
  • /branches/parser-work/phase3/includes/AutoLoader.php (modified) (history)
  • /branches/parser-work/phase3/includes/parser/ParseEngine.php (modified) (history)
  • /branches/parser-work/phase3/includes/parser/Parser.php (modified) (history)
  • /branches/parser-work/phase3/includes/parser/WikiTextGrammar.xml (modified) (history)

Diff [purge]

Index: branches/parser-work/phase3/includes/parser/Parser.php
@@ -2788,6 +2788,17 @@
27892789 $this->addTrackingCategory( "$limitationType-category" );
27902790 }
27912791
 2792+ function transformParseDom($inNode) {
 2793+ if ($inNode->nodeName == "newLine") {
 2794+ $inNode->setAttribute("tag", "\n");
 2795+ }
 2796+ foreach ($inNode->childNodes as $child) {
 2797+ if ($child instanceof DOMElement) {
 2798+ $this->transformParseDom($child);
 2799+ }
 2800+ }
 2801+ }
 2802+
27922803 /**
27932804 * Return the text of a template, after recursively
27942805 * replacing any variables or templates within the template.
@@ -3965,23 +3976,19 @@
39663977 * @param Title &$title the Title object for the current article
39673978 * @param User $user the User object describing the current user
39683979 * @param ParserOptions $options parsing options
3969 - * @param bool $clearState whether to clear the parser state first
39703980 * @return string the altered wiki markup
39713981 * @public
39723982 */
3973 - function preSaveTransform( $text, Title $title, $user, $options, $clearState = true ) {
 3983+ function preSaveTransform( $text, Title $title, $user, $options ) {
39743984 $this->mOptions = $options;
39753985 $this->setTitle( $title );
39763986 $this->setOutputType( self::OT_WIKI );
 3987+ $this->clearState();
39773988
3978 - if ( $clearState ) {
3979 - $this->clearState();
3980 - }
3981 -
3982 - $pairs = array(
3983 - "\r\n" => "\n",
3984 - );
3985 - $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
 3989+ $parser = new ParseEngine("includes/parser/WikiTextGrammar.xml");
 3990+ $dom = $parser->parse($text);
 3991+ $this->transformParseDom($dom->documentElement);
 3992+ $text = ParseEngine::unparse($dom->documentElement);
39863993 $text = $this->pstPass2( $text, $user );
39873994 $text = $this->mStripState->unstripBoth( $text );
39883995 return $text;
Index: branches/parser-work/phase3/includes/parser/ParseEngine.php
@@ -8,7 +8,7 @@
99 */
1010 class ParseEngine {
1111 const maxIter = 2048;
12 - private $mGrammar, $mTextPats;
 12+ private $mGrammar;
1313
1414 function __construct($grammarFile) {
1515 global $IP;
@@ -43,24 +43,15 @@
4444 return $doc;
4545 }
4646
47 - static function expand($nodeList, $callback, $flags = 0) {
48 - $retStr = "";
49 - foreach ($nodeList as $node) {
50 - if ($node instanceof DOMText) {
51 - $retStr .= $node->data;
 47+ static function unparse($inNode) {
 48+ $retStr = "" . $inNode->getAttribute("tag");
 49+ foreach ($inNode->childNodes as $child) {
 50+ if ($child instanceof DOMText) {
 51+ $retStr .= $child->data;
5252 } else {
53 - $methodName = $node->nodeName . "Substitution";
54 - if (method_exists($callback, $methodName) && call_user_func_array(array($callback, $methodName), array($node, &$outStr, $flags))) {
55 - $retStr .= $outStr;
56 - } else {
57 - $retStr .= $node->getAttribute("tag") . self::expand($node->childNodes, $callback, $flags);
58 - }
 53+ $retStr .= self::unparse($child);
5954 }
6055 }
61 - global $wgDebugParserLog;
62 - if ($wgDebugParserLog != '') {
63 - wfErrorLog("Expand returned: $retStr\n", $wgDebugParserLog);
64 - }
6556 return $retStr;
6657 }
6758
Index: branches/parser-work/phase3/includes/parser/WikiTextGrammar.xml
@@ -1,19 +1,14 @@
22 <?xml version="1.0"?>
3 -<Grammar rootTag="root" startRule="start">
 3+<Grammar rootTag="root" startRule="start" version="1.0">
44 <Sequence name="start" >
5 - <Choice failSafe="true">
6 - <Assignment tagName="onlyInclude" tag="(?=.*(&lt;onlyinclude>))" regex="true">
7 - <Reference name="endText" />
8 - </Assignment>
9 - <Reference name="postNewline" />
10 - </Choice>
 5+ <Reference name="postNewLine" />
116 <Reference name="main" />
127 </Sequence>
138 <Text name="main">
149 <Choice>
1510 <Sequence>
16 - <Reference name="newline" />
17 - <Reference name="postNewline" />
 11+ <Reference name="newLine" />
 12+ <Reference name="postNewLine" />
1813 </Sequence>
1914 <Assignment tagName="link" tag="[[">
2015 <Reference name="endText" var="]]" />
@@ -37,14 +32,6 @@
3833 </Sequence>
3934 </Assignment>
4035 <Reference name="comment" />
41 - <Assignment tagName="onlyInclude" tag="&lt;/onlyinclude>">
42 - <Sequence>
43 - <Reference name="main" />
44 - <Choice failSafe="true">
45 - <Assignment tagName="endTag" tag="&lt;onlyinclude>" />
46 - </Choice>
47 - </Sequence>
48 - </Assignment>
4936 <Assignment tagName="noWiki" tag="&lt;nowiki>">
5037 <Sequence>
5138 <Text />
@@ -72,7 +59,7 @@
7360 <Reference name="main" />
7461 <Assignment tagName="endTag" tag="~r" />
7562 </Sequence>
76 - <Assignment name="newline" tagName="newLine" tag="&#10;" />
 63+ <Assignment name="newLine" tagName="newLine" tag="\r?\n" regex="true" />
7764 <Assignment name="eol" tagName="eol" tag="(?=\n|$)" regex="true" />
7865 <Choice name="ignoreList" failSafe="true">
7966 <Sequence>
@@ -83,7 +70,7 @@
8471 <Reference name="ignoreList" />
8572 </Sequence>
8673 </Choice>
87 - <Choice name="postNewline" failSafe="true">
 74+ <Choice name="postNewLine" failSafe="true">
8875 <Sequence>
8976 <Assignment tagName="h" tag="(={1,6})" regex="true">
9077 <Reference name="endText" />
@@ -113,7 +100,7 @@
114101 </Choice>
115102 <Choice failSafe="true">
116103 <Sequence>
117 - <Reference name="newline" />
 104+ <Reference name="newLine" />
118105 <Reference name="itemList" />
119106 </Sequence>
120107 </Choice>
Index: branches/parser-work/phase3/includes/AutoLoader.php
@@ -463,6 +463,7 @@
464464 'PPNode_Hash_Tree' => 'includes/parser/Preprocessor_Hash.php',
465465 'PPTemplateFrame_DOM' => 'includes/parser/Preprocessor_DOM.php',
466466 'PPTemplateFrame_Hash' => 'includes/parser/Preprocessor_Hash.php',
 467+ 'ParseEngine' => 'includes/parser/ParseEngine.php',
467468 'Parser' => 'includes/parser/Parser.php',
468469 'ParserCache' => 'includes/parser/ParserCache.php',
469470 'ParserOptions' => 'includes/parser/ParserOptions.php',

Status & tagging log