Merge "mediawiki.api: Remove console warning for legacy token type"
[mediawiki.git] / includes / parser / PPDStackElement_Hash.php
blob06d48b95be393f146e0c81b775d2bd5b0855dd9c
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.
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.
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
18 * @file
19 * @ingroup Parser
22 namespace MediaWiki\Parser;
24 /**
25 * @ingroup Parser
27 * @property PPDPart_Hash[] $parts
28 * @property int $startPos
30 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
31 class PPDStackElement_Hash {
32 /**
33 * @var string Opening character (\n for heading)
35 public $open;
37 /**
38 * @var string Matching closing character
40 public $close;
42 /**
43 * @var string Saved prefix that may affect later processing,
44 * e.g. to differentiate `-{{{{` and `{{{{` after later seeing `}}}`.
46 public $savedPrefix = '';
48 /**
49 * @var int Start offset of this element in the source wikitext
51 public $startPos;
53 /**
54 * @var int Number of opening characters found (number of "=" for heading)
56 public $count;
58 /**
59 * @var PPDPart_Hash[] Array of PPDPart objects describing pipe-separated parts.
61 public $parts;
63 /**
64 * @var bool True if the open char appeared at the start of the input line.
65 * Not set for headings.
67 public $lineStart;
69 /** @var string */
70 public $partClass = PPDPart_Hash::class;
72 public function __construct( $data = [] ) {
73 $class = $this->partClass;
74 $this->parts = [ new $class ];
76 foreach ( $data as $name => $value ) {
77 $this->$name = $value;
81 public function &getAccum() {
82 return $this->parts[count( $this->parts ) - 1]->out;
85 public function addPart( $s = '' ) {
86 $class = $this->partClass;
87 $this->parts[] = new $class( $s );
90 /**
91 * @return PPDPart_Hash
93 public function getCurrentPart() {
94 return $this->parts[count( $this->parts ) - 1];
97 /**
98 * @return array
100 public function getFlags() {
101 $partCount = count( $this->parts );
102 $findPipe = $this->open != "\n" && $this->open != '[';
103 return [
104 'findPipe' => $findPipe,
105 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ),
106 'inHeading' => $this->open == "\n",
111 * Get the accumulator that would result if the close is not found.
113 * @param int|false $openingCount
114 * @return array
116 public function breakSyntax( $openingCount = false ) {
117 if ( $this->open == "\n" ) {
118 $accum = array_merge( [ $this->savedPrefix ], $this->parts[0]->out );
119 } else {
120 if ( $openingCount === false ) {
121 $openingCount = $this->count;
123 $s = substr( $this->open, 0, -1 );
124 $s .= str_repeat(
125 substr( $this->open, -1 ),
126 $openingCount - strlen( $s )
128 $accum = [ $this->savedPrefix . $s ];
129 $lastIndex = 0;
130 $first = true;
131 foreach ( $this->parts as $part ) {
132 if ( $first ) {
133 $first = false;
134 } elseif ( is_string( $accum[$lastIndex] ) ) {
135 $accum[$lastIndex] .= '|';
136 } else {
137 $accum[++$lastIndex] = '|';
140 foreach ( $part->out as $node ) {
141 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
142 $accum[$lastIndex] .= $node;
143 } else {
144 $accum[++$lastIndex] = $node;
149 return $accum;
153 /** @deprecated class alias since 1.43 */
154 class_alias( PPDStackElement_Hash::class, 'PPDStackElement_Hash' );