r63115 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63114‎ | r63115 | r63116 >
Date:10:23, 1 March 2010
Author:vrandezo
Status:deferred
Tags:
Comment:
Adding link object
Modified paths:
  • /trunk/extensions/PageObjectModel/POM.php (modified) (history)
  • /trunk/extensions/PageObjectModel/POM/Comment.php (modified) (history)
  • /trunk/extensions/PageObjectModel/POM/CommentParser.php (modified) (history)
  • /trunk/extensions/PageObjectModel/POM/Element.php (modified) (history)
  • /trunk/extensions/PageObjectModel/POM/Link.php (added) (history)
  • /trunk/extensions/PageObjectModel/POM/LinkParser.php (added) (history)
  • /trunk/extensions/PageObjectModel/POM/Page.php (modified) (history)
  • /trunk/extensions/PageObjectModel/POM/Template.php (modified) (history)
  • /trunk/extensions/PageObjectModel/POM/TextNode.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PageObjectModel/POM.php
@@ -8,6 +8,8 @@
99 require_once('POM/Parser.php');
1010 require_once('POM/CommentParser.php');
1111 require_once('POM/Comment.php');
 12+require_once('POM/LinkParser.php');
 13+require_once('POM/Link.php');
1214 require_once('POM/TemplateParser.php');
1315 require_once('POM/Template.php');
1416 require_once('POM/TemplateCollection.php');
Index: trunk/extensions/PageObjectModel/POM/Element.php
@@ -7,9 +7,13 @@
88 abstract class POMElement {
99
1010 public $children = array();
 11+
 12+ private $hide = false;
1113
1214 function asString() {
1315 $output = '';
 16+
 17+ if ($this->hide) return $output;
1418
1519 foreach ($this->children as $child)
1620 {
@@ -23,5 +27,17 @@
2428 {
2529 $this->children[] = $el;
2630 }
 31+
 32+ function hide() {
 33+ $this->hide = true;
 34+ }
 35+
 36+ function unhide() {
 37+ $this->hide = false;
 38+ }
 39+
 40+ function hidden() {
 41+ return $this->hide;
 42+ }
2743 }
2844
Index: trunk/extensions/PageObjectModel/POM/LinkParser.php
@@ -0,0 +1,66 @@
 2+<?php
 3+#
 4+# Parsing for internal links and annotations
 5+#
 6+
 7+class POMLinkParser extends POMParser {
 8+ #
 9+ # This function will parse all POMTextNodes in the page and add POMLink nodes if links are found
 10+ #
 11+ static function Parse(POMPage $page) {
 12+ // this array holds shortcuts to templates
 13+ $page->c['links'] = array();
 14+
 15+ // this array will hold updated version of the tree end will replace original one if it was updated
 16+ $newchildren = array();
 17+
 18+ // we'll set it to true if tree was updated
 19+ $replaceoldchildrenwithnew = false;
 20+
 21+ for ($n = 0; $n < sizeof($page->children); $n++) {
 22+ if (is_a($page->children[$n], 'POMTextNode')) {
 23+ $nodetext = $page->children[$n]->asString();
 24+
 25+ while(($open = strpos($nodetext, '[[')) !== FALSE) {
 26+ $position = $open;
 27+
 28+ $nextopen = strpos($nodetext, '[[', $position+2);
 29+ $pipe = strpos($nodetext, '|', $position+2);
 30+ $nextpipe = strpos($nodetext, '|', $pipe+1);
 31+ $close = strpos($nodetext, ']]', $position+2);
 32+
 33+ if (FALSE === $close) break; # some markup confuses this poor parser, and thus it ignores the rest of this node
 34+ if (!(FALSE === $nextpipe) && ($nextpipe < $close)) break;
 35+ if (!(FALSE === $nextopen) && ($nextopen < $close)) break;
 36+
 37+ // part before the template becomes text node
 38+ $newchildren[] = new POMTextNode(substr($nodetext, 0, $open));
 39+
 40+ // part between opening [[ and closing ]] becomes link
 41+ $link = new POMLink(substr($nodetext, $open, $close+2-$open));
 42+ $newchildren[] = $link;
 43+ $page->c['links'][] = $link;
 44+
 45+ $position = $close + 2;
 46+
 47+ // the rest of it should be processed for more templates
 48+ $nodetext = substr($nodetext, $position);
 49+
 50+ $replaceoldchildrenwithnew = true;
 51+ }
 52+
 53+ if (strlen($nodetext) > 0) {
 54+ $newchildren[] = new POMTextNode($nodetext);
 55+
 56+ $replaceoldchildrenwithnew = true;
 57+ }
 58+ } else {
 59+ // preserve non-text nodes in the tree
 60+ $newchildren[] = $page->children[$n];
 61+ }
 62+ }
 63+
 64+ // if tree was updated, let's replace it in the page
 65+ if ($replaceoldchildrenwithnew) $page->children = $newchildren;
 66+ }
 67+}
Index: trunk/extensions/PageObjectModel/POM/TextNode.php
@@ -15,6 +15,7 @@
1616
1717 public function asString()
1818 {
 19+ if ($this->hidden()) return "";
1920 return $this->nodeText;
2021 }
2122 }
Index: trunk/extensions/PageObjectModel/POM/Template.php
@@ -60,6 +60,24 @@
6161 return "";
6262 }
6363
 64+ public function removeParameterByNumber( $number ) {
 65+ if ($number < 0) return;
 66+ if ($number > count($this->parameters)-1) return;
 67+ unset($this->parameters[$number]);
 68+ $this->parameters = array_values($this->parameters);
 69+ }
 70+
 71+ public function getNumberByName( $name ) {
 72+ $trimmed_name = trim($name);
 73+ if (strlen($trimmed_name) == 0)
 74+ throw new WrongParameterNameException('Can\'t get parameter with no name');
 75+
 76+ for ($i = 0; $i < count($this->parameters); $i++) {
 77+ $parameter = $this->parameters[$i];
 78+ if ($parameter->getName() == $trimmed_name) return $i;
 79+ }
 80+ }
 81+
