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 $prefix
42 public function __construct( $prefix ) {
43 $this->prefix
= $prefix;
48 $this->regex
= "/{$this->prefix}([^\x7f]+)" . Parser
::MARKER_SUFFIX
. '/';
49 $this->circularRefGuard
= array();
53 * Add a nowiki strip item
54 * @param string $marker
55 * @param string $value
57 public function addNoWiki( $marker, $value ) {
58 $this->addItem( 'nowiki', $marker, $value );
62 * @param string $marker
63 * @param string $value
65 public function addGeneral( $marker, $value ) {
66 $this->addItem( 'general', $marker, $value );
72 * @param string $marker
73 * @param string $value
75 protected function addItem( $type, $marker, $value ) {
76 if ( !preg_match( $this->regex
, $marker, $m ) ) {
77 throw new MWException( "Invalid marker: $marker" );
80 $this->data
[$type][$m[1]] = $value;
87 public function unstripGeneral( $text ) {
88 return $this->unstripType( 'general', $text );
95 public function unstripNoWiki( $text ) {
96 return $this->unstripType( 'nowiki', $text );
100 * @param string $text
103 public function unstripBoth( $text ) {
104 $text = $this->unstripType( 'general', $text );
105 $text = $this->unstripType( 'nowiki', $text );
110 * @param string $type
111 * @param string $text
114 protected function unstripType( $type, $text ) {
116 if ( !count( $this->data
[$type] ) ) {
120 $oldType = $this->tempType
;
121 $this->tempType
= $type;
122 $text = preg_replace_callback( $this->regex
, array( $this, 'unstripCallback' ), $text );
123 $this->tempType
= $oldType;
131 protected function unstripCallback( $m ) {
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()
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() .
145 $this->circularRefGuard
[$marker] = true;
146 $this->recursionLevel++
;
147 $ret = $this->unstripType( $this->tempType
, $this->data
[$this->tempType
][$marker] );
148 $this->recursionLevel
--;
149 unset( $this->circularRefGuard
[$marker] );
157 * Get a StripState object which is sufficient to unstrip the given text.
158 * It will contain the minimum subset of strip items necessary.
160 * @param string $text
164 public function getSubState( $text ) {
165 $subState = new StripState( $this->prefix
);
168 $startPos = strpos( $text, $this->prefix
, $pos );
169 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
170 if ( $startPos === false ||
$endPos === false ) {
174 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
175 $marker = substr( $text, $startPos, $endPos - $startPos );
176 if ( !preg_match( $this->regex
, $marker, $m ) ) {
181 if ( isset( $this->data
['nowiki'][$key] ) ) {
182 $subState->data
['nowiki'][$key] = $this->data
['nowiki'][$key];
183 } elseif ( isset( $this->data
['general'][$key] ) ) {
184 $subState->data
['general'][$key] = $this->data
['general'][$key];
192 * Merge another StripState object into this one. The strip marker keys
193 * will not be preserved. The strings in the $texts array will have their
194 * strip markers rewritten, the resulting array of strings will be returned.
196 * @param StripState $otherState
197 * @param array $texts
200 public function merge( $otherState, $texts ) {
201 $mergePrefix = Parser
::getRandomString();
203 foreach ( $otherState->data
as $type => $items ) {
204 foreach ( $items as $key => $value ) {
205 $this->data
[$type]["$mergePrefix-$key"] = $value;
209 $this->tempMergePrefix
= $mergePrefix;
210 $texts = preg_replace_callback( $otherState->regex
, array( $this, 'mergeCallback' ), $texts );
211 $this->tempMergePrefix
= null;
219 protected function mergeCallback( $m ) {
221 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser
::MARKER_SUFFIX
;
225 * Remove any strip markers found in the given text.
227 * @param string $text Input string
230 public function killMarkers( $text ) {
231 return preg_replace( $this->regex
, '', $text );