MediaWiki  1.34.0
PPNode_DOM.php
Go to the documentation of this file.
1 <?php
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 class PPNode_DOM implements PPNode {
29 
33  public $node;
34  public $xpath;
35 
36  public function __construct( $node, $xpath = false ) {
37  $this->node = $node;
38  }
39 
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 
65  public function getChildren() {
66  return $this->node->childNodes ? new self( $this->node->childNodes ) : false;
67  }
68 
72  public function getFirstChild() {
73  return $this->node->firstChild ? new self( $this->node->firstChild ) : false;
74  }
75 
79  public function getNextSibling() {
80  return $this->node->nextSibling ? new self( $this->node->nextSibling ) : false;
81  }
82 
88  public function getChildrenOfType( $type ) {
89  return new self( $this->getXPath()->query( $type, $this->node ) );
90  }
91 
95  public function getLength() {
96  if ( $this->node instanceof DOMNodeList ) {
97  return $this->node->length;
98  } else {
99  return false;
100  }
101  }
102 
107  public function item( $i ) {
108  $item = $this->node->item( $i );
109  return $item ? new self( $item ) : false;
110  }
111 
115  public function getName() {
116  if ( $this->node instanceof DOMNodeList ) {
117  return '#nodelist';
118  } else {
119  return $this->node->nodeName;
120  }
121  }
122 
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 
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 
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 }
PPNode_DOM
Definition: PPNode_DOM.php:28
PPNode_DOM\item
item( $i)
Definition: PPNode_DOM.php:107
PPNode_DOM\$xpath
$xpath
Definition: PPNode_DOM.php:34
PPNode_DOM\$node
DOMElement DOMNodeList $node
Definition: PPNode_DOM.php:33
$s
$s
Definition: mergeMessageFileList.php:185
PPNode_DOM\splitHeading
splitHeading()
Split a "<h>" node.
Definition: PPNode_DOM.php:180
PPNode_DOM\getXPath
getXPath()
Definition: PPNode_DOM.php:43
PPNode_DOM\__construct
__construct( $node, $xpath=false)
Definition: PPNode_DOM.php:36
PPNode_DOM\getName
getName()
Definition: PPNode_DOM.php:115
MWException
MediaWiki exception.
Definition: MWException.php:26
PPNode_DOM\getLength
getLength()
Definition: PPNode_DOM.php:95
PPNode
There are three types of nodes:
Definition: PPNode.php:35
PPNode_DOM\getChildren
getChildren()
Definition: PPNode_DOM.php:65
PPNode_DOM\getChildrenOfType
getChildrenOfType( $type)
Definition: PPNode_DOM.php:88
PPNode_DOM\__toString
__toString()
Definition: PPNode_DOM.php:50
PPNode_DOM\splitArg
splitArg()
Split a "<part>" node into an associative array containing:
Definition: PPNode_DOM.php:132
PPNode_DOM\getFirstChild
getFirstChild()
Definition: PPNode_DOM.php:72
PPNode_DOM\splitExt
splitExt()
Split an "<ext>" node into an associative array containing name, attr, inner and close All values in ...
Definition: PPNode_DOM.php:154
PPNode_DOM\getNextSibling
getNextSibling()
Definition: PPNode_DOM.php:79
$type
$type
Definition: testCompression.php:48