Cleaned up OutputPage::replaceVariables() a bit. Some performance gain by doing sever...
[lhc/web/wiklou.git] / includes / MagicWord.php
1 <?
2
3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
4 # Usage:
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
6 #
7 # Possible future improvements:
8 # * Simultaneous searching for a number of magic words
9 # * $wgMagicWords in shared memory
10 #
11 # Please avoid reading the data out of one of these objects and then writing
12 # special case code. If possible, add another match()-like function here.
13
14 /*private*/ $wgMagicFound = false;
15
16 class MagicWord {
17 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
18 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
19 /*private*/ var $mModified;
20
21 function MagicWord($id = 0, $syn = "", $cs = false)
22 {
23 $this->mId = $id;
24 $this->mSynonyms = (array)$syn;
25 $this->mCaseSensitive = $cs;
26 $this->mRegex = "";
27 $this->mRegexStart = "";
28 $this->mVariableRegex = "";
29 $this->mModified = false;
30 }
31
32 # Factory: creates an object representing an ID
33 /*static*/ function &get( $id )
34 {
35 global $wgMagicWords;
36
37 if (!array_key_exists( $id, $wgMagicWords ) ) {
38 $mw = new MagicWord();
39 $mw->load( $id );
40 $wgMagicWords[$id] = $mw;
41 }
42 return $wgMagicWords[$id];
43 }
44
45 # Initialises this object with an ID
46 function load( $id )
47 {
48 global $wgLang;
49 $this->mId = $id;
50 $wgLang->getMagic( $this );
51 }
52
53 # Preliminary initialisation
54 /* private */ function initRegex()
55 {
56 $escSyn = array_map( "preg_quote", $this->mSynonyms );
57 $this->mBaseRegex = implode( "|", $escSyn );
58 $case = $this->mCaseSensitive ? "" : "i";
59 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
60 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
61 $this->mVariableRegex = str_replace( "\\$1", "([A-Za-z0-9_\-]*)", $this->mRegex );
62 }
63
64 # Gets a regex representing matching the word
65 function getRegex()
66 {
67 if ($this->mRegex == "" ) {
68 $this->initRegex();
69 }
70 return $this->mRegex;
71 }
72
73 # Gets a regex matching the word, if it is at the
74 # string start
75 function getRegexStart()
76 {
77 if ($this->mRegex == "" ) {
78 $this->initRegex();
79 }
80 return $this->mRegexStart;
81 }
82
83 # regex without the slashes and what not
84 function getBaseRegex()
85 {
86 if ($this->mRegex == "") {
87 $this->initRegex();
88 }
89 return $this->mBaseRegex;
90 }
91
92 # Returns true if the text contains the word
93 function match( $text ) {
94 return preg_match( $this->getRegex(), $text );
95 }
96
97 # Returns true if the text starts with the word
98 function matchStart( $text )
99 {
100 return preg_match( $this->getRegexStart(), $text );
101 }
102
103 # Returns true if the text matches the word, and alters the
104 # input string, removing all instances of the word
105 function matchAndRemove( &$text )
106 {
107 global $wgMagicFound;
108 $wgMagicFound = false;
109 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
110 return $wgMagicFound;
111 }
112
113 # Replaces the word with something else
114 function replace( $replacement, $subject )
115 {
116 $res = preg_replace( $this->getRegex(), $replacement, $subject );
117 $this->mModified = !($res === $subject);
118 return $res;
119 }
120
121 # Variable handling: {{SUBST:xxx}} style words
122 # Calls back a function to determine what to replace xxx with
123 # Input word must contain $1
124 function substituteCallback( $text, $callback ) {
125 $regex = $this->getVariableRegex();
126 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
127 $this->mModified = !($res === $text);
128 return $res;
129 }
130
131 # Matches the word, where $1 is a wildcard
132 function getVariableRegex()
133 {
134 if ( $this->mVariableRegex == "" ) {
135 $this->initRegex();
136 }
137 return $this->mVariableRegex;
138 }
139
140 # Accesses the synonym list directly
141 function getSynonym( $i ) {
142 return $this->mSynonyms[$i];
143 }
144
145 # Returns true if the last call to replace() or substituteCallback()
146 # returned a modified text, otherwise false.
147 function getWasModified(){
148 return $this->mModified;
149 }
150
151 # $magicarr is an associative array of (magic word ID => replacement)
152 # This method uses the php feature to do several replacements at the same time,
153 # thereby gaining some efficiency. The result is placed in the out variable
154 # $result. The return value is true if something was replaced.
155
156 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
157 $search = array();
158 $replace = array();
159 foreach( $magicarr as $id => $replacement ){
160 $mw = MagicWord::get( $id );
161 $search[] = $mw->getRegex();
162 $replace[] = $replacement;
163 }
164
165 $result = preg_replace( $search, $replace, $subject );
166 return !($result === $subject);
167 }
168 }
169
170 # Used in matchAndRemove()
171 /*private*/ function pregRemoveAndRecord( $match )
172 {
173 global $wgMagicFound;
174 $wgMagicFound = true;
175 return "";
176 }
177
178 ?>