1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
<?php
/**
* Copyright © 2007 Martin Seidel (Xarax) <jodeldi@gmx.de>
*
* Inspired by djvuhandler from Tim Starling
* Modified and written by Xarax
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
class PdfHandler extends ImageHandler {
/**
* @return bool
*/
function isEnabled() {
global $wgPdfProcessor, $wgPdfPostProcessor, $wgPdfInfo;
if ( !isset( $wgPdfProcessor ) || !isset( $wgPdfPostProcessor ) || !isset( $wgPdfInfo ) ) {
wfDebug( "PdfHandler is disabled, please set the following\n" );
wfDebug( "variables in LocalSettings.php:\n" );
wfDebug( "\$wgPdfProcessor, \$wgPdfPostProcessor, \$wgPdfInfo\n" );
return false;
}
return true;
}
/**
* @param $file
* @return bool
*/
function mustRender( $file ) {
return true;
}
/**
* @param $file
* @return bool
*/
function isMultiPage( $file ) {
return true;
}
/**
* @param $name
* @param $value
* @return bool
*/
function validateParam( $name, $value ) {
if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
return ( $value > 0 );
}
return false;
}
/**
* @param $params array
* @return bool|string
*/
function makeParamString( $params ) {
$page = isset( $params['page'] ) ? $params['page'] : 1;
if ( !isset( $params['width'] ) ) {
return false;
}
return "page{$page}-{$params['width']}px";
}
/**
* @param $str string
* @return array|bool
*/
function parseParamString( $str ) {
$m = false;
if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
return array( 'width' => $m[2], 'page' => $m[1] );
}
return false;
}
/**
* @param $params array
* @return array
*/
function getScriptParams( $params ) {
return array(
'width' => $params['width'],
'page' => $params['page'],
);
}
/**
* @return array
*/
function getParamMap() {
return array(
'img_width' => 'width',
'img_page' => 'page',
);
}
/**
* @param $width
* @param $height
* @param $msg
* @return MediaTransformError
*/
protected function doThumbError( $width, $height, $msg ) {
return new MediaTransformError( 'thumbnail_error',
$width, $height, wfMessage( $msg )->inContentLanguage()->text() );
}
/**
* @param $image File
* @param $dstPath string
* @param $dstUrl string
* @param $params array
* @param $flags int
* @return MediaTransformError|MediaTransformOutput|ThumbnailImage|TransformParameterError
*/
function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
global $wgPdfProcessor, $wgPdfPostProcessor, $wgPdfHandlerDpi;
$metadata = $image->getMetadata();
if ( !$metadata ) {
return $this->doThumbError(
isset( $params['width'] ) ? $params['width'] : null,
isset( $params['height'] ) ? $params['height'] : null,
'pdf_no_metadata'
);
}
if ( !$this->normaliseParams( $image, $params ) ) {
return new TransformParameterError( $params );
}
$width = $params['width'];
$height = $params['height'];
$page = $params['page'];
if ( $page > $this->pageCount( $image ) ) {
return $this->doThumbError( $width, $height, 'pdf_page_error' );
}
if ( $flags & self::TRANSFORM_LATER ) {
return new ThumbnailImage( $image, $dstUrl, $width, $height, false, $page );
}
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
return $this->doThumbError( $width, $height, 'thumbnail_dest_directory' );
}
$srcPath = $image->getLocalRefPath();
$cmd = '(' . wfEscapeShellArg(
$wgPdfProcessor,
"-sDEVICE=jpeg",
"-sOutputFile=-",
"-dFirstPage={$page}",
"-dLastPage={$page}",
"-r{$wgPdfHandlerDpi}",
"-dBATCH",
"-dNOPAUSE",
"-q",
$srcPath
);
$cmd .= " | " . wfEscapeShellArg(
$wgPdfPostProcessor,
"-depth",
"8",
"-resize",
$width,
"-",
$dstPath
);
$cmd .= ") 2>&1";
wfProfileIn( 'PdfHandler' );
wfDebug( __METHOD__ . ": $cmd\n" );
$retval = '';
$err = wfShellExec( $cmd, $retval );
wfProfileOut( 'PdfHandler' );
$removed = $this->removeBadFile( $dstPath, $retval );
if ( $retval != 0 || $removed ) {
wfDebugLog( 'thumbnail',
sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
wfHostname(), $retval, trim( $err ), $cmd ) );
return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
} else {
return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
}
}
/**
* @param $image File
* @param $path string
* @return PdfImage
*/
function getPdfImage( $image, $path ) {
if ( !$image ) {
$pdfimg = new PdfImage( $path );
} elseif ( !isset( $image->pdfImage ) ) {
$pdfimg = $image->pdfImage = new PdfImage( $path );
} else {
$pdfimg = $image->pdfImage;
}
return $pdfimg;
}
/**
* @param $image File
* @return bool
*/
function getMetaArray( $image ) {
if ( isset( $image->pdfMetaArray ) ) {
return $image->pdfMetaArray;
}
$metadata = $image->getMetadata();
if ( !$this->isMetadataValid( $image, $metadata ) ) {
wfDebug( "Pdf metadata is invalid or missing, should have been fixed in upgradeRow\n" );
return false;
}
wfProfileIn( __METHOD__ );
wfSuppressWarnings();
$image->pdfMetaArray = unserialize( $metadata );
wfRestoreWarnings();
wfProfileOut( __METHOD__ );
return $image->pdfMetaArray;
}
/**
* @param $image File
* @param $path string
* @return array|bool
*/
function getImageSize( $image, $path ) {
return $this->getPdfImage( $image, $path )->getImageSize();
}
/**
* @param $ext
* @param $mime string
* @param $params null
* @return array
*/
function getThumbType( $ext, $mime, $params = null ) {
global $wgPdfOutputExtension;
static $mime;
if ( !isset( $mime ) ) {
$magic = MimeMagic::singleton();
$mime = $magic->guessTypesForExtension( $wgPdfOutputExtension );
}
return array( $wgPdfOutputExtension, $mime );
}
/**
* @param $image File
* @param $path string
* @return string
*/
function getMetadata( $image, $path ) {
return serialize( $this->getPdfImage( $image, $path )->retrieveMetaData() );
}
/**
* @param $image File
* @param $metadata string
* @return bool
*/
function isMetadataValid( $image, $metadata ) {
if ( !$metadata || $metadata === serialize(array()) ) {
return self::METADATA_BAD;
} elseif ( strpos( $metadata, 'mergedMetadata' ) === false ) {
return self::METADATA_COMPATIBLE;
}
return self::METADATA_GOOD;
}
/**
* @param $image File
* @return bool|int
*/
function formatMetadata( $image ) {
$meta = $image->getMetadata();
if ( !$meta ) {
return false;
}
wfSuppressWarnings();
$meta = unserialize( $meta );
wfRestoreWarnings();
if ( !isset( $meta['mergedMetadata'] )
|| !is_array( $meta['mergedMetadata'] )
|| count( $meta['mergedMetadata'] ) < 1
) {
return false;
}
// Inherited from MediaHandler.
return $this->formatMetadataHelper( $meta['mergedMetadata'] );
}
/**
* @param $image
* @return bool|int
*/
function pageCount( $image ) {
$data = $this->getMetaArray( $image );
if ( !$data || !isset( $data['Pages'] ) ) {
return false;
}
return intval( $data['Pages'] );
}
/**
* @param $image File
* @param $page int
* @return array|bool
*/
function getPageDimensions( $image, $page ) {
$data = $this->getMetaArray( $image );
return PdfImage::getPageSize( $data, $page );
}
/**
* @param $image File
* @param $page int
* @return bool
*/
function getPageText( $image, $page ) {
$data = $this->getMetaArray( $image, true );
if ( !$data || !isset( $data['text'] ) || !isset( $data['text'][$page - 1] ) ) {
return false;
}
return $data['text'][$page - 1];
}
}
|