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;
36 * @param $prefix string
38 function __construct( $prefix ) {
39 $this->prefix
= $prefix;
44 $this->regex
= "/{$this->prefix}([^\x7f]+)" . Parser
::MARKER_SUFFIX
. '/';
48 * Add a nowiki strip item
52 function addNoWiki( $marker, $value ) {
53 $this->addItem( 'nowiki', $marker, $value );
60 function addGeneral( $marker, $value ) {
61 $this->addItem( 'general', $marker, $value );
70 protected function addItem( $type, $marker, $value ) {
71 if ( !preg_match( $this->regex
, $marker, $m ) ) {
72 throw new MWException( "Invalid marker: $marker" );
75 $this->data
[$type][$m[1]] = $value;
82 function unstripGeneral( $text ) {
83 return $this->unstripType( 'general', $text );
90 function unstripNoWiki( $text ) {
91 return $this->unstripType( 'nowiki', $text );
98 function unstripBoth( $text ) {
99 $text = $this->unstripType( 'general', $text );
100 $text = $this->unstripType( 'nowiki', $text );
109 protected function unstripType( $type, $text ) {
111 if ( !count( $this->data
[$type] ) ) {
115 wfProfileIn( __METHOD__
);
116 $this->tempType
= $type;
119 $text = preg_replace_callback( $this->regex
, array( $this, 'unstripCallback' ), $text );
120 } while ( $text !== $oldText );
121 $this->tempType
= null;
122 wfProfileOut( __METHOD__
);
130 protected function unstripCallback( $m ) {
131 if ( isset( $this->data
[$this->tempType
][$m[1]] ) ) {
132 return $this->data
[$this->tempType
][$m[1]];
139 * Get a StripState object which is sufficient to unstrip the given text.
140 * It will contain the minimum subset of strip items necessary.
142 * @param $text string
146 function getSubState( $text ) {
147 $subState = new StripState( $this->prefix
);
150 $startPos = strpos( $text, $this->prefix
, $pos );
151 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
152 if ( $startPos === false ||
$endPos === false ) {
156 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
157 $marker = substr( $text, $startPos, $endPos - $startPos );
158 if ( !preg_match( $this->regex
, $marker, $m ) ) {
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];
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.
178 * @param $otherState StripState
179 * @param $texts Array
182 function merge( $otherState, $texts ) {
183 $mergePrefix = Parser
::getRandomString();
185 foreach ( $otherState->data
as $type => $items ) {
186 foreach ( $items as $key => $value ) {
187 $this->data
[$type]["$mergePrefix-$key"] = $value;
191 $this->tempMergePrefix
= $mergePrefix;
192 $texts = preg_replace_callback( $otherState->regex
, array( $this, 'mergeCallback' ), $texts );
193 $this->tempMergePrefix
= null;
201 protected function mergeCallback( $m ) {
203 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser
::MARKER_SUFFIX
;
207 * Remove any strip markers found in the given text.
209 * @param $text Input string
212 function killMarkers( $text ) {
213 return preg_replace( $this->regex
, '', $text );