* Drop a couple instances of wfTimestamp2Unix & reverse for wfTimestamp()
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 19 Oct 2004 07:12:15 +0000 (07:12 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 19 Oct 2004 07:12:15 +0000 (07:12 +0000)
* Make wfTimestampNow() a wrapper on wfTimestamp()
* Add some tests and docstrings

includes/GlobalFunctions.php
includes/RawPage.php
includes/SpecialValidate.php
maintenance/updateSearchIndex.php
tests/GlobalTest.php

index 2618325..a3d4fa0 100644 (file)
@@ -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() );
 }
 
 /**
index aa21d83..aa22d2b 100644 (file)
@@ -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 '';
index 23bf6f6..aaaac55 100644 (file)
@@ -204,7 +204,7 @@ class Validation {
                        if ( $article_time == $time ) {
                                $tablestyle .=" style='border: 2px solid red'";
                        }
-                       $html .= "<h2>" . wfMsg( 'val_version_of', gmdate( "F d, Y H:i:s", wfTimestamp2Unix( $time ) ) );
+                       $html .= "<h2>" . 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' ) . ")";
index 5c655a5..c4bc483 100644 (file)
@@ -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 );
        } 
 }
 
index 65f84e8..aa44a2b 100644 (file)
@@ -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! */
 }