4 * @todo document, briefly.
12 protected $tempType, $tempMergePrefix;
15 * @param $prefix string
17 function __construct( $prefix ) {
18 $this->prefix
= $prefix;
23 $this->regex
= "/{$this->prefix}([^\x7f]+)" . Parser
::MARKER_SUFFIX
. '/';
27 * Add a nowiki strip item
31 function addNoWiki( $marker, $value ) {
32 $this->addItem( 'nowiki', $marker, $value );
39 function addGeneral( $marker, $value ) {
40 $this->addItem( 'general', $marker, $value );
49 protected function addItem( $type, $marker, $value ) {
50 if ( !preg_match( $this->regex
, $marker, $m ) ) {
51 throw new MWException( "Invalid marker: $marker" );
54 $this->data
[$type][$m[1]] = $value;
61 function unstripGeneral( $text ) {
62 return $this->unstripType( 'general', $text );
69 function unstripNoWiki( $text ) {
70 return $this->unstripType( 'nowiki', $text );
77 function unstripBoth( $text ) {
78 $text = $this->unstripType( 'general', $text );
79 $text = $this->unstripType( 'nowiki', $text );
88 protected function unstripType( $type, $text ) {
90 if ( !count( $this->data
[$type] ) ) {
94 wfProfileIn( __METHOD__
);
95 $this->tempType
= $type;
98 $text = preg_replace_callback( $this->regex
, array( $this, 'unstripCallback' ), $text );
99 } while ( $text !== $oldText );
100 $this->tempType
= null;
101 wfProfileOut( __METHOD__
);
109 protected function unstripCallback( $m ) {
110 if ( isset( $this->data
[$this->tempType
][$m[1]] ) ) {
111 return $this->data
[$this->tempType
][$m[1]];
118 * Get a StripState object which is sufficient to unstrip the given text.
119 * It will contain the minimum subset of strip items necessary.
121 * @param $text string
125 function getSubState( $text ) {
126 $subState = new StripState( $this->prefix
);
129 $startPos = strpos( $text, $this->prefix
, $pos );
130 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
131 if ( $startPos === false ||
$endPos === false ) {
135 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
136 $marker = substr( $text, $startPos, $endPos - $startPos );
137 if ( !preg_match( $this->regex
, $marker, $m ) ) {
142 if ( isset( $this->data
['nowiki'][$key] ) ) {
143 $subState->data
['nowiki'][$key] = $this->data
['nowiki'][$key];
144 } elseif ( isset( $this->data
['general'][$key] ) ) {
145 $subState->data
['general'][$key] = $this->data
['general'][$key];
153 * Merge another StripState object into this one. The strip marker keys
154 * will not be preserved. The strings in the $texts array will have their
155 * strip markers rewritten, the resulting array of strings will be returned.
157 * @param $otherState StripState
158 * @param $texts Array
161 function merge( $otherState, $texts ) {
162 $mergePrefix = Parser
::getRandomString();
164 foreach ( $otherState->data
as $type => $items ) {
165 foreach ( $items as $key => $value ) {
166 $this->data
[$type]["$mergePrefix-$key"] = $value;
170 $this->tempMergePrefix
= $mergePrefix;
171 $texts = preg_replace_callback( $otherState->regex
, array( $this, 'mergeCallback' ), $texts );
172 $this->tempMergePrefix
= null;
180 protected function mergeCallback( $m ) {
182 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser
::MARKER_SUFFIX
;
186 * Remove any strip markers found in the given text.
188 * @param $text Input string
191 function killMarkers( $text ) {
192 return preg_replace( $this->regex
, '', $text );