3 * Holder for stripped items when parsing wiki markup.
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.
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.
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
25 * @todo document, briefly.
33 protected $circularRefGuard;
34 protected $recursionLevel = 0;
36 const UNSTRIP_RECURSION_LIMIT
= 20;
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.
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' );
52 $this->regex
= '/' . Parser
::MARKER_PREFIX
. "([^\x7f<>&'\"]+)" . Parser
::MARKER_SUFFIX
. '/';
53 $this->circularRefGuard
= [];
57 * Add a nowiki strip item
58 * @param string $marker
59 * @param string $value
61 public function addNoWiki( $marker, $value ) {
62 $this->addItem( 'nowiki', $marker, $value );
66 * @param string $marker
67 * @param string $value
69 public function addGeneral( $marker, $value ) {
70 $this->addItem( 'general', $marker, $value );
76 * @param string $marker
77 * @param string $value
79 protected function addItem( $type, $marker, $value ) {
80 if ( !preg_match( $this->regex
, $marker, $m ) ) {
81 throw new MWException( "Invalid marker: $marker" );
84 $this->data
[$type][$m[1]] = $value;
91 public function unstripGeneral( $text ) {
92 return $this->unstripType( 'general', $text );
99 public function unstripNoWiki( $text ) {
100 return $this->unstripType( 'nowiki', $text );
104 * @param string $text
107 public function unstripBoth( $text ) {
108 $text = $this->unstripType( 'general', $text );
109 $text = $this->unstripType( 'nowiki', $text );
114 * @param string $type
115 * @param string $text
118 protected function unstripType( $type, $text ) {
120 if ( !count( $this->data
[$type] ) ) {
124 $callback = function ( $m ) use ( $type ) {
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()
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() .
138 $this->circularRefGuard
[$marker] = true;
139 $this->recursionLevel++
;
140 $value = $this->data
[$type][$marker];
141 if ( $value instanceof Closure
) {
144 $ret = $this->unstripType( $type, $value );
145 $this->recursionLevel
--;
146 unset( $this->circularRefGuard
[$marker] );
153 $text = preg_replace_callback( $this->regex
, $callback, $text );
158 * Get a StripState object which is sufficient to unstrip the given text.
159 * It will contain the minimum subset of strip items necessary.
161 * @param string $text
165 public function getSubState( $text ) {
166 $subState = new StripState();
169 $startPos = strpos( $text, Parser
::MARKER_PREFIX
, $pos );
170 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
171 if ( $startPos === false ||
$endPos === false ) {
175 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
176 $marker = substr( $text, $startPos, $endPos - $startPos );
177 if ( !preg_match( $this->regex
, $marker, $m ) ) {
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];
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.
197 * @param StripState $otherState
198 * @param array $texts
201 public function merge( $otherState, $texts ) {
202 $mergePrefix = wfRandomString( 16 );
204 foreach ( $otherState->data
as $type => $items ) {
205 foreach ( $items as $key => $value ) {
206 $this->data
[$type]["$mergePrefix-$key"] = $value;
210 $callback = function ( $m ) use ( $mergePrefix ) {
212 return Parser
::MARKER_PREFIX
. $mergePrefix . '-' . $key . Parser
::MARKER_SUFFIX
;
214 $texts = preg_replace_callback( $otherState->regex
, $callback, $texts );
219 * Remove any strip markers found in the given text.
221 * @param string $text
224 public function killMarkers( $text ) {
225 return preg_replace( $this->regex
, '', $text );