Remove Revision::getRevisionText from ApiQueryDeletedrevs
[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 * @phan-file-suppress PhanUndeclaredMethod
26 */
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 class PPNode_DOM implements PPNode {
29
30 /**
31 * @var DOMElement|DOMNodeList
32 */
33 public $node;
34 public $xpath;
35
36 public function __construct( $node, $xpath = false ) {
37 $this->node = $node;
38 }
39
40 /**
41 * @return DOMXPath
42 */
43 public function getXPath() {
44 if ( $this->xpath === null ) {
45 $this->xpath = new DOMXPath( $this->node->ownerDocument );
46 }
47 return $this->xpath;
48 }
49
50 public function __toString() {
51 if ( $this->node instanceof DOMNodeList ) {
52 $s = '';
53 foreach ( $this->node as $node ) {
54 $s .= $node->ownerDocument->saveXML( $node );
55 }
56 } else {
57 $s = $this->node->ownerDocument->saveXML( $this->node );
58 }
59 return $s;
60 }
61
62 /**
63 * @return false|PPNode_DOM
64 */
65 public function getChildren() {
66 return $this->node->childNodes ? new self( $this->node->childNodes ) : false;
67 }
68
69 /**
70 * @return false|PPNode_DOM
71 */
72 public function getFirstChild() {
73 return $this->node->firstChild ? new self( $this->node->firstChild ) : false;
74 }
75
76 /**
77 * @return false|PPNode_DOM
78 */
79 public function getNextSibling() {
80 return $this->node->nextSibling ? new self( $this->node->nextSibling ) : false;
81 }
82
83 /**
84 * @param string $type
85 *
86 * @return false|PPNode_DOM
87 */
88 public function getChildrenOfType( $type ) {
89 return new self( $this->getXPath()->query( $type, $this->node ) );
90 }
91
92 /**
93 * @return int
94 */
95 public function getLength() {
96 if ( $this->node instanceof DOMNodeList ) {
97 return $this->node->length;
98 } else {
99 return false;
100 }
101 }
102
103 /**
104 * @param int $i
105 * @return bool|PPNode_DOM
106 */
107 public function item( $i ) {
108 $item = $this->node->item( $i );
109 return $item ? new self( $item ) : false;
110 }
111
112 /**
113 * @return string
114 */
115 public function getName() {
116 if ( $this->node instanceof DOMNodeList ) {
117 return '#nodelist';
118 } else {
119 return $this->node->nodeName;
120 }
121 }
122
123 /**
124 * Split a "<part>" node into an associative array containing:
125 * - name PPNode name
126 * - index String index
127 * - value PPNode value
128 *
129 * @throws MWException
130 * @return array
131 */
132 public function splitArg() {
133 $xpath = $this->getXPath();
134 $names = $xpath->query( 'name', $this->node );
135 $values = $xpath->query( 'value', $this->node );
136 if ( !$names->length || !$values->length ) {
137 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
138 }
139 $name = $names->item( 0 );
140 $index = $name->getAttribute( 'index' );
141 return [
142 'name' => new self( $name ),
143 'index' => $index,
144 'value' => new self( $values->item( 0 ) ) ];
145 }
146
147 /**
148 * Split an "<ext>" node into an associative array containing name, attr, inner and close
149 * All values in the resulting array are PPNodes. Inner and close are optional.
150 *
151 * @throws MWException
152 * @return array
153 */
154 public function splitExt() {
155 $xpath = $this->getXPath();
156 $names = $xpath->query( 'name', $this->node );
157 $attrs = $xpath->query( 'attr', $this->node );
158 $inners = $xpath->query( 'inner', $this->node );
159 $closes = $xpath->query( 'close', $this->node );
160 if ( !$names->length || !$attrs->length ) {
161 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
162 }
163 $parts = [
164 'name' => new self( $names->item( 0 ) ),
165 'attr' => new self( $attrs->item( 0 ) ) ];
166 if ( $inners->length ) {
167 $parts['inner'] = new self( $inners->item( 0 ) );
168 }
169 if ( $closes->length ) {
170 $parts['close'] = new self( $closes->item( 0 ) );
171 }
172 return $parts;
173 }
174
175 /**
176 * Split a "<h>" node
177 * @throws MWException
178 * @return array
179 */
180 public function splitHeading() {
181 if ( $this->getName() !== 'h' ) {
182 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
183 }
184 return [
185 'i' => $this->node->getAttribute( 'i' ),
186 'level' => $this->node->getAttribute( 'level' ),
187 'contents' => $this->getChildren()
188 ];
189 }
190 }