Tim Starling has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/296181

Change subject: Factor out preview parse
......................................................................

Factor out preview parse

Mildly refactor preview parsing, to avoid code duplication in the parser
migration tool that I am developing, which subclasses EditPage.

Change-Id: I3eee8a1ef3007353ba8b129d968d9f4e4d40ef5d
---
M includes/EditPage.php
1 file changed, 39 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/81/296181/1

diff --git a/includes/EditPage.php b/includes/EditPage.php
index 2a80ea6..9c7ccdf 100644
--- a/includes/EditPage.php
+++ b/includes/EditPage.php
@@ -3648,7 +3648,7 @@
         * @return string
         */
        function getPreviewText() {
-               global $wgOut, $wgUser, $wgRawHtml, $wgLang;
+               global $wgOut, $wgRawHtml, $wgLang;
                global $wgAllowUserCss, $wgAllowUserJs;
 
                $stats = $wgOut->getContext()->getStats();
@@ -3702,10 +3702,6 @@
                                $note = wfMessage( 'previewnote' )->plain() . ' 
' . $continueEditing;
                        }
 
-                       $parserOptions = $this->page->makeParserOptions( 
$this->mArticle->getContext() );
-                       $parserOptions->setIsPreview( true );
-                       $parserOptions->setIsSectionPreview( !is_null( 
$this->section ) && $this->section !== '' );
-
                        # don't parse non-wikitext pages, show message about 
preview
                        if ( $this->mTitle->isCssJsSubpage() || 
$this->mTitle->isCssOrJsPage() ) {
                                if ( $this->mTitle->isCssJsSubpage() ) {
@@ -3749,18 +3745,9 @@
                        ContentHandler::runLegacyHooks( 
'EditPageGetPreviewText', $hook_args );
                        Hooks::run( 'EditPageGetPreviewContent', $hook_args );
 
-                       $parserOptions->enableLimitReport();
-
-                       # For CSS/JS pages, we should have called the 
ShowRawCssJs hook here.
-                       # But it's now deprecated, so never mind
-
-                       $pstContent = $content->preSaveTransform( 
$this->mTitle, $wgUser, $parserOptions );
-                       $scopedCallback = $parserOptions->setupFakeRevision(
-                               $this->mTitle, $pstContent, $wgUser );
-                       $parserOutput = $pstContent->getParserOutput( 
$this->mTitle, null, $parserOptions );
-
-                       $parserOutput->setEditSectionTokens( false ); // no 
section edit links
-                       $previewHTML = $parserOutput->getText();
+                       $parserResult = $this->doPreviewParse( $content );
+                       $parserOutput = $parserResult['parserOutput'];
+                       $previewHTML = $parserResult['html'];
                        $this->mParserOutput = $parserOutput;
                        $wgOut->addParserOutputMetadata( $parserOutput );
 
@@ -3768,7 +3755,6 @@
                                $note .= "\n\n" . implode( "\n\n", 
$parserOutput->getWarnings() );
                        }
 
-                       ScopedCallback::consume( $scopedCallback );
                } catch ( MWContentSerializationException $ex ) {
                        $m = wfMessage(
                                'content-failed-to-parse',
@@ -3800,6 +3786,41 @@
        }
 
        /**
+        * Get parser options for a preview
+        * @return ParserOptions
+        */
+       protected function getPreviewParserOptions() {
+               $parserOptions = $this->page->makeParserOptions( 
$this->mArticle->getContext() );
+               $parserOptions->setIsPreview( true );
+               $parserOptions->setIsSectionPreview( !is_null( $this->section ) 
&& $this->section !== '' );
+               $parserOptions->enableLimitReport();
+               return $parserOptions;
+       }
+
+       /**
+        * Parse the page for a preview. Subclasses may override this class, in 
order
+        * to parse with different options, or to otherwise modify the preview 
HTML.
+        *
+        * @param Content @content The page content
+        * @return Associative array with keys:
+        *   - parserOutput: The ParserOutput object
+        *   - html: The HTML to be displayed
+        */
+       protected function doPreviewParse( Content $content ) {
+               global $wgUser;
+               $parserOptions = $this->getPreviewParserOptions();
+               $pstContent = $content->preSaveTransform( $this->mTitle, 
$wgUser, $parserOptions );
+               $scopedCallback = $parserOptions->setupFakeRevision(
+                       $this->mTitle, $pstContent, $wgUser );
+               $parserOutput = $pstContent->getParserOutput( $this->mTitle, 
null, $parserOptions );
+               ScopedCallback::consume( $scopedCallback );
+               $parserOutput->setEditSectionTokens( false ); // no section 
edit links
+               return [
+                       'parserOutput' => $parserOutput,
+                       'html' => $parserOutput->getText() ];
+       }
+
+       /**
         * @return array
         */
        function getTemplates() {

-- 
To view, visit https://gerrit.wikimedia.org/r/296181
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3eee8a1ef3007353ba8b129d968d9f4e4d40ef5d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Tim Starling <tstarl...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to