/** * Test requesting an invalid output format. * @expectedException TimestampException * @covers ConvertibleTimestamp::getTimestamp */ public function testInvalidOutput() { $timestamp = new ConvertibleTimestamp('1343761268'); $timestamp->getTimestamp(98); }
public function timestamp($ts = 0) { $ct = new ConvertibleTimestamp($ts); return $ct->getTimestamp(TS_POSTGRES); }
protected function doGetFileStat(array $params) { $source = $this->resolveToFSPath($params['src']); if ($source === null) { return false; // invalid storage path } $this->trapWarnings(); // don't trust 'false' if there were errors $stat = is_file($source) ? stat($source) : false; // regular files only $hadError = $this->untrapWarnings(); if ($stat) { $ct = new ConvertibleTimestamp($stat['mtime']); return ['mtime' => $ct->getTimestamp(TS_MW), 'size' => $stat['size']]; } elseif (!$hadError) { return false; // file does not exist } else { return null; // failure } }
public function timestamp($ts = 0) { $t = new ConvertibleTimestamp($ts); // Let errors bubble up to avoid putting garbage in the DB return $t->getTimestamp(TS_MW); }
/** * Stream a file to the browser, adding all the headings and fun stuff. * Headers sent include: Content-type, Content-Length, Last-Modified, * and Content-Disposition. * * @param array $headers Any additional headers to send if the file exists * @param bool $sendErrors Send error messages if errors occur (like 404) * @param array $optHeaders HTTP request header map (e.g. "range") (use lowercase keys) * @param integer $flags Bitfield of STREAM_* constants * @throws MWException * @return bool Success */ public function stream($headers = [], $sendErrors = true, $optHeaders = [], $flags = 0) { // Don't stream it out as text/html if there was a PHP error if ((($flags & self::STREAM_HEADLESS) == 0 || $headers) && headers_sent()) { echo "Headers already sent, terminating.\n"; return false; } $headerFunc = $flags & self::STREAM_HEADLESS ? function ($header) { // no-op } : function ($header) { is_int($header) ? HttpStatus::header($header) : header($header); }; MediaWiki\suppressWarnings(); $info = stat($this->path); MediaWiki\restoreWarnings(); if (!is_array($info)) { if ($sendErrors) { self::send404Message($this->path, $flags); } return false; } // Send Last-Modified HTTP header for client-side caching $mtimeCT = new ConvertibleTimestamp($info['mtime']); $headerFunc('Last-Modified: ' . $mtimeCT->getTimestamp(TS_RFC2822)); if (($flags & self::STREAM_ALLOW_OB) == 0) { call_user_func($this->obResetFunc); } $type = call_user_func($this->streamMimeFunc, $this->path); if ($type && $type != 'unknown/unknown') { $headerFunc("Content-type: {$type}"); } else { // Send a content type which is not known to Internet Explorer, to // avoid triggering IE's content type detection. Sending a standard // unknown content type here essentially gives IE license to apply // whatever content type it likes. $headerFunc('Content-type: application/x-wiki'); } // Don't send if client has up to date cache if (isset($optHeaders['if-modified-since'])) { $modsince = preg_replace('/;.*$/', '', $optHeaders['if-modified-since']); if ($mtimeCT->getTimestamp(TS_UNIX) <= strtotime($modsince)) { ini_set('zlib.output_compression', 0); $headerFunc(304); return true; // ok } } // Send additional headers foreach ($headers as $header) { header($header); // always use header(); specifically requested } if (isset($optHeaders['range'])) { $range = self::parseRange($optHeaders['range'], $info['size']); if (is_array($range)) { $headerFunc(206); $headerFunc('Content-Length: ' . $range[2]); $headerFunc("Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}"); } elseif ($range === 'invalid') { if ($sendErrors) { $headerFunc(416); $headerFunc('Cache-Control: no-cache'); $headerFunc('Content-Type: text/html; charset=utf-8'); $headerFunc('Content-Range: bytes */' . $info['size']); } return false; } else { // unsupported Range request (e.g. multiple ranges) $range = null; $headerFunc('Content-Length: ' . $info['size']); } } else { $range = null; $headerFunc('Content-Length: ' . $info['size']); } if (is_array($range)) { $handle = fopen($this->path, 'rb'); if ($handle) { $ok = true; fseek($handle, $range[0]); $remaining = $range[2]; while ($remaining > 0 && $ok) { $bytes = min($remaining, 8 * 1024); $data = fread($handle, $bytes); $remaining -= $bytes; $ok = $data !== false; print $data; } } else { return false; } } else { return readfile($this->path) !== false; // faster } return true; }