Remove ancient, unused testsuite
[mediawiki.git] / includes / MagicWord.php
blobb5ee3e9eb9b0802e90f315e270259872f227f159
1 <?php
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
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.
14 /*private*/ $wgMagicFound = false;
16 class MagicWord {
17 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
18 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
19 /*private*/ var $mModified;
21 function MagicWord($id = 0, $syn = "", $cs = false)
23 $this->mId = $id;
24 $this->mSynonyms = (array)$syn;
25 $this->mCaseSensitive = $cs;
26 $this->mRegex = "";
27 $this->mRegexStart = "";
28 $this->mVariableRegex = "";
29 $this->mVariableStartToEndRegex = "";
30 $this->mModified = false;
33 # Factory: creates an object representing an ID
34 /*static*/ function &get( $id )
36 global $wgMagicWords;
38 if (!array_key_exists( $id, $wgMagicWords ) ) {
39 $mw = new MagicWord();
40 $mw->load( $id );
41 $wgMagicWords[$id] = $mw;
43 return $wgMagicWords[$id];
46 # Initialises this object with an ID
47 function load( $id )
49 global $wgLang;
50 $this->mId = $id;
51 $wgLang->getMagic( $this );
54 # Preliminary initialisation
55 /* private */ function initRegex()
57 $variableClass = "A-Za-z0-9_\-\x80-\xff";
58 $escSyn = array_map( "preg_quote", $this->mSynonyms );
59 $this->mBaseRegex = implode( "|", $escSyn );
60 $case = $this->mCaseSensitive ? "" : "i";
61 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
62 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
63 $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*)", $this->mRegex );
64 $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*)",
65 "/^{$this->mBaseRegex}$/{$case}" );
68 # Gets a regex representing matching the word
69 function getRegex()
71 if ($this->mRegex == "" ) {
72 $this->initRegex();
74 return $this->mRegex;
77 # Gets a regex matching the word, if it is at the
78 # string start
79 function getRegexStart()
81 if ($this->mRegex == "" ) {
82 $this->initRegex();
84 return $this->mRegexStart;
87 # regex without the slashes and what not
88 function getBaseRegex()
90 if ($this->mRegex == "") {
91 $this->initRegex();
93 return $this->mBaseRegex;
96 # Returns true if the text contains the word
97 function match( $text ) {
98 return preg_match( $this->getRegex(), $text );
101 # Returns true if the text starts with the word
102 function matchStart( $text )
104 return preg_match( $this->getRegexStart(), $text );
107 # Returns NULL if there's no match, the value of $1 otherwise
108 # The return code is the matched string, if there's no variable
109 # part in the regex and the matched variable part ($1) if there
110 # is one.
111 function matchVariableStartToEnd( $text ) {
112 $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
113 if ( $matchcount == 0 ) {
114 return NULL;
115 } elseif ( count($matches) == 1 ) {
116 return $matches[0];
117 } else {
118 return $matches[1];
123 # Returns true if the text matches the word, and alters the
124 # input string, removing all instances of the word
125 function matchAndRemove( &$text )
127 global $wgMagicFound;
128 $wgMagicFound = false;
129 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
130 return $wgMagicFound;
133 # Replaces the word with something else
134 function replace( $replacement, $subject )
136 $res = preg_replace( $this->getRegex(), $replacement, $subject );
137 $this->mModified = !($res === $subject);
138 return $res;
141 # Variable handling: {{SUBST:xxx}} style words
142 # Calls back a function to determine what to replace xxx with
143 # Input word must contain $1
144 function substituteCallback( $text, $callback ) {
145 $regex = $this->getVariableRegex();
146 $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
147 $this->mModified = !($res === $text);
148 return $res;
151 # Matches the word, where $1 is a wildcard
152 function getVariableRegex()
154 if ( $this->mVariableRegex == "" ) {
155 $this->initRegex();
157 return $this->mVariableRegex;
160 # Matches the entire string, where $1 is a wildcard
161 function getVariableStartToEndRegex()
163 if ( $this->mVariableStartToEndRegex == "" ) {
164 $this->initRegex();
166 return $this->mVariableStartToEndRegex;
169 # Accesses the synonym list directly
170 function getSynonym( $i ) {
171 return $this->mSynonyms[$i];
174 # Returns true if the last call to replace() or substituteCallback()
175 # returned a modified text, otherwise false.
176 function getWasModified(){
177 return $this->mModified;
180 # $magicarr is an associative array of (magic word ID => replacement)
181 # This method uses the php feature to do several replacements at the same time,
182 # thereby gaining some efficiency. The result is placed in the out variable
183 # $result. The return value is true if something was replaced.
185 /* static */ function replaceMultiple( $magicarr, $subject, &$result ){
186 $search = array();
187 $replace = array();
188 foreach( $magicarr as $id => $replacement ){
189 $mw = MagicWord::get( $id );
190 $search[] = $mw->getRegex();
191 $replace[] = $replacement;
194 $result = preg_replace( $search, $replace, $subject );
195 return !($result === $subject);
199 # Used in matchAndRemove()
200 /*private*/ function pregRemoveAndRecord( $match )
202 global $wgMagicFound;
203 $wgMagicFound = true;
204 return "";