2008-01-21 16:36:08 +00:00
|
|
|
<?php
|
2010-08-22 14:31:05 +00:00
|
|
|
/**
|
|
|
|
|
* Interfaces for preprocessors
|
|
|
|
|
*
|
2012-04-30 09:22:16 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2010-08-22 14:31:05 +00:00
|
|
|
* @file
|
2012-04-30 09:22:16 +00:00
|
|
|
* @ingroup Parser
|
2010-08-22 14:31:05 +00:00
|
|
|
*/
|
2008-01-21 16:36:08 +00:00
|
|
|
|
2015-10-08 20:54:15 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2019-02-28 20:54:49 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2015-10-08 20:54:15 +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
|
|
|
/**
|
|
|
|
|
* @ingroup Parser
|
|
|
|
|
*/
|
2015-10-08 20:54:15 +00:00
|
|
|
abstract class Preprocessor {
|
|
|
|
|
|
|
|
|
|
const CACHE_VERSION = 1;
|
|
|
|
|
|
2015-10-31 23:10:54 +00:00
|
|
|
/**
|
|
|
|
|
* @var array Brace matching rules.
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $rules = [
|
|
|
|
|
'{' => [
|
2015-10-31 23:10:54 +00:00
|
|
|
'end' => '}',
|
2016-02-17 09:09:32 +00:00
|
|
|
'names' => [
|
2015-10-31 23:10:54 +00:00
|
|
|
2 => 'template',
|
|
|
|
|
3 => 'tplarg',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-10-31 23:10:54 +00:00
|
|
|
'min' => 2,
|
|
|
|
|
'max' => 3,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'[' => [
|
2015-10-31 23:10:54 +00:00
|
|
|
'end' => ']',
|
2016-02-17 09:09:32 +00:00
|
|
|
'names' => [ 2 => null ],
|
2015-10-31 23:10:54 +00:00
|
|
|
'min' => 2,
|
|
|
|
|
'max' => 2,
|
2016-09-20 22:26:32 +00:00
|
|
|
],
|
|
|
|
|
'-{' => [
|
|
|
|
|
'end' => '}-',
|
2017-01-19 19:58:05 +00:00
|
|
|
'names' => [ 2 => null ],
|
|
|
|
|
'min' => 2,
|
|
|
|
|
'max' => 2,
|
2016-09-20 22:26:32 +00:00
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-10-31 23:10:54 +00:00
|
|
|
|
2015-10-08 20:54:15 +00:00
|
|
|
/**
|
|
|
|
|
* Store a document tree in the cache.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param int $flags
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param string $tree
|
2015-10-08 20:54:15 +00:00
|
|
|
*/
|
|
|
|
|
protected function cacheSetTree( $text, $flags, $tree ) {
|
|
|
|
|
$config = RequestContext::getMain()->getConfig();
|
|
|
|
|
|
|
|
|
|
$length = strlen( $text );
|
|
|
|
|
$threshold = $config->get( 'PreprocessorCacheThreshold' );
|
|
|
|
|
if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
|
2017-09-09 20:54:39 +00:00
|
|
|
return;
|
2015-10-08 20:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-28 20:54:49 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2017-05-25 19:05:49 +00:00
|
|
|
$key = $cache->makeKey(
|
2016-02-23 01:03:51 +00:00
|
|
|
defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
|
2019-02-28 20:54:49 +00:00
|
|
|
md5( $text ),
|
|
|
|
|
$flags
|
|
|
|
|
);
|
2015-10-26 01:29:47 +00:00
|
|
|
$value = sprintf( "%08d", static::CACHE_VERSION ) . $tree;
|
2015-10-08 20:54:15 +00:00
|
|
|
|
|
|
|
|
$cache->set( $key, $value, 86400 );
|
|
|
|
|
|
|
|
|
|
LoggerFactory::getInstance( 'Preprocessor' )
|
|
|
|
|
->info( "Cached preprocessor output (key: $key)" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:51:33 +00:00
|
|
|
/**
|
2015-10-08 20:54:15 +00:00
|
|
|
* Attempt to load a precomputed document tree for some given wikitext
|
|
|
|
|
* from the cache.
|
2011-05-28 17:51:33 +00:00
|
|
|
*
|
2015-10-08 20:54:15 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @param int $flags
|
|
|
|
|
* @return PPNode_Hash_Tree|bool
|
2011-05-28 17:51:33 +00:00
|
|
|
*/
|
2015-10-08 20:54:15 +00:00
|
|
|
protected function cacheGetTree( $text, $flags ) {
|
|
|
|
|
$config = RequestContext::getMain()->getConfig();
|
|
|
|
|
|
|
|
|
|
$length = strlen( $text );
|
|
|
|
|
$threshold = $config->get( 'PreprocessorCacheThreshold' );
|
|
|
|
|
if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 20:54:49 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2015-10-08 20:54:15 +00:00
|
|
|
|
2017-05-25 19:05:49 +00:00
|
|
|
$key = $cache->makeKey(
|
2016-02-23 01:03:51 +00:00
|
|
|
defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
|
2019-02-28 20:54:49 +00:00
|
|
|
md5( $text ),
|
|
|
|
|
$flags
|
|
|
|
|
);
|
2015-10-08 20:54:15 +00:00
|
|
|
|
|
|
|
|
$value = $cache->get( $key );
|
|
|
|
|
if ( !$value ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$version = intval( substr( $value, 0, 8 ) );
|
2015-10-26 01:29:47 +00:00
|
|
|
if ( $version !== static::CACHE_VERSION ) {
|
2015-10-08 20:54:15 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoggerFactory::getInstance( 'Preprocessor' )
|
|
|
|
|
->info( "Loaded preprocessor output from cache (key: $key)" );
|
|
|
|
|
|
|
|
|
|
return substr( $value, 8 );
|
|
|
|
|
}
|
2008-02-18 07:45:44 +00:00
|
|
|
|
2011-05-01 23:54:41 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new top-level frame for expansion of a page
|
|
|
|
|
*
|
|
|
|
|
* @return PPFrame
|
|
|
|
|
*/
|
2015-10-08 20:54:15 +00:00
|
|
|
abstract public function newFrame();
|
2008-02-18 07:45:44 +00:00
|
|
|
|
2011-05-01 23:54:41 +00:00
|
|
|
/**
|
2014-05-10 23:03:45 +00:00
|
|
|
* Create a new custom frame for programmatic use of parameter replacement
|
|
|
|
|
* as used in some extensions.
|
2011-05-01 23:54:41 +00:00
|
|
|
*
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param array $args
|
2011-11-09 20:52:24 +00:00
|
|
|
*
|
2011-05-01 23:54:41 +00:00
|
|
|
* @return PPFrame
|
|
|
|
|
*/
|
2015-10-08 20:54:15 +00:00
|
|
|
abstract public function newCustomFrame( $args );
|
2008-06-26 13:05:40 +00:00
|
|
|
|
2011-05-28 17:51:33 +00:00
|
|
|
/**
|
2014-05-10 23:03:45 +00:00
|
|
|
* Create a new custom node for programmatic use of parameter replacement
|
|
|
|
|
* as used in some extensions.
|
2011-05-28 17:51:33 +00:00
|
|
|
*
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param array $values
|
2011-05-28 17:51:33 +00:00
|
|
|
*/
|
2015-10-08 20:54:15 +00:00
|
|
|
abstract public function newPartNodeArray( $values );
|
2010-06-10 15:18:43 +00:00
|
|
|
|
2011-05-01 23:54:41 +00:00
|
|
|
/**
|
|
|
|
|
* Preprocess text to a PPNode
|
|
|
|
|
*
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @param int $flags
|
2011-11-09 20:52:24 +00:00
|
|
|
*
|
2011-05-01 23:54:41 +00:00
|
|
|
* @return PPNode
|
|
|
|
|
*/
|
2015-10-08 20:54:15 +00:00
|
|
|
abstract public function preprocessToObj( $text, $flags = 0 );
|
2008-01-21 16:36:08 +00:00
|
|
|
}
|