Merge "Noticed while looking at $wgShowUpdatedMarker related database queries, querie...
[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
35 /**
36 * @param $prefix string
37 */
38 function __construct( $prefix ) {
39 $this->prefix = $prefix;
40 $this->data = array(
41 'nowiki' => array(),
42 'general' => array()
43 );
44 $this->regex = "/{$this->prefix}([^\x7f]+)" . Parser::MARKER_SUFFIX . '/';
45 }
46
47 /**
48 * Add a nowiki strip item
49 * @param $marker
50 * @param $value
51 */
52 function addNoWiki( $marker, $value ) {
53 $this->addItem( 'nowiki', $marker, $value );
54 }
55
56 /**
57 * @param $marker
58 * @param $value
59 */
60 function addGeneral( $marker, $value ) {
61 $this->addItem( 'general', $marker, $value );
62 }
63
64 /**
65 * @throws MWException
66 * @param $type
67 * @param $marker
68 * @param $value
69 */
70 protected function addItem( $type, $marker, $value ) {
71 if ( !preg_match( $this->regex, $marker, $m ) ) {
72 throw new MWException( "Invalid marker: $marker" );
73 }
74
75 $this->data[$type][$m[1]] = $value;
76 }
77
78 /**
79 * @param $text
80 * @return mixed
81 */
82 function unstripGeneral( $text ) {
83 return $this->unstripType( 'general', $text );
84 }
85
86 /**
87 * @param $text
88 * @return mixed
89 */
90 function unstripNoWiki( $text ) {
91 return $this->unstripType( 'nowiki', $text );
92 }
93
94 /**
95 * @param $text
96 * @return mixed
97 */
98 function unstripBoth( $text ) {
99 $text = $this->unstripType( 'general', $text );
100 $text = $this->unstripType( 'nowiki', $text );
101 return $text;
102 }
103
104 /**
105 * @param $type
106 * @param $text
107 * @return mixed
108 */
109 protected function unstripType( $type, $text ) {
110 // Shortcut
111 if ( !count( $this->data[$type] ) ) {
112 return $text;
113 }
114
115 wfProfileIn( __METHOD__ );
116 $this->tempType = $type;
117 do {
118 $oldText = $text;
119 $text = preg_replace_callback( $this->regex, array( $this, 'unstripCallback' ), $text );
120 } while ( $text !== $oldText );
121 $this->tempType = null;
122 wfProfileOut( __METHOD__ );
123 return $text;
124 }
125
126 /**
127 * @param $m array
128 * @return array
129 */
130 protected function unstripCallback( $m ) {
131 if ( isset( $this->data[$this->tempType][$m[1]] ) ) {
132 return $this->data[$this->tempType][$m[1]];
133 } else {
134 return $m[0];
135 }
136 }
137
138 /**
139 * Get a StripState object which is sufficient to unstrip the given text.
140 * It will contain the minimum subset of strip items necessary.
141 *
142 * @param $text string
143 *
144 * @return StripState
145 */
146 function getSubState( $text ) {
147 $subState = new StripState( $this->prefix );
148 $pos = 0;
149 while ( true ) {
150 $startPos = strpos( $text, $this->prefix, $pos );
151 $endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
152 if ( $startPos === false || $endPos === false ) {
153 break;
154 }
155
156 $endPos += strlen( Parser::MARKER_SUFFIX );
157 $marker = substr( $text, $startPos, $endPos - $startPos );
158 if ( !preg_match( $this->regex, $marker, $m ) ) {
159 continue;
160 }
161
162 $key = $m[1];
163 if ( isset( $this->data['nowiki'][$key] ) ) {
164 $subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
165 } elseif ( isset( $this->data['general'][$key] ) ) {
166 $subState->data['general'][$key] = $this->data['general'][$key];
167 }
168 $pos = $endPos;
169 }
170 return $subState;
171 }
172
173 /**
174 * Merge another StripState object into this one. The strip marker keys
175 * will not be preserved. The strings in the $texts array will have their
176 * strip markers rewritten, the resulting array of strings will be returned.
177 *
178 * @param $otherState StripState
179 * @param $texts Array
180 * @return Array
181 */
182 function merge( $otherState, $texts ) {
183 $mergePrefix = Parser::getRandomString();
184
185 foreach ( $otherState->data as $type => $items ) {
186 foreach ( $items as $key => $value ) {
187 $this->data[$type]["$mergePrefix-$key"] = $value;
188 }
189 }
190
191 $this->tempMergePrefix = $mergePrefix;
192 $texts = preg_replace_callback( $otherState->regex, array( $this, 'mergeCallback' ), $texts );
193 $this->tempMergePrefix = null;
194 return $texts;
195 }
196
197 /**
198 * @param $m
199 * @return string
200 */
201 protected function mergeCallback( $m ) {
202 $key = $m[1];
203 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;
204 }
205
206 /**
207 * Remove any strip markers found in the given text.
208 *
209 * @param $text Input string
210 * @return string
211 */
212 function killMarkers( $text ) {
213 return preg_replace( $this->regex, '', $text );
214 }
215 }
216