This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.ext_update.php
557 lines (496 loc) · 21.3 KB
/
class.ext_update.php
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
namespace Causal\Sphinx;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with TYPO3 source code.
*
* The TYPO3 project - inspiring people to share!
*/
$BACK_PATH = $GLOBALS['BACK_PATH'] . TYPO3_mainDir;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use Causal\Sphinx\Utility\Setup;
/**
* Class to be used to initialize the Sphinx Python Documentation Generator locally.
*
* @category Extension Manager
* @package TYPO3
* @subpackage tx_sphinx
* @author Xavier Perseguers <[email protected]>
* @copyright 2013-2016 Causal Sàrl
* @copyright Causal Sàrl
* @license http://www.gnu.org/copyleft/gpl.html
*/
class ext_update extends \TYPO3\CMS\Backend\Module\BaseScriptClass
{
/** @var string */
protected $extKey = 'sphinx';
/** @var array */
protected $configuration;
/**
* Default constructor.
*/
public function __construct()
{
$this->configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey]);
}
/**
* Checks whether the "UPDATE!" menu item should be
* shown.
*
* @return boolean
*/
public function access()
{
return true;
}
/**
* Main method that is called whenever UPDATE! menu
* was clicked.
*
* @return string HTML to display
*/
public function main()
{
$out = array();
$errors = Setup::createLibraryDirectories();
if (count($errors) > 0) {
foreach ($errors as $error) {
$out[] = $this->formatError($error);
}
return implode(LF, $out);
}
// Sphinx actually relies by default on Jinja2 as templating engine and Jinja2 requires Python 2.6
// We don't check this as a hard requirement but will issue a warning
$pythonVersion = Setup::getPythonVersion();
if (version_compare($pythonVersion, '2.6', '<')) {
$out[] = $this->formatWarning('The default templating language in Sphinx is Jinja. Jinja requires at least ' .
'Python 2.6 but you are using ' . $pythonVersion . '. As such it is very likely that you will not be ' .
'able to actually render Sphinx projects.');
}
// Fetch the list of official versions of Sphinx
$availableVersions = Setup::getSphinxAvailableVersions();
// Load the list of locally available versions of Sphinx
$localVersions = Setup::getSphinxLocalVersions();
if (count($availableVersions) == 0) {
$message = <<<HTML
Could not find any version of Sphinx:<br /><br />
<ul>
<li>Are you currently offline?</li>
<li>Does PHP have <a href="http://wiki.typo3.org/Exception/CMS/1318283565">proper OpenSSL support</a>?</li>
HTML;
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['curlUse'] == '1') {
$message .= '<li>You have $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'curlUse\'] == \'1\', ' .
'your OpenSSL configuration might be broken</li>';
}
$message .= '</ul>';
$out[] = $this->formatWarning($message, false);
}
// Handle form operation, if needed
$operation = GeneralUtility::_GP('operation');
if ($operation) {
list($action, $version) = explode('-', $operation, 2);
if (isset($availableVersions[$version])) {
$messages = array();
switch ($action) {
case 'DOWNLOAD':
$this->downloadSphinx($availableVersions[$version], $messages);
break;
case 'BUILD':
$this->buildSphinx($availableVersions[$version], $messages);
break;
case 'IMPORT':
if ($this->downloadSphinx($availableVersions[$version], $messages)) {
$this->buildSphinx($availableVersions[$version], $messages);
}
break;
case 'REMOVE':
$this->removeSphinx($availableVersions[$version], $messages);
break;
}
foreach ($messages as $message) {
switch (true) {
case GeneralUtility::isFirstPartOfStr($message, '[OK] '):
$out[] = $this->formatOk(substr($message, 5));
break;
case GeneralUtility::isFirstPartOfStr($message, '[INFO] '):
$out[] = $this->formatInformation(substr($message, 7));
break;
case GeneralUtility::isFirstPartOfStr($message, '[WARNING] '):
$out[] = $this->formatWarning(substr($message, 10));
break;
case GeneralUtility::isFirstPartOfStr($message, '[ERROR] '):
$out[] = $this->formatError(substr($message, 8));
break;
default:
$out[] = $message;
break;
}
}
$relativeLogFilename = 'typo3temp/logs/sphinx.' . strtolower($action) . '.' . date('YmdHis') . '.log';
$absoluteLogFilename = GeneralUtility::getFileAbsFileName($relativeLogFilename);
Setup::dumpLog($absoluteLogFilename);
// Reload the list of locally available versions of Sphinx
$localVersions = Setup::getSphinxLocalVersions();
} else {
// Log operation?
list($action, $file) = explode('-', $operation, 2);
$fileName = PATH_site . 'typo3temp/logs/' . $file;
if (preg_match('/^sphinx\.[^.]+\.\d+\.log$/', $file) && is_file($fileName)) {
switch ($action) {
case 'SHOWLOG':
$fileContents = file_get_contents($fileName);
$out[] = '<textarea style="width:100%;height:500px">' . htmlspecialchars($fileContents) . '</textarea>';
$out[] = '<p><a href="' . htmlspecialchars(GeneralUtility::linkThisScript()) . '">close</a></p>';
return implode(LF, $out);
break;
case 'REMOVELOG':
unlink($fileName);
break;
}
}
}
}
$restToolsPath = GeneralUtility::getFileAbsFileName('uploads/tx_sphinx/RestTools');
if (is_dir($restToolsPath)) {
$relativePath = \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($restToolsPath);
$out[] = $this->formatWarning('Please remove following directory: "<webroot>/' . $relativePath . '/" since it is not used anymore.', true);
}
$out[] = '<form action="' . htmlspecialchars(GeneralUtility::linkThisScript()) . '" method="post" class="container-fluid">';
$out[] = '<div class="leftColumn col-md-6">';
$out[] = '<p>Following versions of Sphinx may be installed locally:</p>';
$out[] = '<table id="sphinx-versions" class="t3-table table" style="width:auto">';
$out[] = '<tr>';
$out[] = '<th colspan="2"> </th>';
$out[] = '<th>1-click Process</th>';
$out[] = '<th>Manual Process</th>';
$out[] = '</tr>';
$installRst2Pdf = TYPO3_OS !== 'WIN' && $this->configuration['install_rst2pdf'] === '1';
$changes = array();
/** @var \TYPO3\CMS\Core\Imaging\IconFactory $iconFactory */
$iconFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconFactory::class);
foreach ($availableVersions as $version) {
$isInstalled = in_array($version['key'], $localVersions);
$hasSources = Setup::hasSphinxSources($version['key']);
$hasLibraries = Setup::hasPyYaml()
&& Setup::hasPygments($version['key'])
&& Setup::hasT3SphinxThemeRtd()
&& Setup::hasT3FieldListTable()
&& Setup::hasT3TableRows()
&& Setup::hasT3Targets()
&& Setup::hasThirdPartyLibraries()
&& Setup::hasLaTeXPackage();
if ($installRst2Pdf) {
$hasLibraries &= Setup::hasPIL();
$hasLibraries &= Setup::hasRst2Pdf();
}
$out[] = '<tr data-version="' . htmlspecialchars($version['key']) . '">';
$iconChecked = $iconFactory->getIcon('status-status-checked', \TYPO3\CMS\Core\Imaging\Icon::SIZE_SMALL)->render();
$out[] = '<td>' . ($isInstalled ? $iconChecked : '') . '</td>';
$out[] = '<td>';
$out[] = 'Sphinx ' . htmlspecialchars($version['name']);
$out[] = '</td>';
$out[] = '<td><button name="operation" value="IMPORT-' . htmlspecialchars($version['name']) . '"' . ($isInstalled ? ' disabled="disabled"' : '') . '>import</button></td>';
$out[] = '<td>';
$out[] = '<button name="operation" value="DOWNLOAD-' . htmlspecialchars($version['name']) . '"' . ($hasSources && $hasLibraries ? ' disabled="disabled"' : '') . '>download</button>';
$out[] = '<button name="operation" value="BUILD-' . htmlspecialchars($version['name']) . '"' . (!$hasSources ? ' disabled="disabled"' : '') . '>build</button>';
$out[] = '<button name="operation" value="REMOVE-' . htmlspecialchars($version['name']) . '"' . (!($hasSources || $isInstalled) ? ' disabled="disabled"' : '') . '>remove</button>';
$out[] = '</td>';
$out[] = '</tr>';
$changes[$version['key']] = Setup::getChanges($version['key']);
}
$out[] = '</table>';
$logFiles = $this->getLogFiles();
if (count($logFiles) > 0) {
$out[] = '<p>Available logs:</p>';
$out[] = '<table id="sphinx-versions" class="t3-table table" style="width:auto">';
$out[] = '<tr>';
$out[] = '<th>Type</th>';
$out[] = '<th>Date</th>';
$out[] = '<th>Action</th>';
$out[] = '</tr>';
foreach ($logFiles as $logFile) {
$out[] = '<tr>';
$out[] = '<td>' . htmlspecialchars($logFile['type']) . '</td>';
$out[] = '<td>' . date('d.m.Y H:i:s', $logFile['date']) . '</td>';
$out[] = '<td>';
$out[] = '<button name="operation" value="SHOWLOG-' . htmlspecialchars($logFile['file']) . '">open</button>';
$out[] = '<button name="operation" value="REMOVELOG-' . htmlspecialchars($logFile['file']) . '">remove</button>';
$out[] = '</td>';
$out[] = '</tr>';
}
$out[] = '</table>';
$out[] = '<ul>';
$out[] = '</ul>';
}
$out[] = '</div>'; // .leftColumn
$out[] = '<div id="sphinx-changes" class="rightColumn col-md-6"></div>';
$out[] = '</form>';
// JSON-encoding inspired by \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSvalue()
$json = strtr(
json_encode($changes, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG),
array(
'"' => '\'',
'\\\\' => '\\u005C',
' ' => '\\u0020',
'!' => '\\u0021',
'\\t' => '\\u0009',
'\\n' => '\\u000A',
'\\r' => '\\u000D'
)
);
$out[] = '<script type="text/javascript">var sphinxChanges=' . $json . ';</script>';
$out[] = '<script src="../typo3conf/ext/sphinx/Resources/Public/JavaScript/setup.js" type="text/javascript"></script>';
return implode(LF, $out);
}
/**
* Downloads Sphinx and associated libraries.
*
* @param array $data
* @param array &$output
* @return boolean true if operation succeeded, otherwise false
*/
protected function downloadSphinx(array $data, array &$output)
{
$success = true;
$installRst2Pdf = TYPO3_OS !== 'WIN' && $this->configuration['install_rst2pdf'] === '1';
$version = $data['key'];
$url = 'https://github.com' . $data['url'];
if (!Setup::hasSphinxSources($version)) {
$success &= Setup::downloadSphinxSources($version, $url, $output);
}
if ($installRst2Pdf && !Setup::hasPIL()) {
$success &= Setup::downloadPIL($output);
}
if ($installRst2Pdf && !Setup::hasRst2Pdf()) {
$success &= Setup::downloadRst2Pdf($output);
}
if (!Setup::hasPyYaml()) {
$success &= Setup::downloadPyYaml($output);
}
if (!Setup::hasT3SphinxThemeRtd()) {
$success &= Setup::downloadT3SphinxThemeRtd($output);
}
if (!Setup::hasT3FieldListTable()) {
$success &= Setup::downloadT3FieldListTable($output);
}
if (!Setup::hasT3TableRows()) {
$success &= Setup::downloadT3TableRows($output);
}
if (!Setup::hasT3Targets()) {
$success &= Setup::downloadT3Targets($output);
}
if (!Setup::hasThirdPartyLibraries()) {
$success &= Setup::downloadThirdPartyLibraries($output);
}
if (!Setup::hasPygments($version)) {
$success &= Setup::downloadPygments($version, $output);
}
if (!Setup::hasLaTeXPackage()) {
$success &= Setup::downloadLaTeXPackage($output);
}
return $success;
}
/**
* Builds Sphinx associated libraries.
*
* @param array $data
* @param array &$output
* @return boolean true if operation succeeded, otherwise false
*/
protected function buildSphinx(array $data, array &$output)
{
$success = false;
$version = $data['key'];
$installRst2Pdf = TYPO3_OS !== 'WIN' && $this->configuration['install_rst2pdf'] === '1';
if (Setup::hasSphinxSources($version)) {
$success = Setup::buildSphinx($version, $output);
if ($success) {
if ($installRst2Pdf && Setup::hasPIL()) {
$success &= Setup::buildPIL($version, $output);
}
if ($installRst2Pdf && Setup::hasRst2Pdf()) {
$success &= Setup::buildRst2Pdf($version, $output);
}
if (Setup::hasThirdPartyLibraries()) {
$selectedPlugins = GeneralUtility::trimExplode(',', $this->configuration['plugins'], true);
foreach ($selectedPlugins as $selectedPlugin) {
$success &= Setup::buildThirdPartyLibraries('sphinx-contrib' . DIRECTORY_SEPARATOR . $selectedPlugin, $version, $output);
}
}
if (Setup::hasPyYaml()) {
$success &= Setup::buildPyYaml($version, $output);
}
if (Setup::hasPygments($version)) {
$success &= Setup::buildPygments($version, $output);
}
if (Setup::hasT3SphinxThemeRtd()) {
$success &= Setup::buildT3SphinxThemeRtd($version, $output);
}
if (Setup::hasT3FieldListTable()) {
$success &= Setup::buildT3FieldListTable($version, $output);
}
if (Setup::hasT3TableRows()) {
$success &= Setup::buildT3TableRows($version, $output);
}
if (Setup::hasT3Targets()) {
$success &= Setup::buildT3Targets($version, $output);
}
}
}
return $success;
}
/**
* Removes Sphinx (but not associated libraries).
*
* @param array $data
* @param array &$output
* @return void
*/
protected function removeSphinx(array $data, array &$output)
{
$version = $data['key'];
Setup::removeSphinx($version, $output);
}
/**
* Creates an error message for backend output.
*
* @param string $message
* @return string
*/
protected function formatError($message)
{
if (version_compare(TYPO3_branch, '7', '>=')) {
$output = '<div class="alert alert-error">';
$output .= '<div class="media">';
$output .= '<div class="media-body">';
//$output .= '<h4 class="alert-title">Message head</h4>';
$output .= '<p class="alert-message">' . nl2br(htmlspecialchars($message)) . '</p>';
$output .= '</div></div></div>';
} else {
$output = '<div class="typo3-message message-error">';
//$output .= '<div class="message-header">Message head</div>';
$output .= '<div class="message-body">' . nl2br(htmlspecialchars($message)) . '</div>';
$output .= '</div>';
}
return $output;
}
/**
* Creates a warning message for backend output.
*
* @param string $message
* @param boolean $hsc
* @return string
*/
protected function formatWarning($message, $hsc = true)
{
if (version_compare(TYPO3_branch, '7', '>=')) {
$output = '<div class="alert alert-warning">';
$output .= '<div class="media">';
$output .= '<div class="media-body">';
//$output .= '<h4 class="alert-title">Message head</h4>';
if ($hsc) {
$message = nl2br(htmlspecialchars($message));
}
$output .= '<p class="alert-message">' . $message . '</p>';
$output .= '</div></div></div>';
} else {
$output = '<div class="typo3-message message-warning">';
//$output .= '<div class="message-header">Message head</div>';
if ($hsc) {
$message = nl2br(htmlspecialchars($message));
}
$output .= '<div class="message-body">' . $message . '</div>';
$output .= '</div>';
}
return $output;
}
/**
* Creates an information message for backend output.
*
* @param string $message
* @param boolean $hsc
* @return string
*/
protected function formatInformation($message, $hsc = true)
{
if (version_compare(TYPO3_branch, '7', '>=')) {
$output = '<div class="alert alert-info">';
$output .= '<div class="media">';
$output .= '<div class="media-body">';
//$output .= '<h4 class="alert-title">Message head</h4>';
if ($hsc) {
$message = nl2br(htmlspecialchars($message));
}
$output .= '<p class="alert-message">' . $message . '</p>';
$output .= '</div></div></div>';
} else {
$output = '<div class="typo3-message message-information">';
//$output .= '<div class="message-header">Message head</div>';
if ($hsc) {
$message = nl2br(htmlspecialchars($message));
}
$output .= '<div class="message-body">' . $message . '</div>';
$output .= '</div>';
}
return $output;
}
/**
* Creates an OK message for backend output.
*
* @param string $message
* @param boolean $hsc
* @return string
*/
protected function formatOk($message, $hsc = true)
{
if (version_compare(TYPO3_branch, '7', '>=')) {
$output = '<div class="alert alert-success">';
$output .= '<div class="media">';
$output .= '<div class="media-body">';
//$output .= '<h4 class="alert-title">Message head</h4>';
if ($hsc) {
$message = nl2br(htmlspecialchars($message));
}
$output .= '<p class="alert-message">' . $message . '</p>';
$output .= '</div></div></div>';
} else {
$output = '<div class="typo3-message message-ok">';
//$output .= '<div class="message-header">Message head</div>';
if ($hsc) {
$message = nl2br(htmlspecialchars($message));
}
$output .= '<div class="message-body">' . $message . '</div>';
$output .= '</div>';
}
return $output;
}
/**
* Returns the Sphinx log files.
*
* @return array
*/
protected function getLogFiles()
{
$sphinxFiles = array();
$files = GeneralUtility::getFilesInDir(PATH_site . 'typo3temp/logs/');
foreach ($files as $file) {
if (preg_match('/^sphinx\.([^.]+)\.(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)\.log$/', $file, $matches)) {
$sphinxFile = array(
'file' => $file,
'type' => $matches[1],
'date' => mktime($matches[5], $matches[6], $matches[7], $matches[3], $matches[4], $matches[2]),
);
$key = $sphinxFile['date'] . '-' . $sphinxFile['type'];
$sphinxFiles[$key] = $sphinxFile;
}
}
krsort($sphinxFiles);
return array_values($sphinxFiles);
}
}