aboutsummaryrefslogtreecommitdiffstats
path: root/includes/parser/Preprocessor.php
diff options
context:
space:
mode:
authorAaron Schulz <aschulz@wikimedia.org>2020-04-07 16:52:41 -0700
committerAaron Schulz <aschulz@wikimedia.org>2021-01-11 20:21:10 -0800
commitde6eeead21f9cdb1bffa3f1b5877f71d03529722 (patch)
tree1859a76f92f23297836ce23ed44739fd4f99acd2 /includes/parser/Preprocessor.php
parent3ef5ebd504eaac03813eff6defbaf3faf544329e (diff)
downloadmediawikicore-de6eeead21f9cdb1bffa3f1b5877f71d03529722.tar.gz
mediawikicore-de6eeead21f9cdb1bffa3f1b5877f71d03529722.zip
parser: convert Preprocessor to WANCache and inject dependencies
Make the caching logic use getWithSetCallback() and simplify the code given that there is only one Preprocessor subclass. Also, keep the cached values JSON serializable but rely on the serialization in BagOStuff instead for simplicity. Add related class constants for injecting preprocessor flags. Bug: T254608 Change-Id: I72f9f0c0bc352ed5120469090c71294ff0c24999
Diffstat (limited to 'includes/parser/Preprocessor.php')
-rw-r--r--includes/parser/Preprocessor.php132
1 files changed, 45 insertions, 87 deletions
diff --git a/includes/parser/Preprocessor.php b/includes/parser/Preprocessor.php
index 358bce2dca3a..25368f91e1e5 100644
--- a/includes/parser/Preprocessor.php
+++ b/includes/parser/Preprocessor.php
@@ -21,24 +21,27 @@
* @ingroup Parser
*/
-use MediaWiki\Logger\LoggerFactory;
-use MediaWiki\MediaWikiServices;
-
/**
* @ingroup Parser
*/
abstract class Preprocessor {
+ /** Transclusion mode flag for Preprocessor::preprocessToObj() */
+ public const DOM_FOR_INCLUSION = 1;
+ /** Language conversion construct omission flag for Preprocessor::preprocessToObj() */
+ public const DOM_LANG_CONVERSION_DISABLED = 2;
+ /** Preprocessor cache bypass flag for Preprocessor::preprocessToObj */
+ public const DOM_UNCACHED = 4;
+
+ /** @var Parser */
+ public $parser;
- public const CACHE_VERSION = 1;
+ /** @var WANObjectCache */
+ protected $wanCache;
- /**
- * @var Parser
- */
- public $parser;
+ /** @var bool Whether language variant conversion is disabled */
+ protected $disableLangConversion;
- /**
- * @var array Brace matching rules.
- */
+ /** @var array Brace matching rules */
protected $rules = [
'{' => [
'end' => '}',
@@ -64,76 +67,19 @@ abstract class Preprocessor {
];
/**
- * Store a document tree in the cache.
- *
- * @param string $text
- * @param int $flags
- * @param string $tree
+ * @param Parser $parser
+ * @param WANObjectCache|null $wanCache
+ * @param array $options Map of additional options, including:
+ * - disableLangConversion: disable language variant conversion. [Default: false]
*/
- protected function cacheSetTree( $text, $flags, $tree ) {
- $config = RequestContext::getMain()->getConfig();
-
- $length = strlen( $text );
- $threshold = $config->get( 'PreprocessorCacheThreshold' );
- if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
- return;
- }
-
- $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
- $key = $cache->makeKey(
- // @phan-suppress-next-line PhanUndeclaredConstantOfClass
- defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
- md5( $text ),
- $flags
- );
- $value = sprintf( "%08d", static::CACHE_VERSION ) . $tree;
-
- $cache->set( $key, $value, 86400 );
-
- LoggerFactory::getInstance( 'Preprocessor' )
- ->info( "Cached preprocessor output (key: $key)" );
- }
-
- /**
- * Attempt to load a precomputed document tree for some given wikitext
- * from the cache.
- *
- * @param string $text
- * @param int $flags
- * @return PPNode_Hash_Tree|bool
- */
- 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;
- }
-
- $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
-
- $key = $cache->makeKey(
- // @phan-suppress-next-line PhanUndeclaredConstantOfClass
- defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
- md5( $text ),
- $flags
- );
-
- $value = $cache->get( $key );
- if ( !$value ) {
- return false;
- }
-
- $version = intval( substr( $value, 0, 8 ) );
- if ( $version !== static::CACHE_VERSION ) {
- return false;
- }
-
- LoggerFactory::getInstance( 'Preprocessor' )
- ->info( "Loaded preprocessor output from cache (key: $key)" );
-
- return substr( $value, 8 );
+ public function __construct(
+ Parser $parser,
+ WANObjectCache $wanCache = null,
+ array $options = []
+ ) {
+ $this->parser = $parser;
+ $this->wanCache = $wanCache ?: WANObjectCache::newEmpty();
+ $this->disableLangConversion = !empty( $options['disableLangConversion'] );
}
/**
@@ -145,29 +91,41 @@ abstract class Preprocessor {
/**
* Create a new custom frame for programmatic use of parameter replacement
- * as used in some extensions.
*
- * @param array $args
+ * This is useful for certain types of extensions
*
+ * @param array $args
* @return PPFrame
*/
abstract public function newCustomFrame( $args );
/**
* Create a new custom node for programmatic use of parameter replacement
- * as used in some extensions.
+ *
+ * This is useful for certain types of extensions
*
* @param array $values
*/
abstract public function newPartNodeArray( $values );
/**
- * Preprocess text to a PPNode
+ * Get the document object model for the given wikitext
*
- * @param string $text
- * @param int $flags
+ * Any flag added to the $flags parameter here, or any other parameter liable to cause
+ * a change in the DOM tree for the given wikitext, must be passed through the section
+ * identifier in the section edit link and thus back to extractSections().
*
+ * @param string $text Wikitext
+ * @param int $flags Bit field of Preprocessor::DOM_* flags:
+ * - Preprocessor::DOM_FOR_INCLUSION: treat the wikitext as transcluded content from
+ * a page rather than direct content of a page or message. By default, the text is
+ * assumed to be undergoing processing for use by direct page views. The use of this
+ * flag causes text within <noinclude> tags to be ignored, text within <includeonly>
+ * to be included, and text outside of <onlyinclude> to be ignored.
+ * - Preprocessor::DOM_NO_LANG_CONV: do not parse "-{ ... }-" constructs, which are
+ * involved in language variant conversion. (deprecated since 1.36)
+ * - Preprocessor::DOM_UNCACHED: disable use of the preprocessor cache.
* @return PPNode
*/
- abstract public function preprocessToObj( $text, $flags = 0 );
+ abstract public function preprocessToObj( $text, $flags = 0 );
}