r100055 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100054‎ | r100055 | r100056 >
Date:17:30, 17 October 2011
Author:btongminh
Status:deferred (Comments)
Tags:
Comment:
Start on a a test special phase, untested.
Modified paths:
  • /trunk/extensions/VipsScaler/SpecialVipsTest.php (added) (history)
  • /trunk/extensions/VipsScaler/VipsScaler_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/VipsScaler/SpecialVipsTest.php
@@ -0,0 +1,136 @@
 2+<?php
 3+class SpecialVipsTest extends SpecialPage {
 4+ public function __construct() {
 5+ parent::__construct( 'VipsTest', 'vipstest' );
 6+ }
 7+
 8+ /**
 9+ * Entry point
 10+ */
 11+ public function execute( $par ) {
 12+ $request = $this->getRequest();
 13+ if ( $request->getText( 'thumb' ) && $request->getText( 'file' ) ) {
 14+ $this->streamThumbnail();
 15+ } elseif ( $par || $request->getText( 'file' ) ) {
 16+ $this->showForm();
 17+ $this->showThumbnails();
 18+ } else {
 19+ $this->showForm();
 20+ }
 21+ }
 22+
 23+ protected function showThumbnails() {
 24+
 25+ }
 26+
 27+ protected function showForm() {
 28+
 29+ }
 30+
 31+ protected function streamThumbnail() {
 32+ global $wgVipsThumbnailerUrl;
 33+
 34+ $request = $this->getRequest();
 35+
 36+ # Validate title and file existance
 37+ $title = Title::makeTitleSafe( NS_FILE, $request->getText( 'file' ) );
 38+ if ( is_null( $title ) ) {
 39+ return $this->streamError( 404 );
 40+ }
 41+ $file = wfFindFile( $title );
 42+ if ( !$file || !$file->exists() ) {
 43+ return $this->streamError( 404 );
 44+ }
 45+
 46+ # Check if vips can handle this file
 47+ if ( VipsScaler::getVipsHandler( $file ) === false ) {
 48+ return $this->streamError( 500 );
 49+ }
 50+
 51+ # Validate param string
 52+ $handler = $file->getHandler();
 53+ $params = $handler->parseParamString( $request->getText( 'thumb' ) );
 54+ if ( !$handler->normaliseParams( $file, $params ) ) {
 55+ return $this->streamError( 500 );
 56+ }
 57+
 58+ # Get the thumbnail
 59+ if ( is_null( $wgVipsThumbnailerUrl ) ) {
 60+ # No remote scaler, need to do it ourselves.
 61+ # Emulate the BitmapHandlerTransform hook
 62+
 63+ $dstPath = VipsCommand::makeTemp( strrchr( $file->getName(), '.' ) );
 64+ $dstUrl = '';
 65+
 66+ $scalerParams = array(
 67+ # The size to which the image will be resized
 68+ 'physicalWidth' => $params['physicalWidth'],
 69+ 'physicalHeight' => $params['physicalHeight'],
 70+ 'physicalDimensions' => "{$params['physicalWidth']}x{$params['physicalHeight']}",
 71+ # The size of the image on the page
 72+ 'clientWidth' => $params['width'],
 73+ 'clientHeight' => $params['height'],
 74+ # Comment as will be added to the EXIF of the thumbnail
 75+ 'comment' => isset( $params['descriptionUrl'] ) ?
 76+ "File source: {$params['descriptionUrl']}" : '',
 77+ # Properties of the original image
 78+ 'srcWidth' => $file->getWidth(),
 79+ 'srcHeight' => $file->getHeight(),
 80+ 'mimeType' => $file->getMimeType(),
 81+ 'srcPath' => $file->getPath(),
 82+ 'dstPath' => $dstPath,
 83+ 'dstUrl' => $dstUrl,
 84+ );
 85+
 86+
 87+ # Call the hook
 88+ $mto = null;
 89+ if ( VipsScaler::onTransform( $handler, $file, $params, $mto ) ) {
 90+ StreamFile::stream( $dstPath );
 91+ } else {
 92+ $this->streamError( 500 );
 93+ }
 94+
 95+ # Cleanup the temporary file
 96+ wfSuppressWarnings();
 97+ unlink( $dstPath );
 98+ wfRestoreWarning();
 99+
 100+ } else {
 101+ # Request the thumbnail at a remote scaler
 102+ global $wgVipsThumbnailerProxy;
 103+
 104+ $url = wfAppendQuery( $wgVipsThumbnailerUrl, array(
 105+ 'file' => $file->getName(),
 106+ 'thumb' => $handler->makeParamString( $params ) . '-' $file->getName()
 107+ ) );
 108+ $options = array( 'method' => 'GET' );
 109+ if ( $wgVipsThumbnailerProxy ) {
 110+ $options['proxy'] = $wgVipsThumbnailerProxy;
 111+ }
 112+
 113+ $req = MWHttpRequest::factory( $url, $options );
 114+ $status = $req->execute();
 115+ if ( $status->isOk() ) {
 116+ # Disable output and stream the file
 117+ $this->getContext()->getOutput()->disable();
 118+ print 'Content-Type: ' . $file->getMimeType() . "\r\n";
 119+ print 'Content-Length: ' . strlen( $req->getContent() ) . "\r\n";
 120+ print "\r\n";
 121+ print $req->getContent();
 122+ } else {
 123+ return $this->streamError( 500 );
 124+ }
 125+
 126+ }
 127+ }
 128+
 129+ protected function makeUrl( ) {
 130+
 131+ }
 132+
 133+ protected function streamError( $code ) {
 134+
 135+ }
 136+
 137+}
\ No newline at end of file
Property changes on: trunk/extensions/VipsScaler/SpecialVipsTest.php
___________________________________________________________________
Added: svn:eol-style
1138 + native
Index: trunk/extensions/VipsScaler/VipsScaler_body.php
@@ -230,7 +230,7 @@
231231 * @param File $file
232232 * @return mixed String or false
233233 */
234 - protected static function getVipsHandler( $file ) {
 234+ public static function getVipsHandler( $file ) {
235235 list( $major, $minor ) = File::splitMime( $file->getMimeType() );
236236
237237 if ( $major == 'image' && in_array( $minor, array( 'jpeg', 'png', 'tiff' ) ) ) {
@@ -333,7 +333,7 @@
334334 * @param string Extension
335335 * @return string
336336 */
337 - protected static function makeTemp( $extension ) {
 337+ public static function makeTemp( $extension ) {
338338 do {
339339 # Generate a random file
340340 $fileName = wfTempDir() . DIRECTORY_SEPARATOR .

Comments

#Comment by Hashar (talk | contribs)   14:38, 3 November 2011

I have added a lot more in some follow ups. So marking this rev as deferred since there is not that much to review :D

Status & tagging log