7e38acc70c1aedfa42e5f86b5be8352c557770ce
[lhc/web/wiklou.git] / includes / parser / StripState.php
1 <?php
2 /**
3 * Holder for stripped items when parsing wiki markup.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * @todo document, briefly.
26 * @ingroup Parser
27 */
28 class StripState {
29 protected $prefix;
30 protected $data;
31 protected $regex;
32
33 protected $tempType, $tempMergePrefix;
34 protected $circularRefGuard;
35 protected $recursionLevel = 0;
36
37 const UNSTRIP_RECURSION_LIMIT = 20;
38
39 /**
40 * @param string $prefix
41 */
42 public function __construct( $prefix ) {
43 $this->prefix = $prefix;
44 $this->data = array(
45 'nowiki' => array(),
46 'general' => array()
47 );
48 $this->regex = "/{$this->prefix}([^\x7f]+)" . Parser::MARKER_SUFFIX . '/';
49 $this->circularRefGuard = array();
50 }
51
52 /**
53 * Add a nowiki strip item
54 * @param string $marker
55 * @param string $value
56 */
57 public function addNoWiki( $marker, $value ) {
58 $this->addItem( 'nowiki', $marker, $value );
59 }
60
61 /**
62 * @param string $marker
63 * @param string $value
64 */
65 public function addGeneral( $marker, $value ) {
66 $this->addItem( 'general', $marker, $value );
67 }
68
69 /**
70 * @throws MWException
71 * @param string $type
72 * @param string $marker
73 * @param string $value
74 */
75 protected function addItem( $type, $marker, $value ) {
76 if ( !preg_match( $this->regex, $marker, $m ) ) {
77 throw new MWException( "Invalid marker: $marker" );
78 }
79
80 $this->data[$type][$m[1]] = $value;
81 }
82
83 /**
84 * @param string $text
85 * @return mixed
86 */
87 public function unstripGeneral( $text ) {
88 return $this->unstripType( 'general', $text );
89 }
90
91 /**
92 * @param string $text
93 * @return mixed
94 */
95 public function unstripNoWiki( $text ) {
96 return $this->unstripType( 'nowiki', $text );
97 }
98
99 /**
100 * @param string $text
101 * @return mixed
102 */
103 public function unstripBoth( $text ) {
104 $text = $this->unstripType( 'general', $text );
105 $text = $this->unstripType( 'nowiki', $text );
106 return $text;
107 }
108
109 /**
110 * @param string $type
111 * @param string $text
112 * @return mixed
113 */
114 protected function unstripType( $type, $text ) {
115 // Shortcut
116 if ( !count( $this->data[$type] ) ) {
117 return $text;
118 }
119
120 $oldType = $this->tempType;
121 $this->tempType = $type;
122 $text = preg_replace_callback( $this->regex, array( $this, 'unstripCallback' ), $text );
123 $this->tempType = $oldType;
124 return $text;
125 }
126
127 /**
128 * @param array $m
129 * @return array
130 */
131 protected function unstripCallback( $m ) {
132 $marker = $m[1];
133 if ( isset( $this->data[$this->tempType][$marker] ) ) {
134 if ( isset( $this->circularRefGuard[$marker] ) ) {
135 return '<span class="error">'
136 . wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text()
137 . '</span>';
138 }
139 if ( $this->recursionLevel >= self::UNSTRIP_RECURSION_LIMIT ) {
140 return '<span class="error">' .
141 wfMessage( 'parser-unstrip-recursion-limit' )
142 ->numParams( self::UNSTRIP_RECURSION_LIMIT )->inContentLanguage()->text() .
143 '</span>';
144 }
145 $this->circularRefGuard[$marker] = true;
146 $this->recursionLevel++;
147 $value = $this->data[$this->tempType][$marker];
148 if ( $value instanceof Closure ) {
149 $value = $value();
150 }
151 $ret = $this->unstripType( $this->tempType, $value );
152 $this->recursionLevel--;
153 unset( $this->circularRefGuard[$marker] );
154 return $ret;
155 } else {
156 return $m[0];
157 }
158 }
159
160 /**
161 * Get a StripState object which is sufficient to unstrip the given text.
162 * It will contain the minimum subset of strip items necessary.
163 *
164 * @param string $text
165 *
166 * @return StripState
167 */
168 public function getSubState( $text ) {
169 $subState = new StripState( $this->prefix );
170 $pos = 0;
171 while ( true ) {
172 $startPos = strpos( $text, $this->prefix, $pos );
173 $endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
174 if ( $startPos === false || $endPos === false ) {
175 break;
176 }
177
178 $endPos += strlen( Parser::MARKER_SUFFIX );
179 $marker = substr( $text, $startPos, $endPos - $startPos );
180 if ( !preg_match( $this->regex, $marker, $m ) ) {
181 continue;
182 }
183
184 $key = $m[1];
185 if ( isset( $this->data['nowiki'][$key] ) ) {
186 $subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
187 } elseif ( isset( $this->data['general'][$key] ) ) {
188 $subState->data['general'][$key] = $this->data['general'][$key];
189 }
190 $pos = $endPos;
191 }
192 return $subState;
193 }
194
195 /**
196 * Merge another StripState object into this one. The strip marker keys
197 * will not be preserved. The strings in the $texts array will have their
198 * strip markers rewritten, the resulting array of strings will be returned.
199 *
200 * @param StripState $otherState
201 * @param array $texts
202 * @return array
203 */
204 public function merge( $otherState, $texts ) {
205 $mergePrefix = Parser::getRandomString();
206
207 foreach ( $otherState->data as $type => $items ) {
208 foreach ( $items as $key => $value ) {
209 $this->data[$type]["$mergePrefix-$key"] = $value;
210 }
211 }
212
213 $this->tempMergePrefix = $mergePrefix;
214 $texts = preg_replace_callback( $otherState->regex, array( $this, 'mergeCallback' ), $texts );
215 $this->tempMergePrefix = null;
216 return $texts;
217 }
218
219 /**
220 * @param array $m
221 * @return string
222 */
223 protected function mergeCallback( $m ) {
224 $key = $m[1];
225 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;
226 }
227
228 /**
229 * Remove any strip markers found in the given text.
230 *
231 * @param string $text Input string
232 * @return string
233 */
234 public function killMarkers( $text ) {
235 return preg_replace( $this->regex, '', $text );
236 }
237 }