jenkins-bot has submitted this change and it was merged.

Change subject: Hook TitleExists when previewing
......................................................................


Hook TitleExists when previewing

Bug: 70495
Change-Id: I1828fb8d49943520b8af5b92a3e1f1ddde1a5625
---
M SpecialTemplateSandbox.php
M TemplateSandbox.hooks.php
2 files changed, 53 insertions(+), 0 deletions(-)

Approvals:
  Anomie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/SpecialTemplateSandbox.php b/SpecialTemplateSandbox.php
index 8b9404c..cc56023 100644
--- a/SpecialTemplateSandbox.php
+++ b/SpecialTemplateSandbox.php
@@ -17,6 +17,37 @@
                parent::__construct( 'TemplateSandbox' );
        }
 
+       /**
+        * @return ScopedCallback to clean up
+        */
+       private function fakePageExists() {
+               global $wgHooks;
+               $prefixes = $this->prefixes;
+               $inHook = false;
+               $wgHooks['TitleExists']['TemplateSandbox'] =
+                       function( $title, &$exists ) use( $prefixes, &$inHook ) 
{
+                               if ( $exists || $inHook ) {
+                                       return;
+                               }
+                               $inHook = true;
+                               foreach ( $prefixes as $prefix ) {
+                                       $newtitle = Title::newFromText(
+                                               $prefix . '/' . 
$title->getFullText() );
+                                       if ( $newtitle instanceof Title && 
$newtitle->exists() ) {
+                                               $exists = true;
+                                               break;
+                                       }
+                               }
+                               $inHook = false;
+                       };
+               LinkCache::singleton()->clear();
+               return new ScopedCallback( function() {
+                       global $wgHooks;
+                       unset( $wgHooks['TitleExists']['TemplateSandbox'] );
+                       LinkCache::singleton()->clear();
+               } );
+       }
+
        function execute( $par ) {
                $this->setHeaders();
                $this->checkPermissions();
@@ -183,6 +214,7 @@
                $popts = $page->makeParserOptions( $this->getContext() );
                $popts->setIsPreview( true );
                $popts->setIsSectionPreview( false );
+               $fakePageExistsScopedCallback = $this->fakePageExists();
                $this->oldTemplateCallback = $popts->setTemplateCallback( 
array( $this, 'templateCallback' ) );
                $this->title = $title;
                $this->output = $content->getParserOutput( $title, 
$rev->getId(), $popts );
diff --git a/TemplateSandbox.hooks.php b/TemplateSandbox.hooks.php
index 6cc5a8f..ad964b5 100644
--- a/TemplateSandbox.hooks.php
+++ b/TemplateSandbox.hooks.php
@@ -53,6 +53,26 @@
        }
 
        /**
+        * @param Title $templatetitle
+        * @return ScopedCallback to clean up
+        */
+       private static function fakePageExists( $templatetitle ) {
+               global $wgHooks;
+               $wgHooks['TitleExists']['TemplateSandbox'] =
+                       function( $title, &$exists ) use( $templatetitle ) {
+                               if ( $templatetitle->equals( $title ) ) {
+                                       $exists = true;
+                               }
+                       };
+               LinkCache::singleton()->clearBadLink( 
$templatetitle->getPrefixedDBkey() );
+               return new ScopedCallback( function() use( $templatetitle ) {
+                       global $wgHooks;
+                       unset( $wgHooks['TitleExists']['TemplateSandbox'] );
+                       LinkCache::singleton()->clearLink( $templatetitle );
+               } );
+       }
+
+       /**
         * Hook for AlternateEditPreview to output an entirely different preview
         * when our button was clicked.
         *
@@ -148,6 +168,7 @@
                        TemplateSandboxHooks::$oldTemplateCallback = 
$popts->setTemplateCallback(
                                'TemplateSandboxHooks::templateCallback'
                        );
+                       $fakePageExistsScopedCallback = self::fakePageExists( 
$templatetitle );
                        $popts->enableLimitReport();
 
                        $rev = Revision::newFromTitle( $title );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I1828fb8d49943520b8af5b92a3e1f1ddde1a5625
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/TemplateSandbox
Gerrit-Branch: master
Gerrit-Owner: Jackmcbarn <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to