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