3 # This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
5 # if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
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;
17 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
18 /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
20 function MagicWord($id = 0, $syn = "", $cs = false)
23 $this->mSynonyms
= (array)$syn;
24 $this->mCaseSensitive
= $cs;
26 $this->mRegexStart
= "";
27 $this->mVariableRegex
= "";
30 /*static*/ function &get( $id )
34 if (!array_key_exists( $id, $wgMagicWords ) ) {
35 $mw = new MagicWord();
37 $wgMagicWords[$id] = $mw;
39 return $wgMagicWords[$id];
47 $wgLang->getMagic( $this );
50 /* private */ function initRegex()
52 $escSyn = array_map( "preg_quote", $this->mSynonyms
);
53 $this->mBaseRegex
= implode( "|", $escSyn );
54 $case = $this->mCaseSensitive ?
"" : "i";
55 $this->mRegex
= "/{$this->mBaseRegex}/{$case}";
56 $this->mRegexStart
= "/^{$this->mBaseRegex}/{$case}";
57 $this->mVariableRegex
= str_replace( "\\$1", "([A-Za-z0-9]*)", $this->mRegex
);
58 wfDebug( "{$this->mVariableRegex}\n" );
63 if ($this->mRegex
== "" ) {
69 function getRegexStart()
71 if ($this->mRegex
== "" ) {
74 return $this->mRegexStart
;
77 function getBaseRegex()
79 if ($this->mRegex
== "") {
82 return $this->mBaseRegex
;
85 function match( $text ) {
86 return preg_match( $this->getRegex(), $text );
89 function matchStart( $text )
91 return preg_match( $this->getRegexStart(), $text );
94 function matchAndRemove( &$text )
97 $wgMagicFound = false;
98 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
102 function replace( $replacement, $subject )
104 return preg_replace( $this->getRegex(), $replacement, $subject );
107 function substituteCallback( $text, $callback ) {
108 $regex = $this->getVariableRegex();
109 return preg_replace_callback( $this->getVariableRegex(), $callback, $text );
112 function getVariableRegex()
114 if ( $this->mVariableRegex
== "" ) {
117 return $this->mVariableRegex
;
120 function getSynonym( $i ) {
121 return $this->mSynonyms
[$i];
125 /*private*/ function pregRemoveAndRecord( $match )
127 global $wgMagicFound;
128 $wgMagicFound = true;