Merge "Normalize PHPDoc attributes"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Sun, 18 Mar 2018 05:19:04 +0000 (05:19 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sun, 18 Mar 2018 05:19:04 +0000 (05:19 +0000)
16 files changed:
includes/GlobalFunctions.php
includes/Storage/RevisionStore.php
includes/WebRequest.php
includes/api/ApiFormatBase.php
includes/api/ApiQuerySiteinfo.php
includes/debug/MWDebug.php
includes/libs/Timing.php
maintenance/Maintenance.php
maintenance/rebuildFileCache.php
tests/common/TestsAutoLoader.php
tests/parser/ParserTestRunner.php
tests/phpunit/includes/WebRequestTest.php
tests/phpunit/includes/api/ApiTestCaseUpload.php
tests/phpunit/includes/api/ApiUploadTest.php
tests/phpunit/includes/api/ApiUploadTestCase.php [new file with mode: 0644]
tests/phpunit/includes/libs/IPTest.php

index 1d61996..7a5a5d8 100644 (file)
@@ -1050,7 +1050,7 @@ function wfMatchesDomainList( $url, $domains ) {
  */
 function wfDebug( $text, $dest = 'all', array $context = [] ) {
        global $wgDebugRawPage, $wgDebugLogPrefix;
-       global $wgDebugTimestamps, $wgRequestTime;
+       global $wgDebugTimestamps;
 
        if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
                return;
@@ -1061,7 +1061,7 @@ function wfDebug( $text, $dest = 'all', array $context = [] ) {
        if ( $wgDebugTimestamps ) {
                $context['seconds_elapsed'] = sprintf(
                        '%6.4f',
-                       microtime( true ) - $wgRequestTime
+                       microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT']
                );
                $context['memory_used'] = sprintf(
                        '%5.1fM',
@@ -1514,9 +1514,11 @@ function wfHostname() {
  * @return string
  */
 function wfReportTime() {
-       global $wgRequestTime, $wgShowHostnames;
+       global $wgShowHostnames;
 
-       $responseTime = round( ( microtime( true ) - $wgRequestTime ) * 1000 );
+       $elapsed = ( microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'] );
+       // seconds to milliseconds
+       $responseTime = round( $elapsed * 1000 );
        $reportVars = [ 'wgBackendResponseTime' => $responseTime ];
        if ( $wgShowHostnames ) {
                $reportVars['wgHostname'] = wfHostname();
index 98ad287..2e3103c 100644 (file)
@@ -1044,9 +1044,10 @@ class RevisionStore
         * @return RevisionRecord|null
         */
        public function getRevisionByTimestamp( $title, $timestamp ) {
+               $db = $this->getDBConnection( DB_REPLICA );
                return $this->newRevisionFromConds(
                        [
-                               'rev_timestamp' => $timestamp,
+                               'rev_timestamp' => $db->timestamp( $timestamp ),
                                'page_namespace' => $title->getNamespace(),
                                'page_title' => $title->getDBkey()
                        ],
index 0a7f416..26e2d45 100644 (file)
@@ -88,8 +88,7 @@ class WebRequest {
         * @codeCoverageIgnore
         */
        public function __construct() {
-               $this->requestTime = isset( $_SERVER['REQUEST_TIME_FLOAT'] )
-                       ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true );
+               $this->requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
 
                // POST overrides GET data
                // We don't use $_REQUEST here to avoid interference from cookies...
index 18c36de..4b93b31 100644 (file)
@@ -298,7 +298,7 @@ abstract class ApiFormatBase extends ApiBase {
 
                        if ( $this->getIsWrappedHtml() ) {
                                // This is a special output mode mainly intended for ApiSandbox use
-                               $time = microtime( true ) - $this->getConfig()->get( 'RequestTime' );
+                               $time = $this->getMain()->getRequest()->getElapsedTime();
                                $json = FormatJson::encode(
                                        [
                                                'status' => (int)( $this->mHttpStatus ?: 200 ),
index f924736..3048273 100644 (file)
@@ -465,7 +465,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                                'host' => $showHostnames
                                                ? $lb->getServerName( $index )
                                                : '',
-                               'lag' => intval( $lag )
+                               'lag' => $lag
                        ];
                }
 
index 012837f..7479841 100644 (file)
@@ -517,7 +517,7 @@ class MWDebug {
                        return [];
                }
 
-               global $wgVersion, $wgRequestTime;
+               global $wgVersion;
                $request = $context->getRequest();
 
                // HHVM's reported memory usage from memory_get_peak_usage()
@@ -540,7 +540,7 @@ class MWDebug {
                        'gitRevision' => GitInfo::headSHA1(),
                        'gitBranch' => $branch,
                        'gitViewUrl' => GitInfo::headViewUrl(),
-                       'time' => microtime( true ) - $wgRequestTime,
+                       'time' => $request->getElapsedTime(),
                        'log' => self::$log,
                        'debugLog' => self::$debug,
                        'queries' => self::$query,
index 57c253d..7b1a914 100644 (file)
@@ -94,9 +94,7 @@ class Timing implements LoggerAwareInterface {
                                'requestStart' => [
                                        'name'      => 'requestStart',
                                        'entryType' => 'mark',
-                                       'startTime' => isset( $_SERVER['REQUEST_TIME_FLOAT'] )
-                                               ? $_SERVER['REQUEST_TIME_FLOAT']
-                                               : $_SERVER['REQUEST_TIME'],
+                                       'startTime' => $_SERVER['REQUEST_TIME_FLOAT'],
                                        'duration'  => 0,
                                ],
                        ];
index 1778a79..0c79bd5 100644 (file)
@@ -678,7 +678,7 @@ abstract class Maintenance {
         * Do some sanity checking and basic setup
         */
        public function setup() {
-               global $IP, $wgCommandLineMode, $wgRequestTime;
+               global $IP, $wgCommandLineMode;
 
                # Abort if called from a web server
                # wfIsCLI() is not available yet
@@ -715,8 +715,6 @@ abstract class Maintenance {
                # But sometimes this doesn't seem to be the case.
                ini_set( 'max_execution_time', 0 );
 
-               $wgRequestTime = microtime( true );
-
                # Define us as being in MediaWiki
                define( 'MEDIAWIKI', true );
 
index ecdec29..1f89426 100644 (file)
@@ -57,8 +57,6 @@ class RebuildFileCache extends Maintenance {
        }
 
        public function execute() {
-               global $wgRequestTime;
-
                if ( !$this->enabled ) {
                        $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
                }
@@ -90,7 +88,8 @@ class RebuildFileCache extends Maintenance {
                        $this->fatalError( "Nothing to do." );
                }
 
-               $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
+               // Mock request (hack, no real client)
+               $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
 
                # Do remaining chunk
                $end += $batchSize - 1;
@@ -141,22 +140,27 @@ class RebuildFileCache extends Maintenance {
                                        }
 
                                        Wikimedia\suppressWarnings(); // header notices
-                                       // Cache ?action=view
-                                       $wgRequestTime = microtime( true ); # T24852
+
+                                       // 1. Cache ?action=view
+                                       // Be sure to reset the mocked request time (T24852)
+                                       $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
                                        ob_start();
                                        $article->view();
                                        $context->getOutput()->output();
                                        $context->getOutput()->clearHTML();
                                        $viewHtml = ob_get_clean();
                                        $viewCache->saveToFileCache( $viewHtml );
-                                       // Cache ?action=history
-                                       $wgRequestTime = microtime( true ); # T24852
+
+                                       // 2. Cache ?action=history
+                                       // Be sure to reset the mocked request time (T24852)
+                                       $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
                                        ob_start();
                                        Action::factory( 'history', $article, $context )->show();
                                        $context->getOutput()->output();
                                        $context->getOutput()->clearHTML();
                                        $historyHtml = ob_get_clean();
                                        $historyCache->saveToFileCache( $historyHtml );
+
                                        Wikimedia\restoreWarnings();
 
                                        if ( $rebuilt ) {
index a777153..b994f8a 100644 (file)
@@ -77,6 +77,7 @@ $wgAutoloadClasses += [
        'ApiTestCase' => "$testDir/phpunit/includes/api/ApiTestCase.php",
        'ApiTestCaseUpload' => "$testDir/phpunit/includes/api/ApiTestCaseUpload.php",
        'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestContext.php",
+       'ApiUploadTestCase' => "$testDir/phpunit/includes/api/ApiUploadTestCase.php",
        'MockApi' => "$testDir/phpunit/includes/api/MockApi.php",
        'MockApiQueryBase' => "$testDir/phpunit/includes/api/MockApiQueryBase.php",
        'UserWrapper' => "$testDir/phpunit/includes/api/UserWrapper.php",
index a03f969..f0c815f 100644 (file)
@@ -636,7 +636,6 @@ class ParserTestRunner {
 
        /**
         * Remove last character if it is a newline
-        * @group utility
         * @param string $s
         * @return string
         */
index 936f4f5..9583921 100644 (file)
@@ -26,7 +26,7 @@ class WebRequestTest extends MediaWikiTestCase {
        public function testDetectServer( $expected, $input, $description ) {
                $this->setMwGlobals( 'wgAssumeProxiesUseDefaultProtocolPorts', true );
 
-               $_SERVER = $input;
+               $this->setServerVars( $input );
                $result = WebRequest::detectServer();
                $this->assertEquals( $expected, $result, $description );
        }
@@ -363,7 +363,7 @@ class WebRequestTest extends MediaWikiTestCase {
         * @covers WebRequest::getIP
         */
        public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
-               $_SERVER = $input;
+               $this->setServerVars( $input );
                $this->setMwGlobals( [
                        'wgUsePrivateIPs' => $private,
                        'wgHooks' => [
@@ -608,8 +608,19 @@ class WebRequestTest extends MediaWikiTestCase {
         * @covers WebRequest::getAcceptLang
         */
        public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
-               $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
+               $this->setServerVars( [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ] );
                $request = new WebRequest();
                $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
        }
+
+       protected function setServerVars( $vars ) {
+               // Don't remove vars which should be available in all SAPI.
+               if ( !isset( $vars['REQUEST_TIME_FLOAT'] ) ) {
+                       $vars['REQUEST_TIME_FLOAT'] = $_SERVER['REQUEST_TIME_FLOAT'];
+               }
+               if ( !isset( $vars['REQUEST_TIME'] ) ) {
+                       $vars['REQUEST_TIME'] = $_SERVER['REQUEST_TIME'];
+               }
+               $_SERVER = $vars;
+       }
 }
index f15da2e..3670fad 100644 (file)
@@ -1,153 +1,8 @@
 <?php
 
 /**
- * Abstract class to support upload tests
+ * For backward compatibility since 1.31
  */
-abstract class ApiTestCaseUpload extends ApiTestCase {
-       /**
-        * Fixture -- run before every test
-        */
-       protected function setUp() {
-               parent::setUp();
+abstract class ApiTestCaseUpload extends ApiUploadTestCase {
 
-               $this->setMwGlobals( [
-                       'wgEnableUploads' => true,
-                       'wgEnableAPI' => true,
-               ] );
-
-               $this->clearFakeUploads();
-       }
-
-       /**
-        * Helper function -- remove files and associated articles by Title
-        *
-        * @param Title $title Title to be removed
-        *
-        * @return bool
-        */
-       public function deleteFileByTitle( $title ) {
-               if ( $title->exists() ) {
-                       $file = wfFindFile( $title, [ 'ignoreRedirect' => true ] );
-                       $noOldArchive = ""; // yes this really needs to be set this way
-                       $comment = "removing for test";
-                       $restrictDeletedVersions = false;
-                       $status = FileDeleteForm::doDelete(
-                               $title,
-                               $file,
-                               $noOldArchive,
-                               $comment,
-                               $restrictDeletedVersions
-                       );
-
-                       if ( !$status->isGood() ) {
-                               return false;
-                       }
-
-                       $page = WikiPage::factory( $title );
-                       $page->doDeleteArticle( "removing for test" );
-
-                       // see if it now doesn't exist; reload
-                       $title = Title::newFromText( $title->getText(), NS_FILE );
-               }
-
-               return !( $title && $title instanceof Title && $title->exists() );
-       }
-
-       /**
-        * Helper function -- remove files and associated articles with a particular filename
-        *
-        * @param string $fileName Filename to be removed
-        *
-        * @return bool
-        */
-       public function deleteFileByFileName( $fileName ) {
-               return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
-       }
-
-       /**
-        * Helper function -- given a file on the filesystem, find matching
-        * content in the db (and associated articles) and remove them.
-        *
-        * @param string $filePath Path to file on the filesystem
-        *
-        * @return bool
-        */
-       public function deleteFileByContent( $filePath ) {
-               $hash = FSFile::getSha1Base36FromPath( $filePath );
-               $dupes = RepoGroup::singleton()->findBySha1( $hash );
-               $success = true;
-               foreach ( $dupes as $dupe ) {
-                       $success &= $this->deleteFileByTitle( $dupe->getTitle() );
-               }
-
-               return $success;
-       }
-
-       /**
-        * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
-        * (This is what PHP would normally do).
-        *
-        * @param string $fieldName Name this would have in the upload form
-        * @param string $fileName Name to title this
-        * @param string $type MIME type
-        * @param string $filePath Path where to find file contents
-        *
-        * @throws Exception
-        * @return bool
-        */
-       function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
-               $tmpName = $this->getNewTempFile();
-               if ( !file_exists( $filePath ) ) {
-                       throw new Exception( "$filePath doesn't exist!" );
-               }
-
-               if ( !copy( $filePath, $tmpName ) ) {
-                       throw new Exception( "couldn't copy $filePath to $tmpName" );
-               }
-
-               clearstatcache();
-               $size = filesize( $tmpName );
-               if ( $size === false ) {
-                       throw new Exception( "couldn't stat $tmpName" );
-               }
-
-               $_FILES[$fieldName] = [
-                       'name' => $fileName,
-                       'type' => $type,
-                       'tmp_name' => $tmpName,
-                       'size' => $size,
-                       'error' => null
-               ];
-
-               return true;
-       }
-
-       function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
-               $tmpName = $this->getNewTempFile();
-               // copy the chunk data to temp location:
-               if ( !file_put_contents( $tmpName, $chunkData ) ) {
-                       throw new Exception( "couldn't copy chunk data to $tmpName" );
-               }
-
-               clearstatcache();
-               $size = filesize( $tmpName );
-               if ( $size === false ) {
-                       throw new Exception( "couldn't stat $tmpName" );
-               }
-
-               $_FILES[$fieldName] = [
-                       'name' => $fileName,
-                       'type' => $type,
-                       'tmp_name' => $tmpName,
-                       'size' => $size,
-                       'error' => null
-               ];
-       }
-
-       /**
-        * Remove traces of previous fake uploads
-        */
-       function clearFakeUploads() {
-               $_FILES = [];
-       }
 }
index 345f196..41c9aed 100644 (file)
@@ -22,7 +22,7 @@
  *
  * @covers ApiUpload
  */
-class ApiUploadTest extends ApiTestCaseUpload {
+class ApiUploadTest extends ApiUploadTestCase {
        /**
         * Testing login
         * XXX this is a funny way of getting session context
diff --git a/tests/phpunit/includes/api/ApiUploadTestCase.php b/tests/phpunit/includes/api/ApiUploadTestCase.php
new file mode 100644 (file)
index 0000000..3c7efd5
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+
+/**
+ * Abstract class to support upload tests
+ */
+abstract class ApiUploadTestCase extends ApiTestCase {
+       /**
+        * Fixture -- run before every test
+        */
+       protected function setUp() {
+               parent::setUp();
+
+               $this->setMwGlobals( [
+                       'wgEnableUploads' => true,
+                       'wgEnableAPI' => true,
+               ] );
+
+               $this->clearFakeUploads();
+       }
+
+       /**
+        * Helper function -- remove files and associated articles by Title
+        *
+        * @param Title $title Title to be removed
+        *
+        * @return bool
+        */
+       public function deleteFileByTitle( $title ) {
+               if ( $title->exists() ) {
+                       $file = wfFindFile( $title, [ 'ignoreRedirect' => true ] );
+                       $noOldArchive = ""; // yes this really needs to be set this way
+                       $comment = "removing for test";
+                       $restrictDeletedVersions = false;
+                       $status = FileDeleteForm::doDelete(
+                               $title,
+                               $file,
+                               $noOldArchive,
+                               $comment,
+                               $restrictDeletedVersions
+                       );
+
+                       if ( !$status->isGood() ) {
+                               return false;
+                       }
+
+                       $page = WikiPage::factory( $title );
+                       $page->doDeleteArticle( "removing for test" );
+
+                       // see if it now doesn't exist; reload
+                       $title = Title::newFromText( $title->getText(), NS_FILE );
+               }
+
+               return !( $title && $title instanceof Title && $title->exists() );
+       }
+
+       /**
+        * Helper function -- remove files and associated articles with a particular filename
+        *
+        * @param string $fileName Filename to be removed
+        *
+        * @return bool
+        */
+       public function deleteFileByFileName( $fileName ) {
+               return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
+       }
+
+       /**
+        * Helper function -- given a file on the filesystem, find matching
+        * content in the db (and associated articles) and remove them.
+        *
+        * @param string $filePath Path to file on the filesystem
+        *
+        * @return bool
+        */
+       public function deleteFileByContent( $filePath ) {
+               $hash = FSFile::getSha1Base36FromPath( $filePath );
+               $dupes = RepoGroup::singleton()->findBySha1( $hash );
+               $success = true;
+               foreach ( $dupes as $dupe ) {
+                       $success &= $this->deleteFileByTitle( $dupe->getTitle() );
+               }
+
+               return $success;
+       }
+
+       /**
+        * Fake an upload by dumping the file into temp space, and adding info to $_FILES.
+        * (This is what PHP would normally do).
+        *
+        * @param string $fieldName Name this would have in the upload form
+        * @param string $fileName Name to title this
+        * @param string $type MIME type
+        * @param string $filePath Path where to find file contents
+        *
+        * @throws Exception
+        * @return bool
+        */
+       function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
+               $tmpName = $this->getNewTempFile();
+               if ( !file_exists( $filePath ) ) {
+                       throw new Exception( "$filePath doesn't exist!" );
+               }
+
+               if ( !copy( $filePath, $tmpName ) ) {
+                       throw new Exception( "couldn't copy $filePath to $tmpName" );
+               }
+
+               clearstatcache();
+               $size = filesize( $tmpName );
+               if ( $size === false ) {
+                       throw new Exception( "couldn't stat $tmpName" );
+               }
+
+               $_FILES[$fieldName] = [
+                       'name' => $fileName,
+                       'type' => $type,
+                       'tmp_name' => $tmpName,
+                       'size' => $size,
+                       'error' => null
+               ];
+
+               return true;
+       }
+
+       function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
+               $tmpName = $this->getNewTempFile();
+               // copy the chunk data to temp location:
+               if ( !file_put_contents( $tmpName, $chunkData ) ) {
+                       throw new Exception( "couldn't copy chunk data to $tmpName" );
+               }
+
+               clearstatcache();
+               $size = filesize( $tmpName );
+               if ( $size === false ) {
+                       throw new Exception( "couldn't stat $tmpName" );
+               }
+
+               $_FILES[$fieldName] = [
+                       'name' => $fileName,
+                       'type' => $type,
+                       'tmp_name' => $tmpName,
+                       'size' => $size,
+                       'error' => null
+               ];
+       }
+
+       /**
+        * Remove traces of previous fake uploads
+        */
+       function clearFakeUploads() {
+               $_FILES = [];
+       }
+}
index 6a75181..9702c82 100644 (file)
@@ -16,7 +16,7 @@ class IPTest extends PHPUnit\Framework\TestCase {
         * @covers IP::isIPAddress
         * @dataProvider provideInvalidIPs
         */
-       public function isNotIPAddress( $val, $desc ) {
+       public function testIsNotIPAddress( $val, $desc ) {
                $this->assertFalse( IP::isIPAddress( $val ), $desc );
        }