r64112 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64111‎ | r64112 | r64113 >
Date:13:36, 24 March 2010
Author:jojo
Status:ok
Tags:
Comment:
use GET to download from mw-serve; remove that irky curl header-handling stuff
Modified paths:
  • /trunk/extensions/Collection/Collection.body.php (modified) (history)
  • /trunk/extensions/Collection/Collection.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Collection/Collection.php
@@ -93,6 +93,12 @@
9494 'rl' => 'PDF',
9595 );
9696
 97+
 98+$wgCollectionContentTypeToFilename = array(
 99+ 'application/pdf' => 'collection.pdf',
 100+ 'application/vnd.oasis.opendocument.text' => 'collection.odt',
 101+);
 102+
97103 $wgCollectionPortletFormats = array( 'rl' );
98104
99105 $wgCollectionPortletForLoggedInUsersOnly = false;
Index: trunk/extensions/Collection/Collection.body.php
@@ -1116,34 +1116,24 @@
11171117 function download() {
11181118 global $wgOut;
11191119 global $wgRequest;
 1120+ global $wgCollectionContentTypeToFilename;
11201121
11211122 $tempfile = tmpfile();
1122 - $headers = self::mwServeCommand( 'download', array(
 1123+ $info = self::mwServeCommand( 'download', array(
11231124 'collection_id' => $wgRequest->getVal( 'collection_id' ),
11241125 'writer' => $wgRequest->getVal( 'writer' ),
11251126 ), $timeout=false, $toFile=$tempfile );
1126 - if ( !$headers ) {
 1127+ if ( !$info ) {
11271128 $wgOut->showErrorPage( 'coll-download_notfound_title', 'coll-download_notfound_text' );
11281129 return;
11291130 }
1130 - if ( isset( $headers['error'] ) ) {
1131 - $wgOut->showErrorPage(
1132 - 'coll-download_failed_title',
1133 - 'coll-download_failed_text',
1134 - array( $headers['error'] )
1135 - );
1136 - return;
1137 - }
11381131 wfResetOutputBuffers();
1139 - if ( isset( $headers['content-type'] ) ) {
1140 - header( 'Content-Type: ' . $headers['content-type']);
 1132+ $ct = $info['content_type'];
 1133+ header( 'Content-Type: ' . $ct );
 1134+ header( 'Content-Length: ' . $info['download_content_length'] );
 1135+ if ( isset( $wgCollectionContentTypeToFilename[$ct] ) ) {
 1136+ header( 'Content-Disposition: inline; filename=' . $wgCollectionContentTypeToFilename[$ct] );
11411137 }
1142 - if ( isset( $headers['content-disposition'] ) ) {
1143 - header( 'Content-Disposition: ' . $headers['content-disposition']);
1144 - }
1145 - if ( isset( $headers['content-length'] ) ) {
1146 - header( 'Content-Length: ' . $headers['content-length']);
1147 - }
11481138 fseek( $tempfile, 0 );
11491139 fpassthru( $tempfile );
11501140 $wgOut->disable();
@@ -1235,11 +1225,16 @@
12361226 $args['login_credentials'] = $wgCollectionMWServeCredentials;
12371227 }
12381228 $errorMessage = '';
1239 - $headers = array();
1240 - $response = self::post( $wgCollectionMWServeURL, $args, $errorMessage, $headers, $timeout, $toFile );
 1229+ if ( $command == 'download' ) {
 1230+ $method = 'GET';
 1231+ } else {
 1232+ $method = 'POST';
 1233+ }
 1234+ $info = false;
 1235+ $response = self::curlreq( $method, $wgCollectionMWServeURL, $args, $errorMessage, $info, $timeout, $toFile );
12411236 if ( $toFile ) {
1242 - if ( $headers ) {
1243 - return $headers;
 1237+ if ( $info ) {
 1238+ return $info;
12441239 } else {
12451240 return array( 'error' => $errorMessage );
12461241 }
@@ -1278,12 +1273,15 @@
12791274 return $json_response;
12801275 }
12811276
1282 - static function post( $url, $postFields, &$errorMessage, &$headers,
 1277+ static function curlreq( $method, $url, $postFields, &$errorMessage, &$info,
12831278 $timeout=true, $toFile=null ) {
12841279 global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle, $wgVersion;
12851280 global $wgCollectionMWServeCert;
12861281 global $wgCollectionVersion;
12871282
 1283+ if ( $method == 'GET') {
 1284+ $url = wfAppendQuery( $url, $postFields );
 1285+ }
12881286 $c = curl_init( $url );
12891287 curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy);
12901288 $userAgent = wfGetAgent();
@@ -1292,9 +1290,14 @@
12931291 }
12941292 $userAgent .= " (via MediaWiki/$wgVersion, Collection/$wgCollectionVersion)";
12951293 curl_setopt( $c, CURLOPT_USERAGENT, $userAgent);
1296 - curl_setopt( $c, CURLOPT_POST, true );
1297 - curl_setopt( $c, CURLOPT_POSTFIELDS, $postFields );
 1294+ if ( $method == 'POST' ) {
 1295+ curl_setopt( $c, CURLOPT_POST, true );
 1296+ curl_setopt( $c, CURLOPT_POSTFIELDS, $postFields );
 1297+ } else {
 1298+ curl_setopt( $c, CURLOPT_FOLLOWLOCATION, true );
 1299+ }
12981300 curl_setopt( $c, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
 1301+ curl_setopt( $c, CURLOPT_HEADER, false );
12991302 if ( is_object( $wgTitle ) ) {
13001303 curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() );
13011304 }
@@ -1309,33 +1312,20 @@
13101313 curl_setopt ($c, CURLOPT_CAINFO, $wgCollectionMWServeCert);
13111314 }
13121315
1313 - $headerStream = tmpfile();
1314 - curl_setopt( $c, CURLOPT_WRITEHEADER, $headerStream );
13151316 if ( $toFile ) {
13161317 curl_setopt( $c, CURLOPT_FILE, $toFile );
13171318 } else {
13181319 curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
13191320 }
1320 -
13211321 $result = curl_exec( $c );
 1322+ $text = false;
 1323+ $info = false;
13221324 if ( curl_errno( $c ) != CURLE_OK ) {
1323 - $text = false;
13241325 $errorMessage = curl_error( $c );
1325 - $headers = false;
13261326 } elseif ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) {
1327 - $text = false;
13281327 $errorMessage = 'HTTP status ' . curl_getinfo( $c, CURLINFO_HTTP_CODE );
1329 - $headers = false;
13301328 } else {
1331 - $headerSize = curl_getinfo( $c, CURLINFO_HEADER_SIZE );
1332 - fseek( $headerStream, 0 );
1333 - $headerLines = explode( "\n", fread( $headerStream, $headerSize ) );
1334 - foreach( $headerLines as $line ) {
1335 - if ( preg_match( "/^(.+?):\s+(.+)$/", trim( $line ), $matches ) ) {
1336 - $headers[ strtolower( $matches[1] ) ] = $matches[2];
1337 - }
1338 - unset( $matches );
1339 - }
 1329+ $info = curl_getinfo( $c );
13401330 if ( !$toFile ) {
13411331 $text = $result;
13421332 }

Status & tagging log