2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2012-05-12 20:33:02 +00:00
|
|
|
* See docs/magicword.txt.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2008-03-12 17:37:03 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Parser
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-08-31 09:46:37 +00:00
|
|
|
|
2018-07-24 16:44:09 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2012-07-10 12:48:06 +00:00
|
|
|
* This class encapsulates "magic words" such as "#redirect", __NOTOC__, etc.
|
2012-02-01 20:53:38 +00:00
|
|
|
*
|
|
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
2018-07-24 16:44:09 +00:00
|
|
|
* if ( $magicWordFactory->get( 'redirect' )->match( $text ) ) {
|
2012-02-01 20:53:38 +00:00
|
|
|
* // some code
|
|
|
|
|
* }
|
|
|
|
|
* @endcode
|
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
|
|
|
*
|
2012-01-18 17:43:08 +00:00
|
|
|
* To add magic words in an extension, use $magicWords in a file listed in
|
|
|
|
|
* $wgExtensionMessagesFiles[].
|
2012-07-10 12:48:06 +00:00
|
|
|
*
|
2012-02-01 20:53:38 +00:00
|
|
|
* @par Example:
|
|
|
|
|
* @code
|
2016-09-12 10:06:37 +00:00
|
|
|
* $magicWords = [];
|
2012-01-18 17:43:08 +00:00
|
|
|
*
|
2016-09-12 10:06:37 +00:00
|
|
|
* $magicWords['en'] = [
|
2017-02-25 21:53:36 +00:00
|
|
|
* 'magicwordkey' => [ 0, 'case_insensitive_magic_word' ],
|
|
|
|
|
* 'magicwordkey2' => [ 1, 'CASE_sensitive_magic_word2' ],
|
2016-09-12 10:06:37 +00:00
|
|
|
* ];
|
2012-02-01 20:53:38 +00:00
|
|
|
* @endcode
|
2012-01-18 17:43:08 +00:00
|
|
|
*
|
|
|
|
|
* For magic words which are also Parser variables, add a MagicWordwgVariableIDs
|
2006-07-14 16:36:35 +00:00
|
|
|
* hook. Use string keys.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Parser
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-08-31 09:46:37 +00:00
|
|
|
class MagicWord {
|
2014-05-12 14:42:51 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
|
2017-10-02 11:52:17 +00:00
|
|
|
/** @var string */
|
2014-05-12 14:42:51 +00:00
|
|
|
public $mId;
|
|
|
|
|
|
2017-10-02 11:52:17 +00:00
|
|
|
/** @var string[] */
|
2014-05-12 14:42:51 +00:00
|
|
|
public $mSynonyms;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
public $mCaseSensitive;
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mRegex = '';
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mRegexStart = '';
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mRegexStartToEnd = '';
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mBaseRegex = '';
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mVariableRegex = '';
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mVariableStartToEndRegex = '';
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $mModified = false;
|
|
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
|
private $mFound = false;
|
2006-07-14 16:36:35 +00:00
|
|
|
|
2018-07-26 12:21:31 +00:00
|
|
|
/** @var Language */
|
|
|
|
|
private $contLang;
|
|
|
|
|
|
2004-09-03 23:00:01 +00:00
|
|
|
/**#@-*/
|
2004-01-07 02:51:47 +00:00
|
|
|
|
2017-10-02 11:52:17 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new MagicWord object
|
|
|
|
|
*
|
2018-07-24 16:44:09 +00:00
|
|
|
* Use factory instead: MagicWordFactory::get
|
2017-10-02 11:52:17 +00:00
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null $id The internal name of the magic word
|
2017-10-02 11:52:17 +00:00
|
|
|
* @param string[]|string $syn synonyms for the magic word
|
|
|
|
|
* @param bool $cs If magic word is case sensitive
|
2018-07-26 12:21:31 +00:00
|
|
|
* @param Language|null $contLang Content language
|
2017-10-02 11:52:17 +00:00
|
|
|
*/
|
2018-07-26 12:21:31 +00:00
|
|
|
public function __construct( $id = null, $syn = [], $cs = false, Language $contLang = null ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->mId = $id;
|
|
|
|
|
$this->mSynonyms = (array)$syn;
|
|
|
|
|
$this->mCaseSensitive = $cs;
|
2019-04-15 08:48:13 +00:00
|
|
|
$this->contLang = $contLang ?: MediaWikiServices::getInstance()->getContentLanguage();
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Factory: creates an object representing an ID
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2017-10-02 11:52:17 +00:00
|
|
|
* @param string $id The internal name of the magic word
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2011-02-08 23:18:13 +00:00
|
|
|
* @return MagicWord
|
2018-07-24 16:44:09 +00:00
|
|
|
* @deprecated since 1.32, use MagicWordFactory::get
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2018-07-24 16:44:09 +00:00
|
|
|
public static function get( $id ) {
|
2018-07-30 14:57:53 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-07-24 16:44:09 +00:00
|
|
|
return MediaWikiServices::getInstance()->getMagicWordFactory()->get( $id );
|
2006-07-14 16:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array of parser variable IDs
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2017-10-02 11:52:17 +00:00
|
|
|
* @return string[]
|
2018-07-24 16:44:09 +00:00
|
|
|
* @deprecated since 1.32, use MagicWordFactory::getVariableIDs
|
2006-07-14 16:36:35 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public static function getVariableIDs() {
|
2018-07-30 14:57:53 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-07-24 16:44:09 +00:00
|
|
|
return MediaWikiServices::getInstance()->getMagicWordFactory()->getVariableIDs();
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-30 11:58:19 +00:00
|
|
|
/**
|
|
|
|
|
* Get an array of parser substitution modifier IDs
|
2017-10-02 11:52:17 +00:00
|
|
|
* @return string[]
|
2018-07-24 16:44:09 +00:00
|
|
|
* @deprecated since 1.32, use MagicWordFactory::getSubstIDs
|
2010-01-30 11:58:19 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public static function getSubstIDs() {
|
2018-07-30 14:57:53 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-07-24 16:44:09 +00:00
|
|
|
return MediaWikiServices::getInstance()->getMagicWordFactory()->getSubstIDs();
|
2010-01-30 11:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
|
|
|
|
* Allow external reads of TTL array
|
|
|
|
|
*
|
2017-10-02 11:52:17 +00:00
|
|
|
* @param string $id
|
2014-05-10 18:26:47 +00:00
|
|
|
* @return int
|
2018-07-24 16:44:09 +00:00
|
|
|
* @deprecated since 1.32, use MagicWordFactory::getCacheTTL
|
2011-05-21 19:55:05 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public static function getCacheTTL( $id ) {
|
2018-07-30 14:57:53 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-07-24 16:44:09 +00:00
|
|
|
return MediaWikiServices::getInstance()->getMagicWordFactory()->getCacheTTL( $id );
|
2008-01-09 20:47:46 +00:00
|
|
|
}
|
2008-02-20 08:53:12 +00:00
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
|
|
|
|
* Get a MagicWordArray of double-underscore entities
|
|
|
|
|
*
|
2011-05-26 19:52:56 +00:00
|
|
|
* @return MagicWordArray
|
2018-07-24 16:44:09 +00:00
|
|
|
* @deprecated since 1.32, use MagicWordFactory::getDoubleUnderscoreArray
|
2011-05-21 19:55:05 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public static function getDoubleUnderscoreArray() {
|
2018-07-30 14:57:53 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-07-24 16:44:09 +00:00
|
|
|
return MediaWikiServices::getInstance()->getMagicWordFactory()->getDoubleUnderscoreArray();
|
2009-08-26 16:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
|
|
|
|
* Initialises this object with an ID
|
|
|
|
|
*
|
2017-10-02 11:52:17 +00:00
|
|
|
* @param string $id
|
2012-08-04 23:47:23 +00:00
|
|
|
* @throws MWException
|
2011-05-21 19:55:05 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function load( $id ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->mId = $id;
|
2018-07-26 12:21:31 +00:00
|
|
|
$this->contLang->getMagic( $this );
|
2006-10-02 17:04:13 +00:00
|
|
|
if ( !$this->mSynonyms ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mSynonyms = [ 'brionmademeputthishere' ];
|
2012-06-05 22:58:54 +00:00
|
|
|
throw new MWException( "Error: invalid magic word '$id'" );
|
2006-10-02 17:04:13 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function initRegex() {
|
2011-02-16 07:24:36 +00:00
|
|
|
// Sort the synonyms by length, descending, so that the longest synonym
|
|
|
|
|
// matches in precedence to the shortest
|
|
|
|
|
$synonyms = $this->mSynonyms;
|
2016-02-17 09:09:32 +00:00
|
|
|
usort( $synonyms, [ $this, 'compareStringLength' ] );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$escSyn = [];
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $synonyms as $synonym ) {
|
2006-01-08 04:25:43 +00:00
|
|
|
// In case a magic word contains /, like that's going to happen;)
|
|
|
|
|
$escSyn[] = preg_quote( $synonym, '/' );
|
2013-04-20 22:49:30 +00:00
|
|
|
}
|
2004-08-22 17:24:50 +00:00
|
|
|
$this->mBaseRegex = implode( '|', $escSyn );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-10-08 13:00:42 +00:00
|
|
|
$case = $this->mCaseSensitive ? '' : 'iu';
|
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}";
|
2013-12-17 18:14:15 +00:00
|
|
|
$this->mRegexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}";
|
2005-05-04 07:49:42 +00:00
|
|
|
$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
|
|
|
|
2011-02-16 07:24:36 +00:00
|
|
|
/**
|
2011-09-04 21:40:17 +00:00
|
|
|
* A comparison function that returns -1, 0 or 1 depending on whether the
|
|
|
|
|
* first string is longer, the same length or shorter than the second
|
2011-02-16 07:24:36 +00:00
|
|
|
* string.
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $s1
|
|
|
|
|
* @param string $s2
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return int
|
2011-02-16 07:24:36 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function compareStringLength( $s1, $s2 ) {
|
2011-02-16 07:24:36 +00:00
|
|
|
$l1 = strlen( $s1 );
|
|
|
|
|
$l2 = strlen( $s2 );
|
Use PHP 7 '<=>' operator in 'sort()' callbacks
`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser,
and `0` if they are equal, which are exactly the values 'sort()'
callbacks are supposed to return.
It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]`
to sort arrays of objects first by 'x', and by 'y' if they are equal.
* Replace a common pattern like `return $a < $b ? -1 : 1` with the
new operator (and similar patterns with the variables, the numbers
or the comparison inverted). Some of the uses were previously not
correctly handling the variables being equal; this is now
automatically fixed.
* Also replace `return $a - $b`, which is equivalent to `return
$a <=> $b` if both variables are integers but less intuitive.
* (Do not replace `return strcmp( $a, $b )`. It is also equivalent
when both variables are strings, but if any of the variables is not,
'strcmp()' converts it to a string before comparison, which could
give different results than '<=>', so changing this would require
careful review and isn't worth it.)
* Also replace `return $a > $b`, which presumably sort of works most
of the time (returns `1` if `$b` is lesser, and `0` if they are
equal or `$a` is lesser) but is erroneous.
Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
2017-10-06 20:39:13 +00:00
|
|
|
return $l2 <=> $l1; // descending
|
2011-02-16 07:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a regex representing matching the word
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getRegex() {
|
2013-02-03 20:05:24 +00:00
|
|
|
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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2006-01-08 05:35:03 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getRegexCase() {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $this->mRegex === '' ) {
|
2006-01-08 05:35:03 +00:00
|
|
|
$this->initRegex();
|
2013-04-20 22:49:30 +00:00
|
|
|
}
|
2006-01-08 05:35:03 +00:00
|
|
|
|
2006-10-08 13:00:42 +00:00
|
|
|
return $this->mCaseSensitive ? '' : 'iu';
|
2006-01-08 05:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a regex matching the word, if it is at the string start
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getRegexStart() {
|
2013-02-03 20:05:24 +00:00
|
|
|
if ( $this->mRegex == '' ) {
|
2003-08-31 09:46:37 +00:00
|
|
|
$this->initRegex();
|
|
|
|
|
}
|
|
|
|
|
return $this->mRegexStart;
|
|
|
|
|
}
|
2004-01-12 00:55:01 +00:00
|
|
|
|
2013-12-17 18:14:15 +00:00
|
|
|
/**
|
|
|
|
|
* Gets a regex matching the word from start to end of a string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getRegexStartToEnd() {
|
2013-12-17 18:14:15 +00:00
|
|
|
if ( $this->mRegexStartToEnd == '' ) {
|
|
|
|
|
$this->initRegex();
|
|
|
|
|
}
|
|
|
|
|
return $this->mRegexStartToEnd;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* regex without the slashes and what not
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getBaseRegex() {
|
2013-02-03 20:05:24 +00:00
|
|
|
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
|
|
|
/**
|
2010-01-12 07:24:25 +00:00
|
|
|
* Returns true if the text contains the word
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2010-01-12 07:24:25 +00:00
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function match( $text ) {
|
2010-01-12 07:24:25 +00:00
|
|
|
return (bool)preg_match( $this->getRegex(), $text );
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2010-01-12 07:24:25 +00:00
|
|
|
* Returns true if the text starts with the word
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2010-01-12 07:24:25 +00:00
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function matchStart( $text ) {
|
2010-01-12 07:24:25 +00:00
|
|
|
return (bool)preg_match( $this->getRegexStart(), $text );
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-17 18:14:15 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the text matched the word
|
|
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text
|
2013-12-17 18:14:15 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function matchStartToEnd( $text ) {
|
2013-12-17 18:14:15 +00:00
|
|
|
return (bool)preg_match( $this->getRegexStartToEnd(), $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.
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function matchVariableStartToEnd( $text ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$matches = [];
|
2004-01-12 00:55:01 +00:00
|
|
|
$matchcount = preg_match( $this->getVariableStartToEndRegex(), $text, $matches );
|
|
|
|
|
if ( $matchcount == 0 ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2004-01-12 00:55:01 +00:00
|
|
|
} 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.
|
2017-02-20 22:44:19 +00:00
|
|
|
# See also T8526
|
2006-07-11 13:42:34 +00:00
|
|
|
|
2013-02-03 20:05:24 +00:00
|
|
|
$matches = array_values( array_filter( $matches ) );
|
2006-07-11 13:42:34 +00:00
|
|
|
|
2013-02-03 20:05:24 +00:00
|
|
|
if ( count( $matches ) == 1 ) {
|
2011-05-21 19:55:05 +00:00
|
|
|
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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param string &$text
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function matchAndRemove( &$text ) {
|
2006-07-14 16:36:35 +00:00
|
|
|
$this->mFound = false;
|
2014-05-12 14:42:51 +00:00
|
|
|
$text = preg_replace_callback(
|
|
|
|
|
$this->getRegex(),
|
2017-02-01 04:01:54 +00:00
|
|
|
[ $this, 'pregRemoveAndRecord' ],
|
2014-05-12 14:42:51 +00:00
|
|
|
$text
|
|
|
|
|
);
|
|
|
|
|
|
2006-07-14 16:36:35 +00:00
|
|
|
return $this->mFound;
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param string &$text
|
2011-05-21 19:55:05 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function matchStartAndRemove( &$text ) {
|
2006-07-14 16:36:35 +00:00
|
|
|
$this->mFound = false;
|
2014-05-12 14:42:51 +00:00
|
|
|
$text = preg_replace_callback(
|
|
|
|
|
$this->getRegexStart(),
|
2017-02-01 04:01:54 +00:00
|
|
|
[ $this, 'pregRemoveAndRecord' ],
|
2014-05-12 14:42:51 +00:00
|
|
|
$text
|
|
|
|
|
);
|
|
|
|
|
|
2006-07-14 16:36:35 +00:00
|
|
|
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()
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function pregRemoveAndRecord() {
|
2006-07-14 16:36:35 +00:00
|
|
|
$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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $replacement
|
|
|
|
|
* @param string $subject
|
|
|
|
|
* @param int $limit
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function replace( $replacement, $subject, $limit = -1 ) {
|
2014-05-12 14:42:51 +00:00
|
|
|
$res = preg_replace(
|
|
|
|
|
$this->getRegex(),
|
|
|
|
|
StringUtils::escapeRegexReplacement( $replacement ),
|
|
|
|
|
$subject,
|
|
|
|
|
$limit
|
|
|
|
|
);
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->mModified = $res !== $subject;
|
2004-01-07 02:51:47 +00:00
|
|
|
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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @param callable $callback
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function substituteCallback( $text, $callback ) {
|
2004-01-07 02:51:47 +00:00
|
|
|
$res = preg_replace_callback( $this->getVariableRegex(), $callback, $text );
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->mModified = $res !== $text;
|
2004-01-07 02:51:47 +00:00
|
|
|
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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getVariableRegex() {
|
2004-08-12 13:32:04 +00:00
|
|
|
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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getVariableStartToEndRegex() {
|
2004-08-12 13:32:04 +00:00
|
|
|
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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param int $i
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getSynonym( $i ) {
|
2003-09-21 13:10:10 +00:00
|
|
|
return $this->mSynonyms[$i];
|
|
|
|
|
}
|
2004-01-07 02:51:47 +00:00
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
2017-10-02 11:52:17 +00:00
|
|
|
* @return string[]
|
2011-05-21 19:55:05 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getSynonyms() {
|
2006-07-02 17:47:24 +00:00
|
|
|
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.
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getWasModified() {
|
2004-01-07 02:51:47 +00:00
|
|
|
return $this->mModified;
|
|
|
|
|
}
|
2004-01-10 22:49:37 +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
|
2011-05-21 19:55:05 +00:00
|
|
|
*
|
2017-10-02 11:52:17 +00:00
|
|
|
* @param string[] &$array
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $value
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function addToArray( &$array, $value ) {
|
2006-07-03 08:57:29 +00:00
|
|
|
foreach ( $this->mSynonyms as $syn ) {
|
2018-07-26 12:21:31 +00:00
|
|
|
$array[$this->contLang->lc( $syn )] = $value;
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-07-03 08:57:29 +00:00
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function isCaseSensitive() {
|
2006-07-03 08:57:29 +00:00
|
|
|
return $this->mCaseSensitive;
|
|
|
|
|
}
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
|
2011-05-21 19:55:05 +00:00
|
|
|
/**
|
2017-10-02 11:52:17 +00:00
|
|
|
* @return string
|
2011-05-21 19:55:05 +00:00
|
|
|
*/
|
2016-03-01 10:56:04 +00:00
|
|
|
public function getId() {
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
return $this->mId;
|
|
|
|
|
}
|
2003-08-31 09:46:37 +00:00
|
|
|
}
|