Anomie has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/340769 )

Change subject: Use new OutputPage::addContentOverride to allow previewing 
edits to JS/CSS
......................................................................

Use new OutputPage::addContentOverride to allow previewing edits to JS/CSS

The interface doesn't display by default on JS and CSS pages, though. A
gadget may be used to do that.

This also makes Special:TemplateSandbox always require a POST, since
previewing JS is a sensitive operation.

Bug: T112474
Depends-On: Ib9d2ce42931c1de8372e231314a1f672d7e2ac0e
Change-Id: I83fa0856c52d659f228ed96aa9a5ad582c4512f9
---
M SpecialTemplateSandbox.php
M TemplateSandbox.hooks.php
M TemplateSandboxLogic.php
M i18n/en.json
M i18n/qqq.json
5 files changed, 63 insertions(+), 9 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TemplateSandbox 
refs/changes/69/340769/1

diff --git a/SpecialTemplateSandbox.php b/SpecialTemplateSandbox.php
index ee20ab7..f26c040 100644
--- a/SpecialTemplateSandbox.php
+++ b/SpecialTemplateSandbox.php
@@ -25,7 +25,6 @@
                $this->checkPermissions();
 
                $request = $this->getRequest();
-               $requirePost = $this->getConfig()->get( 'RawHtml' );
 
                if ( $par !== null && !$request->getCheck( 'page' ) ) {
                        $request->setVal( 'page', $par );
@@ -65,7 +64,7 @@
                                'rows' => 5,
                        ],
                ], $this->getContext() );
-               $form->setMethod( $requirePost ? 'post' : 'get' );
+               $form->setMethod( 'post' );
                $form->setSubmitCallback( [ $this, 'onSubmit' ] );
                $form->setWrapperLegend( $this->msg( 'templatesandbox-legend' ) 
);
                $form->addHeaderText( $this->msg( 'templatesandbox-text' 
)->parseAsBlock() );
@@ -79,7 +78,7 @@
                }
 
                $error = false;
