Révision 76bdcd04
Ajouté par Assos Assos il y a presque 6 ans
drupal7/sites/all/modules/print/print_pdf/print_pdf.drush.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* drush integration for print_pdf module PDF libraries download. |
|
5 |
* Provide drush integration for print_pdf module PDF libraries download.
|
|
6 | 6 |
*/ |
7 | 7 |
|
8 |
/** |
|
9 |
* The PDF project download URL |
|
10 |
*/ |
|
11 |
|
|
12 |
// TCPDF is in sourceforge, and nicely provides a link to the latest version |
|
13 |
define('TCPDF_DOWNLOAD_URI', 'http://sourceforge.net/projects/tcpdf/files/latest'); |
|
14 |
|
|
15 |
// URI to the the latest dompdf version.. Hardcoded version unfortunately |
|
16 |
define('DOMPDF_DOWNLOAD_URI', 'https://github.com/dompdf/dompdf/releases/download/v0.6.1/dompdf-0.6.1.zip'); |
|
17 |
|
|
18 |
// wkhtmltopdf is a binary, requiring a different download for each platform |
|
19 |
define('WKHTMLTOPDF_AMD64_DOWNLOAD_URI', 'http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.0/wkhtmltox-linux-amd64_0.12.0-03c001d.tar.xz'); |
|
20 |
define('WKHTMLTOPDF_I386_DOWNLOAD_URI', 'http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.0/wkhtmltox-linux-i386_0.12.0-03c001d.tar.xz'); |
|
21 |
define('WKHTMLTOPDF_WIN64_DOWNLOAD_URI', 'http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.0/wkhtmltox-win64_0.12.0-03c001d.exe'); |
|
22 |
define('WKHTMLTOPDF_WIN_DOWNLOAD_URI', 'http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.0/wkhtmltox-win32_0.12.0-03c001d.exe'); |
|
23 |
define('WKHTMLTOPDF_OSX_DOWNLOAD_URI', 'http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-OSX-0.10.0_rc2-static.tar.bz2'); |
|
24 |
|
|
25 | 8 |
/** |
26 | 9 |
* Implements hook_drush_command(). |
27 | 10 |
*/ |
28 | 11 |
function print_pdf_drush_command() { |
29 | 12 |
$items = array(); |
30 | 13 |
|
14 |
$pdf_libs = array(); |
|
15 |
drush_command_invoke_all_ref('drush_pdf_libs_alter', $pdf_libs); |
|
16 |
|
|
31 | 17 |
$items['print-pdf-download'] = array( |
32 |
'description' => 'Download a PDF library.', |
|
18 |
'description' => 'Download and extract a PDF library.',
|
|
33 | 19 |
'arguments' => array( |
34 |
'library' => dt('The PDF library to download. Either tcpdf, dompdf or wkhtmltopdf.'),
|
|
20 |
'library' => dt('The PDF library to download. Available choices: !libs.', array('!libs' => implode(', ', array_keys($pdf_libs)))),
|
|
35 | 21 |
), |
36 | 22 |
'options' => array( |
37 | 23 |
'path' => dt('A path to the download folder. If omitted Drush will use the default location (@path).', array('@path' => 'sites/all/libraries')), |
38 | 24 |
), |
39 | 25 |
'aliases' => array('pdfdl'), |
40 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, // No site or config needed. |
|
26 |
// No site or config needed. |
|
27 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, |
|
41 | 28 |
); |
42 | 29 |
|
43 | 30 |
return $items; |
44 | 31 |
} |
45 | 32 |
|
46 | 33 |
/** |
47 |
* Download and extract PDF archive. |
|
48 |
*/ |
|
49 |
function drush_print_pdf_download($library) { |
|
50 |
if (isset($library)) { |
|
51 |
$download_url = _drush_print_pdf_download_url($library); |
|
52 |
if ($download_url) { |
|
53 |
$path = drush_get_option('path'); |
|
54 |
if (empty($path)) { |
|
55 |
$path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites/all/libraries'; |
|
56 |
} |
|
57 |
|
|
58 |
// Create the path if it does not exist. |
|
59 |
if (!is_dir($path)) { |
|
60 |
drush_op('mkdir', $path); |
|
61 |
drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice'); |
|
62 |
} |
|
63 |
|
|
64 |
// Chdir to the download location. |
|
65 |
$olddir = getcwd(); |
|
66 |
drush_op('chdir', $path); |
|
67 |
|
|
68 |
// Warn about an existing dir |
|
69 |
if (is_dir($library)) { |
|
70 |
// drush_op('rmdir', $library); // Directory must be empty for the php rmdir to work.. |
|
71 |
drush_log(dt('An existing @library was overwritten at @path', array('@library' => $library, '@path' => $path . '/' . $library)), 'notice'); |
|
72 |
} |
|
73 |
|
|
74 |
// Download the archive |
|
75 |
$filename = _drush_print_pdf_download_file($download_url); |
|
76 |
if ($filename) { |
|
77 |
$extract_ret = _drush_print_pdf_download_extract($filename); |
|
78 |
if ($extract_ret) { |
|
79 |
// Remove the archive |
|
80 |
drush_op('unlink', $filename); |
|
81 |
drush_log(dt('@file has been downloaded and extracted in @path', array('@file' => $filename, '@path' => $path)), 'success'); |
|
82 |
} |
|
83 |
else { |
|
84 |
drush_log(dt('@file has been downloaded to @path, but extract failed. Check that you have the necessary program installed, and if necessary extract it manually.', |
|
85 |
array('@file' => $filename, '@path' => $path)), 'warning'); |
|
86 |
} |
|
87 |
} |
|
88 |
else { |
|
89 |
drush_log(dt('Drush was unable to download @library to @path', array('@library' => $library, '@path' => $path)), 'error'); |
|
90 |
} |
|
91 |
|
|
92 |
// Set working directory back to the previous working directory. |
|
93 |
drush_op('chdir', $olddir); |
|
94 |
} |
|
95 |
} |
|
96 |
else { |
|
97 |
drush_log(dt('Please specify a PDF library. Currently supported libraries are dompdf, tcpdf and wkhtmltopdf.'), 'error'); |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* Discover the correct URL of the package to download |
|
34 |
* Implements drush_hook_COMMAND_validate(). |
|
103 | 35 |
*/ |
104 |
function _drush_print_pdf_download_url($library) { |
|
105 |
$ret = FALSE; |
|
106 |
|
|
107 |
switch (drupal_strtolower($library)) { |
|
108 |
case 'dompdf': |
|
109 |
$ret = DOMPDF_DOWNLOAD_URI; |
|
110 |
break; |
|
111 |
case 'tcpdf': |
|
112 |
$ret = TCPDF_DOWNLOAD_URI; |
|
113 |
break; |
|
114 |
case 'wkhtmltopdf': |
|
115 |
switch (drupal_substr(php_uname('s'), 0, 3)) { |
|
116 |
case 'Lin': |
|
117 |
$ret = (php_uname('m') == 'x86_64') ? WKHTMLTOPDF_AMD64_DOWNLOAD_URI : WKHTMLTOPDF_I386_DOWNLOAD_URI; |
|
118 |
break; |
|
119 |
case 'Win': |
|
120 |
$ret = WKHTMLTOPDF_WIN_DOWNLOAD_URI; |
|
121 |
break; |
|
122 |
case 'Dar': |
|
123 |
$ret = WKHTMLTOPDF_OSX_DOWNLOAD_URI; |
|
124 |
break; |
|
125 |
default: |
|
126 |
drush_log(dt('wkhtmltopdf is not supported in this system, please choose another library.'), 'error'); |
|
127 |
break; |
|
128 |
} |
|
129 |
break; |
|
130 |
default: |
|
131 |
drush_log(dt('Unknown PDF library specified, please use one of the supported PDF libraries.'), 'error'); |
|
132 |
break; |
|
133 |
} |
|
134 |
|
|
135 |
return $ret; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Helper to download and extract the zip/tar archive. |
|
140 |
*/ |
|
141 |
function _drush_print_pdf_download_extract($filename) { |
|
142 |
$arch_ret = FALSE; |
|
143 |
|
|
144 |
if (drush_op('is_file', $filename)) { |
|
145 |
switch (drush_op('mime_content_type', $filename)) { |
|
146 |
case 1: |
|
147 |
$arch_ret = TRUE; |
|
148 |
break; |
|
149 |
case 'application/zip': |
|
150 |
// Decompress the zip archive |
|
151 |
$arch_ret = drush_shell_exec('unzip -qq -o ' . $filename); |
|
152 |
// ZIP archives usually get the access rights wrong |
|
153 |
drush_log(dt('@filename is a Zip file. Check the access permissions of the extracted files.', array('@filename' => $filename)), 'warning'); |
|
154 |
break; |
|
155 |
case 'application/x-gzip': |
|
156 |
// Decompress the tar gz archive |
|
157 |
$arch_ret = drush_shell_exec('tar xzf ' . $filename); |
|
158 |
break; |
|
159 |
case 'application/x-bzip2': |
|
160 |
// Decompress the tar bz2 archive |
|
161 |
$arch_ret = drush_shell_exec('tar xjf ' . $filename); |
|
162 |
break; |
|
163 |
case 'application/x-xz': |
|
164 |
// Decompress the tar xz archive |
|
165 |
$arch_ret = drush_shell_exec('tar xJf %s', $filename); |
|
166 |
break; |
|
167 |
} |
|
168 |
} |
|
169 |
else { |
|
170 |
drush_log(dt('@filename not found.', array('@filename' => $filename)), 'error'); |
|
36 |
function drush_print_pdf_download_validate($library = NULL) { |
|
37 |
if (is_null($library)) { |
|
38 |
$pdf_libs = array(); |
|
39 |
drush_command_invoke_all_ref('drush_pdf_libs_alter', $pdf_libs); |
|
40 |
|
|
41 |
drush_set_error('DRUSH_PDFDL_MISSING_ARG', dt("Usage: drush !cmd <library>\nWhere <library> is one of the following: !libs\n\nTry 'drush !cmd --help' for more information.", array( |
|
42 |
'!cmd' => 'print-pdf-download', |
|
43 |
'!libs' => implode(', ', array_keys($pdf_libs)), |
|
44 |
))); |
|
171 | 45 |
} |
172 |
|
|
173 |
return $arch_ret; |
|
174 | 46 |
} |
175 | 47 |
|
176 | 48 |
/** |
177 |
* Download a file using wget or curl |
|
178 |
* |
|
179 |
* Adapted from a function in drush/includes/drush.inc to support 302 redirects. |
|
180 |
* |
|
181 |
* @param string $download_url |
|
182 |
* The path to the file to download |
|
49 |
* Download and extract PDF archive. |
|
183 | 50 |
* |
184 |
* @return string |
|
185 |
* The filename that was downloaded, or NULL if the file could not be |
|
186 |
* downloaded. |
|
51 |
* @param string $library |
|
52 |
* Library to download. |
|
187 | 53 |
*/ |
188 |
function _drush_print_pdf_download_file($download_url) { |
|
189 |
$wget_ret = drush_shell_exec("wget -nv --trust-server-names %s", $download_url); |
|
190 |
|
|
191 |
if (!drush_get_context('DRUSH_SIMULATE')) { |
|
192 |
if ($wget_ret) { |
|
193 |
// Get the filename of the saved file from the output |
|
194 |
$wget_out = explode('"', array_shift(drush_shell_exec_output())); |
|
195 |
$filename = $wget_out[1]; |
|
196 |
} |
|
197 |
else { |
|
198 |
$tempnam = uniqid('drush_print_pdf_'); |
|
199 |
|
|
200 |
$curl_ret = drush_shell_exec("curl -s -L -o %s %s -w '%%{url_effective}'", $tempnam, $download_url); |
|
201 |
if ($curl_ret) { |
|
202 |
// File was donwloaded with the tempname |
|
54 |
function drush_print_pdf_download($library) { |
|
55 |
$pdf_libs = array(); |
|
56 |
drush_command_invoke_all_ref('drush_pdf_libs_alter', $pdf_libs); |
|
203 | 57 |
|
204 |
// Find the effective name |
|
205 |
$filename = explode('/', array_shift(drush_shell_exec_output())); |
|
206 |
$filename = array_pop($filename); |
|
58 |
if (isset($library) && isset($pdf_libs[drupal_strtolower($library)])) { |
|
59 |
$func = $pdf_libs[drupal_strtolower($library)]['callback']; |
|
207 | 60 |
|
208 |
// Rename file from tempname to effective name |
|
209 |
if (!drush_op('rename', $tempnam, './' . $filename)) { |
|
210 |
$filename = $tempnam; |
|
211 |
} |
|
212 |
} |
|
213 |
else { |
|
214 |
$filename = FALSE; |
|
215 |
} |
|
61 |
$download_url = $func(); |
|
62 |
if ($download_url) { |
|
63 |
_print_drush_download_lib($library, $download_url); |
|
216 | 64 |
} |
217 | 65 |
} |
218 | 66 |
else { |
219 |
$filename = basename($download_url);
|
|
67 |
drush_log(dt('Please specify a PDF library. Available choices: !libs.', array('!libs' => implode(', ', array_keys($pdf_libs)))), 'error');
|
|
220 | 68 |
} |
221 |
|
|
222 |
return $filename; |
|
223 | 69 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules