From 963901c8dd9eaebd042c3ab20c30fb555f2adbed Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 19 Oct 2004 07:12:15 +0000 Subject: [PATCH] * Drop a couple instances of wfTimestamp2Unix & reverse for wfTimestamp() * Make wfTimestampNow() a wrapper on wfTimestamp() * Add some tests and docstrings --- includes/GlobalFunctions.php | 45 +++++++++++----------- includes/RawPage.php | 2 +- includes/SpecialValidate.php | 2 +- maintenance/updateSearchIndex.php | 2 +- tests/GlobalTest.php | 63 +++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 26 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 2618325333..a3d4fa0fa6 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -854,8 +854,16 @@ function wfAcceptToPrefs( $accept, $def = '*/*' ) { } /** - * @todo document - * @private + * Checks if a given MIME type matches any of the keys in the given + * array. Basic wildcards are accepted in the array keys. + * + * Returns the matching MIME type (or wildcard) if a match, otherwise + * NULL if no match. + * + * @param string $type + * @param array $avail + * @return string + * @access private */ function mimeTypeMatch( $type, $avail ) { if( array_key_exists($type, $avail) ) { @@ -873,6 +881,15 @@ function mimeTypeMatch( $type, $avail ) { } /** + * Returns the 'best' match between a client's requested internet media types + * and the server's list of available types. Each list should be an associative + * array of type to preference (preference is a float between 0.0 and 1.0). + * Wildcards in the types are acceptable. + * + * @param array $cprefs Client's acceptable type list + * @param array $sprefs Server's offered types + * @return string + * * @todo FIXME: doesn't handle params like 'text/plain; charset=UTF-8' * XXX: generalize to negotiate other stuff */ @@ -923,31 +940,13 @@ function wfArrayLookup( $a, $b ) { return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) ); } - /** - * Ideally we'd be using actual time fields in the db - * @todo fixme - */ -function wfTimestamp2Unix( $ts ) { - return gmmktime( ( (int)substr( $ts, 8, 2) ), - (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ), - (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ), - (int)substr( $ts, 0, 4 ) ); -} - -/** - * @todo document - */ -function wfUnix2Timestamp( $unixtime ) { - return gmdate( 'YmdHis', $unixtime ); -} - -/** - * @todo document + * Convenience function; returns MediaWiki timestamp for the present time. + * @return string */ function wfTimestampNow() { # return NOW - return gmdate( 'YmdHis' ); + return wfTimestamp( TS_MW, time() ); } /** diff --git a/includes/RawPage.php b/includes/RawPage.php index aa21d8311d..aa22d2b007 100644 --- a/includes/RawPage.php +++ b/includes/RawPage.php @@ -115,7 +115,7 @@ class RawPage { $res = $dbr->query( $sql, $fname ); if( $s = $dbr->fetchObject( $res ) ) { $rawtext = Article::getRevisionText( $s, "" ); - header( 'Last-modified: '.gmdate( "D, j M Y H:i:s", wfTimestamp2Unix( $s->timestamp )).' GMT' ); + header( 'Last-modified: '.gmdate( "D, j M Y H:i:s", wfTimestamp( TS_UNIX, $s->timestamp )).' GMT' ); return $rawtext; } else { return ''; diff --git a/includes/SpecialValidate.php b/includes/SpecialValidate.php index 23bf6f62ea..aaaac55c41 100644 --- a/includes/SpecialValidate.php +++ b/includes/SpecialValidate.php @@ -204,7 +204,7 @@ class Validation { if ( $article_time == $time ) { $tablestyle .=" style='border: 2px solid red'"; } - $html .= "

" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $time ) ) ); + $html .= "

" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp( TW_UNIX, $time ) ) ); $this->find_this_version ( $article_title , $time , $table_id , $table_name ); if( $table_name == "cur" ) { $html .= " (" . wfMsg( 'val_this_is_current_version' ) . ")"; diff --git a/maintenance/updateSearchIndex.php b/maintenance/updateSearchIndex.php index 5c655a551a..c4bc483dd0 100644 --- a/maintenance/updateSearchIndex.php +++ b/maintenance/updateSearchIndex.php @@ -36,7 +36,7 @@ if ( isset( $options['s'] ) ) { } else { $start = @file_get_contents( $posFile ); if ( !$start ) { - $start = wfUnix2Timestamp( time() - 86400 ); + $start = wfTimestamp( TS_MW, time() - 86400 ); } } diff --git a/tests/GlobalTest.php b/tests/GlobalTest.php index 65f84e8f12..aa44a2b0b7 100644 --- a/tests/GlobalTest.php +++ b/tests/GlobalTest.php @@ -121,6 +121,69 @@ class GlobalTest extends PHPUnit_TestCase { array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) ); } + function testMimeTypeMatch() { + $this->assertEquals( + 'text/html', + mimeTypeMatch( 'text/html', + array( 'application/xhtml+xml' => 1.0, + 'text/html' => 0.7, + 'text/plain' => 0.3 ) ) ); + $this->assertEquals( + 'text/*', + mimeTypeMatch( 'text/html', + array( 'image/*' => 1.0, + 'text/*' => 0.5 ) ) ); + $this->assertEquals( + '*/*', + mimeTypeMatch( 'text/html', + array( '*/*' => 1.0 ) ) ); + $this->assertNull( + mimeTypeMatch( 'text/html', + array( 'image/png' => 1.0, + 'image/svg+xml' => 0.5 ) ) ); + } + + function testNegotiateType() { + $this->assertEquals( + 'text/html', + wfNegotiateType( + array( 'application/xhtml+xml' => 1.0, + 'text/html' => 0.7, + 'text/plain' => 0.5, + 'text/*' => 0.2 ), + array( 'text/html' => 1.0 ) ) ); + $this->assertEquals( + 'application/xhtml+xml', + wfNegotiateType( + array( 'application/xhtml+xml' => 1.0, + 'text/html' => 0.7, + 'text/plain' => 0.5, + 'text/*' => 0.2 ), + array( 'application/xhtml+xml' => 1.0, + 'text/html' => 0.5 ) ) ); + $this->assertEquals( + 'text/html', + wfNegotiateType( + array( 'text/html' => 1.0, + 'text/plain' => 0.5, + 'text/*' => 0.5, + 'application/xhtml+xml' => 0.2 ), + array( 'application/xhtml+xml' => 1.0, + 'text/html' => 0.5 ) ) ); + $this->assertEquals( + 'text/html', + wfNegotiateType( + array( 'text/*' => 1.0, + 'image/*' => 0.7, + '*/*' => 0.3 ), + array( 'application/xhtml+xml' => 1.0, + 'text/html' => 0.5 ) ) ); + $this->assertNull( + wfNegotiateType( + array( 'text/*' => 1.0 ), + array( 'application/xhtml+xml' => 1.0 ) ) ); + } + /* TODO: many more! */ } -- 2.20.1