Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / includes / parser / PPNode_DOM.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Parser
20 */
21
22 /**
23 * @deprecated since 1.34, use PPNode_Hash_{Tree,Text,Array,Attr}
24 * @ingroup Parser
25 */
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27 class PPNode_DOM implements PPNode {
28
29 /**
30 * @var DOMElement|DOMNodeList
31 */
32 public $node;
33 public $xpath;
34
35 public function __construct( $node, $xpath = false ) {
36 $this->node = $node;
37 }
38
39 /**
40 * @return DOMXPath
41 */
42 public function getXPath() {
43 if ( $this->xpath === null ) {
44 $this->xpath = new DOMXPath( $this->node->ownerDocument );
45 }
46 return $this->xpath;
47 }
48
49 public function __toString() {
50 if ( $this->node instanceof DOMNodeList ) {
51 $s = '';
52 foreach ( $this->node as $node ) {
53 $s .= $node->ownerDocument->saveXML( $node );
54 }
55 } else {
56 $s = $this->node->ownerDocument->saveXML( $this->node );
57 }
58 return $s;
59 }
60
61 /**
62 * @return false|PPNode_DOM
63 */
64 public function getChildren() {
65 return $this->node->childNodes ? new self( $this->node->childNodes ) : false;
66 }
67
68 /**
69 * @return false|PPNode_DOM
70 */
71 public function getFirstChild() {
72 return $this->node->firstChild ? new self( $this->node->firstChild ) : false;
73 }
74
75 /**
76 * @return false|PPNode_DOM
77 */
78 public function getNextSibling() {
79 return $this->node->nextSibling ? new self( $this->node->nextSibling ) : false;
80 }
81
82 /**
83 * @param string $type
84 *
85 * @return false|PPNode_DOM
86 */
87 public function getChildrenOfType( $type ) {
88 return new self( $this->getXPath()->query( $type, $this->node ) );
89 }
90
91 /**
92 * @return int
93 */
94 public function getLength() {
95 if ( $this->node instanceof DOMNodeList ) {
96 return $this->node->length;
97 } else {
98 return false;
99 }
100 }
101
102 /**
103 * @param int $i
104 * @return bool|PPNode_DOM
105 */
106 public function item( $i ) {
107 $item = $this->node->item( $i );
108 return $item ? new self( $item ) : false;
109 }
110
111 /**
112 * @return string
113 */
114 public function getName() {
115 if ( $this->node instanceof DOMNodeList ) {
116 return '#nodelist';
117 } else {
118 return $this->node->nodeName;
119 }
120 }
121
122 /**
123 * Split a "<part>" node into an associative array containing:
124 * - name PPNode name
125 * - index String index
126 * - value PPNode value
127 *
128 * @throws MWException
129 * @return array
130 */
131 public function splitArg() {
132 $xpath = $this->getXPath();
133 $names = $xpath->query( 'name', $this->node );
134 $values = $xpath->query( 'value', $this->node );
135 if ( !$names->length || !$values->length ) {
136 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
137 }
138 $name = $names->item( 0 );
139 $index = $name->getAttribute( 'index' );
140 return [
141 'name' => new self( $name ),
142 'index' => $index,
143 'value' => new self( $values->item( 0 ) ) ];
144 }
145
146 /**
147 * Split an "<ext>" node into an associative array containing name, attr, inner and close
148 * All values in the resulting array are PPNodes. Inner and close are optional.
149 *
150 * @throws MWException
151 * @return array
152 */
153 public function splitExt() {
154 $xpath = $this->getXPath();
155 $names = $xpath->query( 'name', $this->node );
156 $attrs = $xpath->query( 'attr', $this->node );
157 $inners = $xpath->query( 'inner', $this->node );
158 $closes = $xpath->query( 'close', $this->node );
159 if ( !$names->length || !$attrs->length ) {
160 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
161 }
162 $parts = [
163 'name' => new self( $names->item( 0 ) ),
164 'attr' => new self( $attrs->item( 0 ) ) ];
165 if ( $inners->length ) {
166 $parts['inner'] = new self( $inners->item( 0 ) );
167 }
168 if ( $closes->length ) {
169 $parts['close'] = new self( $closes->item( 0 ) );
170 }
171 return $parts;
172 }
173
174 /**
175 * Split a "<h>" node
176 * @throws MWException
177 * @return array
178 */
179 public function splitHeading() {
180 if ( $this->getName() !== 'h' ) {
181 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
182 }
183 return [
184 'i' => $this->node->getAttribute( 'i' ),
185 'level' => $this->node->getAttribute( 'level' ),
186 'contents' => $this->getChildren()
187 ];
188 }
189 }