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.
34 protected $tempType, $tempMergePrefix;
35 protected $circularRefGuard;
36 protected $recursionLevel = 0;
38 const UNSTRIP_RECURSION_LIMIT
= 20;
43 public function __construct( $id ) {
45 $this->prefix
= Parser
::MARKER_PREFIX
. $id;
50 $this->regex
= "/" . Parser
::MARKER_PREFIX
.
51 '(' . Parser
::MARKER_STATE_ID_REGEX
. ")([^\x7f]+)" . Parser
::MARKER_SUFFIX
. '/';
52 $this->circularRefGuard
= array();
56 * Add a nowiki strip item
57 * @param string $marker
58 * @param string $value
60 public function addNoWiki( $marker, $value ) {
61 $this->addItem( 'nowiki', $marker, $value );
65 * @param string $marker
66 * @param string $value
68 public function addGeneral( $marker, $value ) {
69 $this->addItem( 'general', $marker, $value );
75 * @param string $marker
76 * @param string $value
78 protected function addItem( $type, $marker, $value ) {
79 if ( !preg_match( $this->regex
, $marker, $m ) ||
$m[1] !== $this->id
) {
80 throw new MWException( "Invalid marker: $marker" );
83 $this->data
[$type][$m[2]] = $value;
90 public function unstripGeneral( $text ) {
91 return $this->unstripType( 'general', $text );
98 public function unstripNoWiki( $text ) {
99 return $this->unstripType( 'nowiki', $text );
103 * @param string $text
106 public function unstripBoth( $text ) {
107 $text = $this->unstripType( 'general', $text );
108 $text = $this->unstripType( 'nowiki', $text );
113 * @param string $type
114 * @param string $text
117 protected function unstripType( $type, $text ) {
119 if ( !count( $this->data
[$type] ) ) {
123 wfProfileIn( __METHOD__
);
124 $oldType = $this->tempType
;
125 $this->tempType
= $type;
126 $text = preg_replace_callback( $this->regex
, array( $this, 'unstripCallback' ), $text );
127 $this->tempType
= $oldType;
128 wfProfileOut( __METHOD__
);
136 protected function unstripCallback( $m ) {
138 if ( $m[1] === $this->id
&& 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 $ret = $this->unstripType( $this->tempType
, $this->data
[$this->tempType
][$marker] );
153 $this->recursionLevel
--;
154 unset( $this->circularRefGuard
[$marker] );
162 * Get a StripState object which is sufficient to unstrip the given text.
163 * It will contain the minimum subset of strip items necessary.
165 * @param string $text
169 public function getSubState( $text ) {
170 $subState = new StripState( $this->id
);
173 $startPos = strpos( $text, $this->prefix
, $pos );
174 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
175 if ( $startPos === false ||
$endPos === false ) {
179 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
180 $marker = substr( $text, $startPos, $endPos - $startPos );
181 if ( !preg_match( $this->regex
, $marker, $m ) ||
$m[1] !== $this->id
) {
186 if ( isset( $this->data
['nowiki'][$key] ) ) {
187 $subState->data
['nowiki'][$key] = $this->data
['nowiki'][$key];
188 } elseif ( isset( $this->data
['general'][$key] ) ) {
189 $subState->data
['general'][$key] = $this->data
['general'][$key];
197 * Merge another StripState object into this one. The strip marker keys
198 * will not be preserved. The strings in the $texts array will have their
199 * strip markers rewritten, the resulting array of strings will be returned.
201 * @param StripState $otherState
202 * @param array $texts
205 public function merge( $otherState, $texts ) {
206 $mergePrefix = Parser
::getRandomString();
208 foreach ( $otherState->data
as $type => $items ) {
209 foreach ( $items as $key => $value ) {
210 $this->data
[$type]["$mergePrefix-$key"] = $value;
214 $this->tempMergePrefix
= $mergePrefix;
215 $texts = preg_replace_callback( $otherState->regex
, array( $this, 'mergeCallback' ), $texts );
216 $this->tempMergePrefix
= null;
224 protected function mergeCallback( $m ) {
225 if ( $m[1] === $this->id
) {
227 return "{$this->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 $id = $this->id
; // PHP 5.3 hack
241 return preg_replace_callback( $this->regex
,
242 function ( $m ) use ( $id ) {
243 if ( $m[1] === $id ) {