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 $tempType, $tempMergePrefix;
34 protected $circularRefGuard;
35 protected $recursionLevel = 0;
37 const UNSTRIP_RECURSION_LIMIT
= 20;
40 * @param string|null $prefix
41 * @since 1.26 The prefix argument should be omitted, as the strip marker
42 * prefix string is now a constant.
44 public function __construct( $prefix = null ) {
45 if ( $prefix !== null ) {
46 wfDeprecated( __METHOD__
. ' with called with $prefix argument' .
47 ' (call with no arguments instead)', '1.26' );
53 $this->regex
= '/' . Parser
::MARKER_PREFIX
. "([^\x7f]+)" . Parser
::MARKER_SUFFIX
. '/';
54 $this->circularRefGuard
= array();
58 * Add a nowiki strip item
59 * @param string $marker
60 * @param string $value
62 public function addNoWiki( $marker, $value ) {
63 $this->addItem( 'nowiki', $marker, $value );
67 * @param string $marker
68 * @param string $value
70 public function addGeneral( $marker, $value ) {
71 $this->addItem( 'general', $marker, $value );
77 * @param string $marker
78 * @param string $value
80 protected function addItem( $type, $marker, $value ) {
81 if ( !preg_match( $this->regex
, $marker, $m ) ) {
82 throw new MWException( "Invalid marker: $marker" );
85 $this->data
[$type][$m[1]] = $value;
92 public function unstripGeneral( $text ) {
93 return $this->unstripType( 'general', $text );
100 public function unstripNoWiki( $text ) {
101 return $this->unstripType( 'nowiki', $text );
105 * @param string $text
108 public function unstripBoth( $text ) {
109 $text = $this->unstripType( 'general', $text );
110 $text = $this->unstripType( 'nowiki', $text );
115 * @param string $type
116 * @param string $text
119 protected function unstripType( $type, $text ) {
121 if ( !count( $this->data
[$type] ) ) {
125 $oldType = $this->tempType
;
126 $this->tempType
= $type;
127 $text = preg_replace_callback( $this->regex
, array( $this, 'unstripCallback' ), $text );
128 $this->tempType
= $oldType;
136 protected function unstripCallback( $m ) {
138 if ( isset( $this->data
[$this->tempType
][$marker] ) ) {
139 if ( isset( $this->circularRefGuard
[$marker] ) ) {
140 return '<span class="error">'
141 . wfMessage( 'parser-unstrip-loop-warning' )->inContentLanguage()->text()
144 if ( $this->recursionLevel
>= self
::UNSTRIP_RECURSION_LIMIT
) {
145 return '<span class="error">' .
146 wfMessage( 'parser-unstrip-recursion-limit' )
147 ->numParams( self
::UNSTRIP_RECURSION_LIMIT
)->inContentLanguage()->text() .
150 $this->circularRefGuard
[$marker] = true;
151 $this->recursionLevel++
;
152 $value = $this->data
[$this->tempType
][$marker];
153 if ( $value instanceof Closure
) {
156 $ret = $this->unstripType( $this->tempType
, $value );
157 $this->recursionLevel
--;
158 unset( $this->circularRefGuard
[$marker] );
166 * Get a StripState object which is sufficient to unstrip the given text.
167 * It will contain the minimum subset of strip items necessary.
169 * @param string $text
173 public function getSubState( $text ) {
174 $subState = new StripState();
177 $startPos = strpos( $text, Parser
::MARKER_PREFIX
, $pos );
178 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
179 if ( $startPos === false ||
$endPos === false ) {
183 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
184 $marker = substr( $text, $startPos, $endPos - $startPos );
185 if ( !preg_match( $this->regex
, $marker, $m ) ) {
190 if ( isset( $this->data
['nowiki'][$key] ) ) {
191 $subState->data
['nowiki'][$key] = $this->data
['nowiki'][$key];
192 } elseif ( isset( $this->data
['general'][$key] ) ) {
193 $subState->data
['general'][$key] = $this->data
['general'][$key];
201 * Merge another StripState object into this one. The strip marker keys
202 * will not be preserved. The strings in the $texts array will have their
203 * strip markers rewritten, the resulting array of strings will be returned.
205 * @param StripState $otherState
206 * @param array $texts
209 public function merge( $otherState, $texts ) {
210 $mergePrefix = wfRandomString( 16 );
212 foreach ( $otherState->data
as $type => $items ) {
213 foreach ( $items as $key => $value ) {
214 $this->data
[$type]["$mergePrefix-$key"] = $value;
218 $this->tempMergePrefix
= $mergePrefix;
219 $texts = preg_replace_callback( $otherState->regex
, array( $this, 'mergeCallback' ), $texts );
220 $this->tempMergePrefix
= null;
228 protected function mergeCallback( $m ) {
230 return Parser
::MARKER_PREFIX
. $this->tempMergePrefix
. '-' . $key . Parser
::MARKER_SUFFIX
;
234 * Remove any strip markers found in the given text.
236 * @param string $text Input string
239 public function killMarkers( $text ) {
240 return preg_replace( $this->regex
, '', $text );