Index: trunk/extensions/PdfHandler/PdfImage.php |
— | — | @@ -1,104 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | - /** |
5 | | - * |
6 | | - * Copyright (C) 2007 Xarax <jodeldi@gmx.de> |
7 | | - * |
8 | | - * This program is free software; you can redistribute it and/or modify |
9 | | - * it under the terms of the GNU General Public License as published by |
10 | | - * the Free Software Foundation; either version 2 of the License, or |
11 | | - * (at your option) any later version. |
12 | | - * |
13 | | - * This program is distributed in the hope that it will be useful, |
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | - * GNU General Public License for more details. |
17 | | - * |
18 | | - * You should have received a copy of the GNU General Public License along |
19 | | - * with this program; if not, write to the Free Software Foundation, Inc., |
20 | | - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | | - * http://www.gnu.org/copyleft/gpl.html |
22 | | - * |
23 | | - */ |
24 | | - |
25 | | - /** |
26 | | - * inspired by djvuimage from brion vibber |
27 | | - * modified and written by xarax |
28 | | - */ |
29 | | - |
30 | | -class PdfImage { |
31 | | - |
32 | | - function __construct( $filename ) { |
33 | | - $this->mFilename = $filename; |
34 | | - } |
35 | | - |
36 | | - public function isValid() { |
37 | | - return true; |
38 | | - } |
39 | | - |
40 | | - public function getImageSize() { |
41 | | - global $wgPdfHandlerDpi; |
42 | | - |
43 | | - $tree = new SimpleXMLElement( $this->retrieveMetadata( ) ); |
44 | | - |
45 | | - if ( !$tree ) return false; |
46 | | - |
47 | | - $o = $tree->BODY[0]->Pagesize; |
48 | | - |
49 | | - if ( $o ) { |
50 | | - $size = explode("x", $o, 2); |
51 | | - |
52 | | - if ( $size ) { |
53 | | - $width = intval( round( trim($size[0]) / 72 * $wgPdfHandlerDpi ) ); |
54 | | - $height = explode( " ", trim($size[1]), 2 ); |
55 | | - $height = intval( round( trim($height[0]) / 72 * $wgPdfHandlerDpi ) ); |
56 | | - |
57 | | - return array( $width, $height, 'Pdf', |
58 | | - "width=\"$width\" height=\"$height\"" ); |
59 | | - } |
60 | | - } |
61 | | - return false; |
62 | | - } |
63 | | - |
64 | | - function retrieveMetaData() { |
65 | | - global $wgPdfInfo; |
66 | | - |
67 | | - if ( $wgPdfInfo ) { |
68 | | - wfProfileIn( 'pdfinfo' ); |
69 | | - $cmd = "(" . wfEscapeShellArg( $wgPdfInfo ) . " " . $this->mFilename . ")"; |
70 | | - $dump = wfShellExec( $cmd, $retval ); |
71 | | - $xml = $this->convertDumpToXML( $dump ); |
72 | | - wfProfileOut( 'pdfinfo' ); |
73 | | - } else { |
74 | | - $xml = null; |
75 | | - } |
76 | | - return $xml; |
77 | | - } |
78 | | - |
79 | | - function createMetadataBody() { |
80 | | - $xml = "<?xml version=\"1.0\" ?>\n"; |
81 | | - $xml .= "<!DOCTYPE PdfXML PUBLIC \"-//W3C//DTD PdfXML 1.1//EN\" \"pubtext/PdfXML-s.dtd\">\n"; |
82 | | - $xml .= "<PdfXML>\n"; |
83 | | - $xml .= "<HEAD></HEAD>\n"; |
84 | | - $xml .= "<BODY>\n"; |
85 | | - $xml .= "</BODY></PdfXML>"; |
86 | | - return $xml; |
87 | | - } |
88 | | - |
89 | | - function convertDumpToXML( $dump ) { |
90 | | - if ( strval( $dump ) == '' ) return false; |
91 | | - |
92 | | - $xml = $this->createMetadataBody(); |
93 | | - $doc = new SimpleXMLElement($xml); |
94 | | - |
95 | | - if (!$doc) return; |
96 | | - |
97 | | - $lines = explode("\n", $dump); |
98 | | - |
99 | | - for ($i = 1; $i < count($lines); $i++) { |
100 | | - $value = explode(':', trim($lines[$i]), 2); |
101 | | - $doc->BODY[0]->addChild(str_replace(' ', '', $value[0]), trim($value[1])); |
102 | | - } |
103 | | - return $doc->asXML(); |
104 | | - } |
105 | | -} |
Index: trunk/extensions/PdfHandler/PdfLoader.php |
— | — | @@ -1,61 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | - /** |
5 | | - * |
6 | | - * Copyright (C) 2007 Martin Seidel <jodeldi@gmx.de> |
7 | | - * |
8 | | - * This program is free software; you can redistribute it and/or modify |
9 | | - * it under the terms of the GNU General Public License as published by |
10 | | - * the Free Software Foundation; either version 2 of the License, or |
11 | | - * (at your option) any later version. |
12 | | - * |
13 | | - * This program is distributed in the hope that it will be useful, |
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | - * GNU General Public License for more details. |
17 | | - * |
18 | | - * You should have received a copy of the GNU General Public License along |
19 | | - * with this program; if not, write to the Free Software Foundation, Inc., |
20 | | - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | | - * http://www.gnu.org/copyleft/gpl.html |
22 | | - * |
23 | | - */ |
24 | | - |
25 | | - # Not a valid entry point, skip unless MEDIAWIKI is defined |
26 | | - if (!defined('MEDIAWIKI')) { |
27 | | - echo "PdfHandler extension"; |
28 | | - exit(1); |
29 | | - } |
30 | | - |
31 | | - $wgExtensionCredits['other'][] = array( |
32 | | - 'name'=>'PDF Handler', |
33 | | - 'author'=>'Xarax', |
34 | | - 'description'=>"Handler for viewing PDF files in image mode", |
35 | | - 'url' => 'http://www.mediawiki.org/wiki/Extension:PdfHandler', |
36 | | - ); |
37 | | - |
38 | | - if (!isset($wgPdfOutputExtension)) $wgPdfOutputExtension = "jpg"; |
39 | | - if (!isset($wgPdfHandlerDpi)) $wgPdfHandlerDpi = 150; |
40 | | - |
41 | | - $wgAutoloadClasses['PdfImage'] = dirname(__FILE__) . '/PdfImage.php'; |
42 | | - $wgAutoloadClasses['PdfHandler'] = dirname(__FILE__) . '/PdfHandler.php'; |
43 | | - $wgMediaHandlers['application/pdf'] = 'PdfHandler'; |
44 | | - $wgExtensionFunctions[] = 'wfPdfHandlerLoadMessages'; |
45 | | - |
46 | | - /* load messages */ |
47 | | - function wfPdfHandlerLoadMessages() { |
48 | | - global $wgMessageCache; |
49 | | - static $msgLoaded = false; |
50 | | - |
51 | | - if ( $msgLoaded ) |
52 | | - return false; |
53 | | - |
54 | | - $msgLoaded = true; |
55 | | - require( dirname( __FILE__ ) . '/PdfHandler_i18n.php' ); |
56 | | - |
57 | | - foreach ( efPdfHandlerMessages() as $lang => $messagesForLang ) { |
58 | | - $wgMessageCache->addMessages( $messagesForLang, $lang ); |
59 | | - } |
60 | | - |
61 | | - return true; |
62 | | - } |
Index: trunk/extensions/PdfHandler/PdfHandler_i18n.php |
— | — | @@ -1,26 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Internationalisation file for PDFHandler extension |
6 | | - */ |
7 | | -function efPdfHandlerMessages() { |
8 | | - |
9 | | - $messages = array( |
10 | | - 'en' => array( |
11 | | - 'pdf_no_xml' => 'cannot get metadata from pdf', |
12 | | - 'pdf_page_error' => 'page number not in range', |
13 | | - ), |
14 | | - |
15 | | - 'de' => array( |
16 | | - 'pdf_no_xml' => 'Keine Metadaten im PDF vorhanden.', |
17 | | - 'pdf_page_error' => 'Seitenzahl außerhalb des Dokumentes.', |
18 | | - ), |
19 | | - |
20 | | - 'nl' => array( |
21 | | - 'pdf_no_xml' => 'de metadata van de PDF kan niet uitgelezen worden', |
22 | | - 'pdf_page_error' => 'paginanummer komt niet voor in document', |
23 | | - ), |
24 | | -); |
25 | | - |
26 | | - return $messages; |
27 | | -} |
Index: trunk/extensions/PdfHandler/PdfHandler_body.php |
— | — | @@ -0,0 +1,235 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | + /** |
| 5 | + * |
| 6 | + * Copyright (C) 2007 Martin Seidel (Xarax) <jodeldi@gmx.de> |
| 7 | + * |
| 8 | + * Inspired by djvuhandler from Tim Starling |
| 9 | + * Modified and written by Xarax |
| 10 | + * |
| 11 | + * This program is free software; you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation; either version 2 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License along |
| 22 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | + * http://www.gnu.org/copyleft/gpl.html |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +class PdfHandler extends ImageHandler { |
| 29 | + |
| 30 | + function isEnabled() { |
| 31 | + global $wgPdfProcessor; |
| 32 | + global $wgPdfPostProcessor; |
| 33 | + global $wgPdfInfo; |
| 34 | + |
| 35 | + if ( !isset( $wgPdfProcessor ) || !isset( $wgPdfPostProcessor) || !isset( $wgPdfInfo ) ) { |
| 36 | + wfDebug( "PdfHandler is disabled, please set the following\n" ); |
| 37 | + wfDebug( "variables in LocalSettings.php:\n" ); |
| 38 | + wfDebug( "\$wgPdfProcessor, \$wgPdfPostProcessor, \$wgPdfInfo\n" ); |
| 39 | + return false; |
| 40 | + } |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + function mustRender() { return true; } |
| 45 | + |
| 46 | + function isMultiPage() { return true; } |
| 47 | + |
| 48 | + function validateParam( $name, $value ) { |
| 49 | + if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) |
| 50 | + return ( $value <= 0 ) ? false : true; |
| 51 | + else |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + function makeParamString( $params ) { |
| 56 | + $page = isset( $params['page'] ) ? $params['page'] : 1; |
| 57 | + if ( !isset( $params['width'] ) ) return false; |
| 58 | + return "page{$page}-{$params['width']}px"; |
| 59 | + } |
| 60 | + |
| 61 | + function parseParamString( $str ) { |
| 62 | + $m = false; |
| 63 | + |
| 64 | + if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) |
| 65 | + return array( 'width' => $m[2], 'page' => $m[1] ); |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + function getScriptParams( $params ) { |
| 71 | + return array( |
| 72 | + 'width' => $params['width'], |
| 73 | + 'page' => $params['page'], |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + function getParamMap() { |
| 78 | + return array( |
| 79 | + 'img_width' => 'width', |
| 80 | + 'img_page' => 'page', |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
| 85 | + global $wgPdfProcessor; |
| 86 | + global $wgPdfPostProcessor; |
| 87 | + global $wgPdfHandlerDpi; |
| 88 | + |
| 89 | + $xml = $image->getMetadata(); |
| 90 | + |
| 91 | + if ( !$xml ) |
| 92 | + return new MediaTransformError( 'thumbnail_error', |
| 93 | + @$params['width'], |
| 94 | + @$params['height'], |
| 95 | + wfMsg( 'pdf_no_xml' ) ); |
| 96 | + |
| 97 | + if ( !$this->normaliseParams( $image, $params ) ) |
| 98 | + return new TransformParameterError( $params ); |
| 99 | + |
| 100 | + $width = $params['width']; |
| 101 | + $height = $params['height']; |
| 102 | + $srcPath = $image->getPath(); |
| 103 | + $page = $params['page']; |
| 104 | + |
| 105 | + if ( $page > $this->pageCount( $image ) ) |
| 106 | + return new MediaTransformError( 'thumbnail_error', |
| 107 | + $width, |
| 108 | + $height, |
| 109 | + wfMsg( 'pdf_page_error' ) ); |
| 110 | + |
| 111 | + if ( $flags & self::TRANSFORM_LATER ) |
| 112 | + return new ThumbnailImage( $image, $dstUrl, $width, |
| 113 | + $height, $dstPath, $page ); |
| 114 | + |
| 115 | + if ( !wfMkdirParents( dirname( $dstPath ) ) ) |
| 116 | + return new MediaTransformError( 'thumbnail_error', |
| 117 | + $width, |
| 118 | + $height, |
| 119 | + wfMsg( 'thumbnail_dest_directory' ) ); |
| 120 | + |
| 121 | + $cmd = '(' . wfEscapeShellArg( $wgPdfProcessor ); |
| 122 | + $cmd .= " -sDEVICE=jpeg -sOutputFile=- -dFirstPage={$page} -dLastPage={$page}"; |
| 123 | + $cmd .= " -r{$wgPdfHandlerDpi} -dBATCH -dNOPAUSE -q ". wfEscapeShellArg( $srcPath ); |
| 124 | + $cmd .= " | " . wfEscapeShellArg( $wgPdfPostProcessor ); |
| 125 | + $cmd .= " -depth 8 -resize {$width} - "; |
| 126 | + $cmd .= wfEscapeShellArg( $dstPath ) . ")"; |
| 127 | + |
| 128 | + wfProfileIn( 'PdfHandler' ); |
| 129 | + wfDebug( __METHOD__.": $cmd\n" ); |
| 130 | + $err = wfShellExec( $cmd, $retval ); |
| 131 | + wfProfileOut( 'PdfHandler' ); |
| 132 | + |
| 133 | + $removed = $this->removeBadFile( $dstPath, $retval ); |
| 134 | + |
| 135 | + if ( $retval != 0 || $removed ) { |
| 136 | + wfDebugLog( 'thumbnail', |
| 137 | + sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"', |
| 138 | + wfHostname(), $retval, trim($err), $cmd ) ); |
| 139 | + return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); |
| 140 | + } else { |
| 141 | + return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page ); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + function getPdfImage( $image, $path ) { |
| 146 | + if ( !$image ) |
| 147 | + $pdfimg = new PdfImage( $path ); |
| 148 | + elseif ( !isset( $image->pdfImage ) ) |
| 149 | + $pdfimg = $image->pdfImage = new PdfImage( $path ); |
| 150 | + else |
| 151 | + $pdfimg = $image->pdfImage; |
| 152 | + |
| 153 | + return $pdfimg; |
| 154 | + } |
| 155 | + |
| 156 | + function getMetaTree( $image ) { |
| 157 | + if ( isset( $image->pdfMetaTree ) ) |
| 158 | + return $image->pdfMetaTree; |
| 159 | + |
| 160 | + $metadata = $image->getMetadata(); |
| 161 | + |
| 162 | + if ( !$this->isMetadataValid( $image, $metadata ) ) { |
| 163 | + wfDebug( "Pdf XML metadata is invalid or missing, should have been fixed in upgradeRow\n" ); |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + wfProfileIn( __METHOD__ ); |
| 168 | + wfSuppressWarnings(); |
| 169 | + |
| 170 | + try { |
| 171 | + $image->pdfMetaTree = new SimpleXMLElement( $metadata ); |
| 172 | + } catch( Exception $e ) { |
| 173 | + $image->pdfMetaTree = false; |
| 174 | + } |
| 175 | + |
| 176 | + wfRestoreWarnings(); |
| 177 | + wfProfileOut( __METHOD__ ); |
| 178 | + |
| 179 | + return $image->pdfMetaTree; |
| 180 | + } |
| 181 | + |
| 182 | + function getImageSize( $image, $path ) { |
| 183 | + return $this->getPdfImage( $image, $path )->getImageSize(); |
| 184 | + } |
| 185 | + |
| 186 | + function getThumbType( $ext, $mime ) { |
| 187 | + global $wgPdfOutputExtension; |
| 188 | + static $mime; |
| 189 | + |
| 190 | + if ( !isset( $mime ) ) { |
| 191 | + $magic = MimeMagic::singleton(); |
| 192 | + $mime = $magic->guessTypesForExtension( $wgPdfOutputExtension ); |
| 193 | + } |
| 194 | + return array( $wgPdfOutputExtension, $mime ); |
| 195 | + } |
| 196 | + |
| 197 | + function getMetadata( $image, $path ) { |
| 198 | + return $this->getPdfImage( $image, $path )->retrieveMetaData(); |
| 199 | + } |
| 200 | + |
| 201 | + function isMetadataValid( $image, $metadata ) { |
| 202 | + return !empty( $metadata ) && $metadata != serialize(array()); |
| 203 | + } |
| 204 | + |
| 205 | + function pageCount( $image ) { |
| 206 | + $tree = $this->getMetaTree( $image ); |
| 207 | + if ( !$tree ) return false; |
| 208 | + return intval( $tree->BODY[0]->Pages ); |
| 209 | + } |
| 210 | + |
| 211 | + function getPageDimensions( $image, $page ) { |
| 212 | + global $wgPdfHandlerDpi; |
| 213 | + |
| 214 | + $tree = $this->getMetaTree( $image ); |
| 215 | + |
| 216 | + if ( $tree ) { |
| 217 | + $o = $tree->BODY[0]->Pagesize; |
| 218 | + |
| 219 | + if ( $o ) { |
| 220 | + $size = explode( "x", $o, 2 ); |
| 221 | + |
| 222 | + if ( $size ) { |
| 223 | + $width = intval( trim( $size[0] ) / 72 * $wgPdfHandlerDpi ); |
| 224 | + $height = explode( " ", trim( $size[1] ), 2 ); |
| 225 | + $height = intval( trim( $height[0] ) / 72 * $wgPdfHandlerDpi ); |
| 226 | + |
| 227 | + return array( |
| 228 | + 'width' => $width, |
| 229 | + 'height' => $height |
| 230 | + ); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + return false; |
| 235 | + } |
| 236 | +} |
Property changes on: trunk/extensions/PdfHandler/PdfHandler_body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 237 | + native |
Index: trunk/extensions/PdfHandler/PdfHandler.i18n.php |
— | — | @@ -0,0 +1,21 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Internationalisation file for PDFHandler extension |
| 6 | + */ |
| 7 | +function efPdfHandlerMessages() { |
| 8 | + |
| 9 | + $messages = array( |
| 10 | + 'en' => array( |
| 11 | + 'pdf_no_xml' => 'cannot get metadata from pdf', |
| 12 | + 'pdf_page_error' => 'page number not in range', |
| 13 | + ), |
| 14 | + |
| 15 | + 'de' => array( |
| 16 | + 'pdf_no_xml' => 'Keine Metadaten im PDF vorhanden.', |
| 17 | + 'pdf_page_error' => 'Seitenzahl außerhalb des Dokumentes.', |
| 18 | + ), |
| 19 | +); |
| 20 | + |
| 21 | + return $messages; |
| 22 | +} |
Property changes on: trunk/extensions/PdfHandler/PdfHandler.i18n.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 23 | + native |
Index: trunk/extensions/PdfHandler/PdfHandler.php |
— | — | @@ -1,230 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | - /** |
5 | | - * |
6 | | - * Copyright (C) 2007 Xarax <jodeldi@gmx.de> |
7 | | - * |
8 | | - * This program is free software; you can redistribute it and/or modify |
9 | | - * it under the terms of the GNU General Public License as published by |
10 | | - * the Free Software Foundation; either version 2 of the License, or |
11 | | - * (at your option) any later version. |
12 | | - * |
13 | | - * This program is distributed in the hope that it will be useful, |
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | - * GNU General Public License for more details. |
17 | | - * |
18 | | - * You should have received a copy of the GNU General Public License along |
19 | | - * with this program; if not, write to the Free Software Foundation, Inc., |
20 | | - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | | - * http://www.gnu.org/copyleft/gpl.html |
22 | | - * |
23 | | - */ |
24 | | - |
25 | | - /* |
26 | | - * inspired by djvuhandler from tim starling |
27 | | - * modified and written by xarax |
28 | | - */ |
29 | | - |
30 | | -class PdfHandler extends ImageHandler { |
31 | | - |
32 | | - function isEnabled() { |
33 | | - global $wgPdfProcessor; |
34 | | - global $wgPdfPostProcessor; |
35 | | - global $wgPdfInfo; |
36 | | - |
37 | | - if ( !isset( $wgPdfProcessor ) || !isset( $wgPdfPostProcessor) || !isset( $wgPdfInfo ) ) { |
38 | | - wfDebug( "PdfHandler is disabled, please set the following\n" ); |
39 | | - wfDebug( "variables in LocalSettings.php:\n" ); |
40 | | - wfDebug( "\$wgPdfProcessor, \$wgPdfPostProcessor, \$wgPdfInfo\n" ); |
41 | | - return false; |
42 | | - } |
43 | | - return true; |
44 | | - } |
45 | | - |
46 | | - function mustRender() { return true; } |
47 | | - |
48 | | - function isMultiPage() { return true; } |
49 | | - |
50 | | - function validateParam( $name, $value ) { |
51 | | - if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) |
52 | | - return ( $value <= 0 ) ? false : true; |
53 | | - else |
54 | | - return false; |
55 | | - } |
56 | | - |
57 | | - function makeParamString( $params ) { |
58 | | - $page = isset( $params['page'] ) ? $params['page'] : 1; |
59 | | - if ( !isset( $params['width'] ) ) return false; |
60 | | - return "page{$page}-{$params['width']}px"; |
61 | | - } |
62 | | - |
63 | | - function parseParamString( $str ) { |
64 | | - $m = false; |
65 | | - |
66 | | - if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) |
67 | | - return array( 'width' => $m[2], 'page' => $m[1] ); |
68 | | - |
69 | | - return false; |
70 | | - } |
71 | | - |
72 | | - function getScriptParams( $params ) { |
73 | | - return array( |
74 | | - 'width' => $params['width'], |
75 | | - 'page' => $params['page'], |
76 | | - ); |
77 | | - } |
78 | | - |
79 | | - function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
80 | | - global $wgPdfProcessor; |
81 | | - global $wgPdfPostProcessor; |
82 | | - global $wgPdfHandlerDpi; |
83 | | - |
84 | | - $xml = $image->getMetadata(); |
85 | | - |
86 | | - if ( !$xml ) |
87 | | - return new MediaTransformError( 'thumbnail_error', |
88 | | - @$params['width'], |
89 | | - @$params['height'], |
90 | | - wfMsg( 'pdf_no_xml' ) ); |
91 | | - |
92 | | - if ( !$this->normaliseParams( $image, $params ) ) |
93 | | - return new TransformParameterError( $params ); |
94 | | - |
95 | | - $width = $params['width']; |
96 | | - $height = $params['height']; |
97 | | - $srcPath = $image->getPath(); |
98 | | - $page = $params['page']; |
99 | | - |
100 | | - if ( $page > $this->pageCount( $image ) ) |
101 | | - return new MediaTransformError( 'thumbnail_error', |
102 | | - $width, |
103 | | - $height, |
104 | | - wfMsg( 'pdf_page_error' ) ); |
105 | | - |
106 | | - if ( $flags & self::TRANSFORM_LATER ) |
107 | | - return new ThumbnailImage( $image, $dstUrl, $width, |
108 | | - $height, $dstPath, $page ); |
109 | | - |
110 | | - if ( !wfMkdirParents( dirname( $dstPath ) ) ) |
111 | | - return new MediaTransformError( 'thumbnail_error', |
112 | | - $width, |
113 | | - $height, |
114 | | - wfMsg( 'thumbnail_dest_directory' ) ); |
115 | | - |
116 | | - $cmd = '(' . wfEscapeShellArg( $wgPdfProcessor ); |
117 | | - $cmd .= " -sDEVICE=jpeg -sOutputFile=- -dFirstPage={$page} -dLastPage={$page}"; |
118 | | - $cmd .= " -r{$wgPdfHandlerDpi} -dBATCH -dNOPAUSE -q ". wfEscapeShellArg( $srcPath ); |
119 | | - $cmd .= " | " . wfEscapeShellArg( $wgPdfPostProcessor ); |
120 | | - $cmd .= " -depth 8 -resize {$width} - "; |
121 | | - $cmd .= wfEscapeShellArg( $dstPath ) . ")"; |
122 | | - |
123 | | - wfProfileIn( 'PdfHandler' ); |
124 | | - wfDebug( __METHOD__.": $cmd\n" ); |
125 | | - $err = wfShellExec( $cmd, $retval ); |
126 | | - wfProfileOut( 'PdfHandler' ); |
127 | | - |
128 | | - $removed = $this->removeBadFile( $dstPath, $retval ); |
129 | | - |
130 | | - if ( $retval != 0 || $removed ) { |
131 | | - wfDebugLog( 'thumbnail', |
132 | | - sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"', |
133 | | - wfHostname(), $retval, trim($err), $cmd ) ); |
134 | | - return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); |
135 | | - } else { |
136 | | - return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page ); |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - function getPdfImage( $image, $path ) { |
141 | | - if ( !$image ) |
142 | | - $pdfimg = new PdfImage( $path ); |
143 | | - elseif ( !isset( $image->pdfImage ) ) |
144 | | - $pdfimg = $image->pdfImage = new PdfImage( $path ); |
145 | | - else |
146 | | - $pdfimg = $image->pdfImage; |
147 | | - |
148 | | - return $pdfimg; |
149 | | - } |
150 | | - |
151 | | - function getMetaTree( $image ) { |
152 | | - if ( isset( $image->pdfMetaTree ) ) |
153 | | - return $image->pdfMetaTree; |
154 | | - |
155 | | - $metadata = $image->getMetadata(); |
156 | | - |
157 | | - if ( !$this->isMetadataValid( $image, $metadata ) ) { |
158 | | - wfDebug( "Pdf XML metadata is invalid or missing, should have been fixed in upgradeRow\n" ); |
159 | | - return false; |
160 | | - } |
161 | | - |
162 | | - wfProfileIn( __METHOD__ ); |
163 | | - wfSuppressWarnings(); |
164 | | - |
165 | | - try { |
166 | | - $image->pdfMetaTree = new SimpleXMLElement( $metadata ); |
167 | | - } catch( Exception $e ) { |
168 | | - $image->pdfMetaTree = false; |
169 | | - } |
170 | | - |
171 | | - wfRestoreWarnings(); |
172 | | - wfProfileOut( __METHOD__ ); |
173 | | - |
174 | | - return $image->pdfMetaTree; |
175 | | - } |
176 | | - |
177 | | - function getImageSize( $image, $path ) { |
178 | | - return $this->getPdfImage( $image, $path )->getImageSize(); |
179 | | - } |
180 | | - |
181 | | - function getThumbType( $ext, $mime ) { |
182 | | - global $wgPdfOutputExtension; |
183 | | - static $mime; |
184 | | - |
185 | | - if ( !isset( $mime ) ) { |
186 | | - $magic = MimeMagic::singleton(); |
187 | | - $mime = $magic->guessTypesForExtension( $wgPdfOutputExtension ); |
188 | | - } |
189 | | - return array( $wgPdfOutputExtension, $mime ); |
190 | | - } |
191 | | - |
192 | | - function getMetadata( $image, $path ) { |
193 | | - return $this->getPdfImage( $image, $path )->retrieveMetaData(); |
194 | | - } |
195 | | - |
196 | | - function isMetadataValid( $image, $metadata ) { |
197 | | - return !empty( $metadata ) && $metadata != serialize(array()); |
198 | | - } |
199 | | - |
200 | | - function pageCount( $image ) { |
201 | | - $tree = $this->getMetaTree( $image ); |
202 | | - if ( !$tree ) return false; |
203 | | - return intval( $tree->BODY[0]->Pages ); |
204 | | - } |
205 | | - |
206 | | - function getPageDimensions( $image, $page ) { |
207 | | - global $wgPdfHandlerDpi; |
208 | | - |
209 | | - $tree = $this->getMetaTree( $image ); |
210 | | - |
211 | | - if ( $tree ) { |
212 | | - $o = $tree->BODY[0]->Pagesize; |
213 | | - |
214 | | - if ( $o ) { |
215 | | - $size = explode("x", $o, 2); |
216 | | - |
217 | | - if ( $size ) { |
218 | | - $width = intval( trim($size[0]) / 72 * $wgPdfHandlerDpi ); |
219 | | - $height = explode( " ", trim($size[1]), 2 ); |
220 | | - $height = intval( trim($height[0]) / 72 * $wgPdfHandlerDpi ); |
221 | | - |
222 | | - return array( |
223 | | - 'width' => $width, |
224 | | - 'height' => $height |
225 | | - ); |
226 | | - } |
227 | | - } |
228 | | - } |
229 | | - return false; |
230 | | - } |
231 | | -} |
Index: trunk/extensions/PdfHandler/PdfHandler.php |
— | — | @@ -0,0 +1,61 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | + /** |
| 5 | + * |
| 6 | + * Copyright (C) 2007 Martin Seidel (Xarax) <jodeldi@gmx.de> |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License along |
| 19 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + * http://www.gnu.org/copyleft/gpl.html |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | + # Not a valid entry point, skip unless MEDIAWIKI is defined |
| 26 | + if (!defined('MEDIAWIKI')) { |
| 27 | + echo "PdfHandler extension"; |
| 28 | + exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + $wgExtensionCredits['other'][] = array( |
| 32 | + 'name' => 'PDF Handler', |
| 33 | + 'author' =>' Xarax', |
| 34 | + 'description' => 'Handler for viewing PDF files in image mode', |
| 35 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:PdfHandler', |
| 36 | + ); |
| 37 | + |
| 38 | + if ( !isset( $wgPdfOutputExtension ) ) $wgPdfOutputExtension = "jpg"; |
| 39 | + if ( !isset( $wgPdfHandlerDpi ) ) $wgPdfHandlerDpi = 150; |
| 40 | + |
| 41 | + $wgAutoloadClasses['PdfImage'] = dirname(__FILE__) . '/PdfHandler.image.php'; |
| 42 | + $wgAutoloadClasses['PdfHandler'] = dirname(__FILE__) . '/PdfHandler_body.php'; |
| 43 | + $wgMediaHandlers['application/pdf'] = 'PdfHandler'; |
| 44 | + $wgExtensionFunctions[] = 'wfPdfHandlerLoadMessages'; |
| 45 | + |
| 46 | + /* load messages */ |
| 47 | + function wfPdfHandlerLoadMessages() { |
| 48 | + global $wgMessageCache; |
| 49 | + static $msgLoaded = false; |
| 50 | + |
| 51 | + if ( $msgLoaded ) |
| 52 | + return false; |
| 53 | + |
| 54 | + $msgLoaded = true; |
| 55 | + require( dirname( __FILE__ ) . '/PdfHandler.i18n.php' ); |
| 56 | + |
| 57 | + foreach ( efPdfHandlerMessages() as $lang => $messagesForLang ) { |
| 58 | + $wgMessageCache->addMessages( $messagesForLang, $lang ); |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
Property changes on: trunk/extensions/PdfHandler/PdfHandler.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 63 | + native |
Index: trunk/extensions/PdfHandler/PdfHandler.image.php |
— | — | @@ -0,0 +1,104 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | + /** |
| 5 | + * |
| 6 | + * Copyright (C) 2007 Xarax <jodeldi@gmx.de> |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License along |
| 19 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + * http://www.gnu.org/copyleft/gpl.html |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | + /** |
| 26 | + * inspired by djvuimage from brion vibber |
| 27 | + * modified and written by xarax |
| 28 | + */ |
| 29 | + |
| 30 | +class PdfImage { |
| 31 | + |
| 32 | + function __construct( $filename ) { |
| 33 | + $this->mFilename = $filename; |
| 34 | + } |
| 35 | + |
| 36 | + public function isValid() { |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + public function getImageSize() { |
| 41 | + global $wgPdfHandlerDpi; |
| 42 | + |
| 43 | + $tree = new SimpleXMLElement( $this->retrieveMetadata( ) ); |
| 44 | + |
| 45 | + if ( !$tree ) return false; |
| 46 | + |
| 47 | + $o = $tree->BODY[0]->Pagesize; |
| 48 | + |
| 49 | + if ( $o ) { |
| 50 | + $size = explode("x", $o, 2); |
| 51 | + |
| 52 | + if ( $size ) { |
| 53 | + $width = intval( round( trim($size[0]) / 72 * $wgPdfHandlerDpi ) ); |
| 54 | + $height = explode( " ", trim($size[1]), 2 ); |
| 55 | + $height = intval( round( trim($height[0]) / 72 * $wgPdfHandlerDpi ) ); |
| 56 | + |
| 57 | + return array( $width, $height, 'Pdf', |
| 58 | + "width=\"$width\" height=\"$height\"" ); |
| 59 | + } |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + function retrieveMetaData() { |
| 65 | + global $wgPdfInfo; |
| 66 | + |
| 67 | + if ( $wgPdfInfo ) { |
| 68 | + wfProfileIn( 'pdfinfo' ); |
| 69 | + $cmd = "(" . wfEscapeShellArg( $wgPdfInfo ) . " " . $this->mFilename . ")"; |
| 70 | + $dump = wfShellExec( $cmd, $retval ); |
| 71 | + $xml = $this->convertDumpToXML( $dump ); |
| 72 | + wfProfileOut( 'pdfinfo' ); |
| 73 | + } else { |
| 74 | + $xml = null; |
| 75 | + } |
| 76 | + return $xml; |
| 77 | + } |
| 78 | + |
| 79 | + function createMetadataBody() { |
| 80 | + $xml = "<?xml version=\"1.0\" ?>\n"; |
| 81 | + $xml .= "<!DOCTYPE PdfXML PUBLIC \"-//W3C//DTD PdfXML 1.1//EN\" \"pubtext/PdfXML-s.dtd\">\n"; |
| 82 | + $xml .= "<PdfXML>\n"; |
| 83 | + $xml .= "<HEAD></HEAD>\n"; |
| 84 | + $xml .= "<BODY>\n"; |
| 85 | + $xml .= "</BODY></PdfXML>"; |
| 86 | + return $xml; |
| 87 | + } |
| 88 | + |
| 89 | + function convertDumpToXML( $dump ) { |
| 90 | + if ( strval( $dump ) == '' ) return false; |
| 91 | + |
| 92 | + $xml = $this->createMetadataBody(); |
| 93 | + $doc = new SimpleXMLElement($xml); |
| 94 | + |
| 95 | + if (!$doc) return; |
| 96 | + |
| 97 | + $lines = explode("\n", $dump); |
| 98 | + |
| 99 | + for ($i = 1; $i < count($lines); $i++) { |
| 100 | + $value = explode(':', trim($lines[$i]), 2); |
| 101 | + $doc->BODY[0]->addChild(str_replace(' ', '', $value[0]), trim($value[1])); |
| 102 | + } |
| 103 | + return $doc->asXML(); |
| 104 | + } |
| 105 | +} |
Property changes on: trunk/extensions/PdfHandler/PdfHandler.image.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 106 | + native |