}
/**
- * @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) ) {
}
/**
+ * 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
*/
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() );
}
/**
$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 '';
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' ) . ")";
} else {
$start = @file_get_contents( $posFile );
if ( !$start ) {
- $start = wfUnix2Timestamp( time() - 86400 );
+ $start = wfTimestamp( TS_MW, time() - 86400 );
}
}
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! */
}