Merge "Remove $purgeBlobs parameter from LocalisationCacheRecache hook"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 16 Jul 2019 00:13:53 +0000 (00:13 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 16 Jul 2019 00:13:53 +0000 (00:13 +0000)
RELEASE-NOTES-1.34
docs/hooks.txt
img_auth.php
tests/phpunit/includes/resourceloader/ResourceLoaderFilePathTest.php [new file with mode: 0644]
tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php

index 24809dd..1463764 100644 (file)
@@ -67,6 +67,8 @@ For notes on 1.33.x and older releases, see HISTORY.
   from other users originating from Special:EmailUser.
 
 === New developer features in 1.34 ===
+* The ImgAuthModifyHeaders hook was added to img_auth.php to allow modification
+  of headers in private wikis.
 * Language::formatTimePeriod now supports the new 'avoidhours' option to output
   strings like "5 days ago" instead of "5 days 13 hours ago".
 
index 3edd10d..36e0891 100644 (file)
@@ -1810,7 +1810,7 @@ $page: ImagePage object
 $page: ImagePage object
 &$toc: Array of <li> strings
 
-'ImgAuthBeforeStream': executed before file is streamed to user, but only when
+'ImgAuthBeforeStream': Executed before file is streamed to user, but only when
 using img_auth.php.
 &$title: the Title object of the file as it would appear for the upload page
 &$path: the original file and path name when img_auth was invoked by the web
@@ -1823,6 +1823,14 @@ using img_auth.php.
   $result[2 through n]=Parameters passed to body text message. Please note the
   header message cannot receive/use parameters.
 
+'ImgAuthModifyHeaders': Executed just before a file is streamed to a user via
+img_auth.php, allowing headers to be modified beforehand.
+$title: LinkTarget object
+&$headers: HTTP headers ( name => value, names are case insensitive ).
+  Two headers get special handling: If-Modified-Since (value must be
+  a valid HTTP date) and Range (must be of the form "bytes=(\d*-\d*)")
+  will be honored when streaming the file.
+
 'ImportHandleLogItemXMLTag': When parsing a XML tag in a log item.
 Return false to stop further processing of the tag
 $reader: XMLReader object
index 1434125..914014d 100644 (file)
@@ -138,12 +138,13 @@ function wfImageAuthMain() {
 
        $headers = []; // extra HTTP headers to send
 
+       $title = Title::makeTitleSafe( NS_FILE, $name );
+
        if ( !$publicWiki ) {
                // For private wikis, run extra auth checks and set cache control headers
-               $headers[] = 'Cache-Control: private';
-               $headers[] = 'Vary: Cookie';
+               $headers['Cache-Control'] = 'private';
+               $headers['Vary'] = 'Cookie';
 
-               $title = Title::makeTitleSafe( NS_FILE, $name );
                if ( !$title instanceof Title ) { // files have valid titles
                        wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
                        return;
@@ -167,19 +168,22 @@ function wfImageAuthMain() {
                }
        }
 
-       $options = []; // HTTP header options
        if ( isset( $_SERVER['HTTP_RANGE'] ) ) {
-               $options['range'] = $_SERVER['HTTP_RANGE'];
+               $headers['Range'] = $_SERVER['HTTP_RANGE'];
        }
        if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
-               $options['if-modified-since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
+               $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
        }
 
        if ( $request->getCheck( 'download' ) ) {
-               $headers[] = 'Content-Disposition: attachment';
+               $headers['Content-Disposition'] = 'attachment';
        }
 
+       // Allow modification of headers before streaming a file
+       Hooks::run( 'ImgAuthModifyHeaders', [ $title->getTitleValue(), &$headers ] );
+
        // Stream the requested file
+       list( $headers, $options ) = HTTPFileStreamer::preprocessHeaders( $headers );
        wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
        $repo->streamFileWithStatus( $filename, $headers, $options );
 }
diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderFilePathTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderFilePathTest.php
new file mode 100644 (file)
index 0000000..292340b
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+class ResourceLoaderFilePathTest extends PHPUnit\Framework\TestCase {
+       /**
+        * @covers ResourceLoaderFilePath::__construct
+        */
+       public function testConstructor() {
+               $resourceLoaderFilePath = new ResourceLoaderFilePath(
+                       'dummy/path', 'localBasePath', 'remoteBasePath'
+               );
+
+               $this->assertInstanceOf( ResourceLoaderFilePath::class, $resourceLoaderFilePath );
+       }
+
+       /**
+        * @covers ResourceLoaderFilePath::getLocalPath
+        */
+       public function testGetLocalPath() {
+               $resourceLoaderFilePath = new ResourceLoaderFilePath(
+                       'dummy/path', 'localBasePath', 'remoteBasePath'
+               );
+
+               $this->assertSame(
+                       'localBasePath/dummy/path', $resourceLoaderFilePath->getLocalPath()
+               );
+       }
+
+       /**
+        * @covers ResourceLoaderFilePath::getRemotePath
+        */
+       public function testGetRemotePath() {
+               $resourceLoaderFilePath = new ResourceLoaderFilePath(
+                       'dummy/path', 'localBasePath', 'remoteBasePath'
+               );
+
+               $this->assertSame(
+                       'remoteBasePath/dummy/path', $resourceLoaderFilePath->getRemotePath()
+               );
+       }
+
+       /**
+        * @covers ResourceLoaderFilePath::getPath
+        */
+       public function testGetPath() {
+               $resourceLoaderFilePath = new ResourceLoaderFilePath(
+                       'dummy/path', 'localBasePath', 'remoteBasePath'
+               );
+
+               $this->assertSame(
+                       'dummy/path', $resourceLoaderFilePath->getPath()
+               );
+       }
+}
index e8a0884..5964915 100644 (file)
@@ -4,10 +4,12 @@ use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\TestingAccessWrapper;
 
+/**
+ * @covers ResourceLoaderWikiModule
+ */
 class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
 
        /**
-        * @covers ResourceLoaderWikiModule::__construct
         * @dataProvider provideConstructor
         */
        public function testConstructor( $params ) {
@@ -15,6 +17,13 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                $this->assertInstanceOf( ResourceLoaderWikiModule::class, $module );
        }
 
+       public static function provideConstructor() {
+               yield 'null' => [ null ];
+               yield 'empty' => [ [] ];
+               yield 'unknown settings' => [ [ 'foo' => 'baz' ] ];
+               yield 'real settings' => [ [ 'MediaWiki:Common.js' ] ];
+       }
+
        private function prepareTitleInfo( array $mockInfo ) {
                $module = TestingAccessWrapper::newFromClass( ResourceLoaderWikiModule::class );
                $info = [];
@@ -24,21 +33,8 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                return $info;
        }
 
-       public static function provideConstructor() {
-               return [
-                       // Nothing
-                       [ null ],
-                       [ [] ],
-                       // Unrecognized settings
-                       [ [ 'foo' => 'baz' ] ],
-                       // Real settings
-                       [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
-               ];
-       }
-
        /**
         * @dataProvider provideGetPages
-        * @covers ResourceLoaderWikiModule::getPages
         */
        public function testGetPages( $params, Config $config, $expected ) {
                $module = new ResourceLoaderWikiModule( $params );
@@ -48,7 +44,7 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                $getPages = new ReflectionMethod( $module, 'getPages' );
                $getPages->setAccessible( true );
                $out = $getPages->invoke( $module, ResourceLoaderContext::newDummyContext() );
-               $this->assertEquals( $expected, $out );
+               $this->assertSame( $expected, $out );
        }
 
        public static function provideGetPages() {
@@ -84,98 +80,131 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
        }
 
        /**
-        * @covers ResourceLoaderWikiModule::getGroup
         * @dataProvider provideGetGroup
         */
        public function testGetGroup( $params, $expected ) {
                $module = new ResourceLoaderWikiModule( $params );
-               $this->assertEquals( $expected, $module->getGroup() );
+               $this->assertSame( $expected, $module->getGroup() );
        }
 
        public static function provideGetGroup() {
-               return [
-                       // No group specified
-                       [ [], null ],
-                       // A random group
-                       [ [ 'group' => 'foobar' ], 'foobar' ],
+               yield 'no group' => [ [], null ];
+               yield 'some group' => [ [ 'group' => 'foobar' ], 'foobar' ];
+       }
+
+       /**
+        * @dataProvider provideGetType
+        */
+       public function testGetType( $params, $expected ) {
+               $module = new ResourceLoaderWikiModule( $params );
+               $this->assertSame( $expected, $module->getType() );
+       }
+
+       public static function provideGetType() {
+               yield 'empty' => [
+                       [],
+                       ResourceLoaderWikiModule::LOAD_GENERAL,
+               ];
+               yield 'scripts' => [
+                       [ 'scripts' => [ 'Example.js' ] ],
+                       ResourceLoaderWikiModule::LOAD_GENERAL,
+               ];
+               yield 'styles' => [
+                       [ 'styles' => [ 'Example.css' ] ],
+                       ResourceLoaderWikiModule::LOAD_STYLES,
+               ];
+               yield 'styles and scripts' => [
+                       [ 'styles' => [ 'Example.css' ], 'scripts' => [ 'Example.js' ] ],
+                       ResourceLoaderWikiModule::LOAD_GENERAL,
                ];
        }
 
        /**
-        * @covers ResourceLoaderWikiModule::isKnownEmpty
         * @dataProvider provideIsKnownEmpty
         */
        public function testIsKnownEmpty( $titleInfo, $group, $dependencies, $expected ) {
                $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
-                       ->setMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
-                       ->getMock();
-               $module->expects( $this->any() )
-                       ->method( 'getTitleInfo' )
-                       ->will( $this->returnValue( $this->prepareTitleInfo( $titleInfo ) ) );
-               $module->expects( $this->any() )
-                       ->method( 'getGroup' )
-                       ->will( $this->returnValue( $group ) );
-               $module->expects( $this->any() )
-                       ->method( 'getDependencies' )
-                       ->will( $this->returnValue( $dependencies ) );
-               $context = $this->getMockBuilder( ResourceLoaderContext::class )
                        ->disableOriginalConstructor()
+                       ->setMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
                        ->getMock();
-               $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
+               $module->method( 'getTitleInfo' )
+                       ->willReturn( $this->prepareTitleInfo( $titleInfo ) );
+               $module->method( 'getGroup' )
+                       ->willReturn( $group );
+               $module->method( 'getDependencies' )
+                       ->willReturn( $dependencies );
+               $context = $this->createMock( ResourceLoaderContext::class );
+               $this->assertSame( $expected, $module->isKnownEmpty( $context ) );
        }
 
        public static function provideIsKnownEmpty() {
-               return [
-                       // No valid pages
-                       [ [], 'test1', [], true ],
-                       // 'site' module with a non-empty page
-                       [
-                               [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
-                               'site',
-                               [],
-                               false,
-                       ],
-                       // 'site' module without existing pages but dependencies
-                       [
-                               [],
-                               'site',
-                               [ 'mobile.css' ],
-                               false,
-                       ],
-                       // 'site' module which is empty but has dependencies
-                       [
-                               [ 'MediaWiki:Common.js' => [ 'page_len' => 0 ] ],
-                               'site',
-                               [ 'mobile.css' ],
-                               false,
-                       ],
-                       // 'site' module with an empty page
-                       [
-                               [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
-                               'site',
-                               [],
-                               false,
-                       ],
-                       // 'user' module with a non-empty page
-                       [
-                               [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
-                               'user',
-                               [],
-                               false,
-                       ],
-                       // 'user' module with an empty page
-                       [
-                               [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
-                               'user',
-                               [],
-                               true,
-                       ],
+               yield 'nothing' => [
+                       [],
+                       null,
+                       [],
+                       // No pages exist, considered empty.
+                       true,
+               ];
+
+               yield 'an empty page exists (no group)' => [
+                       [ 'Project:Example/foo.js' => [ 'page_len' => 0 ] ],
+                       null,
+                       [],
+                       // There is an existing page, so we should let the module be queued.
+                       // Its emptiness might be temporary, hence considered non-empty (T70488).
+                       false,
+               ];
+               yield 'an empty page exists (site group)' => [
+                       [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
+                       'site',
+                       [],
+                       // There is an existing page, hence considered non-empty.
+                       false,
+               ];
+               yield 'an empty page exists (user group)' => [
+                       [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
+                       'user',
+                       [],
+                       // There is an existing page, but it is empty.
+                       // For user-specific modules, don't bother loading a known-empty module.
+                       // Given user-specific HTML output, this will vary and re-appear if/when
+                       // the page becomes non-empty again.
+                       true,
+               ];
+
+               yield 'no pages but having dependencies (no group)' => [
+                       [],
+                       null,
+                       [ 'another-module' ],
+                       false,
+               ];
+               yield 'no pages but having dependencies (site group)' => [
+                       [],
+                       'site',
+                       [ 'another-module' ],
+                       false,
+               ];
+               yield 'no pages but having dependencies (user group)' => [
+                       [],
+                       'user',
+                       [ 'another-module' ],
+                       false,
+               ];
+
+               yield 'a non-empty page exists (user group)' => [
+                       [ 'User:Example/foo.js' => [ 'page_len' => 25 ] ],
+                       'user',
+                       [],
+                       false,
+               ];
+               yield 'a non-empty page exists (site group)' => [
+                       [ 'MediaWiki:Foo.js' => [ 'page_len' => 25 ] ],
+                       'site',
+                       [],
+                       false,
                ];
        }
 
-       /**
-        * @covers ResourceLoaderWikiModule::getTitleInfo
-        */
        public function testGetTitleInfo() {
                $pages = [
                        'MediaWiki:Common.css' => [ 'type' => 'styles' ],
@@ -187,26 +216,20 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                ] );
                $expected = $titleInfo;
 
-               $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
-                       ->setMethods( [ 'getPages' ] )
+               $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
+                       ->setMethods( [ 'getPages', 'getTitleInfo' ] )
                        ->getMock();
                $module->method( 'getPages' )->willReturn( $pages );
-               // Can't mock static methods
-               $module::$returnFetchTitleInfo = $titleInfo;
+               $module->method( 'getTitleInfo' )->willReturn( $titleInfo );
 
                $context = $this->getMockBuilder( ResourceLoaderContext::class )
                        ->disableOriginalConstructor()
                        ->getMock();
 
                $module = TestingAccessWrapper::newFromObject( $module );
-               $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
+               $this->assertSame( $expected, $module->getTitleInfo( $context ), 'Title info' );
        }
 
-       /**
-        * @covers ResourceLoaderWikiModule::getTitleInfo
-        * @covers ResourceLoaderWikiModule::setTitleInfo
-        * @covers ResourceLoaderWikiModule::preloadTitleInfo
-        */
        public function testGetPreloadedTitleInfo() {
                $pages = [
                        'MediaWiki:Common.css' => [ 'type' => 'styles' ],
@@ -240,17 +263,14 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                );
                TestResourceLoaderWikiModule::preloadTitleInfo(
                        $context,
-                       wfGetDB( DB_REPLICA ),
+                       $this->createMock( IDatabase::class ),
                        [ 'testmodule' ]
                );
 
                $module = TestingAccessWrapper::newFromObject( $module );
-               $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
+               $this->assertSame( $expected, $module->getTitleInfo( $context ), 'Title info' );
        }
 
-       /**
-        * @covers ResourceLoaderWikiModule::preloadTitleInfo
-        */
        public function testGetPreloadedBadTitle() {
                // Set up
                TestResourceLoaderWikiModule::$returnFetchTitleInfo = [];
@@ -267,7 +287,7 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                // Act
                TestResourceLoaderWikiModule::preloadTitleInfo(
                        $context,
-                       wfGetDB( DB_REPLICA ),
+                       $this->createMock( IDatabase::class ),
                        [ 'testmodule' ]
                );
 
@@ -276,58 +296,52 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                $this->assertSame( [], $module->getTitleInfo( $context ), 'Title info' );
        }
 
-       /**
-        * @covers ResourceLoaderWikiModule::preloadTitleInfo
-        */
        public function testGetPreloadedTitleInfoEmpty() {
                $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
-               // Covers early return
+               // This covers the early return case
                $this->assertSame(
                        null,
                        ResourceLoaderWikiModule::preloadTitleInfo(
                                $context,
-                               wfGetDB( DB_REPLICA ),
+                               $this->createMock( IDatabase::class ),
                                []
                        )
                );
        }
 
        public static function provideGetContent() {
-               return [
-                       'Bad title' => [ null, '[x]' ],
-                       'Dead redirect' => [ null, [
-                               'text' => 'Dead redirect',
-                               'title' => 'Dead_redirect',
-                               'redirect' => 1,
-                       ] ],
-                       'Bad content model' => [ null, [
-                               'text' => 'MediaWiki:Wikitext',
-                               'ns' => NS_MEDIAWIKI,
-                               'title' => 'Wikitext',
-                       ] ],
-                       'No JS content found' => [ null, [
-                               'text' => 'MediaWiki:Script.js',
-                               'ns' => NS_MEDIAWIKI,
-                               'title' => 'Script.js',
-                       ] ],
-                       'No CSS content found' => [ null, [
-                               'text' => 'MediaWiki:Styles.css',
-                               'ns' => NS_MEDIAWIKI,
-                               'title' => 'Script.css',
-                       ] ],
-               ];
+               yield 'Bad title' => [ null, '[x]' ];
+               yield 'Dead redirect' => [ null, [
+                       'text' => 'Dead redirect',
+                       'title' => 'Dead_redirect',
+                       'redirect' => 1,
+               ] ];
+               yield 'Bad content model' => [ null, [
+                       'text' => 'MediaWiki:Wikitext',
+                       'ns' => NS_MEDIAWIKI,
+                       'title' => 'Wikitext',
+               ] ];
+               yield 'No JS content found' => [ null, [
+                       'text' => 'MediaWiki:Script.js',
+                       'ns' => NS_MEDIAWIKI,
+                       'title' => 'Script.js',
+               ] ];
+               yield 'No CSS content found' => [ null, [
+                       'text' => 'MediaWiki:Styles.css',
+                       'ns' => NS_MEDIAWIKI,
+                       'title' => 'Script.css',
+               ] ];
        }
 
        /**
-        * @covers ResourceLoaderWikiModule::getContent
         * @dataProvider provideGetContent
         */
        public function testGetContent( $expected, $title ) {
                $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
                $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
                        ->setMethods( [ 'getContentObj' ] )->getMock();
-               $module->expects( $this->any() )
-                       ->method( 'getContentObj' )->willReturn( null );
+               $module->method( 'getContentObj' )
+                       ->willReturn( null );
 
                if ( is_array( $title ) ) {
                        $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
@@ -344,23 +358,18 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                }
 
                $module = TestingAccessWrapper::newFromObject( $module );
-               $this->assertEquals(
+               $this->assertSame(
                        $expected,
                        $module->getContent( $titleText, $context )
                );
        }
 
-       /**
-        * @covers ResourceLoaderWikiModule::getContent
-        * @covers ResourceLoaderWikiModule::getContentObj
-        * @covers ResourceLoaderWikiModule::shouldEmbedModule
-        */
        public function testContentOverrides() {
                $pages = [
                        'MediaWiki:Common.css' => [ 'type' => 'style' ],
                ];
 
-               $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
+               $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
                        ->setMethods( [ 'getPages' ] )
                        ->getMock();
                $module->method( 'getPages' )->willReturn( $pages );
@@ -377,7 +386,7 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                } );
 
                $this->assertTrue( $module->shouldEmbedModule( $context ) );
-               $this->assertEquals( [
+               $this->assertSame( [
                        'all' => [
                                "/*\nMediaWiki:Common.css\n*/\n.override{}"
                        ]
@@ -392,10 +401,6 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                $this->assertFalse( $module->shouldEmbedModule( $context ) );
        }
 
-       /**
-        * @covers ResourceLoaderWikiModule::getContent
-        * @covers ResourceLoaderWikiModule::getContentObj
-        */
        public function testGetContentForRedirects() {
                // Set up context and module object
                $context = new DerivativeResourceLoaderContext(
@@ -404,11 +409,10 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
                        ->setMethods( [ 'getPages' ] )
                        ->getMock();
-               $module->expects( $this->any() )
-                       ->method( 'getPages' )
-                       ->will( $this->returnValue( [
+               $module->method( 'getPages' )
+                       ->willReturn( [
                                'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
-                       ] ) );
+                       ] );
                $context->setContentOverrideCallback( function ( Title $title ) {
                        if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
                                $handler = new JavaScriptContentHandler();
@@ -430,14 +434,14 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                        1 // redirect
                );
 
-               $this->assertEquals(
+               $this->assertSame(
                        "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
                        $module->getScript( $context ),
                        'Redirect resolved by getContent'
                );
        }
 
-       function tearDown() {
+       public function tearDown() {
                Title::clearCaches();
                parent::tearDown();
        }