X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FMagicWord.php;h=767331da6f25e7f742570a7571d8c7c115d02e6c;hb=facfb655f76ec06a8a8bf571d9c43fb57650e1c8;hp=dc98d9db08edf25402474df7e0518bad0c568213;hpb=a2bc5d8338c0a546307cb3f8fc2e99b86ecf60cd;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/MagicWord.php b/includes/MagicWord.php index dc98d9db08..767331da6f 100644 --- a/includes/MagicWord.php +++ b/includes/MagicWord.php @@ -1,4 +1,4 @@ -mId = $id; $this->mSynonyms = (array)$syn; $this->mCaseSensitive = $cs; - $this->mRegex = ""; - $this->mRegexStart = ""; - $this->mVariableRegex = ""; + $this->mRegex = ''; + $this->mRegexStart = ''; + $this->mVariableRegex = ''; + $this->mVariableStartToEndRegex = ''; + $this->mModified = false; } - /*static*/ function &get( $id ) - { + # Factory: creates an object representing an ID + /*static*/ function &get( $id ) { global $wgMagicWords; + if ( !is_array( $wgMagicWords ) ) { + wfDebugDieBacktrace( "Incorrect initialisation order, \$wgMagicWords does not exist\n" ); + } if (!array_key_exists( $id, $wgMagicWords ) ) { $mw = new MagicWord(); $mw->load( $id ); @@ -39,94 +95,169 @@ class MagicWord { return $wgMagicWords[$id]; } - function load( $id ) - { - global $wgLang; - + # Initialises this object with an ID + function load( $id ) { + global $wgLang; $this->mId = $id; $wgLang->getMagic( $this ); } - /* private */ function initRegex() - { + # Preliminary initialisation + /* private */ function initRegex() { + $variableClass = Title::legalChars(); $escSyn = array_map( "preg_quote", $this->mSynonyms ); $this->mBaseRegex = implode( "|", $escSyn ); $case = $this->mCaseSensitive ? "" : "i"; $this->mRegex = "/{$this->mBaseRegex}/{$case}"; $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}"; - $this->mVariableRegex = str_replace( "\\$1", "([A-Za-z0-9]*)", $this->mRegex ); - wfDebug( "{$this->mVariableRegex}\n" ); + $this->mVariableRegex = str_replace( "\\$1", "([$variableClass]*?)", $this->mRegex ); + $this->mVariableStartToEndRegex = str_replace( "\\$1", "([$variableClass]*?)", + "/^({$this->mBaseRegex})$/{$case}" ); } - function getRegex() - { - if ($this->mRegex == "" ) { + # Gets a regex representing matching the word + function getRegex() { + if ($this->mRegex == '' ) { $this->initRegex(); } return $this->mRegex; } - function getRegexStart() - { - if ($this->mRegex == "" ) { + # Gets a regex matching the word, if it is at the + # string start + function getRegexStart() { + if ($this->mRegex == '' ) { $this->initRegex(); } return $this->mRegexStart; } - - function getBaseRegex() - { - if ($this->mRegex == "") { + + # regex without the slashes and what not + function getBaseRegex() { + if ($this->mRegex == '') { $this->initRegex(); } return $this->mBaseRegex; } + # Returns true if the text contains the word function match( $text ) { return preg_match( $this->getRegex(), $text ); } - function matchStart( $text ) - { + # Returns true if the text starts with the word + function matchStart( $text ) { return preg_match( $this->getRegexStart(), $text ); } - function matchAndRemove( &$text ) - { + # Returns NULL if there's no match, the value of $1 otherwise + # The return code is the matched string, if there's no variable + # part in the regex and the matched variable part ($1) if there + # is one. + function matchVariableStartToEnd( $text ) { + $matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches ); + if ( $matchcount == 0 ) { + return NULL; + } elseif ( count($matches) == 1 ) { + return $matches[0]; + } else { + return $matches[1]; + } + } + + + # Returns true if the text matches the word, and alters the + # input string, removing all instances of the word + function matchAndRemove( &$text ) { global $wgMagicFound; $wgMagicFound = false; - $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text ); + $text = preg_replace_callback( $this->getRegex(), 'pregRemoveAndRecord', $text ); return $wgMagicFound; } - function replace( $replacement, $subject ) - { - return preg_replace( $this->getRegex(), $replacement, $subject ); + function matchStartAndRemove( &$text ) { + global $wgMagicFound; + $wgMagicFound = false; + $text = preg_replace_callback( $this->getRegexStart(), 'pregRemoveAndRecord', $text ); + return $wgMagicFound; + } + + + # Replaces the word with something else + function replace( $replacement, $subject ) { + $res = preg_replace( $this->getRegex(), $replacement, $subject ); + $this->mModified = !($res === $subject); + return $res; } + # Variable handling: {{SUBST:xxx}} style words + # Calls back a function to determine what to replace xxx with + # Input word must contain $1 function substituteCallback( $text, $callback ) { $regex = $this->getVariableRegex(); - return preg_replace_callback( $this->getVariableRegex(), $callback, $text ); + $res = preg_replace_callback( $this->getVariableRegex(), $callback, $text ); + $this->mModified = !($res === $text); + return $res; } - function getVariableRegex() - { - if ( $this->mVariableRegex == "" ) { + # Matches the word, where $1 is a wildcard + function getVariableRegex() { + if ( $this->mVariableRegex == '' ) { $this->initRegex(); } return $this->mVariableRegex; } + # Matches the entire string, where $1 is a wildcard + function getVariableStartToEndRegex() { + if ( $this->mVariableStartToEndRegex == '' ) { + $this->initRegex(); + } + return $this->mVariableStartToEndRegex; + } + + # Accesses the synonym list directly function getSynonym( $i ) { return $this->mSynonyms[$i]; } + + # Returns true if the last call to replace() or substituteCallback() + # returned a modified text, otherwise false. + function getWasModified(){ + return $this->mModified; + } + + # $magicarr is an associative array of (magic word ID => replacement) + # This method uses the php feature to do several replacements at the same time, + # thereby gaining some efficiency. The result is placed in the out variable + # $result. The return value is true if something was replaced. + + /* static */ function replaceMultiple( $magicarr, $subject, &$result ){ + $search = array(); + $replace = array(); + foreach( $magicarr as $id => $replacement ){ + $mw = MagicWord::get( $id ); + $search[] = $mw->getRegex(); + $replace[] = $replacement; + } + + $result = preg_replace( $search, $replace, $subject ); + return !($result === $subject); + } + + # Adds all the synonyms of this MagicWord to an array, to allow quick lookup in a list of magic words + function addToArray( &$array, $value ) { + foreach ( $this->mSynonyms as $syn ) { + $array[$syn] = $value; + } + } } -/*private*/ function pregRemoveAndRecord( $match ) -{ +# Used in matchAndRemove() +/*private*/ function pregRemoveAndRecord( $match ) { global $wgMagicFound; $wgMagicFound = true; - return ""; + return ''; } ?>