Added per page support for inhibiting editsection links via the __NOEDITSECTION__...
[mediawiki.git] / includes / MagicWord.php
blobdc98d9db08edf25402474df7e0518bad0c568213
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 /*static*/ function &get( $id )
32 global $wgMagicWords;
34 if (!array_key_exists( $id, $wgMagicWords ) ) {
35 $mw = new MagicWord();
36 $mw->load( $id );
37 $wgMagicWords[$id] = $mw;
39 return $wgMagicWords[$id];
42 function load( $id )
44 global $wgLang;
46 $this->mId = $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" );
61 function getRegex()
63 if ($this->mRegex == "" ) {
64 $this->initRegex();
66 return $this->mRegex;
69 function getRegexStart()
71 if ($this->mRegex == "" ) {
72 $this->initRegex();
74 return $this->mRegexStart;
77 function getBaseRegex()
79 if ($this->mRegex == "") {
80 $this->initRegex();
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 )
96 global $wgMagicFound;
97 $wgMagicFound = false;
98 $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
99 return $wgMagicFound;
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 == "" ) {
115 $this->initRegex();
117 return $this->mVariableRegex;
120 function getSynonym( $i ) {
121 return $this->mSynonyms[$i];
125 /*private*/ function pregRemoveAndRecord( $match )
127 global $wgMagicFound;
128 $wgMagicFound = true;
129 return "";