6482 public function getParameterByNumber($number) {
6583 if ($number < 0) return "";
6684 if ($number > count($this->parameters)-1) return "";
@@ -100,7 +118,15 @@
101119
102120 return null; # none matched
103121 }
104 -
 122+
 123+ public function addParameter($name, $value) {
 124+ if (strlen(trim($name)) == 0)
 125+ throw new WrongParameterNameException("Can't set parameter with no name");
 126+
 127+ # add parameter to parameters array
 128+ $this->parameters[] = new POMTemplateNamedParameter($name, $value);
 129+ }
 130+
105131 public function setParameter($name, $value,
106132 $ignore_name_spacing = true,
107133 $ignore_value_spacing = true,
@@ -164,6 +190,8 @@
165191
166192 public function asString()
167193 {
 194+ if ($this->hidden()) return "";
 195+
168196 $text = '{{'.$this->title_triple->toString();
169197
170198 for ($i = 0; $i < count($this->parameters); $i++)
Index: trunk/extensions/PageObjectModel/POM/Page.php
@@ -9,7 +9,7 @@
1010
1111 var $templates; # shortcut to $c['templates'], set up if POMTemplateParser was used
1212
13 - public function POMPage($text, $parsers = array('POMCommentParser', 'POMTemplateParser'))
 13+ public function POMPage($text, $parsers = array('POMCommentParser', 'POMTemplateParser', 'POMLinkParser'))
1414 {
1515 $this->addChild(new POMTextNode($text));
1616
Index: trunk/extensions/PageObjectModel/POM/Link.php
@@ -0,0 +1,65 @@
 2+<?php
 3+#
 4+# The Link class represents internal links of the form [[x::y|z]] (x and z being optional)
 5+#
 6+require_once('Util.php');
 7+
 8+class POMLink extends POMElement {
 9+ protected $destination;
 10+ protected $properties = array();
 11+ protected $display = null;
 12+
 13+ public function POMLink($text) {
 14+ $this->children = null; // forcefully ignore children
 15+
 16+ # Remove braces at the beginning and at the end
 17+ $text = substr($text, 2, strlen($text) - 4);
 18+
 19+ # Split by pipe
 20+ $parts = explode('|', $text);
 21+ if (count($parts)>2) {
 22+ die("[ERROR] Parsing a link with more than one pipe character: [[" . $text . "]]\n");
 23+ }
 24+ if (count($parts)==2) {
 25+ $this->display = $parts[1];
 26+ }
 27+ # Split first part by ::
 28+ $moreparts = explode("::", $parts[0]);
 29+ $this->destination = new POMUtilTrimTriple($moreparts[count($moreparts)-1]);
 30+ if (count($moreparts)>1) {
 31+ for ($i = 0; $i < count($moreparts)-1; $i++) {
 32+ $this->properties[] = new POMUtilTrimTriple($moreparts[$i]);
 33+ }
 34+ }
 35+ }
 36+
 37+ public function getDestination() {
 38+ return $this->destination->trimmed;
 39+ }
 40+
 41+ public function getProperties() {
 42+ $return = array();
 43+ foreach ($this->properties as $property) $return[] = $property->trimmed;
 44+ return $return;
 45+ }
 46+
 47+ public function removePropertyByNumber($i) {
 48+ if ($i < 0) return;
 49+ if ($i >= count($this->properties)) return;
 50+ unset($this->properties[$i]);
 51+ }
 52+
 53+ public function getDisplay() {
 54+ return $this->display;
 55+ }
 56+
 57+ public function asString() {
 58+ if ($this->hidden()) return "";
 59+ $text = '[[';
 60+ foreach ($this->properties as $property) $text .= $property->toString() . '::';
 61+ $text .= $this->destination->toString();
 62+ if (!is_null($this->display)) $text .= '|' . $this->display;
 63+ $text .= ']]';
 64+ return $text;
 65+ }
 66+}
Index: trunk/extensions/PageObjectModel/POM/CommentParser.php
@@ -39,7 +39,7 @@
4040 $newchildren[] = new POMTextNode(substr($nodetext, 0, $open));
4141
4242 // part between opening <!-- and closing --> becomes comment
43 - $comment = new POMComment(substr($nodetext, $open+4, $position-($open+4)));
 43+ $comment = new POMComment(substr($nodetext, $open, $position+3-$open));
4444 $newchildren[] = $comment;
4545 $page->c['comments'][] = $comment;
4646
Index: trunk/extensions/PageObjectModel/POM/Comment.php
@@ -8,11 +8,13 @@
99 protected $nodeText = '';
1010
1111 public function POMComment($text) {
 12+ $text = substr($text, 4, strlen($text) - 7);
1213 $this->nodeText = $text;
1314 $this->children = null; // forcefully ignore children
1415 }
1516
1617 public function asString() {
 18+ if ($this->hidden()) return "";
1719 return '<!--' . $this->nodeText . '-->';
1820 }
1921 }

Status & tagging log