Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / parser / PPDStack.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 * Stack class to help Preprocessor::preprocessToObj()
24 * @ingroup Parser
25 */
26 class PPDStack {
27 public $stack, $rootAccum;
28
29 /**
30 * @var PPDStack|false
31 */
32 public $top;
33 public $out;
34 public $elementClass = PPDStackElement::class;
35
36 public static $false = false;
37
38 public function __construct() {
39 $this->stack = [];
40 $this->top = false;
41 $this->rootAccum = '';
42 $this->accum =& $this->rootAccum;
43 }
44
45 /**
46 * @return int
47 */
48 public function count() {
49 return count( $this->stack );
50 }
51
52 public function &getAccum() {
53 return $this->accum;
54 }
55
56 /**
57 * @return bool|PPDPart
58 */
59 public function getCurrentPart() {
60 if ( $this->top === false ) {
61 return false;
62 } else {
63 return $this->top->getCurrentPart();
64 }
65 }
66
67 public function push( $data ) {
68 if ( $data instanceof $this->elementClass ) {
69 $this->stack[] = $data;
70 } else {
71 $class = $this->elementClass;
72 $this->stack[] = new $class( $data );
73 }
74 $this->top = $this->stack[count( $this->stack ) - 1];
75 $this->accum =& $this->top->getAccum();
76 }
77
78 public function pop() {
79 if ( $this->stack === [] ) {
80 throw new MWException( __METHOD__ . ': no elements remaining' );
81 }
82 $temp = array_pop( $this->stack );
83
84 if ( count( $this->stack ) ) {
85 $this->top = $this->stack[count( $this->stack ) - 1];
86 $this->accum =& $this->top->getAccum();
87 } else {
88 $this->top = self::$false;
89 $this->accum =& $this->rootAccum;
90 }
91 return $temp;
92 }
93
94 public function addPart( $s = '' ) {
95 $this->top->addPart( $s );
96 $this->accum =& $this->top->getAccum();
97 }
98
99 /**
100 * @return array
101 */
102 public function getFlags() {
103 if ( $this->stack === [] ) {
104 return [
105 'findEquals' => false,
106 'findPipe' => false,
107 'inHeading' => false,
108 ];
109 } else {
110 return $this->top->getFlags();
111 }
112 }
113 }