http://www.mediawiki.org/wiki/Special:Code/MediaWiki/65437
Revision: 65437
Author: raymond
Date: 2010-04-22 19:52:58 +0000 (Thu, 22 Apr 2010)
Log Message:
-----------
Run stylize.php
Modified Paths:
--------------
trunk/extensions/ParserWiki/ParseEngine.php
trunk/extensions/ParserWiki/ParserWiki.php
Modified: trunk/extensions/ParserWiki/ParseEngine.php
===================================================================
--- trunk/extensions/ParserWiki/ParseEngine.php 2010-04-22 19:51:41 UTC (rev
65436)
+++ trunk/extensions/ParserWiki/ParseEngine.php 2010-04-22 19:52:58 UTC (rev
65437)
@@ -10,136 +10,136 @@
const maxIter = 4096;
private $mGrammar;
- function __construct($grammar) {
- $xpath = new DOMXPath($grammar);
- $rootRules = $xpath->query("/grammar/parseObject |
/grammar/rule/parseObject");
- foreach ($rootRules as $child) {
- $this->pushTags($child, NULL);
+ function __construct( $grammar ) {
+ $xpath = new DOMXPath( $grammar );
+ $rootRules = $xpath->query( "/grammar/parseObject |
/grammar/rule/parseObject" );
+ foreach ( $rootRules as $child ) {
+ $this->pushTags( $child, NULL );
}
$this->mGrammar = $grammar;
}
- function parse($text) {
- wfDebugLog("ParseEngine", "==========Start Parse
Engine==========\n");
- $xpath = new DOMXPath($this->mGrammar);
- $rootAssign = $xpath->query("/grammar/parseObject")->item(0);
+ function parse( $text ) {
+ wfDebugLog( "ParseEngine", "==========Start Parse
Engine==========\n" );
+ $xpath = new DOMXPath( $this->mGrammar );
+ $rootAssign = $xpath->query( "/grammar/parseObject" )->item( 0
);
$doc = new DOMDocument();
- if (! $this->parseRec($rootAssign, "", "", $iter, $text, $doc))
{
+ if ( ! $this->parseRec( $rootAssign, "", "", $iter, $text, $doc
) ) {
$doc = NULL;
}
return $doc;
}
- static function unparse($inNodes) {
+ static function unparse( $inNodes ) {
$retStr = "";
- foreach ($inNodes as $child) {
- if ($child instanceof DOMText) {
+ foreach ( $inNodes as $child ) {
+ if ( $child instanceof DOMText ) {
$retStr .= $child->data;
} else {
- $retStr .= $child->getAttribute("tag") .
self::unparse($child->childNodes);
+ $retStr .= $child->getAttribute( "tag" ) .
self::unparse( $child->childNodes );
}
}
return $retStr;
}
- private function parseRec($rule, $replaceStr, $saveTags, &$iter,
&$text, &$outNode) {
+ private function parseRec( $rule, $replaceStr, $saveTags, &$iter,
&$text, &$outNode ) {
$iter ++;
- if ($iter > ParseEngine::maxIter) {
- throw new MWException("Parser iterated too many times.
Probable loop in grammar.");
+ if ( $iter > ParseEngine::maxIter ) {
+ throw new MWException( "Parser iterated too many times.
Probable loop in grammar." );
}
$rule = $rule->firstChild;
- if ($rule->nodeName == "assignment" || $rule->nodeName ==
"reference" || $rule->nodeName == "text") {
- $saveTags = str_replace("~r", preg_quote($replaceStr,
"/"), $saveTags);
- $newTags = $rule->getAttribute("saveTags");
- if ($saveTags == "") {
+ if ( $rule->nodeName == "assignment" || $rule->nodeName ==
"reference" || $rule->nodeName == "text" ) {
+ $saveTags = str_replace( "~r", preg_quote( $replaceStr,
"/" ), $saveTags );
+ $newTags = $rule->getAttribute( "saveTags" );
+ if ( $saveTags == "" ) {
$saveTags = $newTags;
- } elseif ($newTags != "") {
+ } elseif ( $newTags != "" ) {
$saveTags .= "|" . $newTags;
}
}
$dom = $outNode->ownerDocument == NULL ? $outNode :
$outNode->ownerDocument;
- $xpath = new DOMXPath($rule->ownerDocument);
- $childRules = $xpath->query("parseObject", $rule);
+ $xpath = new DOMXPath( $rule->ownerDocument );
+ $childRules = $xpath->query( "parseObject", $rule );
$retCode = TRUE;
- if ($rule->nodeName == "assignment") {
- $patterns = $xpath->query("pattern", $rule);
+ if ( $rule->nodeName == "assignment" ) {
+ $patterns = $xpath->query( "pattern", $rule );
$tag = "";
- if ($patterns->length > 0) {
- $pattern = str_replace("~r", $replaceStr,
$patterns->item(0)->getAttribute("tag"));
- $retCode = preg_match("/^$pattern/s", $text,
$matches);
- if ($retCode) {
+ if ( $patterns->length > 0 ) {
+ $pattern = str_replace( "~r", $replaceStr,
$patterns->item( 0 )->getAttribute( "tag" ) );
+ $retCode = preg_match( "/^$pattern/s", $text,
$matches );
+ if ( $retCode ) {
$tag = $matches[0];
- if (isset($matches[1])) {
+ if ( isset( $matches[1] ) ) {
$replaceStr = $matches[1];
}
}
}
- if ($retCode) {
+ if ( $retCode ) {
$newText = $text;
- $newElement =
$dom->createElement($rule->getAttribute("tag"));
- if ($tag != "") {
- $newText = substr($newText,
strlen($tag));
- $newElement->setAttribute("tag", $tag);
+ $newElement = $dom->createElement(
$rule->getAttribute( "tag" ) );
+ if ( $tag != "" ) {
+ $newText = substr( $newText, strlen(
$tag ) );
+ $newElement->setAttribute( "tag", $tag
);
}
- $retCode = $childRules->length <= 0 ||
$this->parseRec($childRules->item(0), $replaceStr, $saveTags, $iter, $newText,
$newElement);
- if ($retCode) {
- $outNode->appendChild($newElement);
+ $retCode = $childRules->length <= 0 ||
$this->parseRec( $childRules->item( 0 ), $replaceStr, $saveTags, $iter,
$newText, $newElement );
+ if ( $retCode ) {
+ $outNode->appendChild( $newElement );
$text = $newText;
}
}
- } elseif ($rule->nodeName == "sequence") {
- $pushInd = $rule->getAttribute("pushInd");
- if ($pushInd > 0) {
+ } elseif ( $rule->nodeName == "sequence" ) {
+ $pushInd = $rule->getAttribute( "pushInd" );
+ if ( $pushInd > 0 ) {
$saveText = $text;
- $saveNode = $outNode->cloneNode(TRUE);
+ $saveNode = $outNode->cloneNode( TRUE );
}
- foreach ($childRules as $i => $child) {
+ foreach ( $childRules as $i => $child ) {
$pushTags = $i >= $pushInd ? $saveTags : "";
- $retCode = $this->parseRec($child, $replaceStr,
$pushTags, $iter, $text, $outNode);
- if (! $retCode) {
- if ($i > 0) {
+ $retCode = $this->parseRec( $child,
$replaceStr, $pushTags, $iter, $text, $outNode );
+ if ( ! $retCode ) {
+ if ( $i > 0 ) {
$text = $saveText;
$outNode = $saveNode;
}
break;
}
}
- } elseif ($rule->nodeName == "choice") {
- foreach ($childRules as $child) {
- $retCode = $this->parseRec($child, $replaceStr,
$saveTags, $iter, $text, $outNode);
- if ($retCode) {
+ } elseif ( $rule->nodeName == "choice" ) {
+ foreach ( $childRules as $child ) {
+ $retCode = $this->parseRec( $child,
$replaceStr, $saveTags, $iter, $text, $outNode );
+ if ( $retCode ) {
break;
}
}
- $retCode |= $rule->hasAttribute("tag");
- } elseif ($rule->nodeName == "reference") {
- $childRule = $rule->getAttribute("tag");
- wfDebugLog("ParseEngine", "Entering $childRule\n");
- $varNode = $xpath->query("pattern", $rule);
- if ($varNode->length > 0) {
- $replaceStr = str_replace("~r", $replaceStr,
$varNode->item(0)->getAttribute("tag"));
+ $retCode |= $rule->hasAttribute( "tag" );
+ } elseif ( $rule->nodeName == "reference" ) {
+ $childRule = $rule->getAttribute( "tag" );
+ wfDebugLog( "ParseEngine", "Entering $childRule\n" );
+ $varNode = $xpath->query( "pattern", $rule );
+ if ( $varNode->length > 0 ) {
+ $replaceStr = str_replace( "~r", $replaceStr,
$varNode->item( 0 )->getAttribute( "tag" ) );
}
- $refRule =
$xpath->query("/grammar/ru...@tag='$childRule']/parseObject")->item(0);
- $retCode = $this->parseRec($refRule, $replaceStr,
$saveTags, $iter, $text, $outNode);
- wfDebugLog("ParseEngine", "Exiting $childRule, Return
Code - $retCode\n");
- wfDebugLog("ParseEngine", "text - $text\n");
- } elseif ($rule->nodeName == "text") {
- $tagSearch = $rule->getAttribute("childTags");
- if ($tagSearch == "") {
+ $refRule = $xpath->query(
"/grammar/ru...@tag='$childRule']/parseObject" )->item( 0 );
+ $retCode = $this->parseRec( $refRule, $replaceStr,
$saveTags, $iter, $text, $outNode );
+ wfDebugLog( "ParseEngine", "Exiting $childRule, Return
Code - $retCode\n" );
+ wfDebugLog( "ParseEngine", "text - $text\n" );
+ } elseif ( $rule->nodeName == "text" ) {
+ $tagSearch = $rule->getAttribute( "childTags" );
+ if ( $tagSearch == "" ) {
$tagSearch = $saveTags;
- } elseif ($saveTags != "") {
+ } elseif ( $saveTags != "" ) {
$tagSearch .= "|" . $saveTags;
}
- $childNode = $childRules->length <= 0 ? NULL :
$childRules->item(0);
- while ($text != "" && ($saveTags == "" || !
preg_match("/^($saveTags)/s", $text))) {
- $offset = $childNode != NULL &&
$this->parseRec($childNode, $replaceStr, "", $iter, $text, $outNode) ? 0 : 1;
- if (preg_match("/$tagSearch/s", $text,
$matches, PREG_OFFSET_CAPTURE, $offset)) {
- if ($matches[0][1] > 0) {
-
$outNode->appendChild($dom->createTextNode(substr($text, 0, $matches[0][1])));
- $text = substr($text,
$matches[0][1]);
+ $childNode = $childRules->length <= 0 ? NULL :
$childRules->item( 0 );
+ while ( $text != "" && ( $saveTags == "" || !
preg_match( "/^($saveTags)/s", $text ) ) ) {
+ $offset = $childNode != NULL &&
$this->parseRec( $childNode, $replaceStr, "", $iter, $text, $outNode ) ? 0 : 1;
+ if ( preg_match( "/$tagSearch/s", $text,
$matches, PREG_OFFSET_CAPTURE, $offset ) ) {
+ if ( $matches[0][1] > 0 ) {
+ $outNode->appendChild(
$dom->createTextNode( substr( $text, 0, $matches[0][1] ) ) );
+ $text = substr( $text,
$matches[0][1] );
}
} else {
-
$outNode->appendChild($dom->createTextNode($text));
+ $outNode->appendChild(
$dom->createTextNode( $text ) );
$text = "";
}
}
@@ -147,85 +147,85 @@
return $retCode;
}
- private function pushTags($rule, $tagStr) {
+ private function pushTags( $rule, $tagStr ) {
$rule = $rule->firstChild;
- $xpath = new DOMXPath($rule->ownerDocument);
- $childRules = $xpath->query("parseObject", $rule);
- if ($rule->nodeName == "sequence") {
+ $xpath = new DOMXPath( $rule->ownerDocument );
+ $childRules = $xpath->query( "parseObject", $rule );
+ if ( $rule->nodeName == "sequence" ) {
$pushInd = 0;
- for ($i = $childRules->length - 1; $i >= 0; $i --) {
- $this->pushTags($childRules->item($i), $tagStr);
- if ($i > 0) {
- if
($this->pullTags($childRules->item($i), $iter, $childTag)) {
- if ($tagStr == "") {
+ for ( $i = $childRules->length - 1; $i >= 0; $i -- ) {
+ $this->pushTags( $childRules->item( $i ),
$tagStr );
+ if ( $i > 0 ) {
+ if ( $this->pullTags(
$childRules->item( $i ), $iter, $childTag ) ) {
+ if ( $tagStr == "" ) {
$tagStr = $childTag;
- } elseif ($childTag != "") {
+ } elseif ( $childTag != "" ) {
$tagStr .= "|" .
$childTag;
}
} else {
- if ($pushInd < $i) {
+ if ( $pushInd < $i ) {
$pushInd = $i;
}
$tagStr = $childTag;
}
}
}
- $rule->setAttribute("pushInd", $pushInd);
+ $rule->setAttribute( "pushInd", $pushInd );
} else {
- if ($rule->nodeName != "choice") {
- $rule->setAttribute("saveTags", $tagStr);
+ if ( $rule->nodeName != "choice" ) {
+ $rule->setAttribute( "saveTags", $tagStr );
$tagStr = NULL;
- if ($rule->nodeName == "text") {
+ if ( $rule->nodeName == "text" ) {
$childTags = "";
- foreach ($childRules as $child) {
- if ($childTags != "") {
+ foreach ( $childRules as $child ) {
+ if ( $childTags != "" ) {
$childTags .= "|";
}
- $this->pullTags($child, $iter,
$childTag);
+ $this->pullTags( $child, $iter,
$childTag );
$childTags .= $childTag;
}
- $rule->setAttribute("childTags",
$childTags);
+ $rule->setAttribute( "childTags",
$childTags );
}
}
- foreach ($childRules as $child) {
- $this->pushTags($child, $tagStr);
+ foreach ( $childRules as $child ) {
+ $this->pushTags( $child, $tagStr );
}
}
}
- private function pullTags($rule, &$iter, &$childTags) {
+ private function pullTags( $rule, &$iter, &$childTags ) {
$iter ++;
- if ($iter > ParseEngine::maxIter) {
- throw new MWException("Collecter iterated too many
times. Probable loop in grammar.");
+ if ( $iter > ParseEngine::maxIter ) {
+ throw new MWException( "Collecter iterated too many
times. Probable loop in grammar." );
}
$rule = $rule->firstChild;
- $xpath = new DOMXPath($rule->ownerDocument);
+ $xpath = new DOMXPath( $rule->ownerDocument );
$childTags = "";
$failSafe = TRUE;
- if ($rule->nodeName == "assignment") {
- $patterns = $xpath->query("pattern", $rule);
- if ($patterns->length > 0) {
- $childTags =
$patterns->item(0)->getAttribute("tag");
+ if ( $rule->nodeName == "assignment" ) {
+ $patterns = $xpath->query( "pattern", $rule );
+ if ( $patterns->length > 0 ) {
+ $childTags = $patterns->item( 0
)->getAttribute( "tag" );
}
$failSafe = FALSE;
- } elseif ($rule->nodeName == "choice" || $rule->nodeName ==
"sequence") {
- $childRules = $xpath->query("parseObject", $rule);
+ } elseif ( $rule->nodeName == "choice" || $rule->nodeName ==
"sequence" ) {
+ $childRules = $xpath->query( "parseObject", $rule );
$failSafe = $rule->nodeName == "sequence";
- foreach ($childRules as $child) {
- $failSafe = $this->pullTags($child, $iter,
$newTags);
- if ($childTags == "") {
+ foreach ( $childRules as $child ) {
+ $failSafe = $this->pullTags( $child, $iter,
$newTags );
+ if ( $childTags == "" ) {
$childTags = $newTags;
- } elseif ($newTags != "") {
+ } elseif ( $newTags != "" ) {
$childTags .= "|" . $newTags;
}
- if (($failSafe && $rule->nodeName == "choice")
|| (! $failSafe && $rule->nodeName == "sequence")) {
+ if ( ( $failSafe && $rule->nodeName == "choice"
) || ( ! $failSafe && $rule->nodeName == "sequence" ) ) {
break;
}
}
- $failSafe |= $rule->hasAttribute("tag");
- } elseif ($rule->nodeName == "reference") {
- $refRule =
$xpath->query("/grammar/ru...@tag='{$rule->getAttribute("tag")}']/parseObject")->item(0);
- $failSafe = $this->pullTags($refRule, $iter,
$childTags);
+ $failSafe |= $rule->hasAttribute( "tag" );
+ } elseif ( $rule->nodeName == "reference" ) {
+ $refRule = $xpath->query(
"/grammar/ru...@tag='{$rule->getAttribute("tag")}']/parseObject" )->item( 0 );
+ $failSafe = $this->pullTags( $refRule, $iter,
$childTags );
}
return $failSafe;
}
Modified: trunk/extensions/ParserWiki/ParserWiki.php
===================================================================
--- trunk/extensions/ParserWiki/ParserWiki.php 2010-04-22 19:51:41 UTC (rev
65436)
+++ trunk/extensions/ParserWiki/ParserWiki.php 2010-04-22 19:52:58 UTC (rev
65437)
@@ -22,10 +22,10 @@
$wgAutoloadClasses["ParseEngine"] = dirname( __FILE__ ) . "/ParseEngine.php";
$wgTheParserWiki = new ParserWiki();
-$wgHooks["ParserBeforeStrip"][] = array($wgTheParserWiki, "callFromParse");
+$wgHooks["ParserBeforeStrip"][] = array( $wgTheParserWiki, "callFromParse" );
-define ( "NS_GRAMMAR" , 91628);
-define ( "NS_GRAMMAR_TALK" , 91629);
+define ( "NS_GRAMMAR" , 91628 );
+define ( "NS_GRAMMAR_TALK" , 91629 );
$wgExtraNamespaces[NS_GRAMMAR] = "Grammar";
$wgExtraNamespaces[NS_GRAMMAR_TALK] = "Grammar_talk";
@@ -36,20 +36,20 @@
$this->mEngines = array();
}
- function callFromParse($unUsed, &$text) {
+ function callFromParse( $unUsed, &$text ) {
global $wgParserWikiGrammar;
$engine = $this->mEngines[$wgParserWikiGrammar];
- if ($engine == NULL) {
- $revision =
Revision::newFromTitle(Title::newFromText($wgParserWikiGrammar, NS_GRAMMAR));
+ if ( $engine == NULL ) {
+ $revision = Revision::newFromTitle( Title::newFromText(
$wgParserWikiGrammar, NS_GRAMMAR ) );
$grammar = new DOMDocument();
- if ($revision == NULL || !
$grammar->loadXML($revision->getText(), LIBXML_NOBLANKS)) {
+ if ( $revision == NULL || ! $grammar->loadXML(
$revision->getText(), LIBXML_NOBLANKS ) ) {
return TRUE;
}
- $engine = new ParseEngine($grammar);
+ $engine = new ParseEngine( $grammar );
$this->mEngines[$wgParserWikiGrammar] = $engine;
}
- $parseTree = $engine->parse($text);
- if ($parseTree == NULL) {
+ $parseTree = $engine->parse( $text );
+ if ( $parseTree == NULL ) {
return TRUE;
}
$text = $parseTree->saveXML();
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs