Index: trunk/extensions/PageObjectModel/POM.php |
— | — | @@ -8,6 +8,8 @@ |
9 | 9 | require_once('POM/Parser.php'); |
10 | 10 | require_once('POM/CommentParser.php'); |
11 | 11 | require_once('POM/Comment.php'); |
| 12 | +require_once('POM/LinkParser.php'); |
| 13 | +require_once('POM/Link.php'); |
12 | 14 | require_once('POM/TemplateParser.php'); |
13 | 15 | require_once('POM/Template.php'); |
14 | 16 | require_once('POM/TemplateCollection.php'); |
Index: trunk/extensions/PageObjectModel/POM/Element.php |
— | — | @@ -7,9 +7,13 @@ |
8 | 8 | abstract class POMElement { |
9 | 9 | |
10 | 10 | public $children = array(); |
| 11 | + |
| 12 | + private $hide = false; |
11 | 13 | |
12 | 14 | function asString() { |
13 | 15 | $output = ''; |
| 16 | + |
| 17 | + if ($this->hide) return $output; |
14 | 18 | |
15 | 19 | foreach ($this->children as $child) |
16 | 20 | { |
— | — | @@ -23,5 +27,17 @@ |
24 | 28 | { |
25 | 29 | $this->children[] = $el; |
26 | 30 | } |
| 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 | + } |
27 | 43 | } |
28 | 44 | |
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 @@ |
16 | 16 | |
17 | 17 | public function asString() |
18 | 18 | { |
| 19 | + if ($this->hidden()) return ""; |
19 | 20 | return $this->nodeText; |
20 | 21 | } |
21 | 22 | } |
Index: trunk/extensions/PageObjectModel/POM/Template.php |
— | — | @@ -60,6 +60,24 @@ |
61 | 61 | return ""; |
62 | 62 | } |
63 | 63 | |
| 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 | + |
64 | 82 | public function getParameterByNumber($number) { |
65 | 83 | if ($number < 0) return ""; |
66 | 84 | if ($number > count($this->parameters)-1) return ""; |
— | — | @@ -100,7 +118,15 @@ |
101 | 119 | |
102 | 120 | return null; # none matched |
103 | 121 | } |
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 | + |
105 | 131 | public function setParameter($name, $value, |
106 | 132 | $ignore_name_spacing = true, |
107 | 133 | $ignore_value_spacing = true, |
— | — | @@ -164,6 +190,8 @@ |
165 | 191 | |
166 | 192 | public function asString() |
167 | 193 | { |
| 194 | + if ($this->hidden()) return ""; |
| 195 | + |
168 | 196 | $text = '{{'.$this->title_triple->toString(); |
169 | 197 | |
170 | 198 | for ($i = 0; $i < count($this->parameters); $i++) |
Index: trunk/extensions/PageObjectModel/POM/Page.php |
— | — | @@ -9,7 +9,7 @@ |
10 | 10 | |
11 | 11 | var $templates; # shortcut to $c['templates'], set up if POMTemplateParser was used |
12 | 12 | |
13 | | - public function POMPage($text, $parsers = array('POMCommentParser', 'POMTemplateParser')) |
| 13 | + public function POMPage($text, $parsers = array('POMCommentParser', 'POMTemplateParser', 'POMLinkParser')) |
14 | 14 | { |
15 | 15 | $this->addChild(new POMTextNode($text)); |
16 | 16 | |
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 @@ |
40 | 40 | $newchildren[] = new POMTextNode(substr($nodetext, 0, $open)); |
41 | 41 | |
42 | 42 | // 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)); |
44 | 44 | $newchildren[] = $comment; |
45 | 45 | $page->c['comments'][] = $comment; |
46 | 46 | |
Index: trunk/extensions/PageObjectModel/POM/Comment.php |
— | — | @@ -8,11 +8,13 @@ |
9 | 9 | protected $nodeText = ''; |
10 | 10 | |
11 | 11 | public function POMComment($text) { |
| 12 | + $text = substr($text, 4, strlen($text) - 7); |
12 | 13 | $this->nodeText = $text; |
13 | 14 | $this->children = null; // forcefully ignore children |
14 | 15 | } |
15 | 16 | |
16 | 17 | public function asString() { |
| 18 | + if ($this->hidden()) return ""; |
17 | 19 | return '<!--' . $this->nodeText . '-->'; |
18 | 20 | } |
19 | 21 | } |