From 2a30aa1d256d4cc72637f94e733a9031f47f1be4 Mon Sep 17 00:00:00 2001 From: Robert Leverington Date: Sun, 16 Jan 2011 21:12:26 +0000 Subject: [PATCH] Add new hook ArticlePrepareTextForEdit, called when preparing text to be saved. Add new parser option "PreSaveTransform" that allows the pre-save transformation to be selectively disabled. --- RELEASE-NOTES | 3 +++ docs/hooks.txt | 4 ++++ includes/Article.php | 19 ++++++++++++++++--- includes/parser/Parser.php | 4 +++- includes/parser/ParserOptions.php | 3 +++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index bd8c551348..9db072aabb 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -46,6 +46,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 6672) Images are now autorotated according to their EXIF orientation. This only affects thumbnails; the source remains unrotated. * (bug 25708) Update case mappings and normalization to Unicode 6.0.0 +* New hook ArticlePrepareTextForEdit added, called when preparing text to be saved. +* New parser option PreSaveTransform added, allows the pre-save transformation + to be selectively disabled. === Bug fixes in 1.18 === * (bug 23119) WikiError class and subclasses are now marked as deprecated diff --git a/docs/hooks.txt b/docs/hooks.txt index dfc7a95235..dfa34fea60 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -441,6 +441,10 @@ $row: row (object) returned from the database server $article: article (object) that data will be loaded $fields: fileds (array) to load from the database +'ArticlePrepareTextForEdit': called when preparing text to be saved +$article: the article being saved +$popts: parser options to be used for pre-save transformation + 'ArticleProtect': before an article is protected $article: the article being protected $user: the user doing the protection diff --git a/includes/Article.php b/includes/Article.php index b0a176430c..44bd713078 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -3596,10 +3596,17 @@ class Article { global $wgParser; + if( $user === null ) { + global $wgUser; + $user = $wgUser; + } + $popts = ParserOptions::newFromUser( $user ); + wfRunHooks( 'ArticlePrepareTextForEdit', array( $this, $popts ) ); + $edit = (object)array(); $edit->revid = $revid; $edit->newText = $text; - $edit->pst = $this->preSaveTransform( $text, $user ); + $edit->pst = $this->preSaveTransform( $text, $user, $popts ); $edit->popts = $this->getParserOptions( true ); $edit->output = $wgParser->parse( $edit->pst, $this->mTitle, $edit->popts, true, true, $revid ); $edit->oldText = $this->getRawText(); @@ -3876,10 +3883,12 @@ class Article { * @param $text String article contents * @param $user User object: user doing the edit, $wgUser will be used if * null is given + * @param $popts ParserOptions object: parser options, default options for + * the user loaded if null given * @return string article contents with altered wikitext markup (signatures * converted, {{subst:}}, templates, etc.) */ - public function preSaveTransform( $text, User $user = null ) { + public function preSaveTransform( $text, User $user = null, ParserOptions $popts = null ) { global $wgParser; if ( $user === null ) { @@ -3887,7 +3896,11 @@ class Article { $user = $wgUser; } - return $wgParser->preSaveTransform( $text, $this->mTitle, $user, ParserOptions::newFromUser( $user ) ); + if ( $popts === null ) { + $popts = ParserOptions::newFromUser( $user ); + } + + return $wgParser->preSaveTransform( $text, $this->mTitle, $user, $popts ); } /* Caching functions */ diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 777db678ef..7eadf397eb 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4053,7 +4053,9 @@ class Parser { "\r\n" => "\n", ); $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text ); - $text = $this->pstPass2( $text, $user ); + if( $options->getPreSaveTransform() ) { + $text = $this->pstPass2( $text, $user ); + } $text = $this->mStripState->unstripBoth( $text ); $this->setUser( null ); #Reset diff --git a/includes/parser/ParserOptions.php b/includes/parser/ParserOptions.php index 99d680c971..a9a5467ebd 100644 --- a/includes/parser/ParserOptions.php +++ b/includes/parser/ParserOptions.php @@ -36,6 +36,7 @@ class ParserOptions { var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc. var $mExternalLinkTarget; # Target attribute for external links var $mCleanSignatures; # + var $mPreSaveTransform = true; # Transform wiki markup when saving the page. var $mNumberHeadings; # Automatically number headings var $mMath; # User math preference (as integer) @@ -82,6 +83,7 @@ class ParserOptions { function getIsPrintable() { $this->optionUsed('printable'); return $this->mIsPrintable; } function getUser() { return $this->mUser; } + function getPreSaveTransform() { return $this->mPreSaveTransform; } function getSkin( $title = null ) { if ( !isset( $this->mSkin ) ) { @@ -140,6 +142,7 @@ class ParserOptions { function setMath( $x ) { return wfSetVar( $this->mMath, $x ); } function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); } function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); } + function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); } function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); } function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); } -- 2.20.1