Store the options used by the parsing in ParserOutput, per r70783 CR.

This commit is contained in:
Platonides 2010-12-26 19:21:45 +00:00
parent 442359e489
commit fe14fdefa7
4 changed files with 48 additions and 26 deletions

View file

@ -22,7 +22,7 @@
* produces altered wiki markup.
* preprocess()
* removes HTML comments and expands templates
* cleanSig()
* cleanSig() / cleanSigInSig()
* Cleans a signature before saving it to preferences
* extractSections()
* Extracts sections from an article for section editing
@ -192,6 +192,7 @@ class Parser {
$this->firstCallInit();
}
$this->mOutput = new ParserOutput;
$this->mOptions->registerWatcher( array( $this->mOutput, 'recordOption' ) );
$this->mAutonumber = 0;
$this->mLastSection = '';
$this->mDTopen = false;
@ -268,12 +269,11 @@ class Parser {
wfProfileIn( __METHOD__ );
wfProfileIn( $fname );
$this->mOptions = $options;
if ( $clearState ) {
$this->clearState();
}
$options->resetUsage();
$this->mOptions = $options;
$this->setTitle( $title ); # Page title has to be set for the pre-processor
$oldRevisionId = $this->mRevisionId;
@ -465,10 +465,9 @@ class Parser {
*/
function preprocess( $text, $title, $options, $revid = null ) {
wfProfileIn( __METHOD__ );
$this->mOptions = $options;
$this->clearState();
$this->setOutputType( self::OT_PREPROCESS );
$options->resetUsage();
$this->mOptions = $options;
$this->setTitle( $title );
if ( $revid !== null ) {
$this->mRevisionId = $revid;
@ -489,10 +488,9 @@ class Parser {
*/
public function getPreloadText( $text, $title, $options ) {
# Parser (re)initialisation
$this->mOptions = $options;
$this->clearState();
$this->setOutputType( self::OT_PLAIN );
$options->resetUsage();
$this->mOptions = $options;
$this->setTitle( $title );
$flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES;
@ -4029,7 +4027,6 @@ class Parser {
* @return String: the altered wiki markup
*/
public function preSaveTransform( $text, Title $title, $user, $options, $clearState = true ) {
$options->resetUsage();
$this->mOptions = $options;
$this->setTitle( $title );
$this->setUser( $user );
@ -4211,9 +4208,9 @@ class Parser {
function cleanSig( $text, $parsing = false ) {
if ( !$parsing ) {
global $wgTitle;
$this->mOptions = new ParserOptions;
$this->clearState();
$this->setTitle( $wgTitle );
$this->mOptions = new ParserOptions;
$this->setOutputType = self::OT_PREPROCESS;
}
@ -4861,9 +4858,9 @@ class Parser {
*/
private function extractSections( $text, $section, $mode, $newText='' ) {
global $wgTitle;
$this->mOptions = new ParserOptions;
$this->clearState();
$this->setTitle( $wgTitle ); # not generally used but removes an ugly failure mode
$this->mOptions = new ParserOptions;
$this->setOutputType( self::OT_PLAIN );
$outText = '';
$frame = $this->getPreprocessor()->newFrame();
@ -5143,13 +5140,13 @@ class Parser {
* strip/replaceVariables/unstrip for preprocessor regression testing
*/
function testSrvus( $text, $title, $options, $outputType = self::OT_HTML ) {
$this->mOptions = $options;
$this->clearState();
if ( !$title instanceof Title ) {
$title = Title::newFromText( $title );
}
$this->mTitle = $title;
$options->resetUsage();
$this->mOptions = $options;
$this->setOutputType( $outputType );
$text = $this->replaceVariables( $text );
$text = $this->mStripState->unstripBoth( $text );

View file

@ -170,7 +170,7 @@ class ParserCache {
$now = wfTimestampNow();
$optionsKey = new CacheTime;
$optionsKey->mUsedOptions = $popts->usedOptions();
$optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
$optionsKey->updateCacheExpiry( $expire );
$optionsKey->setCacheTime( $now );

View file

@ -49,16 +49,16 @@ class ParserOptions {
var $mExtraKey = ''; # Extra key that should be present in the caching key.
protected $accessedOptions;
protected $onAccessCallback = null;
function getUseDynamicDates() { return $this->mUseDynamicDates; }
function getInterwikiMagic() { return $this->mInterwikiMagic; }
function getAllowExternalImages() { return $this->mAllowExternalImages; }
function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
function getEditSection() { $this->accessedOptions['editsection'] = true;
function getEditSection() { $this->optionUsed('editsection');
return $this->mEditSection; }
function getNumberHeadings() { $this->accessedOptions['numberheadings'] = true;
function getNumberHeadings() { $this->optionUsed('numberheadings');
return $this->mNumberHeadings; }
function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
function getTidy() { return $this->mTidy; }
@ -73,14 +73,14 @@ class ParserOptions {
function getEnableLimitReport() { return $this->mEnableLimitReport; }
function getCleanSignatures() { return $this->mCleanSignatures; }
function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
function getMath() { $this->accessedOptions['math'] = true;
function getMath() { $this->optionUsed('math');
return $this->mMath; }
function getThumbSize() { $this->accessedOptions['thumbsize'] = true;
function getThumbSize() { $this->optionUsed('thumbsize');
return $this->mThumbSize; }
function getIsPreview() { return $this->mIsPreview; }
function getIsSectionPreview() { return $this->mIsSectionPreview; }
function getIsPrintable() { $this->accessedOptions['printable'] = true;
function getIsPrintable() { $this->optionUsed('printable');
return $this->mIsPrintable; }
function getUser() { return $this->mUser; }
@ -92,7 +92,7 @@ class ParserOptions {
}
function getDateFormat() {
$this->accessedOptions['dateformat'] = true;
$this->optionUsed('dateformat');
if ( !isset( $this->mDateFormat ) ) {
$this->mDateFormat = $this->mUser->getDatePreference();
}
@ -112,7 +112,7 @@ class ParserOptions {
* producing inconsistent tables (Bug 14404).
*/
function getUserLang() {
$this->accessedOptions['userlang'] = true;
$this->optionUsed('userlang');
return $this->mUserLang;
}
@ -211,17 +211,20 @@ class ParserOptions {
}
/**
* Returns the options from this ParserOptions which have been used.
* Registers a callback for tracking which ParserOptions which are used.
* This is a private API with the parser.
*/
public function usedOptions() {
return array_keys( $this->accessedOptions );
function registerWatcher( $callback ) {
$this->onAccessCallback = $callback;
}
/**
* Resets the memory of options usage.
* Called when an option is accessed.
*/
public function resetUsage() {
$this->accessedOptions = array();
protected function optionUsed( $optionName ) {
if ( $this->onAccessCallback ) {
call_user_func( $this->onAccessCallback, $optionName );
}
}
/**

View file

@ -123,6 +123,7 @@ class ParserOutput extends CacheTime
$mProperties = array(), # Name/value pairs to be cached in the DB
$mTOCHTML = ''; # HTML of the TOC
private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
private $mAccessedOptions = null; # List of ParserOptions (stored in the keys)
function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
$containsOldMagic = false, $titletext = '' )
@ -327,4 +328,25 @@ class ParserOutput extends CacheTime
}
return $this->mProperties;
}
/**
* Returns the options from its ParserOptions which have been taken
* into account to produce this output or false if not available.
* @return mixed Array/false
*/
public function getUsedOptions() {
if ( !isset( $this->mAccessedOptions ) ) {
return false;
}
return array_keys( $this->mAccessedOptions );
}
/**
* Callback passed by the Parser to the ParserOptions to keep track of which options are used.
* @access private
*/
function recordOption( $option ) {
$this->mAccessedOptions[$option] = true;
}
}