Merge "rdbms: allow callers to hint that native insertSelect() is safe"
[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 $circularRefGuard;
34 protected $recursionLevel = 0;
35
36 const UNSTRIP_RECURSION_LIMIT = 20;
37
38 /**
39 * @param string|null $prefix
40 * @since 1.26 The prefix argument should be omitted, as the strip marker
41 * prefix string is now a constant.
42 */
43 public function __construct( $prefix = null ) {
44 if ( $prefix !== null ) {
45 wfDeprecated( __METHOD__ . ' with called with $prefix argument' .
46 ' (call with no arguments instead)', '1.26' );
47 }
48 $this->data = [
49 'nowiki' => [],
50 'general' => []
51 ];
52 $this->regex = '/' . Parser::MARKER_PREFIX . "([^\x7f<>&'\"]+)" . Parser::MARKER_SUFFIX . '/';
53 $this->circularRefGuard = [];
54 }
55
56 /**
57 * Add a nowiki strip item
58 * @param string $marker
59 * @param string $value
60 */
61 public function addNoWiki( $marker, $value ) {
62 $this->addItem( 'nowiki', $marker, $value );
63 }
64
65 /**
66 * @param string $marker
67 * @param string $value
68 */
69 public function addGeneral( $marker, $value ) {
70 $this->addItem( 'general', $marker, $value );
71 }
72
73 /**
74 * @throws MWException
75 * @param string $type
76 * @param string $marker
77 * @param string $value
78 */
79 protected function addItem( $type, $marker, $value ) {
80 if ( !preg_match( $this->regex, $marker, $m ) ) {
81 throw new MWException( "Invalid marker: $marker" );
82 }
83
84 $this->data[$type][$m[1]] = $value;
85 }
86
87 /**
88 * @param string $text
89 * @return mixed
90 */
91 public function unstripGeneral( $text ) {
92 return $this->unstripType( 'general', $text );
93 }
94
95 /**
96 * @param string $text
97 * @return mixed
98 */
99 public function unstripNoWiki( $text ) {
100 return $this->unstripType( 'nowiki', $text );
101 }
102
103 /**
104 * @param string $text
105 * @return mixed
106 */
107 public function unstripBoth( $text ) {
108 $text = $this->unstripType( 'general', $text );
109 $text = $this->unstripType( 'nowiki', $text );
110 return $text;
111 }
112
113 /**
114 * @param string $type
115 * @param string $text
116 * @return mixed
117 */
118 protected function unstripType( $type, $text ) {
119 // Shortcut
120 if ( !count( $this->data[$type] ) ) {
121 return $text;
122 }
123
124 $callback = function ( $m ) use ( $type ) {
125 $marker = $m[1];
126 if ( isset( $this->data[$type][$marker] ) ) {
127 if ( isset( $this->circularRefGuard[$marker] ) ) {
128 return '<span class="error">'
129 . wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text()
130 . '</span>';
131 }
132 if ( $this->recursionLevel >= self::UNSTRIP_RECURSION_LIMIT ) {
133 return '<span class="error">' .
134 wfMessage( 'parser-unstrip-recursion-limit' )
135 ->numParams( self::UNSTRIP_RECURSION_LIMIT )->inContentLanguage()->text() .
136 '</span>';
137 }
138 $this->circularRefGuard[$marker] = true;
139 $this->recursionLevel++;
140 $value = $this->data[$type][$marker];
141 if ( $value instanceof Closure ) {
142 $value = $value();
143 }
144 $ret = $this->unstripType( $type, $value );
145 $this->recursionLevel--;
146 unset( $this->circularRefGuard[$marker] );
147 return $ret;
148 } else {
149 return $m[0];
150 }
151 };
152
153 $text = preg_replace_callback( $this->regex, $callback, $text );
154 return $text;
155 }
156
157 /**
158 * Get a StripState object which is sufficient to unstrip the given text.
159 * It will contain the minimum subset of strip items necessary.
160 *
161 * @param string $text
162 *
163 * @return StripState
164 */
165 public function getSubState( $text ) {
166 $subState = new StripState();
167 $pos = 0;
168 while ( true ) {
169 $startPos = strpos( $text, Parser::MARKER_PREFIX, $pos );
170 $endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
171 if ( $startPos === false || $endPos === false ) {
172 break;
173 }
174
175 $endPos += strlen( Parser::MARKER_SUFFIX );
176 $marker = substr( $text, $startPos, $endPos - $startPos );
177 if ( !preg_match( $this->regex, $marker, $m ) ) {
178 continue;
179 }
180
181 $key = $m[1];
182 if ( isset( $this->data['nowiki'][$key] ) ) {
183 $subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
184 } elseif ( isset( $this->data['general'][$key] ) ) {
185 $subState->data['general'][$key] = $this->data['general'][$key];
186 }
187 $pos = $endPos;
188 }
189 return $subState;
190 }
191
192 /**
193 * Merge another StripState object into this one. The strip marker keys
194 * will not be preserved. The strings in the $texts array will have their
195 * strip markers rewritten, the resulting array of strings will be returned.
196 *
197 * @param StripState $otherState
198 * @param array $texts
199 * @return array
200 */
201 public function merge( $otherState, $texts ) {
202 $mergePrefix = wfRandomString( 16 );
203
204 foreach ( $otherState->data as $type => $items ) {
205 foreach ( $items as $key => $value ) {
206 $this->data[$type]["$mergePrefix-$key"] = $value;
207 }
208 }
209
210 $callback = function ( $m ) use ( $mergePrefix ) {
211 $key = $m[1];
212 return Parser::MARKER_PREFIX . $mergePrefix . '-' . $key . Parser::MARKER_SUFFIX;
213 };
214 $texts = preg_replace_callback( $otherState->regex, $callback, $texts );
215 return $texts;
216 }
217
218 /**
219 * Remove any strip markers found in the given text.
220 *
221 * @param string $text
222 * @return string
223 */
224 public function killMarkers( $text ) {
225 return preg_replace( $this->regex, '', $text );
226 }
227 }