r65437 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65436‎ | r65437 | r65438 >
Date:19:52, 22 April 2010
Author:raymond
Status:ok
Tags:
Comment:
Run stylize.php
Modified paths:
  • /trunk/extensions/ParserWiki/ParseEngine.php (modified) (history)
  • /trunk/extensions/ParserWiki/ParserWiki.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserWiki/ParseEngine.php
@@ -10,136 +10,136 @@
1111 const maxIter = 4096;
1212 private $mGrammar;
1313
14 - function __construct($grammar) {
15 - $xpath = new DOMXPath($grammar);
16 - $rootRules = $xpath->query("/grammar/parseObject | /grammar/rule/parseObject");
17 - foreach ($rootRules as $child) {
18 - $this->pushTags($child, NULL);
 14+ function __construct( $grammar ) {
 15+ $xpath = new DOMXPath( $grammar );
 16+ $rootRules = $xpath->query( "/grammar/parseObject | /grammar/rule/parseObject" );
 17+ foreach ( $rootRules as $child ) {
 18+ $this->pushTags( $child, NULL );
1919 }
2020 $this->mGrammar = $grammar;
2121 }
2222
23 - function parse($text) {
24 - wfDebugLog("ParseEngine", "==========Start Parse Engine==========\n");
25 - $xpath = new DOMXPath($this->mGrammar);
26 - $rootAssign = $xpath->query("/grammar/parseObject")->item(0);
 23+ function parse( $text ) {
 24+ wfDebugLog( "ParseEngine", "==========Start Parse Engine==========\n" );
 25+ $xpath = new DOMXPath( $this->mGrammar );
 26+ $rootAssign = $xpath->query( "/grammar/parseObject" )->item( 0 );
2727 $doc = new DOMDocument();
28 - if (! $this->parseRec($rootAssign, "", "", $iter, $text, $doc)) {
 28+ if ( ! $this->parseRec( $rootAssign, "", "", $iter, $text, $doc ) ) {
2929 $doc = NULL;
3030 }
3131 return $doc;
3232 }
3333
34 - static function unparse($inNodes) {
 34+ static function unparse( $inNodes ) {
3535 $retStr = "";
36 - foreach ($inNodes as $child) {
37 - if ($child instanceof DOMText) {
 36+ foreach ( $inNodes as $child ) {
 37+ if ( $child instanceof DOMText ) {
3838 $retStr .= $child->data;
3939 } else {
40 - $retStr .= $child->getAttribute("tag") . self::unparse($child->childNodes);
 40+ $retStr .= $child->getAttribute( "tag" ) . self::unparse( $child->childNodes );
4141 }
4242 }
4343 return $retStr;
4444 }
4545
46 - private function parseRec($rule, $replaceStr, $saveTags, &$iter, &$text, &$outNode) {
 46+ private function parseRec( $rule, $replaceStr, $saveTags, &$iter, &$text, &$outNode ) {
4747 $iter ++;
48 - if ($iter > ParseEngine::maxIter) {
49 - throw new MWException("Parser iterated too many times. Probable loop in grammar.");
 48+ if ( $iter > ParseEngine::maxIter ) {
 49+ throw new MWException( "Parser iterated too many times. Probable loop in grammar." );
5050 }
5151 $rule = $rule->firstChild;
52 - if ($rule->nodeName == "assignment" || $rule->nodeName == "reference" || $rule->nodeName == "text") {
53 - $saveTags = str_replace("~r", preg_quote($replaceStr, "/"), $saveTags);
54 - $newTags = $rule->getAttribute("saveTags");
55 - if ($saveTags == "") {
 52+ if ( $rule->nodeName == "assignment" || $rule->nodeName == "reference" || $rule->nodeName == "text" ) {
 53+ $saveTags = str_replace( "~r", preg_quote( $replaceStr, "/" ), $saveTags );
 54+ $newTags = $rule->getAttribute( "saveTags" );
 55+ if ( $saveTags == "" ) {
5656 $saveTags = $newTags;
57 - } elseif ($newTags != "") {
 57+ } elseif ( $newTags != "" ) {
5858 $saveTags .= "|" . $newTags;
5959 }
6060 }
6161 $dom = $outNode->ownerDocument == NULL ? $outNode : $outNode->ownerDocument;
62 - $xpath = new DOMXPath($rule->ownerDocument);
63 - $childRules = $xpath->query("parseObject", $rule);
 62+ $xpath = new DOMXPath( $rule->ownerDocument );
 63+ $childRules = $xpath->query( "parseObject", $rule );
6464 $retCode = TRUE;
65 - if ($rule->nodeName == "assignment") {
66 - $patterns = $xpath->query("pattern", $rule);
 65+ if ( $rule->nodeName == "assignment" ) {
 66+ $patterns = $xpath->query( "pattern", $rule );
6767 $tag = "";
68 - if ($patterns->length > 0) {
69 - $pattern = str_replace("~r", $replaceStr, $patterns->item(0)->getAttribute("tag"));
70 - $retCode = preg_match("/^$pattern/s", $text, $matches);
71 - if ($retCode) {
 68+ if ( $patterns->length > 0 ) {
 69+ $pattern = str_replace( "~r", $replaceStr, $patterns->item( 0 )->getAttribute( "tag" ) );
 70+ $retCode = preg_match( "/^$pattern/s", $text, $matches );
 71+ if ( $retCode ) {
7272 $tag = $matches[0];
73 - if (isset($matches[1])) {
 73+ if ( isset( $matches[1] ) ) {
7474 $replaceStr = $matches[1];
7575 }
7676 }
7777 }
78 - if ($retCode) {
 78+ if ( $retCode ) {
7979 $newText = $text;
80 - $newElement = $dom->createElement($rule->getAttribute("tag"));
81 - if ($tag != "") {
82 - $newText = substr($newText, strlen($tag));
83 - $newElement->setAttribute("tag", $tag);
 80+ $newElement = $dom->createElement( $rule->getAttribute( "tag" ) );
 81+ if ( $tag != "" ) {
 82+ $newText = substr( $newText, strlen( $tag ) );
 83+ $newElement->setAttribute( "tag", $tag );
8484 }
85 - $retCode = $childRules->length <= 0 || $this->parseRec($childRules->item(0), $replaceStr, $saveTags, $iter, $newText, $newElement);
86 - if ($retCode) {
87 - $outNode->appendChild($newElement);
 85+ $retCode = $childRules->length <= 0 || $this->parseRec( $childRules->item( 0 ), $replaceStr, $saveTags, $iter, $newText, $newElement );
 86+ if ( $retCode ) {
 87+ $outNode->appendChild( $newElement );
8888 $text = $newText;
8989 }
9090 }
91 - } elseif ($rule->nodeName == "sequence") {
92 - $pushInd = $rule->getAttribute("pushInd");
93 - if ($pushInd > 0) {
 91+ } elseif ( $rule->nodeName == "sequence" ) {
 92+ $pushInd = $rule->getAttribute( "pushInd" );
 93+ if ( $pushInd > 0 ) {
9494 $saveText = $text;
95 - $saveNode = $outNode->cloneNode(TRUE);
 95+ $saveNode = $outNode->cloneNode( TRUE );
9696 }
97 - foreach ($childRules as $i => $child) {
 97+ foreach ( $childRules as $i => $child ) {
9898 $pushTags = $i >= $pushInd ? $saveTags : "";
99 - $retCode = $this->parseRec($child, $replaceStr, $pushTags, $iter, $text, $outNode);
100 - if (! $retCode) {
101 - if ($i > 0) {
 99+ $retCode = $this->parseRec( $child, $replaceStr, $pushTags, $iter, $text, $outNode );
 100+ if ( ! $retCode ) {
 101+ if ( $i > 0 ) {
102102 $text = $saveText;
103103 $outNode = $saveNode;
104104 }
105105 break;
106106 }
107107 }
108 - } elseif ($rule->nodeName == "choice") {
109 - foreach ($childRules as $child) {
110 - $retCode = $this->parseRec($child, $replaceStr, $saveTags, $iter, $text, $outNode);
111 - if ($retCode) {
 108+ } elseif ( $rule->nodeName == "choice" ) {
 109+ foreach ( $childRules as $child ) {
 110+ $retCode = $this->parseRec( $child, $replaceStr, $saveTags, $iter, $text, $outNode );
 111+ if ( $retCode ) {
112112 break;
113113 }
114114 }
115 - $retCode |= $rule->hasAttribute("tag");
116 - } elseif ($rule->nodeName == "reference") {
117 - $childRule = $rule->getAttribute("tag");
118 - wfDebugLog("ParseEngine", "Entering $childRule\n");
119 - $varNode = $xpath->query("pattern", $rule);
120 - if ($varNode->length > 0) {
121 - $replaceStr = str_replace("~r", $replaceStr, $varNode->item(0)->getAttribute("tag"));
 115+ $retCode |= $rule->hasAttribute( "tag" );
 116+ } elseif ( $rule->nodeName == "reference" ) {
 117+ $childRule = $rule->getAttribute( "tag" );
 118+ wfDebugLog( "ParseEngine", "Entering $childRule\n" );
 119+ $varNode = $xpath->query( "pattern", $rule );
 120+ if ( $varNode->length > 0 ) {
 121+ $replaceStr = str_replace( "~r", $replaceStr, $varNode->item( 0 )->getAttribute( "tag" ) );
122122 }
123 - $refRule = $xpath->query("/grammar/rule[@tag='$childRule']/parseObject")->item(0);
124 - $retCode = $this->parseRec($refRule, $replaceStr, $saveTags, $iter, $text, $outNode);
125 - wfDebugLog("ParseEngine", "Exiting $childRule, Return Code - $retCode\n");
126 - wfDebugLog("ParseEngine", "text - $text\n");
127 - } elseif ($rule->nodeName == "text") {
128 - $tagSearch = $rule->getAttribute("childTags");
129 - if ($tagSearch == "") {
 123+ $refRule = $xpath->query( "/grammar/rule[@tag='$childRule']/parseObject" )->item( 0 );
 124+ $retCode = $this->parseRec( $refRule, $replaceStr, $saveTags, $iter, $text, $outNode );
 125+ wfDebugLog( "ParseEngine", "Exiting $childRule, Return Code - $retCode\n" );
 126+ wfDebugLog( "ParseEngine", "text - $text\n" );
 127+ } elseif ( $rule->nodeName == "text" ) {
 128+ $tagSearch = $rule->getAttribute( "childTags" );
 129+ if ( $tagSearch == "" ) {
130130 $tagSearch = $saveTags;
131 - } elseif ($saveTags != "") {
 131+ } elseif ( $saveTags != "" ) {
132132 $tagSearch .= "|" . $saveTags;
133133 }
134 - $childNode = $childRules->length <= 0 ? NULL : $childRules->item(0);
135 - while ($text != "" && ($saveTags == "" || ! preg_match("/^($saveTags)/s", $text))) {
136 - $offset = $childNode != NULL && $this->parseRec($childNode, $replaceStr, "", $iter, $text, $outNode) ? 0 : 1;
137 - if (preg_match("/$tagSearch/s", $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
138 - if ($matches[0][1] > 0) {
139 - $outNode->appendChild($dom->createTextNode(substr($text, 0, $matches[0][1])));
140 - $text = substr($text, $matches[0][1]);
 134+ $childNode = $childRules->length <= 0 ? NULL : $childRules->item( 0 );
 135+ while ( $text != "" && ( $saveTags == "" || ! preg_match( "/^($saveTags)/s", $text ) ) ) {
 136+ $offset = $childNode != NULL && $this->parseRec( $childNode, $replaceStr, "", $iter, $text, $outNode ) ? 0 : 1;
 137+ if ( preg_match( "/$tagSearch/s", $text, $matches, PREG_OFFSET_CAPTURE, $offset ) ) {
 138+ if ( $matches[0][1] > 0 ) {
 139+ $outNode->appendChild( $dom->createTextNode( substr( $text, 0, $matches[0][1] ) ) );
 140+ $text = substr( $text, $matches[0][1] );
141141 }
142142 } else {
143 - $outNode->appendChild($dom->createTextNode($text));
 143+ $outNode->appendChild( $dom->createTextNode( $text ) );
144144 $text = "";
145145 }
146146 }
@@ -147,85 +147,85 @@
148148 return $retCode;
149149 }
150150
151 - private function pushTags($rule, $tagStr) {
 151+ private function pushTags( $rule, $tagStr ) {
152152 $rule = $rule->firstChild;
153 - $xpath = new DOMXPath($rule->ownerDocument);
154 - $childRules = $xpath->query("parseObject", $rule);
155 - if ($rule->nodeName == "sequence") {
 153+ $xpath = new DOMXPath( $rule->ownerDocument );
 154+ $childRules = $xpath->query( "parseObject", $rule );
 155+ if ( $rule->nodeName == "sequence" ) {
156156 $pushInd = 0;
157 - for ($i = $childRules->length - 1; $i >= 0; $i --) {
158 - $this->pushTags($childRules->item($i), $tagStr);
159 - if ($i > 0) {
160 - if ($this->pullTags($childRules->item($i), $iter, $childTag)) {
161 - if ($tagStr == "") {
 157+ for ( $i = $childRules->length - 1; $i >= 0; $i -- ) {
 158+ $this->pushTags( $childRules->item( $i ), $tagStr );
 159+ if ( $i > 0 ) {
 160+ if ( $this->pullTags( $childRules->item( $i ), $iter, $childTag ) ) {
 161+ if ( $tagStr == "" ) {
162162 $tagStr = $childTag;
163 - } elseif ($childTag != "") {
 163+ } elseif ( $childTag != "" ) {
164164 $tagStr .= "|" . $childTag;
165165 }
166166 } else {
167 - if ($pushInd < $i) {
 167+ if ( $pushInd < $i ) {
168168 $pushInd = $i;
169169 }
170170 $tagStr = $childTag;
171171 }
172172 }
173173 }
174 - $rule->setAttribute("pushInd", $pushInd);
 174+ $rule->setAttribute( "pushInd", $pushInd );
175175 } else {
176 - if ($rule->nodeName != "choice") {
177 - $rule->setAttribute("saveTags", $tagStr);
 176+ if ( $rule->nodeName != "choice" ) {
 177+ $rule->setAttribute( "saveTags", $tagStr );
178178 $tagStr = NULL;
179 - if ($rule->nodeName == "text") {
 179+ if ( $rule->nodeName == "text" ) {
180180 $childTags = "";
181 - foreach ($childRules as $child) {
182 - if ($childTags != "") {
 181+ foreach ( $childRules as $child ) {
 182+ if ( $childTags != "" ) {
183183 $childTags .= "|";
184184 }
185 - $this->pullTags($child, $iter, $childTag);
 185+ $this->pullTags( $child, $iter, $childTag );
186186 $childTags .= $childTag;
187187 }
188 - $rule->setAttribute("childTags", $childTags);
 188+ $rule->setAttribute( "childTags", $childTags );
189189 }
190190 }
191 - foreach ($childRules as $child) {
192 - $this->pushTags($child, $tagStr);
 191+ foreach ( $childRules as $child ) {
 192+ $this->pushTags( $child, $tagStr );
193193 }
194194 }
195195 }
196196
197 - private function pullTags($rule, &$iter, &$childTags) {
 197+ private function pullTags( $rule, &$iter, &$childTags ) {
198198 $iter ++;
199 - if ($iter > ParseEngine::maxIter) {
200 - throw new MWException("Collecter iterated too many times. Probable loop in grammar.");
 199+ if ( $iter > ParseEngine::maxIter ) {
 200+ throw new MWException( "Collecter iterated too many times. Probable loop in grammar." );
201201 }
202202 $rule = $rule->firstChild;
203 - $xpath = new DOMXPath($rule->ownerDocument);
 203+ $xpath = new DOMXPath( $rule->ownerDocument );
204204 $childTags = "";
205205 $failSafe = TRUE;
206 - if ($rule->nodeName == "assignment") {
207 - $patterns = $xpath->query("pattern", $rule);
208 - if ($patterns->length > 0) {
209 - $childTags = $patterns->item(0)->getAttribute("tag");
 206+ if ( $rule->nodeName == "assignment" ) {
 207+ $patterns = $xpath->query( "pattern", $rule );
 208+ if ( $patterns->length > 0 ) {
 209+ $childTags = $patterns->item( 0 )->getAttribute( "tag" );
210210 }
211211 $failSafe = FALSE;
212 - } elseif ($rule->nodeName == "choice" || $rule->nodeName == "sequence") {
213 - $childRules = $xpath->query("parseObject", $rule);
 212+ } elseif ( $rule->nodeName == "choice" || $rule->nodeName == "sequence" ) {
 213+ $childRules = $xpath->query( "parseObject", $rule );
214214 $failSafe = $rule->nodeName == "sequence";
215 - foreach ($childRules as $child) {
216 - $failSafe = $this->pullTags($child, $iter, $newTags);
217 - if ($childTags == "") {
 215+ foreach ( $childRules as $child ) {
 216+ $failSafe = $this->pullTags( $child, $iter, $newTags );
 217+ if ( $childTags == "" ) {
218218 $childTags = $newTags;
219 - } elseif ($newTags != "") {
 219+ } elseif ( $newTags != "" ) {
220220 $childTags .= "|" . $newTags;
221221 }
222 - if (($failSafe && $rule->nodeName == "choice") || (! $failSafe && $rule->nodeName == "sequence")) {
 222+ if ( ( $failSafe && $rule->nodeName == "choice" ) || ( ! $failSafe && $rule->nodeName == "sequence" ) ) {
223223 break;
224224 }
225225 }
226 - $failSafe |= $rule->hasAttribute("tag");
227 - } elseif ($rule->nodeName == "reference") {
228 - $refRule = $xpath->query("/grammar/rule[@tag='{$rule->getAttribute("tag")}']/parseObject")->item(0);
229 - $failSafe = $this->pullTags($refRule, $iter, $childTags);
 226+ $failSafe |= $rule->hasAttribute( "tag" );
 227+ } elseif ( $rule->nodeName == "reference" ) {
 228+ $refRule = $xpath->query( "/grammar/rule[@tag='{$rule->getAttribute("tag")}']/parseObject" )->item( 0 );
 229+ $failSafe = $this->pullTags( $refRule, $iter, $childTags );
230230 }
231231 return $failSafe;
232232 }
Index: trunk/extensions/ParserWiki/ParserWiki.php
@@ -22,10 +22,10 @@
2323 $wgAutoloadClasses["ParseEngine"] = dirname( __FILE__ ) . "/ParseEngine.php";
2424
2525 $wgTheParserWiki = new ParserWiki();
26 -$wgHooks["ParserBeforeStrip"][] = array($wgTheParserWiki, "callFromParse");
 26+$wgHooks["ParserBeforeStrip"][] = array( $wgTheParserWiki, "callFromParse" );
2727
28 -define ( "NS_GRAMMAR" , 91628);
29 -define ( "NS_GRAMMAR_TALK" , 91629);
 28+define ( "NS_GRAMMAR" , 91628 );
 29+define ( "NS_GRAMMAR_TALK" , 91629 );
3030 $wgExtraNamespaces[NS_GRAMMAR] = "Grammar";
3131 $wgExtraNamespaces[NS_GRAMMAR_TALK] = "Grammar_talk";
3232
@@ -36,20 +36,20 @@
3737 $this->mEngines = array();
3838 }
3939
40 - function callFromParse($unUsed, &$text) {
 40+ function callFromParse( $unUsed, &$text ) {
4141 global $wgParserWikiGrammar;
4242 $engine = $this->mEngines[$wgParserWikiGrammar];
43 - if ($engine == NULL) {
44 - $revision = Revision::newFromTitle(Title::newFromText($wgParserWikiGrammar, NS_GRAMMAR));
 43+ if ( $engine == NULL ) {
 44+ $revision = Revision::newFromTitle( Title::newFromText( $wgParserWikiGrammar, NS_GRAMMAR ) );
4545 $grammar = new DOMDocument();
46 - if ($revision == NULL || ! $grammar->loadXML($revision->getText(), LIBXML_NOBLANKS)) {
 46+ if ( $revision == NULL || ! $grammar->loadXML( $revision->getText(), LIBXML_NOBLANKS ) ) {
4747 return TRUE;
4848 }
49 - $engine = new ParseEngine($grammar);
 49+ $engine = new ParseEngine( $grammar );
5050 $this->mEngines[$wgParserWikiGrammar] = $engine;
5151 }
52 - $parseTree = $engine->parse($text);
53 - if ($parseTree == NULL) {
 52+ $parseTree = $engine->parse( $text );
 53+ if ( $parseTree == NULL ) {
5454 return TRUE;
5555 }
5656 $text = $parseTree->saveXML();

Status & tagging log