Index: trunk/extensions/Collection/Collection.php |
— | — | @@ -93,6 +93,12 @@ |
94 | 94 | 'rl' => 'PDF', |
95 | 95 | ); |
96 | 96 | |
| 97 | + |
| 98 | +$wgCollectionContentTypeToFilename = array( |
| 99 | + 'application/pdf' => 'collection.pdf', |
| 100 | + 'application/vnd.oasis.opendocument.text' => 'collection.odt', |
| 101 | +); |
| 102 | + |
97 | 103 | $wgCollectionPortletFormats = array( 'rl' ); |
98 | 104 | |
99 | 105 | $wgCollectionPortletForLoggedInUsersOnly = false; |
Index: trunk/extensions/Collection/Collection.body.php |
— | — | @@ -1116,34 +1116,24 @@ |
1117 | 1117 | function download() { |
1118 | 1118 | global $wgOut; |
1119 | 1119 | global $wgRequest; |
| 1120 | + global $wgCollectionContentTypeToFilename; |
1120 | 1121 | |
1121 | 1122 | $tempfile = tmpfile(); |
1122 | | - $headers = self::mwServeCommand( 'download', array( |
| 1123 | + $info = self::mwServeCommand( 'download', array( |
1123 | 1124 | 'collection_id' => $wgRequest->getVal( 'collection_id' ), |
1124 | 1125 | 'writer' => $wgRequest->getVal( 'writer' ), |
1125 | 1126 | ), $timeout=false, $toFile=$tempfile ); |
1126 | | - if ( !$headers ) { |
| 1127 | + if ( !$info ) { |
1127 | 1128 | $wgOut->showErrorPage( 'coll-download_notfound_title', 'coll-download_notfound_text' ); |
1128 | 1129 | return; |
1129 | 1130 | } |
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 | | - } |
1138 | 1131 | 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] ); |
1141 | 1137 | } |
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 | | - } |
1148 | 1138 | fseek( $tempfile, 0 ); |
1149 | 1139 | fpassthru( $tempfile ); |
1150 | 1140 | $wgOut->disable(); |
— | — | @@ -1235,11 +1225,16 @@ |
1236 | 1226 | $args['login_credentials'] = $wgCollectionMWServeCredentials; |
1237 | 1227 | } |
1238 | 1228 | $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 ); |
1241 | 1236 | if ( $toFile ) { |
1242 | | - if ( $headers ) { |
1243 | | - return $headers; |
| 1237 | + if ( $info ) { |
| 1238 | + return $info; |
1244 | 1239 | } else { |
1245 | 1240 | return array( 'error' => $errorMessage ); |
1246 | 1241 | } |
— | — | @@ -1278,12 +1273,15 @@ |
1279 | 1274 | return $json_response; |
1280 | 1275 | } |
1281 | 1276 | |
1282 | | - static function post( $url, $postFields, &$errorMessage, &$headers, |
| 1277 | + static function curlreq( $method, $url, $postFields, &$errorMessage, &$info, |
1283 | 1278 | $timeout=true, $toFile=null ) { |
1284 | 1279 | global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle, $wgVersion; |
1285 | 1280 | global $wgCollectionMWServeCert; |
1286 | 1281 | global $wgCollectionVersion; |
1287 | 1282 | |
| 1283 | + if ( $method == 'GET') { |
| 1284 | + $url = wfAppendQuery( $url, $postFields ); |
| 1285 | + } |
1288 | 1286 | $c = curl_init( $url ); |
1289 | 1287 | curl_setopt($c, CURLOPT_PROXY, $wgHTTPProxy); |
1290 | 1288 | $userAgent = wfGetAgent(); |
— | — | @@ -1292,9 +1290,14 @@ |
1293 | 1291 | } |
1294 | 1292 | $userAgent .= " (via MediaWiki/$wgVersion, Collection/$wgCollectionVersion)"; |
1295 | 1293 | 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 | + } |
1298 | 1300 | curl_setopt( $c, CURLOPT_HTTPHEADER, array( 'Expect:' ) ); |
| 1301 | + curl_setopt( $c, CURLOPT_HEADER, false ); |
1299 | 1302 | if ( is_object( $wgTitle ) ) { |
1300 | 1303 | curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() ); |
1301 | 1304 | } |
— | — | @@ -1309,33 +1312,20 @@ |
1310 | 1313 | curl_setopt ($c, CURLOPT_CAINFO, $wgCollectionMWServeCert); |
1311 | 1314 | } |
1312 | 1315 | |
1313 | | - $headerStream = tmpfile(); |
1314 | | - curl_setopt( $c, CURLOPT_WRITEHEADER, $headerStream ); |
1315 | 1316 | if ( $toFile ) { |
1316 | 1317 | curl_setopt( $c, CURLOPT_FILE, $toFile ); |
1317 | 1318 | } else { |
1318 | 1319 | curl_setopt( $c, CURLOPT_RETURNTRANSFER, true ); |
1319 | 1320 | } |
1320 | | - |
1321 | 1321 | $result = curl_exec( $c ); |
| 1322 | + $text = false; |
| 1323 | + $info = false; |
1322 | 1324 | if ( curl_errno( $c ) != CURLE_OK ) { |
1323 | | - $text = false; |
1324 | 1325 | $errorMessage = curl_error( $c ); |
1325 | | - $headers = false; |
1326 | 1326 | } elseif ( curl_getinfo( $c, CURLINFO_HTTP_CODE ) != 200 ) { |
1327 | | - $text = false; |
1328 | 1327 | $errorMessage = 'HTTP status ' . curl_getinfo( $c, CURLINFO_HTTP_CODE ); |
1329 | | - $headers = false; |
1330 | 1328 | } 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 ); |
1340 | 1330 | if ( !$toFile ) { |
1341 | 1331 | $text = $result; |
1342 | 1332 | } |