Fix function level comments that start with /* not /**
[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 */
26 function addNoWiki( $marker, $value ) {
27 $this->addItem( 'nowiki', $marker, $value );
28 }
29
30 function addGeneral( $marker, $value ) {
31 $this->addItem( 'general', $marker, $value );
32 }
33
34 protected function addItem( $type, $marker, $value ) {
35 if ( !preg_match( $this->regex, $marker, $m ) ) {
36 throw new MWException( "Invalid marker: $marker" );
37 }
38
39 $this->data[$type][$m[1]] = $value;
40 }
41
42 function unstripGeneral( $text ) {
43 return $this->unstripType( 'general', $text );
44 }
45
46 function unstripNoWiki( $text ) {
47 return $this->unstripType( 'nowiki', $text );
48 }
49
50 function unstripBoth( $text ) {
51 $text = $this->unstripType( 'general', $text );
52 $text = $this->unstripType( 'nowiki', $text );
53 return $text;
54 }
55
56 protected function unstripType( $type, $text ) {
57 // Shortcut
58 if ( !count( $this->data[$type] ) ) {
59 return $text;
60 }
61
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__ );
67 return $out;
68 }
69
70 protected function unstripCallback( $m ) {
71 if ( isset( $this->data[$this->tempType][$m[1]] ) ) {
72 return $this->data[$this->tempType][$m[1]];
73 } else {
74 return $m[0];
75 }
76 }
77
78 /**
79 * Get a StripState object which is sufficient to unstrip the given text.
80 * It will contain the minimum subset of strip items necessary.
81 */
82 function getSubState( $text ) {
83 $subState = new StripState( $this->prefix );
84 $pos = 0;
85 while ( true ) {
86 $startPos = strpos( $text, $this->prefix, $pos );
87 $endPos = strpos( $text, Parser::MARKER_SUFFIX, $pos );
88 if ( $startPos === false || $endPos === false ) {
89 break;
90 }
91
92 $endPos += strlen( Parser::MARKER_SUFFIX );
93 $marker = substr( $text, $startPos, $endPos - $startPos );
94 if ( !preg_match( $this->regex, $marker, $m ) ) {
95 continue;
96 }
97
98 $key = $m[1];
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];
103 }
104 $pos = $endPos;
105 }
106 return $subState;
107 }
108
109 /**
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.
113 *
114 * @param $otherState StripState
115 * @param $texts Array
116 * @return Array
117 */
118 function merge( $otherState, $texts ) {
119 $mergePrefix = Parser::getRandomString();
120
121 foreach ( $otherState->data as $type => $items ) {
122 foreach ( $items as $key => $value ) {
123 $this->data[$type]["$mergePrefix-$key"] = $value;
124 }
125 }
126
127 $this->tempMergePrefix = $mergePrefix;
128 $texts = preg_replace_callback( $otherState->regex, array( $this, 'mergeCallback' ), $texts );
129 $this->tempMergePrefix = null;
130 return $texts;
131 }
132
133 protected function mergeCallback( $m ) {
134 $key = $m[1];
135 return "{$this->prefix}{$this->tempMergePrefix}-$key" . Parser::MARKER_SUFFIX;
136 }
137 }
138