Legoktm has uploaded a new change for review.

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

Change subject: Implement a read-only mode that prevents creation of new short 
URLs
......................................................................

Implement a read-only mode that prevents creation of new short URLs

We would like to get this extension deployed to Wikimedia sites so we
can prepare some surrounding infrastructure like Apache, Varnish, dumps,
etc., which should all be easier once the extension already exists.

This will allow us to install the extension but not allow any short
codes to be created in the database.

Change-Id: I648071bcb34259bb44b4d9a62f57d4c5bfdaac29
---
M ApiShortenUrl.php
M README
M SpecialUrlShortener.php
M UrlShortener.hooks.php
M UrlShortener.utils.php
M extension.json
M i18n/en.json
M i18n/qqq.json
8 files changed, 56 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UrlShortener 
refs/changes/26/276826/1

diff --git a/ApiShortenUrl.php b/ApiShortenUrl.php
index 55d769a..8f8631e 100644
--- a/ApiShortenUrl.php
+++ b/ApiShortenUrl.php
@@ -11,6 +11,12 @@
 class ApiShortenUrl extends ApiBase {
 
        public function execute() {
+               global $wgUrlShortenerReadOnly;
+
+               if ( $wgUrlShortenerReadOnly ) {
+                       $this->dieUsage( 'No new short urls may be created', 
'urlshortener-disabled' );
+               }
+
                $params = $this->extractRequestParams();
 
                $url = $params['url'];
diff --git a/README b/README
index 58faf97..93d014f 100644
--- a/README
+++ b/README
@@ -105,6 +105,12 @@
 $wgUrlShortenerIdSet = 
'023456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz$-_.!';
 </source>
 
+=== Read-only mode ===
+
+Set $wgUrlShortenerReadOnly to true to prevent users from creating new
+short URLs. This is mainly intended as a hack while deploying to Wikimedia 
sites
+and will be removed once it is no longer needed.
+
 == License ==
 
 © 2014 Yuvaraj Pandian ([email protected]) under the WTFPL, see the COPYING
diff --git a/SpecialUrlShortener.php b/SpecialUrlShortener.php
index 9db5119..7e999cf 100644
--- a/SpecialUrlShortener.php
+++ b/SpecialUrlShortener.php
@@ -20,6 +20,17 @@
                parent::__construct( 'UrlShortener' );
        }
 
+       public function execute( $par ) {
+               global $wgUrlShortenerReadOnly;
+
+               if ( $wgUrlShortenerReadOnly ) {
+                       $this->setHeaders();
+                       $this->getOutput()->addWikiMsg( 'urlshortener-disabled' 
);
+               } else {
+                       parent::execute( $par );
+               }
+       }
+
        protected function getDisplayFormat() {
                return 'ooui';
        }
@@ -126,4 +137,15 @@
        protected function getGroupName() {
                return 'pagetools';
        }
+
+       /**
+        * Don't list this page if in read only mode
+        *
+        * @return bool
+        */
+       public function isListed() {
+               global $wgUrlShortenerReadOnly;
+
+               return !$wgUrlShortenerReadOnly;
+       }
 }
diff --git a/UrlShortener.hooks.php b/UrlShortener.hooks.php
index c9f97c3..f7a4865 100644
--- a/UrlShortener.hooks.php
+++ b/UrlShortener.hooks.php
@@ -27,7 +27,11 @@
        }
 
        public static function onBeforePageDisplay( OutputPage $out ) {
-               $out->addModules( 'ext.urlShortener.toolbar' );
+               global $wgUrlShortenerReadOnly;
+
+               if ( !$wgUrlShortenerReadOnly ) {
+                       $out->addModules( 'ext.urlShortener.toolbar' );
+               }
        }
 
        /**
@@ -37,6 +41,12 @@
         * @param array $toolbox
         */
        public static function onBaseTemplateToolbox( BaseTemplate $template, 
array &$toolbox ) {
+               global $wgUrlShortenerReadOnly;
+
+               if ( $wgUrlShortenerReadOnly ) {
+                       return;
+               }
+
                $skin = $template->getSkin();
                if ( $skin->getTitle()->isSpecial( 'UrlShortener' ) ) {
                        return;
diff --git a/UrlShortener.utils.php b/UrlShortener.utils.php
index ae057e3..166a450 100755
--- a/UrlShortener.utils.php
+++ b/UrlShortener.utils.php
@@ -53,6 +53,13 @@
                                return Status::newFatal( 
'urlshortener-ratelimit' );
                        }
 
+                       global $wgUrlShortenerReadOnly;
+                       if ( $wgUrlShortenerReadOnly ) {
+                               // All code paths should already have checked 
for this,
+                               // but lets be on the safe side.
+                               return Status::newFatal( 
'urlshortener-disabled' );
+                       }
+
                        $rowData = array(
                                'usc_url' => $url,
                                'usc_url_hash' => md5( $url )
diff --git a/extension.json b/extension.json
index 394a220..8c622a3 100644
--- a/extension.json
+++ b/extension.json
@@ -104,7 +104,8 @@
                "UrlShortenerIdSet": 
"023456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz$-_.!",
                "UrlShortenerServer": false,
                "UrlShortenerTemplate": false,
-               "UrlShortenerAllowArbitraryPorts": false
+               "UrlShortenerAllowArbitraryPorts": false,
+               "UrlShortenerReadOnly": true
        },
        "manifest_version": 1
 }
diff --git a/i18n/en.json b/i18n/en.json
index 7083726..d519683 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -21,6 +21,7 @@
        "urlshortener-error-badports": "URLs that contain ports are not allowed 
to be shortened",
        "urlshortener-error-nouserpass": "URLs that contain a username or 
password are not allowed to be shortened",
        "urlshortener-failed-try-again": "Failed. Try again?",
+       "urlshortener-disabled": "Creating new short URLs is temporarily 
disabled.",
        "apihelp-shortenurl-description": "Shorten a long URL into a shorter 
one.",
        "apihelp-shortenurl-param-url": "URL to be shortened.",
        "apihelp-shortenurl-example-1": "Get the short URL for 
<kbd><nowiki>https://en.wikipedia.org/wiki/Arctica</nowiki></kbd>."
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 7ef6818..4ff39e4 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -22,6 +22,7 @@
        "urlshortener-error-badports": "Error message shown when the URL cannot 
be shortened because it contains a port (e.g. http://example.org:90/path)",
        "urlshortener-error-nouserpass": "Error message shown when the URL 
cannot be shortened because it contains a username or password (e.g. 
http://user:[email protected]/)",
        "urlshortener-failed-try-again": "Generic failure message with option 
to try again",
+       "urlshortener-disabled": "Error message shown when the extension is 
configured in read-only mode.",
        "apihelp-shortenurl-description": 
"{{doc-apihelp-description|shortenurl}}",
        "apihelp-shortenurl-param-url": "{{doc-apihelp-param|shortenurl|url}}",
        "apihelp-shortenurl-example-1": "{{doc-apihelp-example|shortenurl}}"

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I648071bcb34259bb44b4d9a62f57d4c5bfdaac29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UrlShortener
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

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

Reply via email to