Jackmcbarn has uploaded a new change for review.

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

Change subject: Use RevisionFromTitle instead of a template callback
......................................................................

Use RevisionFromTitle instead of a template callback

RevisionFromTitle will catch things like Scribunto's getContent() and the
REVISIONUSER/REVISIONTIMESTAMP parser functions. Use it in place of the
template callback, and also avoid duplicating a lot of logic from
statelessFetchTemplate.

Bug: 70495
Change-Id: Idefaa89e665a0caf7b47b6f67649b6a87b524ff4
---
M TemplateSandbox.hooks.php
1 file changed, 23 insertions(+), 83 deletions(-)


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

diff --git a/TemplateSandbox.hooks.php b/TemplateSandbox.hooks.php
index ad964b5..3ab7bf7 100644
--- a/TemplateSandbox.hooks.php
+++ b/TemplateSandbox.hooks.php
@@ -1,17 +1,5 @@
 <?php
 class TemplateSandboxHooks {
-       private static $template = null;
-
-       /**
-        * @var Content
-        */
-       private static $content = null;
-
-       /**
-        * @var callback
-        */
-       private static $oldTemplateCallback = null;
-
        /**
         * Hook for EditPage::importFormData to parse our new form fields, and 
if
         * necessary put $editpage into "preview" mode.
@@ -54,9 +42,11 @@
 
        /**
         * @param Title $templatetitle
+        * @param Content $content
+        * @param User $user
         * @return ScopedCallback to clean up
         */
-       private static function fakePageExists( $templatetitle ) {
+       private static function setFakePageHooks( $templatetitle, $content, 
$user ) {
                global $wgHooks;
                $wgHooks['TitleExists']['TemplateSandbox'] =
                        function( $title, &$exists ) use( $templatetitle ) {
@@ -64,10 +54,26 @@
                                        $exists = true;
                                }
                        };
+               $wgHooks['RevisionFromTitle']['TemplateSandbox'] =
+                       function( $title, &$id, &$revision ) use( 
$templatetitle, $content, $user ) {
+                               if ( $templatetitle->equals( $title ) ) {
+                                       $revision = new Revision( array(
+                                               'page' => 
$templatetitle->getArticleID(),
+                                               'user_text' => $user->getName(),
+                                               'user' => $user->getId(),
+                                               'parent_id' => 
$templatetitle->getLatestRevId(),
+                                               'title' => $templatetitle,
+                                               'content' => $content
+                                       ) );
+                                       return false;
+                               }
+                               return true;
+                       };
                LinkCache::singleton()->clearBadLink( 
$templatetitle->getPrefixedDBkey() );
                return new ScopedCallback( function() use( $templatetitle ) {
                        global $wgHooks;
                        unset( $wgHooks['TitleExists']['TemplateSandbox'] );
+                       unset( $wgHooks['RevisionFromTitle']['TemplateSandbox'] 
);
                        LinkCache::singleton()->clearLink( $templatetitle );
                } );
        }
@@ -127,7 +133,6 @@
                $parserOutput = null;
 
                try {
-                       TemplateSandboxHooks::$template = 
$templatetitle->getFullText();
                        if ( $editpage->sectiontitle !== '' ) {
                                $sectionTitle = $editpage->sectiontitle;
                        } else {
@@ -135,14 +140,14 @@
                        }
 
                        if ( $editpage->getArticle()->exists() ) {
-                               TemplateSandboxHooks::$content = 
$editpage->getArticle()->replaceSectionContent(
+                               $fakeContent = 
$editpage->getArticle()->replaceSectionContent(
                                        $editpage->section, $content, 
$sectionTitle, $editpage->edittime
                                );
                        } else {
                                if ( $editpage->section === 'new' ) {
                                        $content = $content->addSectionHeader( 
$sectionTitle );
                                }
-                               TemplateSandboxHooks::$content = $content;
+                               $fakeContent = $content;
                        }
 
                        // Apply PST to the to-be-saved text
@@ -152,9 +157,7 @@
                        $popts->setEditSection( false );
                        $popts->setIsPreview( true );
                        $popts->setIsSectionPreview( false );
-                       TemplateSandboxHooks::$content = 
TemplateSandboxHooks::$content->preSaveTransform(
-                               $templatetitle, $wgUser, $popts
-                       );
+                       $fakeContent = $fakeContent->preSaveTransform( 
$templatetitle, $wgUser, $popts );
 
                        $note = wfMessage( 'templatesandbox-previewnote', 
$title->getFullText() )->plain() .
                                ' [[#' . EditPage::EDITFORM_ID . '|' . 
$wgLang->getArrow() . ' ' .
@@ -165,10 +168,7 @@
                        $popts->setEditSection( false );
                        $popts->setIsPreview( true );
                        $popts->setIsSectionPreview( false );
-                       TemplateSandboxHooks::$oldTemplateCallback = 
$popts->setTemplateCallback(
-                               'TemplateSandboxHooks::templateCallback'
-                       );
-                       $fakePageExistsScopedCallback = self::fakePageExists( 
$templatetitle );
+                       $fakePageExistsScopedCallback = self::setFakePageHooks( 
$templatetitle, $fakeContent, $wgUser );
                        $popts->enableLimitReport();
 
                        $rev = Revision::newFromTitle( $title );
@@ -211,66 +211,6 @@
 
                wfProfileOut( __METHOD__ );
                return false;
-       }
-
-       /**
-        * @param $title Title
-        * @param $parser Parser|bool
-        * @return array|mixed
-        */
-       static function templateCallback( $title, $parser = false ) {
-               global $wgUser;
-
-               // Note that Parser::statelessFetchTemplate currently only 
handles one
-               // level of redirection, regardless of $wgMaxRedirects. We 
reproduce
-               // this behavior here.
-               $match = ( $title->getFullText() == 
TemplateSandboxHooks::$template );
-               $rtitle = null;
-               if ( !$match && $title->isRedirect() ) {
-                       $rtitle = WikiPage::factory( $title 
)->getRedirectTarget();
-                       $match = ( $rtitle->getFullText() == 
TemplateSandboxHooks::$template );
-               }
-               if ( $match ) {
-                       $deps[] = array(
-                               'title' => $title,
-                               'page_id' => $title->getArticleID(),
-                               'rev_id' => 0,
-                       );
-                       $finalTitle = $title;
-                       if ( $rtitle ) {
-                               $deps[] = array(
-                                       'title' => $rtitle,
-                                       'page_id' => $rtitle->getArticleID(),
-                                       'rev_id' => 0,
-                               );
-                               $finalTitle = $rtitle;
-                       }
-
-                       $content = TemplateSandboxHooks::$content;
-                       if ( !$rtitle && $content->isRedirect() ) {
-                               $newTitle = $content->getRedirectTarget();
-                               $rev = Revision::newFromTitle( $newTitle );
-                               if ( $rev ) {
-                                       $content = $rev->getContent( 
Revision::FOR_THIS_USER, $wgUser );
-                                       $finalTitle = $newTitle;
-                               }
-                               $deps[] = array(
-                                       'title' => $newTitle,
-                                       'page_id' => $newTitle->getArticleID(),
-                                       'rev_id' => 0,
-                               );
-                       }
-                       $text = $content->getWikitextForTransclusion();
-                       if ( $text === null ) {
-                               $text = false;
-                       }
-                       return array(
-                               'text' => $text,
-                               'finalTitle' => $finalTitle,
-                               'deps' => $deps,
-                       );
-               }
-               return call_user_func( 
TemplateSandboxHooks::$oldTemplateCallback, $title, $parser );
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idefaa89e665a0caf7b47b6f67649b6a87b524ff4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TemplateSandbox
Gerrit-Branch: master
Gerrit-Owner: Jackmcbarn <jackmcb...@gmail.com>

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

Reply via email to