Cache redirects from Special:Redirect
authorBrian Wolff <bawolff+wn@gmail.com>
Fri, 1 Feb 2019 01:54:08 +0000 (01:54 +0000)
committerBrian Wolff <bawolff+wn@gmail.com>
Sun, 17 Feb 2019 10:32:57 +0000 (10:32 +0000)
People sometimes link these from high traffic places, so it is
important to cache in varnish.

File's with height can change so only cache that for 10 seconds.

Also change from 302 to 301.

Change-Id: I87a60c812cd1aa78a36359090c0cb8390be7183f

includes/specials/SpecialRedirect.php

index 1b2bda9..c4e4635 100644 (file)
@@ -119,7 +119,9 @@ class SpecialRedirect extends FormSpecialPage {
                        // ... and we can
                        if ( $mto && !$mto->isError() ) {
                                // ... change the URL to point to a thumbnail.
-                               $url = $mto->getUrl();
+                               // Note: This url is more temporary as can change
+                               // if file is reuploaded and has different aspect ratio.
+                               $url = [ $mto->getUrl(), $height === -1 ? 301 : 302 ];
                        }
                }
 
@@ -224,7 +226,21 @@ class SpecialRedirect extends FormSpecialPage {
                                break;
                }
                if ( $status && $status->isGood() ) {
-                       $this->getOutput()->redirect( $status->getValue() );
+                       // These urls can sometimes be linked from prominent places,
+                       // so varnish cache.
+                       $value = $status->getValue();
+                       if ( is_array( $value ) ) {
+                               list( $url, $code ) = $value;
+                       } else {
+                               $url = $value;
+                               $code = 301;
+                       }
+                       if ( $code === 301 ) {
+                               $this->getOutput()->setCdnMaxage( 60 * 60 );
+                       } else {
+                               $this->getOutput()->setCdnMaxage( 10 );
+                       }
+                       $this->getOutput()->redirect( $url, $code );
 
                        return true;
                }