4 * @todo document, briefly.
12 protected $tempType, $tempMergePrefix;
14 function __construct( $prefix ) {
15 $this->prefix
= $prefix;
20 $this->regex
= "/{$this->prefix}([^\x7f]+)" . Parser
::MARKER_SUFFIX
. '/';
24 * Add a nowiki strip item
26 function addNoWiki( $marker, $value ) {
27 $this->addItem( 'nowiki', $marker, $value );
30 function addGeneral( $marker, $value ) {
31 $this->addItem( 'general', $marker, $value );
34 protected function addItem( $type, $marker, $value ) {
35 if ( !preg_match( $this->regex
, $marker, $m ) ) {
36 throw new MWException( "Invalid marker: $marker" );
39 $this->data
[$type][$m[1]] = $value;
42 function unstripGeneral( $text ) {
43 return $this->unstripType( 'general', $text );
46 function unstripNoWiki( $text ) {
47 return $this->unstripType( 'nowiki', $text );
50 function unstripBoth( $text ) {
51 $text = $this->unstripType( 'general', $text );
52 $text = $this->unstripType( 'nowiki', $text );
56 protected function unstripType( $type, $text ) {
58 if ( !count( $this->data
[$type] ) ) {
62 wfProfileIn( __METHOD__
);
63 $this->tempType
= $type;
64 $out = preg_replace_callback( $this->regex
, array( $this, 'unstripCallback' ), $text );
65 $this->tempType
= null;
66 wfProfileOut( __METHOD__
);
70 protected function unstripCallback( $m ) {
71 if ( isset( $this->data
[$this->tempType
][$m[1]] ) ) {
72 return $this->data
[$this->tempType
][$m[1]];
79 * Get a StripState object which is sufficient to unstrip the given text.
80 * It will contain the minimum subset of strip items necessary.
82 function getSubState( $text ) {
83 $subState = new StripState( $this->prefix
);
86 $startPos = strpos( $text, $this->prefix
, $pos );
87 $endPos = strpos( $text, Parser
::MARKER_SUFFIX
, $pos );
88 if ( $startPos === false ||
$endPos === false ) {
92 $endPos +
= strlen( Parser
::MARKER_SUFFIX
);
93 $marker = substr( $text, $startPos, $endPos - $startPos );
94 if ( !preg_match( $this->regex
, $marker, $m ) ) {
99 if ( isset( $this->data
['nowiki'][$key] ) ) {
100 $subState->data
['nowiki'][$key] = $this->data
['nowiki'][$key];
101 } elseif ( isset( $this->data
['general'][$key] ) ) {
102 $subState->data
['general'][$key] = $this->data
['general'][$key];
110 * Merge another StripState object into this one. The strip marker keys
111 * will not be preserved. The strings in the $texts array will have their
112 * strip markers rewritten, the resulting array of strings will be returned.
114 * @param $otherState StripState
115 * @param $texts Array
118 function merge( $otherState, $texts ) {
119 $mergePrefix = Parser
::getRandomString();
121 foreach ( $otherState->data
as $type => $items ) {
122 foreach ( $items as $key => $value ) {
123 $this->data
[$type]["$mergePrefix-$key"] = $value;
127 $this->tempMergePrefix
= $mergePrefix;
128 $texts = preg_replace_callback( $otherState->regex
, array( $this, 'mergeCallback' ), $texts );
129 $this->tempMergePrefix
= null;
133 protected function mergeCallback( $m ) {
135 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser
::MARKER_SUFFIX
;