From a57b64436cf2404d4daa23700607ce1e5848ddf5 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sat, 1 Oct 2016 21:51:51 -0700 Subject: [PATCH] Use wikimedia/timestamp Bug: T100924 Depends-On: I0a067367cda6885fa45631ed7c18799d653dc9bf Change-Id: I69ba64e364df8af089c1c918cdf32f99454e693a --- RELEASE-NOTES-1.29 | 1 + autoload.php | 2 - composer.json | 6 +- includes/Defines.php | 1 - includes/MWTimestamp.php | 1 + includes/MergeHistory.php | 1 + includes/api/ApiMain.php | 1 + includes/compat/Timestamp.php | 18 ++ includes/libs/filebackend/FSFileBackend.php | 1 + .../libs/filebackend/FileBackendStore.php | 1 + .../libs/filebackend/HTTPFileStreamer.php | 1 + includes/libs/rdbms/database/Database.php | 1 + .../libs/rdbms/database/DatabasePostgres.php | 1 + includes/libs/time/ConvertibleTimestamp.php | 269 ------------------ includes/libs/time/TimestampException.php | 7 - includes/libs/time/defines.php | 52 ---- includes/libs/xmp/XMPValidate.php | 1 + includes/media/FormatMetadata.php | 1 + includes/pager/ReverseChronologicalPager.php | 1 + includes/utils/AutoloadGenerator.php | 9 + .../libs/time/ConvertibleTimestampTest.php | 144 ---------- 21 files changed, 44 insertions(+), 476 deletions(-) create mode 100644 includes/compat/Timestamp.php delete mode 100644 includes/libs/time/ConvertibleTimestamp.php delete mode 100644 includes/libs/time/TimestampException.php delete mode 100644 includes/libs/time/defines.php delete mode 100644 tests/phpunit/includes/libs/time/ConvertibleTimestampTest.php diff --git a/RELEASE-NOTES-1.29 b/RELEASE-NOTES-1.29 index a1ce9d9b79..9f2c850b70 100644 --- a/RELEASE-NOTES-1.29 +++ b/RELEASE-NOTES-1.29 @@ -58,6 +58,7 @@ production. === External library changes in 1.29 === ==== Upgraded external libraries ==== +* Added wikimedia/timestamp v1.0.0. * Updated QUnit from v1.22.0 to v1.23.1. * Updated cssjanus from v1.1.2 to 1.1.3. * Updated psr/log from v1.0.0 to v1.0.2. diff --git a/autoload.php b/autoload.php index aa4e544e82..e5879d944a 100644 --- a/autoload.php +++ b/autoload.php @@ -289,7 +289,6 @@ $wgAutoloadLocalClasses = [ 'ConvertLinks' => __DIR__ . '/maintenance/convertLinks.php', 'ConvertUserOptions' => __DIR__ . '/maintenance/convertUserOptions.php', 'ConverterRule' => __DIR__ . '/languages/ConverterRule.php', - 'ConvertibleTimestamp' => __DIR__ . '/includes/libs/time/ConvertibleTimestamp.php', 'Cookie' => __DIR__ . '/includes/libs/Cookie.php', 'CookieJar' => __DIR__ . '/includes/libs/CookieJar.php', 'CopyFileBackend' => __DIR__ . '/maintenance/copyFileBackend.php', @@ -1438,7 +1437,6 @@ $wgAutoloadLocalClasses = [ 'ThumbnailRenderJob' => __DIR__ . '/includes/jobqueue/jobs/ThumbnailRenderJob.php', 'TidyUpBug37714' => __DIR__ . '/maintenance/tidyUpBug37714.php', 'TiffHandler' => __DIR__ . '/includes/media/Tiff.php', - 'TimestampException' => __DIR__ . '/includes/libs/time/TimestampException.php', 'Timing' => __DIR__ . '/includes/libs/Timing.php', 'Title' => __DIR__ . '/includes/Title.php', 'TitleArray' => __DIR__ . '/includes/TitleArray.php', diff --git a/composer.json b/composer.json index bc0ce17abf..fe68a61178 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "wikimedia/running-stat": "1.1.0", "wikimedia/scoped-callback": "1.0.0", "wikimedia/utfnormal": "1.1.0", + "wikimedia/timestamp": "1.0.0", "wikimedia/wait-condition-loop": "1.0.1", "wikimedia/wrappedstring": "2.2.0", "zordius/lightncandy": "0.23" @@ -75,7 +76,10 @@ "autoload": { "psr-0": { "ComposerHookHandler": "includes/composer" - } + }, + "files": [ + "includes/compat/Timestamp.php" + ] }, "autoload-dev": { "files": [ diff --git a/includes/Defines.php b/includes/Defines.php index 35c2a2d8ad..bd92ff0683 100644 --- a/includes/Defines.php +++ b/includes/Defines.php @@ -21,7 +21,6 @@ */ require_once __DIR__ . '/libs/mime/defines.php'; -require_once __DIR__ . '/libs/time/defines.php'; require_once __DIR__ . '/libs/rdbms/defines.php'; require_once __DIR__ . '/compat/normal/UtfNormalDefines.php'; diff --git a/includes/MWTimestamp.php b/includes/MWTimestamp.php index c1e5cc410b..7f3649e39c 100644 --- a/includes/MWTimestamp.php +++ b/includes/MWTimestamp.php @@ -21,6 +21,7 @@ * @since 1.20 * @author Tyler Romeo, 2012 */ +use Wikimedia\Timestamp\ConvertibleTimestamp; /** * Library for creating and parsing MW-style timestamps. Based on the JS diff --git a/includes/MergeHistory.php b/includes/MergeHistory.php index e57f88099a..8cf3af1581 100644 --- a/includes/MergeHistory.php +++ b/includes/MergeHistory.php @@ -24,6 +24,7 @@ * * @file */ +use Wikimedia\Timestamp\TimestampException; /** * Handles the backend logic of merging the histories of two diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 6d76f8c944..604fdf9e70 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -26,6 +26,7 @@ */ use MediaWiki\Logger\LoggerFactory; +use Wikimedia\Timestamp\TimestampException; /** * This is the main API class, used for both external and internal processing. diff --git a/includes/compat/Timestamp.php b/includes/compat/Timestamp.php new file mode 100644 index 0000000000..805e61ec65 --- /dev/null +++ b/includes/compat/Timestamp.php @@ -0,0 +1,18 @@ + 'U', - TS_MW => 'YmdHis', - TS_DB => 'Y-m-d H:i:s', - TS_ISO_8601 => 'Y-m-d\TH:i:s\Z', - TS_ISO_8601_BASIC => 'Ymd\THis\Z', - TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness - TS_RFC2822 => 'D, d M Y H:i:s', - TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500 - TS_POSTGRES => 'Y-m-d H:i:s', - ]; - - /** - * The actual timestamp being wrapped (DateTime object). - * @var DateTime - */ - public $timestamp; - - /** - * Make a new timestamp and set it to the specified time, - * or the current time if unspecified. - * - * @param bool|string|int|float|DateTime $timestamp Timestamp to set, or false for current time - */ - public function __construct( $timestamp = false ) { - if ( $timestamp instanceof DateTime ) { - $this->timestamp = $timestamp; - } else { - $this->setTimestamp( $timestamp ); - } - } - - /** - * Set the timestamp to the specified time, or the current time if unspecified. - * - * Parse the given timestamp into either a DateTime object or a Unix timestamp, - * and then store it. - * - * @param string|bool $ts Timestamp to store, or false for now - * @throws TimestampException - */ - public function setTimestamp( $ts = false ) { - $m = []; - $da = []; - $strtime = ''; - - // We want to catch 0, '', null... but not date strings starting with a letter. - if ( !$ts || $ts === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) { - $uts = time(); - $strtime = "@$uts"; - } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) { - # TS_DB - } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) { - # TS_EXIF - } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) { - # TS_MW - } elseif ( preg_match( '/^(-?\d{1,13})(\.\d+)?$/D', $ts, $m ) ) { - # TS_UNIX - $strtime = "@{$m[1]}"; // https://secure.php.net/manual/en/datetime.formats.compound.php - } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) { - # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6 - $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3", - str_replace( '+00:00', 'UTC', $ts ) ); - } elseif ( preg_match( - '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z?$/', - $ts, - $da - ) ) { - # TS_ISO_8601 - } elseif ( preg_match( - '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z?$/', - $ts, - $da - ) ) { - # TS_ISO_8601_BASIC - } elseif ( preg_match( - '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', - $ts, - $da - ) ) { - # TS_POSTGRES - } elseif ( preg_match( - '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', - $ts, - $da - ) ) { - # TS_POSTGRES - } elseif ( preg_match( - # Day of week - '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . - # dd Mon yyyy - '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' . - # hh:mm:ss - '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S', - $ts - ) ) { - # TS_RFC2822, accepting a trailing comment. - # See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171 - # The regex is a superset of rfc2822 for readability - $strtime = strtok( $ts, ';' ); - } elseif ( preg_match( '/^[A-Z][a-z]{5,8}, \d\d-[A-Z][a-z]{2}-\d{2} \d\d:\d\d:\d\d/', $ts ) ) { - # TS_RFC850 - $strtime = $ts; - } elseif ( preg_match( '/^[A-Z][a-z]{2} [A-Z][a-z]{2} +\d{1,2} \d\d:\d\d:\d\d \d{4}/', $ts ) ) { - # asctime - $strtime = $ts; - } else { - throw new TimestampException( __METHOD__ . ": Invalid timestamp - $ts" ); - } - - if ( !$strtime ) { - $da = array_map( 'intval', $da ); - $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00"; - $strtime = call_user_func_array( "sprintf", $da ); - } - - try { - $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) ); - } catch ( Exception $e ) { - throw new TimestampException( __METHOD__ . ': Invalid timestamp format.', $e->getCode(), $e ); - } - - if ( $final === false ) { - throw new TimestampException( __METHOD__ . ': Invalid timestamp format.' ); - } - - $this->timestamp = $final; - } - - /** - * Convert a timestamp string to a given format. - * - * @param int $style Constant Output format for timestamp - * @param string $ts Timestamp - * @return string|bool Formatted timestamp or false on failure - */ - public static function convert( $style = TS_UNIX, $ts ) { - try { - $ct = new static( $ts ); - return $ct->getTimestamp( $style ); - } catch ( TimestampException $e ) { - return false; - } - } - - /** - * Get the current time in the given format - * - * @param int $style Constant Output format for timestamp - * @return string - */ - public static function now( $style = TS_MW ) { - return static::convert( $style, time() ); - } - - /** - * Get the timestamp represented by this object in a certain form. - * - * Convert the internal timestamp to the specified format and then - * return it. - * - * @param int $style Constant Output format for timestamp - * @throws TimestampException - * @return string The formatted timestamp - */ - public function getTimestamp( $style = TS_UNIX ) { - if ( !isset( self::$formats[$style] ) ) { - throw new TimestampException( __METHOD__ . ': Illegal timestamp output type.' ); - } - - $output = $this->timestamp->format( self::$formats[$style] ); - - if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) { - $output .= ' GMT'; - } - - if ( $style == TS_MW && strlen( $output ) !== 14 ) { - throw new TimestampException( __METHOD__ . ': The timestamp cannot be represented in ' . - 'the specified format' ); - } - - return $output; - } - - /** - * @return string - */ - public function __toString() { - return $this->getTimestamp(); - } - - /** - * Calculate the difference between two ConvertibleTimestamp objects. - * - * @param ConvertibleTimestamp $relativeTo Base time to calculate difference from - * @return DateInterval|bool The DateInterval object representing the - * difference between the two dates or false on failure - */ - public function diff( ConvertibleTimestamp $relativeTo ) { - return $this->timestamp->diff( $relativeTo->timestamp ); - } - - /** - * Set the timezone of this timestamp to the specified timezone. - * - * @param string $timezone Timezone to set - * @throws TimestampException - */ - public function setTimezone( $timezone ) { - try { - $this->timestamp->setTimezone( new DateTimeZone( $timezone ) ); - } catch ( Exception $e ) { - throw new TimestampException( __METHOD__ . ': Invalid timezone.', $e->getCode(), $e ); - } - } - - /** - * Get the timezone of this timestamp. - * - * @return DateTimeZone The timezone - */ - public function getTimezone() { - return $this->timestamp->getTimezone(); - } - - /** - * Format the timestamp in a given format. - * - * @param string $format Pattern to format in - * @return string The formatted timestamp - */ - public function format( $format ) { - return $this->timestamp->format( $format ); - } -} diff --git a/includes/libs/time/TimestampException.php b/includes/libs/time/TimestampException.php deleted file mode 100644 index 36ffdeeaf9..0000000000 --- a/includes/libs/time/TimestampException.php +++ /dev/null @@ -1,7 +0,0 @@ -classes[] = $this->alias['name']; $this->alias = null; $this->startToken = null; + } elseif ( !is_array( $token ) || ( + $token[0] !== T_STRING && + $token[0] !== T_DOUBLE_COLON && + $token[0] !== T_CLASS && + $token[0] !== T_WHITESPACE + ) ) { + // Ignore this call to class_alias() - compat/Timestamp.php + $this->alias = null; + $this->startToken = null; } } break; diff --git a/tests/phpunit/includes/libs/time/ConvertibleTimestampTest.php b/tests/phpunit/includes/libs/time/ConvertibleTimestampTest.php deleted file mode 100644 index d48caf37f0..0000000000 --- a/tests/phpunit/includes/libs/time/ConvertibleTimestampTest.php +++ /dev/null @@ -1,144 +0,0 @@ -assertInternalType( 'string', $timestamp->getTimestamp() ); - $this->assertNotEmpty( $timestamp->getTimestamp() ); - $this->assertNotEquals( false, strtotime( $timestamp->getTimestamp( TS_MW ) ) ); - } - - /** - * @covers ConvertibleTimestamp::__toString - */ - public function testToString() { - $timestamp = new ConvertibleTimestamp( '1406833268' ); // Equivalent to 20140731190108 - $this->assertEquals( '1406833268', $timestamp->__toString() ); - } - - public static function provideValidTimestampDifferences() { - return [ - [ '1406833268', '1406833269', '00 00 00 01' ], - [ '1406833268', '1406833329', '00 00 01 01' ], - [ '1406833268', '1406836929', '00 01 01 01' ], - [ '1406833268', '1406923329', '01 01 01 01' ], - ]; - } - - /** - * @dataProvider provideValidTimestampDifferences - * @covers ConvertibleTimestamp::diff - */ - public function testDiff( $timestamp1, $timestamp2, $expected ) { - $timestamp1 = new ConvertibleTimestamp( $timestamp1 ); - $timestamp2 = new ConvertibleTimestamp( $timestamp2 ); - $diff = $timestamp1->diff( $timestamp2 ); - $this->assertEquals( $expected, $diff->format( '%D %H %I %S' ) ); - } - - /** - * Test parsing of valid timestamps and outputing to MW format. - * @dataProvider provideValidTimestamps - * @covers ConvertibleTimestamp::getTimestamp - */ - public function testValidParse( $format, $original, $expected ) { - $timestamp = new ConvertibleTimestamp( $original ); - $this->assertEquals( $expected, $timestamp->getTimestamp( TS_MW ) ); - } - - /** - * Test outputting valid timestamps to different formats. - * @dataProvider provideValidTimestamps - * @covers ConvertibleTimestamp::getTimestamp - */ - public function testValidOutput( $format, $expected, $original ) { - $timestamp = new ConvertibleTimestamp( $original ); - $this->assertEquals( $expected, (string)$timestamp->getTimestamp( $format ) ); - } - - /** - * Test an invalid timestamp. - * @expectedException TimestampException - * @covers ConvertibleTimestamp - */ - public function testInvalidParse() { - new ConvertibleTimestamp( "This is not a timestamp." ); - } - - /** - * @dataProvider provideValidTimestamps - * @covers ConvertibleTimestamp::convert - */ - public function testConvert( $format, $expected, $original ) { - $this->assertSame( $expected, ConvertibleTimestamp::convert( $format, $original ) ); - } - - /** - * Format an invalid timestamp. - * @covers ConvertibleTimestamp::convert - */ - public function testConvertInvalid() { - $this->assertSame( false, ConvertibleTimestamp::convert( 'Not a timestamp', 0 ) ); - } - - /** - * Test an out of range timestamp - * @dataProvider provideOutOfRangeTimestamps - * @expectedException TimestampException - * @covers ConvertibleTimestamp - */ - public function testOutOfRangeTimestamps( $format, $input ) { - $timestamp = new ConvertibleTimestamp( $input ); - $timestamp->getTimestamp( $format ); - } - - /** - * Test requesting an invalid output format. - * @expectedException TimestampException - * @covers ConvertibleTimestamp::getTimestamp - */ - public function testInvalidOutput() { - $timestamp = new ConvertibleTimestamp( '1343761268' ); - $timestamp->getTimestamp( 98 ); - } - - /** - * Returns a list of valid timestamps in the format: - * [ type, timestamp_of_type, timestamp_in_MW ] - */ - public static function provideValidTimestamps() { - return [ - // Various formats - [ TS_UNIX, '1343761268', '20120731190108' ], - [ TS_MW, '20120731190108', '20120731190108' ], - [ TS_DB, '2012-07-31 19:01:08', '20120731190108' ], - [ TS_ISO_8601, '2012-07-31T19:01:08Z', '20120731190108' ], - [ TS_ISO_8601_BASIC, '20120731T190108Z', '20120731190108' ], - [ TS_EXIF, '2012:07:31 19:01:08', '20120731190108' ], - [ TS_RFC2822, 'Tue, 31 Jul 2012 19:01:08 GMT', '20120731190108' ], - [ TS_ORACLE, '31-07-2012 19:01:08.000000', '20120731190108' ], - [ TS_POSTGRES, '2012-07-31 19:01:08 GMT', '20120731190108' ], - // Some extremes and weird values - [ TS_ISO_8601, '9999-12-31T23:59:59Z', '99991231235959' ], - [ TS_UNIX, '-62135596801', '00001231235959' ] - ]; - } - - /** - * Returns a list of out of range timestamps in the format: - * [ type, timestamp_of_type ] - */ - public static function provideOutOfRangeTimestamps() { - return [ - // Various formats - [ TS_MW, '-62167219201' ], // -0001-12-31T23:59:59Z - [ TS_MW, '253402300800' ], // 10000-01-01T00:00:00Z - ]; - } -} -- 2.20.1