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