2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* File for magic words
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2005-04-28 20:46:11 +00:00
|
|
|
* @subpackage Parser
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-08-31 09:46:37 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
|
|
|
|
|
* Usage:
|
2006-07-14 15:39:23 +00:00
|
|
|
* if (MagicWord::get( 'redirect' )->match( $text ) )
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
|
|
|
|
* Possible future improvements:
|
2004-09-02 23:28:24 +00:00
|
|
|
* * Simultaneous searching for a number of magic words
|
2006-07-14 16:36:35 +00:00
|
|
|
* * MagicWord::$mObjects in shared memory
|
2004-09-02 23:28:24 +00:00
|
|
|
*
|
2006-01-07 13:09:30 +00:00
|
|
|
* Please avoid reading the data out of one of these objects and then writing
|
2004-09-02 23:28:24 +00:00
|
|
|
* special case code. If possible, add another match()-like function here.
|
2004-09-03 23:00:01 +00:00
|
|
|
*
|
2006-07-14 16:36:35 +00:00
|
|
|
* To add magic words in an extension, use the LanguageGetMagic hook. For
|
|
|
|
|
* magic words which are also Parser variables, add a MagicWordwgVariableIDs
|
|
|
|
|
* hook. Use string keys.
|
|
|
|
|
*
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-08-31 09:46:37 +00:00
|
|
|
class MagicWord {
|
2004-09-03 23:00:01 +00:00
|
|
|
/**#@+
|
2006-06-10 18:28:50 +00:00
|
|
|
* @private
|
2004-09-03 23:00:01 +00:00
|
|
|
*/
|
2006-05-11 22:40:38 +00:00
|
|
|
var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
|
|
|
|
|
var $mRegexStart, $mBaseRegex, $mVariableRegex;
|
2006-07-14 16:36:35 +00:00
|
|
|
var $mModified, $mFound;
|
|
|
|
|
|
|
|
|
|
static public $mVariableIDsInitialised = false;
|
|
|
|
|
static public $mVariableIDs = array(
|
|
|
|
|
'currentmonth',
|
|
|
|
|
'currentmonthname',
|
|
|
|
|
'currentmonthnamegen',
|
|
|
|
|
'currentmonthabbrev',
|
|
|
|
|
'currentday',
|
|
|
|
|
'currentday2',
|
|
|
|
|
'currentdayname',
|
|
|
|
|
'currentyear',
|
|
|
|
|
'currenttime',
|
2006-08-14 22:43:50 +00:00
|
|
|
'currenthour',
|
2006-08-23 16:45:49 +00:00
|
|
|
'localmonth',
|
|
|
|
|
'localmonthname',
|
|
|
|
|
'localmonthnamegen',
|
|
|
|
|
'localmonthabbrev',
|
|
|
|
|
'localday',
|
|
|
|
|
'localday2',
|
|
|
|
|
'localdayname',
|
|
|
|
|
'localyear',
|
|
|
|
|
'localtime',
|
|
|
|
|
'localhour',
|
2006-07-14 16:36:35 +00:00
|
|
|
'numberofarticles',
|
|
|
|
|
'numberoffiles',
|
|
|
|
|
'sitename',
|
|
|
|
|
'server',
|
|
|
|
|
'servername',
|
|
|
|
|
'scriptpath',
|
|
|
|
|
'pagename',
|
|
|
|
|
'pagenamee',
|
|
|
|
|
'fullpagename',
|
|
|
|
|
'fullpagenamee',
|
|
|
|
|
'namespace',
|
|
|
|
|
'namespacee',
|
|
|
|
|
'currentweek',
|
|
|
|
|
'currentdow',
|
2006-08-23 16:45:49 +00:00
|
|
|
'localweek',
|
|
|
|
|
'localdow',
|
2006-07-14 16:36:35 +00:00
|
|
|
'revisionid',
|
|
|
|
|
'subpagename',
|
|
|
|
|
'subpagenamee',
|
|
|
|
|
'displaytitle',
|
|
|
|
|
'talkspace',
|
|
|
|
|
'talkspacee',
|
|
|
|
|
'subjectspace',
|
|
|
|
|
'subjectspacee',
|
|
|
|
|
'talkpagename',
|
|
|
|
|
'talkpagenamee',
|
|
|
|
|
'subjectpagename',
|
|
|
|
|
'subjectpagenamee',
|
|
|
|
|
'numberofusers',
|
|
|
|
|
'rawsuffix',
|
|
|
|
|
'newsectionlink',
|
|
|
|
|
'numberofpages',
|
|
|
|
|
'currentversion',
|
|
|
|
|
'basepagename',
|
|
|
|
|
'basepagenamee',
|
|
|
|
|
'urlencode',
|
|
|
|
|
'currenttimestamp',
|
2006-08-23 16:45:49 +00:00
|
|
|
'localtimestamp',
|
2006-07-14 16:36:35 +00:00
|
|
|
'directionmark',
|
|
|
|
|
'language',
|
|
|
|
|
'contentlanguage',
|
|
|
|
|
'pagesinnamespace',
|
|
|
|
|
'numberofadmins',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
static public $mObjects = array();
|
|
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
/**#@-*/
|
2004-01-07 02:51:47 +00:00
|
|
|
|
2004-08-12 13:32:04 +00:00
|
|
|
function MagicWord($id = 0, $syn = '', $cs = false) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->mId = $id;
|
|
|
|
|
$this->mSynonyms = (array)$syn;
|
|
|
|
|
$this->mCaseSensitive = $cs;
|
2004-08-12 13:32:04 +00:00
|
|
|
$this->mRegex = '';
|
|
|
|
|
$this->mRegexStart = '';
|
|
|
|
|
$this->mVariableRegex = '';
|
|
|
|
|
$this->mVariableStartToEndRegex = '';
|
2004-01-07 02:51:47 +00:00
|
|
|
$this->mModified = false;
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Factory: creates an object representing an ID
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
2006-07-10 15:41:30 +00:00
|
|
|
static function &get( $id ) {
|
2006-07-14 16:36:35 +00:00
|
|
|
if (!array_key_exists( $id, self::$mObjects ) ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$mw = new MagicWord();
|
|
|
|
|
$mw->load( $id );
|
2006-07-14 16:36:35 +00:00
|
|
|
self::$mObjects[$id] = $mw;
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
2006-07-14 16:36:35 +00:00
|
|
|
return self::$mObjects[$id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array of parser variable IDs
|
|
|
|
|
*/
|
|
|
|
|
static function getVariableIDs() {
|
|
|
|
|
if ( !self::$mVariableIDsInitialised ) {
|
|
|
|
|
# Deprecated constant definition hook, available for extensions that need it
|
|
|
|
|
$magicWords = array();
|
|
|
|
|
wfRunHooks( 'MagicWordMagicWords', array( &$magicWords ) );
|
|
|
|
|
foreach ( $magicWords as $word ) {
|
|
|
|
|
define( $word, $word );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Get variable IDs
|
|
|
|
|
wfRunHooks( 'MagicWordwgVariableIDs', array( &self::$mVariableIDs ) );
|
|
|
|
|
self::$mVariableIDsInitialised = true;
|
|
|
|
|
}
|
|
|
|
|
return self::$mVariableIDs;
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-12-14 14:25:44 +00:00
|
|
|
# Initialises this object with an ID
|
2004-08-12 13:32:04 +00:00
|
|
|
function load( $id ) {
|
2006-01-07 13:31:29 +00:00
|
|
|
global $wgContLang;
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->mId = $id;
|
2004-09-24 13:14:52 +00:00
|
|
|
$wgContLang->getMagic( $this );
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Preliminary initialisation
|
2006-06-10 18:28:50 +00:00
|
|
|
* @private
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
function initRegex() {
|
2005-05-04 07:49:42 +00:00
|
|
|
#$variableClass = Title::legalChars();
|
|
|
|
|
# This was used for matching "$1" variables, but different uses of the feature will have
|
|
|
|
|
# different restrictions, which should be checked *after* the MagicWord has been matched,
|
|
|
|
|
# not here. - IMSoP
|
2006-01-08 04:25:43 +00:00
|
|
|
|
|
|
|
|
$escSyn = array();
|
|
|
|
|
foreach ( $this->mSynonyms as $synonym )
|
|
|
|
|
// In case a magic word contains /, like that's going to happen;)
|
|
|
|
|
$escSyn[] = preg_quote( $synonym, '/' );
|
2004-08-22 17:24:50 +00:00
|
|
|
$this->mBaseRegex = implode( '|', $escSyn );
|
2006-01-08 04:25:43 +00:00
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
$case = $this->mCaseSensitive ? '' : 'i';
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->mRegex = "/{$this->mBaseRegex}/{$case}";
|
2005-05-04 07:49:42 +00:00
|
|
|
$this->mRegexStart = "/^(?:{$this->mBaseRegex})/{$case}";
|
|
|
|
|
$this->mVariableRegex = str_replace( "\\$1", "(.*?)", $this->mRegex );
|
2006-01-07 13:09:30 +00:00
|
|
|
$this->mVariableStartToEndRegex = str_replace( "\\$1", "(.*?)",
|
2005-05-04 07:49:42 +00:00
|
|
|
"/^(?:{$this->mBaseRegex})$/{$case}" );
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a regex representing matching the word
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function getRegex() {
|
|
|
|
|
if ($this->mRegex == '' ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->initRegex();
|
|
|
|
|
}
|
|
|
|
|
return $this->mRegex;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-08 05:35:03 +00:00
|
|
|
/**
|
|
|
|
|
* Gets the regexp case modifier to use, i.e. i or nothing, to be used if
|
|
|
|
|
* one is using MagicWord::getBaseRegex(), otherwise it'll be included in
|
|
|
|
|
* the complete expression
|
|
|
|
|
*/
|
|
|
|
|
function getRegexCase() {
|
|
|
|
|
if ( $this->mRegex === '' )
|
|
|
|
|
$this->initRegex();
|
|
|
|
|
|
|
|
|
|
return $this->mCaseSensitive ? '' : 'i';
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a regex matching the word, if it is at the string start
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function getRegexStart() {
|
|
|
|
|
if ($this->mRegex == '' ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->initRegex();
|
|
|
|
|
}
|
|
|
|
|
return $this->mRegexStart;
|
|
|
|
|
}
|
2004-01-12 00:55:01 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* regex without the slashes and what not
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function getBaseRegex() {
|
|
|
|
|
if ($this->mRegex == '') {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->initRegex();
|
|
|
|
|
}
|
|
|
|
|
return $this->mBaseRegex;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the text contains the word
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2003-08-31 09:46:37 +00:00
|
|
|
function match( $text ) {
|
|
|
|
|
return preg_match( $this->getRegex(), $text );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the text starts with the word
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function matchStart( $text ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
return preg_match( $this->getRegexStart(), $text );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2004-01-12 00:55:01 +00:00
|
|
|
function matchVariableStartToEnd( $text ) {
|
2005-12-04 21:15:04 +00:00
|
|
|
$matches = array();
|
2004-01-12 00:55:01 +00:00
|
|
|
$matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
|
|
|
|
|
if ( $matchcount == 0 ) {
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2006-07-05 18:25:39 +00:00
|
|
|
# multiple matched parts (variable match); some will be empty because of
|
|
|
|
|
# synonyms. The variable will be the second non-empty one so remove any
|
|
|
|
|
# blank elements and re-sort the indices.
|
2006-07-11 13:42:34 +00:00
|
|
|
# See also bug 6526
|
|
|
|
|
|
2005-05-04 07:49:42 +00:00
|
|
|
$matches = array_values(array_filter($matches));
|
2006-07-11 13:42:34 +00:00
|
|
|
|
|
|
|
|
if ( count($matches) == 1 ) { return $matches[0]; }
|
|
|
|
|
else { return $matches[1]; }
|
2004-01-12 00:55:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the text matches the word, and alters the
|
|
|
|
|
* input string, removing all instances of the word
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function matchAndRemove( &$text ) {
|
2006-07-14 16:36:35 +00:00
|
|
|
$this->mFound = false;
|
|
|
|
|
$text = preg_replace_callback( $this->getRegex(), array( &$this, 'pregRemoveAndRecord' ), $text );
|
|
|
|
|
return $this->mFound;
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
function matchStartAndRemove( &$text ) {
|
2006-07-14 16:36:35 +00:00
|
|
|
$this->mFound = false;
|
|
|
|
|
$text = preg_replace_callback( $this->getRegexStart(), array( &$this, 'pregRemoveAndRecord' ), $text );
|
|
|
|
|
return $this->mFound;
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2006-07-14 16:36:35 +00:00
|
|
|
/**
|
|
|
|
|
* Used in matchAndRemove()
|
|
|
|
|
* @private
|
|
|
|
|
**/
|
|
|
|
|
function pregRemoveAndRecord( $match ) {
|
|
|
|
|
$this->mFound = true;
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Replaces the word with something else
|
|
|
|
|
*/
|
2006-05-23 07:19:01 +00:00
|
|
|
function replace( $replacement, $subject, $limit=-1 ) {
|
|
|
|
|
$res = preg_replace( $this->getRegex(), wfRegexReplacement( $replacement ), $subject, $limit );
|
2004-01-07 02:51:47 +00:00
|
|
|
$this->mModified = !($res === $subject);
|
|
|
|
|
return $res;
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
2003-09-21 13:10:10 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Variable handling: {{SUBST:xxx}} style words
|
|
|
|
|
* Calls back a function to determine what to replace xxx with
|
|
|
|
|
* Input word must contain $1
|
|
|
|
|
*/
|
2003-09-21 13:10:10 +00:00
|
|
|
function substituteCallback( $text, $callback ) {
|
2004-01-07 02:51:47 +00:00
|
|
|
$res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
|
|
|
|
|
$this->mModified = !($res === $text);
|
|
|
|
|
return $res;
|
2003-09-21 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Matches the word, where $1 is a wildcard
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function getVariableRegex() {
|
|
|
|
|
if ( $this->mVariableRegex == '' ) {
|
2003-09-21 13:10:10 +00:00
|
|
|
$this->initRegex();
|
2006-01-07 13:09:30 +00:00
|
|
|
}
|
2003-09-21 13:10:10 +00:00
|
|
|
return $this->mVariableRegex;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Matches the entire string, where $1 is a wildcard
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function getVariableStartToEndRegex() {
|
|
|
|
|
if ( $this->mVariableStartToEndRegex == '' ) {
|
2004-01-12 00:55:01 +00:00
|
|
|
$this->initRegex();
|
2006-01-07 13:09:30 +00:00
|
|
|
}
|
2004-01-12 00:55:01 +00:00
|
|
|
return $this->mVariableStartToEndRegex;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Accesses the synonym list directly
|
|
|
|
|
*/
|
2003-09-21 13:10:10 +00:00
|
|
|
function getSynonym( $i ) {
|
|
|
|
|
return $this->mSynonyms[$i];
|
|
|
|
|
}
|
2004-01-07 02:51:47 +00:00
|
|
|
|
2006-07-02 17:47:24 +00:00
|
|
|
function getSynonyms() {
|
|
|
|
|
return $this->mSynonyms;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2006-01-07 13:09:30 +00:00
|
|
|
* Returns true if the last call to replace() or substituteCallback()
|
2004-09-02 23:28:24 +00:00
|
|
|
* returned a modified text, otherwise false.
|
|
|
|
|
*/
|
2004-01-07 02:51:47 +00:00
|
|
|
function getWasModified(){
|
|
|
|
|
return $this->mModified;
|
|
|
|
|
}
|
2004-01-10 22:49:37 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* $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.
|
2006-01-07 13:09:30 +00:00
|
|
|
* @static
|
2004-09-02 23:28:24 +00:00
|
|
|
**/
|
|
|
|
|
function replaceMultiple( $magicarr, $subject, &$result ){
|
2004-01-10 22:49:37 +00:00
|
|
|
$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);
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Adds all the synonyms of this MagicWord to an array, to allow quick
|
|
|
|
|
* lookup in a list of magic words
|
|
|
|
|
*/
|
2004-08-12 13:32:04 +00:00
|
|
|
function addToArray( &$array, $value ) {
|
2006-07-03 08:57:29 +00:00
|
|
|
foreach ( $this->mSynonyms as $syn ) {
|
|
|
|
|
$array[$syn] = $value;
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-07-03 08:57:29 +00:00
|
|
|
|
|
|
|
|
function isCaseSensitive() {
|
|
|
|
|
return $this->mCaseSensitive;
|
|
|
|
|
}
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2003-09-01 13:13:56 +00:00
|
|
|
?>
|