Fallback to google search box when database is offline
[mediawiki.git] / includes / MagicWord.php
blobf4410b2f2e45e45cf56f3488c5e1eae30cd5534d
1 <?
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;
20 function MagicWord($id = 0, $syn = "", $cs = false)
22 $this->mId = $id;
23 $this->mSynonyms = (array)$syn;
24 $this->mCaseSensitive = $cs;
25 $this->mRegex = "";
26 $this->mRegexStart = "";
27 $this->mVariableRegex = "";
30 # Factory: creates an object representing an ID
31 /*static*/ function &get( $id )
33 global $wgMagicWords;
35 if (!array_key_exists( $id, $wgMagicWords ) ) {
36 $mw = new MagicWord();
37 $mw->load( $id );
38 $wgMagicWords[$id] = $mw;
40 return $wgMagicWords[$id];
43 # Initialises this object with an ID
44 function load( $id )
46 global $wgLang;
48 $this->mId = $id;
49 $wgLang->getMagic( $this );
52 # Preliminary initialisation
53 /* private */ function initRegex()
55 $escSyn = array_map( "preg_quote", $this->mSynonyms );
56 $this->mBaseRegex = implode( "|", $escSyn );
57 $case = $this->mCaseSensitive ? "" : "i";
58 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
59 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
60 $this->mVariableRegex = str_replace( "\\$1", "([A-Za-z0-9_\-]*)", $this->mRegex );
63 # Gets a regex representing matching the word
64 function getRegex()
66 if ($this->mRegex == "" ) {
67 $this->initRegex();
69 return $this->mRegex;
72 # Gets a regex matching the word, if it is at the
73 # string start
74 function getRegexStart()
76 if ($this->mRegex == "" ) {
77 $this->initRegex();
79 return $this->mRegexStart;
82 # regex without the slashes and what not
83 function getBaseRegex()
85 if ($this->mRegex == "") {
86 $this->initRegex();
88 return $this->mBaseRegex;
91 # Returns true if the text contains the word
92 function match( $text ) {
93 return preg_match( $this->getRegex(), $text );
96 # Returns true if the text starts with the word
97 function matchStart( $text )
99 return preg_match( $this->getRegexStart(), $text );
102 # Returns true if the text matches the word, and alters the
103 # input string, removing all instances of the word
104 function matchAndRemove( &$text )
106 global $wgMagicFound;
107 $wgMagicFound = false;
108 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
109 return $wgMagicFound;
112 # Replaces the word with something else
113 function replace( $replacement, $subject )
115 return preg_replace( $this->getRegex(), $replacement, $subject );
118 # Variable handling: {{SUBST:xxx}} style words
119 # Calls back a function to determine what to replace xxx with
120 # Input word must contain $1
121 function substituteCallback( $text, $callback ) {
122 $regex = $this->getVariableRegex();
123 return preg_replace_callback( $this->getVariableRegex(), $callback, $text );
126 # Matches the word, where $1 is a wildcard
127 function getVariableRegex()
129 if ( $this->mVariableRegex == "" ) {
130 $this->initRegex();
132 return $this->mVariableRegex;
135 # Accesses the synonym list directly
136 function getSynonym( $i ) {
137 return $this->mSynonyms[$i];
141 # Used in matchAndRemove()
142 /*private*/ function pregRemoveAndRecord( $match )
144 global $wgMagicFound;
145 $wgMagicFound = true;
146 return "";