-               if ( $requirePost && $this->getRequest()->wasPosted() ) {
+               if ( $this->getRequest()->wasPosted() ) {
                        $user = $this->getUser();
                        if ( $user->isAnon() && !$user->isAllowed( 'edit' ) ) {
                                $error = 'templatesandbox-fail-post-anon';
@@ -97,6 +96,14 @@
                        $this->output->setText( Html::rawElement( 'div', 
$attribs, $this->output->getRawText() ) );
 
                        $output = $this->getOutput();
+                       // Anons have predictable edit tokens, only do the 
JS/CSS preview for logged-in users.
+                       if ( $user->isAnon() ) {
+                               $this->getOutput()->wrapWikiMsg(
+                                       "<div 
class='previewnote'>\n$1\n</div>", 'templatesandbox-anon-limited-preview'
+                               );
+                       } else {
+                               
TemplateSandboxLogic::addSubpageHandlerToOutput( $this->prefixes, $output );
+                       }
                        $output->addParserOutput( $this->output );
 
                        $output->addHTML( Html::rawElement( 'div', [ 'class' => 
'limitreport' ],
diff --git a/TemplateSandbox.hooks.php b/TemplateSandbox.hooks.php
index 7a47640..3d4c78d 100644
--- a/TemplateSandbox.hooks.php
+++ b/TemplateSandbox.hooks.php
@@ -141,10 +141,13 @@
                        $popts->enableLimitReport();
 
                        $rev = call_user_func_array( 
$popts->getCurrentRevisionCallback(), [ $title ] );
-                       $content = $rev->getContent( Revision::FOR_THIS_USER, 
$user );
-                       $parserOutput = $content->getParserOutput( $title, 
$rev->getId(), $popts );
+                       $pageContent = $rev->getContent( 
Revision::FOR_THIS_USER, $user );
+                       $parserOutput = $pageContent->getParserOutput( $title, 
$rev->getId(), $popts );
 
                        $output->addParserOutputMetadata( $parserOutput );
+                       if ( $output->userCanPreview() ) {
+                               $output->addContentOverride( $templatetitle, 
$content );
+                       }
 
                        $dtitle = $parserOutput->getDisplayTitle();
                        $parserOutput->setTitleText( '' );
@@ -353,6 +356,8 @@
        public static function onApiMakeParserOptions(
                $options, $title, $params, $module, &$reset, &$suppressCache
        ) {
+               global $wgHooks;
+
                // Shouldn't happen, but...
                if ( !self::isUsableApiModule( $module ) ) {
                        return true;
@@ -431,6 +436,17 @@
                        $logic = new TemplateSandboxLogic( $prefixes, 
$templatetitle, $content );
                        $reset = $logic->setupForParse( $options );
                        $suppressCache = true;
+
+                       $wgHooks['ApiParseOutputPageForHeadHtml'][] = function 
( $module, $output )
+                               use ( $prefixes, $templatetitle, $content )
+                       {
+                               if ( $prefixes ) {
+                                       
TemplateSandboxLogic::addSubpageHandlerToOutput( $prefixes, $output );
+                               }
+                               if ( $templatetitle ) {
+                                       $output->addContentOverride( 
$templatetitle, $content );
+                               }
+                       };
                }
 
                return true;
diff --git a/TemplateSandboxLogic.php b/TemplateSandboxLogic.php
index b7917ca..078dba9 100644
--- a/TemplateSandboxLogic.php
+++ b/TemplateSandboxLogic.php
@@ -98,4 +98,33 @@
                        LinkCache::singleton()->clear();
                } );
        }
+
+       /**
+        * Add a handler for sandbox subpages to the OutputPage
+        * @param array $prefixes
+        * @param OutputPage $output
+        */
+       public static function addSubpageHandlerToOutput( array $prefixes, 
OutputPage $output ) {
+               $cache = [];
+               $output->addContentOverrideCallback( function ( Title $title ) 
use ( $prefixes, &$cache ) {
+                       $titleText = $title->getFullText();
+                       if ( array_key_exists( $titleText, $cache ) ) {
+                               return $cache[$titleText];
+                       }
+                       foreach ( $prefixes as $prefix ) {
+                               $newtitle = Title::newFromText( $prefix . '/' . 
$title->getFullText() );
+                               if ( $newtitle instanceof Title && 
$newtitle->exists() ) {
+                                       $rev = Revision::newFromTitle( 
$newtitle );
+                                       $content = $rev ? $rev->getContent() : 
null;
+                                       if ( $content ) {
+                                               $cache[$titleText] = $content;
+                                               return $content;
+                                       }
+                               }
+                       }
+                       $cache[$titleText] = null;
+                       return null;
+               } );
+       }
+
 }
diff --git a/i18n/en.json b/i18n/en.json
index e48ea1d..7404e49 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -28,8 +28,9 @@
        "templatesandbox-invalid-prefix": "The sandbox prefix you specified is 
invalid.",
        "templatesandbox-prefix-not-local": "The sandbox prefix you specified 
is not local.",
        "templatesandbox-page-or-revid": "You must enter either a page title or 
a revision ID number.",
-       "templatesandbox-fail-post": "<em>Because {{SITENAME}} has raw HTML 
enabled and there was a loss of session data, the preview is hidden as a 
precaution against JavaScript attacks.</em>\n\n<strong>If this is a legitimate 
preview attempt, please try again.</strong>\nIf it still does not work, try 
[[Special:UserLogout|logging out]] and logging back in.",
-       "templatesandbox-fail-post-anon": "<em>Because {{SITENAME}} has raw 
HTML enabled and you are not logged in, the preview is hidden as a precaution 
against JavaScript attacks.</em>\n\n<strong>If this is a legitimate preview 
attempt, please [[Special:UserLogin|log in]] and try again.</strong>",
+       "templatesandbox-fail-post": "<em>Due to a loss of session data, the 
preview is hidden as a precaution against JavaScript 
attacks.</em>\n\n<strong>If this is a legitimate preview attempt, please try 
again.</strong>\nIf it still does not work, try [[Special:UserLogout|logging 
out]] and logging back in.",
+       "templatesandbox-fail-post-anon": "<em>Because you are not logged in, 
the preview is hidden as a precaution against JavaScript 
attacks.</em>\n\n<strong>If this is a legitimate preview attempt, please 
[[Special:UserLogin|log in]] and try again.</strong>",
+       "templatesandbox-anon-limited-preview": "Preview of JavaScript and CSS 
changes is disabled when not logged in. If you need to preview such changes, 
please [[Special:UserLogin|log in]] and try again.",
        "templatesandbox-editform-need-template": "To preview another page with 
this template, a template name must be specified.",
        "templatesandbox-editform-need-title": "To preview another page with 
this template, a page title must be specified.",
        "templatesandbox-editform-invalid-template": "The name of the template 
you specified is invalid.",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index ca59f32..4acc2a5 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -32,8 +32,9 @@
        "templatesandbox-invalid-prefix": "Error message displayed when the 
sandbox prefix specified in the special page is invalid.",
        "templatesandbox-prefix-not-local": "Error message displayed when the 
sandbox prefix specified in the special page is not local.",
        "templatesandbox-page-or-revid": "Error message displayed when neither 
a page title nor rev_id is given in the special page.",
-       "templatesandbox-fail-post": "Error message displayed when attempting 
to use Special:TemplateSandbox with an invalid edit token on a wiki with 
$wgRawHtml true.\n\nSee also:\n* 
{{msg-mw|expand_templates_preview_fail_html}}\n{{Identical|Loss of session 
data}}",
-       "templatesandbox-fail-post-anon": "Error message displayed when 
attempting to use Special:TemplateSandbox as an anon on a wiki with $wgRawHtml 
true and anon editing disabled.\n\nSee also:\n* 
{{msg-mw|expand_templates_preview_fail_html_anon}}",
+       "templatesandbox-fail-post": "Error message displayed when attempting 
to use Special:TemplateSandbox with an invalid edit token.\n\nSee also:\n* 
{{msg-mw|expand_templates_preview_fail_html}}\n{{Identical|Loss of session 
data}}",
+       "templatesandbox-fail-post-anon": "Error message displayed when 
attempting to use Special:TemplateSandbox as an anon and anon editing 
disabled.\n\nSee also:\n* {{msg-mw|expand_templates_preview_fail_html_anon}}",
+       "templatesandbox-anon-limited-preview": "Message displayed when using 
Special:TemplateSandbox as an anon, to note that full functionality is not 
available.",
        "templatesandbox-editform-need-template": "Error message displayed when 
no template name is given for the editpage form.\n\nSee also:\n* 
{{msg-mw|Templatesandbox-editform-need-title}}",
        "templatesandbox-editform-need-title": "Error message displayed when no 
page title is given for the editpage form.\n\nSee also:\n* 
{{msg-mw|Templatesandbox-editform-need-template}}",
        "templatesandbox-editform-invalid-template": "Error message displayed 
when the template name specified for the editpage form is invalid.",

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I83fa0856c52d659f228ed96aa9a5ad582c4512f9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TemplateSandbox
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to