1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
<?php
/**
* @todo document, briefly.
* @ingroup Parser
*/
class StripState {
protected $prefix;
protected $data;
protected $regex;
protected $tempType, $tempMergePrefix;
function __construct( $prefix ) {
$this->prefix = $prefix;
$this->data = array(
'nowiki' => array(),
'general' => array()
);
$this->regex = "/{$this->prefix}([^\x7f]+)" . Parser::MARKER_SUFFIX . '/';
}
/**
* Add a nowiki strip item
* @param $marker
* @param $value
*/
function addNoWiki( $marker, $value ) {
$this->addItem( 'nowiki', $marker, $value );
}
/**
* @param $marker
* @param $value
*/
function addGeneral( $marker, $value ) {
$this->addItem( 'general', $marker, $value );
}
/**
* @throws MWException
* @param $type
* @param $marker
* @param $value
*/
protected function addItem( $type, $marker, $value ) {
if ( !preg_match( $this->regex, $marker, $m ) ) {
throw new MWException( "Invalid marker: $marker" );
}
$this->data[$type][$m[1]] = $value;
}
/**
* @param $text
* @return mixed
*/
function unstripGeneral( $text ) {
return $this->unstripType( 'general', $text );
}
/**
* @param $text
* @return mixed
*/
function unstripNoWiki( $text ) {
return $this->unstripType( 'nowiki', $text );
}
/**
* @param $text
* @return mixed
*/
function unstripBoth( $text ) {
$text = $this->unstripType( 'general', $text );
$text = $this->unstripType( 'nowiki', $text );
return $text;
}
/**
* @param $type
* @param $text
* @return mixed
*/
protected function unstripType( $type, $text ) {
// Shortcut
if ( !count( $this->data[$type] ) ) {
return $text;
}
wfProfileIn( __METHOD__ );
$this->tempType = $type;
$out = preg_replace_callback( $this->regex, array( $this, 'unstripCallback' ), $text );
$this->tempType = null;
wfProfileOut( __METHOD__ );
return $out;
}
/**
* @param $m array
* @return array
*/
protected function unstripCallback( $m ) {
if ( isset( $this->data[$this->tempType][$m[1]] ) ) {
return $this->data[$this->tempType][$m[1]];
} else {
return $m[0];
}
}
/**
* Get a StripState object which is sufficient to unstrip the given text.
* It will contain the minimum subset of strip items necessary.
*
* @param $text string
*
* @return StripState
*/
function getSubState( $text ) {
$subState = new StripState( $this->prefix );
$pos = 0;
while ( true ) {
$startPos = strpos( $text, $this->prefix, $pos );
$endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
if ( $startPos === false || $endPos === false ) {
break;
}
$endPos += strlen( Parser::MARKER_SUFFIX );
$marker = substr( $text, $startPos, $endPos - $startPos );
if ( !preg_match( $this->regex, $marker, $m ) ) {
continue;
}
$key = $m[1];
if ( isset( $this->data['nowiki'][$key] ) ) {
$subState->data['nowiki'][$key] = $this->data['nowiki'][$key];
} elseif ( isset( $this->data['general'][$key] ) ) {
$subState->data['general'][$key] = $this->data['general'][$key];
}
$pos = $endPos;
}
return $subState;
}
/**
* Merge another StripState object into this one. The strip marker keys
* will not be preserved. The strings in the $texts array will have their
* strip markers rewritten, the resulting array of strings will be returned.
*
* @param $otherState StripState
* @param $texts Array
* @return Array
*/
function merge( $otherState, $texts ) {
$mergePrefix = Parser::getRandomString();
foreach ( $otherState->data as $type => $items ) {
foreach ( $items as $key => $value ) {
$this->data[$type]["$mergePrefix-$key"] = $value;
}
}
$this->tempMergePrefix = $mergePrefix;
$texts = preg_replace_callback( $otherState->regex, array( $this, 'mergeCallback' ), $texts );
$this->tempMergePrefix = null;
return $texts;
}
protected function mergeCallback( $m ) {
$key = $m[1];
return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;
}
}
|