1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Parser
*/
namespace MediaWiki\Parser;
use BadMethodCallException;
use InvalidArgumentException;
use Stringable;
/**
* @ingroup Parser
*/
// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
class PPNode_Hash_Tree implements Stringable, PPNode {
/** @var string */
public $name;
/**
* The store array for children of this node. It is "raw" in the sense that
* nodes are two-element arrays ("descriptors") rather than PPNode_Hash_*
* objects.
* @var array
*/
private $rawChildren;
/**
* The store array for the siblings of this node, including this node itself.
* @var array
*/
private $store;
/**
* The index into $this->store which contains the descriptor of this node.
* @var int
*/
private $index;
/**
* The offset of the name within descriptors, used in some places for
* readability.
*/
public const NAME = 0;
/**
* The offset of the child list within descriptors, used in some places for
* readability.
*/
public const CHILDREN = 1;
/**
* Construct an object using the data from $store[$index]. The rest of the
* store array can be accessed via getNextSibling().
*
* @param array $store
* @param int $index
*/
public function __construct( array $store, $index ) {
$this->store = $store;
$this->index = $index;
[ $this->name, $this->rawChildren ] = $this->store[$index];
}
/**
* Construct an appropriate PPNode_Hash_* object with a class that depends
* on what is at the relevant store index.
*
* @param array $store
* @param int $index
* @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|false
*/
public static function factory( array $store, $index ) {
if ( !isset( $store[$index] ) ) {
return false;
}
$descriptor = $store[$index];
if ( is_string( $descriptor ) ) {
$class = PPNode_Hash_Text::class;
} elseif ( is_array( $descriptor ) ) {
if ( $descriptor[self::NAME][0] === '@' ) {
$class = PPNode_Hash_Attr::class;
} else {
$class = self::class;
}
} else {
throw new InvalidArgumentException( __METHOD__ . ': invalid node descriptor' );
}
return new $class( $store, $index );
}
/**
* Convert a node to XML, for debugging
* @return string
*/
public function __toString() {
$inner = '';
$attribs = '';
for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
if ( $node instanceof PPNode_Hash_Attr ) {
$attribs .= ' ' . $node->name .
'="' . htmlspecialchars( $node->value, ENT_COMPAT ) . '"';
} else {
$inner .= $node->__toString();
}
}
if ( $inner === '' ) {
return "<{$this->name}$attribs/>";
} else {
return "<{$this->name}$attribs>$inner</{$this->name}>";
}
}
/**
* @return PPNode_Hash_Array
*/
public function getChildren() {
$children = [];
foreach ( $this->rawChildren as $i => $child ) {
$children[] = self::factory( $this->rawChildren, $i );
}
return new PPNode_Hash_Array( $children );
}
/**
* Get the first child, or false if there is none. Note that this will
* return a temporary proxy object: different instances will be returned
* if this is called more than once on the same node.
*
* @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|false
*/
public function getFirstChild() {
if ( !isset( $this->rawChildren[0] ) ) {
return false;
} else {
return self::factory( $this->rawChildren, 0 );
}
}
/**
* Get the next sibling, or false if there is none. Note that this will
* return a temporary proxy object: different instances will be returned
* if this is called more than once on the same node.
*
* @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|false
*/
public function getNextSibling() {
return self::factory( $this->store, $this->index + 1 );
}
/**
* Get an array of the children with a given node name
*
* @param string $name
* @return PPNode_Hash_Array
*/
public function getChildrenOfType( $name ) {
$children = [];
foreach ( $this->rawChildren as $i => $child ) {
if ( is_array( $child ) && $child[self::NAME] === $name ) {
$children[] = self::factory( $this->rawChildren, $i );
}
}
return new PPNode_Hash_Array( $children );
}
/**
* Get the raw child array. For internal use.
* @return array
*/
public function getRawChildren() {
return $this->rawChildren;
}
/**
* @return bool
*/
public function getLength() {
return false;
}
/**
* @param int $i
* @return bool
*/
public function item( $i ) {
return false;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Split a "<part>" node into an associative array containing:
* - name PPNode name
* - index String index
* - value PPNode value
*
* @return array
*/
public function splitArg() {
return self::splitRawArg( $this->rawChildren );
}
/**
* Like splitArg() but for a raw child array. For internal use only.
* @param array $children
* @return array
*/
public static function splitRawArg( array $children ) {
$bits = [];
foreach ( $children as $i => $child ) {
if ( !is_array( $child ) ) {
continue;
}
if ( $child[self::NAME] === 'name' ) {
$bits['name'] = new self( $children, $i );
if ( isset( $child[self::CHILDREN][0][self::NAME] )
&& $child[self::CHILDREN][0][self::NAME] === '@index'
) {
$bits['index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
}
} elseif ( $child[self::NAME] === 'value' ) {
$bits['value'] = new self( $children, $i );
}
}
if ( !isset( $bits['name'] ) ) {
throw new InvalidArgumentException( 'Invalid brace node passed to ' . __METHOD__ );
}
if ( !isset( $bits['index'] ) ) {
$bits['index'] = '';
}
return $bits;
}
/**
* Split an "<ext>" node into an associative array containing name, attr, inner and close
* All values in the resulting array are PPNodes. Inner and close are optional.
*
* @return array
*/
public function splitExt() {
return self::splitRawExt( $this->rawChildren );
}
/**
* Like splitExt() but for a raw child array. For internal use only.
* @param array $children
* @return array
*/
public static function splitRawExt( array $children ) {
$bits = [];
foreach ( $children as $i => $child ) {
if ( !is_array( $child ) ) {
continue;
}
switch ( $child[self::NAME] ) {
case 'name':
$bits['name'] = new self( $children, $i );
break;
case 'attr':
$bits['attr'] = new self( $children, $i );
break;
case 'inner':
$bits['inner'] = new self( $children, $i );
break;
case 'close':
$bits['close'] = new self( $children, $i );
break;
}
}
if ( !isset( $bits['name'] ) ) {
throw new InvalidArgumentException( 'Invalid ext node passed to ' . __METHOD__ );
}
return $bits;
}
/**
* Split an "<h>" node
*
* @return array
*/
public function splitHeading() {
if ( $this->name !== 'h' ) {
throw new BadMethodCallException( 'Invalid h node passed to ' . __METHOD__ );
}
return self::splitRawHeading( $this->rawChildren );
}
/**
* Like splitHeading() but for a raw child array. For internal use only.
* @param array $children
* @return array
*/
public static function splitRawHeading( array $children ) {
$bits = [];
foreach ( $children as $child ) {
if ( !is_array( $child ) ) {
continue;
}
if ( $child[self::NAME] === '@i' ) {
$bits['i'] = $child[self::CHILDREN][0];
} elseif ( $child[self::NAME] === '@level' ) {
$bits['level'] = $child[self::CHILDREN][0];
}
}
if ( !isset( $bits['i'] ) ) {
throw new InvalidArgumentException( 'Invalid h node passed to ' . __METHOD__ );
}
return $bits;
}
/**
* Split a "<template>" or "<tplarg>" node
*
* @return array
*/
public function splitTemplate() {
return self::splitRawTemplate( $this->rawChildren );
}
/**
* Like splitTemplate() but for a raw child array. For internal use only.
* @param array $children
* @return array
* @suppress SecurityCheck-XSS
*/
public static function splitRawTemplate( array $children ) {
$parts = [];
$bits = [ 'lineStart' => '' ];
foreach ( $children as $i => $child ) {
if ( !is_array( $child ) ) {
continue;
}
switch ( $child[self::NAME] ) {
case 'title':
$bits['title'] = new self( $children, $i );
break;
case 'part':
$parts[] = new self( $children, $i );
break;
case '@lineStart':
$bits['lineStart'] = '1';
break;
}
}
if ( !isset( $bits['title'] ) ) {
throw new InvalidArgumentException( 'Invalid node passed to ' . __METHOD__ );
}
$bits['parts'] = new PPNode_Hash_Array( $parts );
return $bits;
}
}
/** @deprecated class alias since 1.43 */
class_alias( PPNode_Hash_Tree::class, 'PPNode_Hash_Tree' );
|