import-from-ter1_cli.php 25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#! /usr/local/bin/php -q
<?php

/**
 * This script imports the whole TER1 into the TER2 database structure, creates all the .t3x
 * files and the extensions.xml.gz index. As this script was only neccessary for the initial
 * import of all extensions, it possibly will never be needed again and only stays here
 * for historical reasons.
 *
 * Keep off - never ever use this at TYPO3.org again ...
 */

Markus Sommer's avatar
Markus Sommer committed
13
die('Better not ...');
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

// Defining circumstances for CLI mode:
define('TYPO3_cliMode', true);

// Defining PATH_thisScript here: Must be the ABSOLUTE path of this script in the right context:
// This will work as long as the script is called by it's absolute path!
#define('PATH_thisScript', (is_array($_ENV) && isset($_ENV['_'])) ?$_ENV['_']:$_SERVER['_']);
define('PATH_thisScript', $_SERVER['PHP_SELF']);

require(dirname(PATH_thisScript) . '/conf.php');
require(dirname(PATH_thisScript) . '/' . $BACK_PATH . 'init.php');

require_once(dirname(PATH_thisScript) . '/../class.tx_ter_helper.php');

// =========================================================================

$repositoryDir = dirname(PATH_thisScript) . '/../../../../fileadmin/ter/';
$extensionsPID = 1320;

// =========================================================================

writeExtensionIndexfile();

$res = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
38
39
40
41
42
    '*',
    'tx_extrep_keytable',
    'hidden=0 AND deleted=0',
    '',
    'extension_key ASC'
43
44
45
46
47
48
49
50
51
);

$startTime = time();
$extensionKeyCounter = 0;
$extensionKeysWithProblems = [];
while ($extensionKeyRow = $TYPO3_DB->sql_fetch_assoc($res)) {
    $extensionKeyCounter++;

    $res2 = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
52
53
54
        'username',
        'fe_users',
        'uid = ' . $extensionKeyRow['owner_fe_user']
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    );
    $feUsersRow = $TYPO3_DB->sql_fetch_assoc($res2);
    $accountData['username'] = $feUsersRow['username'];

    echo('importing ' . str_pad($extensionKeyRow['extension_key'] . ' (' . $feUsersRow['username'] . ')', 40, ' '));

    if (intval($extensionKeyRow['members_only'])) {
        $newExtensionKeyRow = [
            'tstamp' => $extensionKeyRow['tstamp'],
            'crdate' => $extensionKeyRow['crdate'],
            'pid' => $GLOBALS['extensionsPID'],
            'title' => $extensionKeyRow['title'],
            'description' => $extensionKeyRow['description'],
            'extensionkey' => $extensionKeyRow['extension_key'],
            'ownerusername' => $accountData['username'],
            'maxstoresize' => $extensionKeyRow['maxStoreSize'],
            'downloadcounter' => $extensionKeyRow['download_counter']
        ];

        $TYPO3_DB->exec_INSERTquery(
Markus Sommer's avatar
Markus Sommer committed
75
76
            'tx_ter_extensionkeys',
            $newExtensionKeyRow
77
78
79
        );
    } else {
        $res2 = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
80
81
82
            '*',
            'tx_extrep_repository',
            'extension_uid = ' . $extensionKeyRow['uid']
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
        );

        $versionOfExtension = 1;
        while ($extensionVersionRow = $TYPO3_DB->sql_fetch_assoc($res2)) {
            $extensionVersionCounter++;

            echo(str_pad($extensionVersionRow['version'], 7, ' '));

            $filesData = unserialize(gzuncompress($extensionVersionRow['datablob']));
            $extensionData = getExtensionDataFromRepositoryRow($extensionKeyRow, $extensionVersionRow);

            if (is_array($filesData)) {
                writeExtensionAndIconFile($extensionData, $filesData);
                writeExtensionInfoToDB($accountData, $extensionData, $filesData, $extensionVersionRow, $versionOfExtension);
                $versionOfExtension++;
            } else {
                $extensionKeysWithProblems [] = $extensionKeyRow['extension_key'];
            }
        }
    }
    echo chr(10);
}

writeExtensionIndexfile();

echo('

--------------------------------------------------------------------------
Summary
--------------------------------------------------------------------------
Number of processed extension keys:                    ' . $extensionKeyCounter . '
Number of processed extension versions:                ' . $extensionVersionCounter . '
Total processing time:                                 ' . (time() - $startTime) . ' seconds

Extension keys with problems:
' . implode(', ', $extensionKeysWithProblems) . '
');


function getExtensionDataFromRepositoryRow($extensionKeyRow, $extensionVersionRow)
{

    // *** DEPENDENCIES

    $typo3VersionMax = $extensionVersionRow['emconf_TYPO3_version_max'] > 0 ? versionConv(
Markus Sommer's avatar
Markus Sommer committed
128
129
        $extensionVersionRow['emconf_TYPO3_version_max'],
        1
130
131
    ) : '';
    $typo3VersionMin = $extensionVersionRow['emconf_TYPO3_version_min'] > 0 ? versionConv(
Markus Sommer's avatar
Markus Sommer committed
132
133
        $extensionVersionRow['emconf_TYPO3_version_min'],
        1
134
135
136
    ) : '';

    $phpVersionMax = $extensionVersionRow['emconf_PHP_version_max'] > 0 ? versionConv(
Markus Sommer's avatar
Markus Sommer committed
137
138
        $extensionVersionRow['emconf_PHP_version_max'],
        1
139
140
    ) : '';
    $phpVersionMin = $extensionVersionRow['emconf_PHP_version_min'] > 0 ? versionConv(
Markus Sommer's avatar
Markus Sommer committed
141
142
        $extensionVersionRow['emconf_PHP_version_min'],
        1
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
    ) : '';

    $typo3VersionRange = (strlen($typo3VersionMin) && strlen($typo3VersionMax)) ? $typo3VersionMin . '-' . $typo3VersionMax : '';
    $phpVersionRange = (strlen($phpVersionMin) && strlen($phpVersionMax)) ? $phpVersionMin . '-' . $phpVersionMax : '';

    $dependenciesArr = [
        [
            'kind' => 'depends',
            'extensionKey' => 'typo3',
            'versionRange' => $typo3VersionRange,
        ],
        [
            'kind' => 'depends',
            'extensionKey' => 'php',
            'versionRange' => $phpVersionRange,
        ]
    ];

    $otherDependenciesArr = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $extensionVersionRow['emconf_dependencies']);
    if (is_array($otherDependenciesArr) && count($otherDependenciesArr)) {
        foreach ($otherDependenciesArr as $dependencyExtKey) {
            $dependenciesArr [] = [
                'kind' => 'depends',
                'extensionKey' => utf8_encode($dependencyExtKey),
                'versionRange' => '',
            ];
        }
    }
    $otherDependenciesArr = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $extensionVersionRow['emconf_conflicts']);
    if (is_array($otherDependenciesArr) && count($otherDependenciesArr)) {
        foreach ($otherDependenciesArr as $dependencyExtKey) {
            $dependenciesArr [] = [
                'kind' => 'conflicts',
                'extensionKey' => utf8_encode($dependencyExtKey),
                'versionRange' => '',
            ];
        }
    }

    // ** COMPILE EVERYTHING

    $extensionData = [
        'extensionKey' => $extensionKeyRow['extension_key'],
        'version' => $extensionVersionRow['version'],
        'metaData' => [
            'title' => utf8_encode($extensionVersionRow['emconf_title']),
            'description' => utf8_encode($extensionVersionRow['emconf_description']),
            'category' => utf8_encode($extensionVersionRow['emconf_category']),
            'state' => $extensionVersionRow['emconf_state'],
            'authorName' => utf8_encode($extensionVersionRow['emconf_author']),
            'authorEmail' => utf8_encode($extensionVersionRow['emconf_author_email']),
            'authorCompany' => utf8_encode($extensionVersionRow['emconf_author_company']),
        ],
        'technicalData' => [
            'dataSize' => $extensionVersionRow['datasize'],
            'dataSizeCompressed' => $extensionVersionRow['datasize_gz'],
            'dependencies' => $dependenciesArr,
            'loadOrder' => $extensionVersionRow['emconf_loadOrder'],
            'uploadFolder' => $extensionVersionRow['emconf_uploadfolder'],
            'createDirs' => $extensionVersionRow['emconf_createDirs'],
            'shy' => $extensionVersionRow['emconf_shy'],
            'modules' => $extensionVersionRow['emconf_module'],
            'modifyTables' => $extensionVersionRow['emconf_modify_tables'],
            'priority' => $extensionVersionRow['emconf_priority'],
            'clearCacheOnLoad' => $extensionVersionRow['emconf_clearCacheOnLoad'],
            'lockType' => $extensionVersionRow['emconf_lockType'],
            'isManualIncluded' => $extensionVersionRow['is_manual_included'] ? 1 : 0,
        ],
        'infoData' => [
            'codeLines' => intval($extensionVersionRow['codelines']),
            'codeBytes' => intval($extensionVersionRow['codebytes']),
            'codingGuidelinesCompliance' => utf8_encode($extensionVersionRow['emconf_CGLcompliance']),
            'codingGuidelinesComplianceNotes' => utf8_encode($extensionVersionRow['emconf_CGLcompliance_note']),
            'uploadComment' => utf8_encode($extensionVersionRow['upload_comment']),
            'techInfo' => unserialize(utf8_encode($extensionVersionRow['techinfo'])),
        ],
    ];

    return $extensionData;
}

function writeExtensionAndIconFile(&$extensionData, $filesData)
{

    // Create an old-style list of dependencies, conflicts etc. which is understood by older
    // versions of the Extension Manager:
    $typo3Version = '';
    $phpVersion = '';
    $dependenciesArr = [];
    $conflictsArr = [];

    foreach ($extensionData['technicalData']['dependencies'] as $dependencyArr) {
        switch ($dependencyArr['extensionKey']) {
Markus Sommer's avatar
Markus Sommer committed
236
            case 'typo3':
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
                $typo3Version = $dependencyArr['versionRange'];
                break;
            case 'php':
                $phpVersion = $dependencyArr['versionRange'];
                break;
            default:
                if ($dependencyArr['kind'] == 'requires') {
                    $dependenciesArr[] = $dependencyArr['extensionKey'];
                } elseif ($dependencyArr['kind'] == 'requires') {
                    $conflictsArr[] = $dependencyArr['extensionKey'];
                }
        }
    }

    // Prepare Files Data Array:
    $preparedFilesDataArr = [];
    foreach ($filesData as $fileData) {
        $preparedFilesDataArr[$fileData['name']] = [
            'name' => $fileData['name'],
            'size' => $fileData['size'],
            'mtime' => $fileData['mtime'],
            'is_executable' => $fileData['is_executable'],
            'content' => $fileData['content'],
            'content_md5' => md5($fileData['content'])
        ];
    }

    // Prepare EM_CONF Array:
    $preparedEMConfArr = [
        'title' => $extensionData['metaData']['title'],
        'description' => $extensionData['metaData']['description'],
        'category' => $extensionData['metaData']['category'],
        'shy' => $extensionData['technicalData']['shy'],
        'dependencies' => implode(',', $dependenciesArr),
        'conflicts' => implode(',', $conflictsArr),
        'priority' => $extensionData['technicalData']['priority'],
        'loadOrder' => $extensionData['technicalData']['loadOrder'],
        'TYPO3_version' => $typo3Version,
        'PHP_version' => $phpVersion,
        'module' => $extensionData['technicalData']['modules'],
        'state' => $extensionData['metaData']['state'],
        'uploadfolder' => $extensionData['technicalData']['uploadFolder'],
        'createDirs' => $extensionData['technicalData']['createDirs'],
        'modify_tables' => $extensionData['technicalData']['modifyTables'],
        'clearCacheOnLoad' => $extensionData['technicalData']['clearCacheOnLoad'],
        'lockType' => $extensionData['technicalData']['lockType'],
        'author' => $extensionData['metaData']['authorName'],
        'author_email' => $extensionData['metaData']['authorEmail'],
        'author_company' => $extensionData['metaData']['authorCompany'],
        'CGLcompliance' => $extensionData['infoData']['codingGuidelineCompliance'],
        'CGLcompliance_note' => $extensionData['infoData']['codeingGuidelineComplianceNote'],
        'version' => $extensionData['version'],
    ];

    // Compile T3X Data Array:
    $dataArr = [
        'extKey' => $extensionData['extensionKey'],
        'EM_CONF' => $preparedEMConfArr,
        'misc' => [],
        'techInfo' => [],
        'FILES' => $preparedFilesDataArr
    ];

    $t3xFileUncompressedData = serialize($dataArr);
    $t3xFileData = md5($t3xFileUncompressedData) . ':gzcompress:' . gzcompress($t3xFileUncompressedData);
    $gifFileData = $preparedFilesDataArr['ext_icon.gif']['content'];


    // Create directories and build filenames:
    $firstLetter = strtolower(substr($extensionData['extensionKey'], 0, 1));
    $secondLetter = strtolower(substr($extensionData['extensionKey'], 1, 1));
    $fullPath = $GLOBALS['repositoryDir'] . $firstLetter . '/' . $secondLetter . '/';

    if (@!is_dir($GLOBALS['repositoryDir'] . $firstLetter)) {
        mkdir($GLOBALS['repositoryDir'] . $firstLetter);
    }
    if (@!is_dir($GLOBALS['repositoryDir'] . $firstLetter . '/' . $secondLetter)) {
        mkdir($GLOBALS['repositoryDir'] . $firstLetter . '/' . $secondLetter);
    }

    list ($majorVersion, $minorVersion, $devVersion) = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(
Markus Sommer's avatar
Markus Sommer committed
318
319
        '.',
        $extensionData['version']
320
321
    );
    $t3xFileName = strtolower(
Markus Sommer's avatar
Markus Sommer committed
322
323
        $extensionData['extensionKey']
    ) . '_' . $majorVersion . '.' . $minorVersion . '.' . $devVersion . '.t3x';
324
    $gifFileName = strtolower(
Markus Sommer's avatar
Markus Sommer committed
325
326
        $extensionData['extensionKey']
    ) . '_' . $majorVersion . '.' . $minorVersion . '.' . $devVersion . '.gif';
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

    // Write the files
    $fh = @fopen($fullPath . $t3xFileName, 'wb');
    if (!$fh) {
    }
    fwrite($fh, $t3xFileData);
    fclose($fh);

    if (strlen($gifFileData)) {
        $fh = @fopen($fullPath . $gifFileName, 'wb');
        if (!$fh) {
        }
        fwrite($fh, $gifFileData);
        fclose($fh);
    }

    // Write some data back to $extensionInfoData:
    $extensionData['t3xFileMD5'] = md5($t3xFileData);
    $extensionData['infoData']['dataSize'] = strlen($t3xFileUncompressedData);
    $extensionData['infoData']['dataSizeCompressed'] = strlen($t3xFileData);
}

function writeExtensionInfoToDB($accountData, $extensionData, $filesData, $extensionVersionRow, $versionOfExtension)
{
    global $TYPO3_DB;

    // Add extension key to key table if we are processing the first version of the extension:
    if ($versionOfExtension == 1) {
        $res = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
356
357
358
            '*',
            'tx_extrep_keytable',
            'extension_key = "' . $extensionData['extensionKey'] . '"'
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        );
        $oldExtensionKeyRow = $TYPO3_DB->sql_fetch_assoc($res);

        $extensionKeyRow = [
            'tstamp' => $extensionVersionRow['tstamp'],
            'crdate' => $extensionVersionRow['crdate'],
            'pid' => $GLOBALS['extensionsPID'],
            'extensionkey' => $extensionData['extensionKey'],
            'title' => $extensionData['metaData']['title'],
            'description' => $extensionData['metaData']['description'],
            'ownerusername' => $accountData['username'],
            'maxstoresize' => $oldExtensionKeyRow['maxStoreSize'],
            'downloadcounter' => $oldExtensionKeyRow['download_counter']
        ];

        $TYPO3_DB->exec_INSERTquery(
Markus Sommer's avatar
Markus Sommer committed
375
376
            'tx_ter_extensionkeys',
            $extensionKeyRow
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
        );
    }

    // Prepare files information:
    foreach ($filesData as $fileData) {
        $extensionData['infoData']['files'][$fileData['name']] = [
            'name' => $fileData['name'],
            'size' => $fileData['size'],
            'mtime' => $fileData['mtime'],
            'is_executable' => $fileData['is_executable'],
        ];
        if ($fileData['name'] == 'doc/manual.sxw') {
            $extensionInfoData['technicalData']['isManualIncluded'] = 1;
        }
    }

    // Prepare the new records:
    $extensionsRow = [
        'tstamp' => $extensionVersionRow['tstamp'],
        'crdate' => $extensionVersionRow['crdate'],
        'pid' => $GLOBALS['extensionsPID'],
        'extensionkey' => $extensionData['extensionKey'],
        'version' => $extensionData['version'],
        'title' => $extensionData['metaData']['title'],
        'description' => $extensionData['metaData']['description'],
        'category' => $extensionData['metaData']['category'],
        'state' => $extensionData['metaData']['state'],
        'ismanualincluded' => $extensionData['technicalData']['isManualIncluded'],
        'downloadcounter' => $extensionVersionRow['download_counter'],
        't3xfilemd5' => $extensionData['t3xFileMD5'],
    ];

    $result = $TYPO3_DB->exec_INSERTquery('tx_ter_extensions', $extensionsRow);
    $extensionUid = $TYPO3_DB->sql_insert_id();
    $extensionDetailsRow = [
        'pid' => $GLOBALS['extensionsPID'],
        'extensionuid' => $extensionUid,
        'uploadcomment' => $extensionData['infoData']['uploadComment'],
        'lastuploadbyusername' => 'ter2-conversion',
        'lastuploaddate' => $extensionVersionRow['tstamp'],
        'datasize' => $extensionData['infoData']['dataSize'],
        'datasizecompressed' => $extensionData['infoData']['dataSizeCompressed'],
        'files' => serialize($extensionData['infoData']['files']),
        'codelines' => $extensionData['infoData']['codeLines'],
        'codebytes' => $extensionData['infoData']['codeBytes'],
        'techinfo' => serialize($extensionData['infoData']['techInfo']),
        'shy' => $extensionData['technicalData']['shy'],
        'dependencies' => serialize($extensionData['technicalData']['dependencies']),
        'createdirs' => $extensionData['technicalData']['createDirs'],
        'priority' => $extensionData['technicalData']['priority'],
        'modules' => $extensionData['technicalData']['modules'],
        'uploadfolder' => $extensionData['technicalData']['uploadFolder'],
        'modifytables' => $extensionData['technicalData']['modifyTables'],
        'clearcacheonload' => $extensionData['technicalData']['clearCacheOnLoad'],
        'locktype' => $extensionData['technicalData']['lockType'],
        'authorname' => $extensionData['metaData']['authorName'],
        'authoremail' => $extensionData['metaData']['authorEmail'],
        'authorcompany' => $extensionData['metaData']['authorCompany'],
        'codingguidelinescompliance' => $extensionData['infoData']['codingGuidelinesComliance'],
        'codingguidelinescompliancenote' => $extensionData['infoData']['codingGuidelinesComlianceNote'],
        'loadorder' => $extensionData['technicalData']['loadOrder'],
    ];

    $TYPO3_DB->exec_INSERTquery('tx_ter_extensiondetails', $extensionDetailsRow);
}

/**
 * Conversion of version numbers to/from doubles
 *
 * @param    mixed        Input string, then a version number like "3.6.0" or "3.4.5rc2". If double it will be converted back to
 *     string. Version numbers after suffix is not supported higher than "9".
 * @param    boolean        If set, the conversion is from double to string, otherwise from string to double.
 * @return    mixed        String or double depending on input.
 */
function versionConv($input, $rev = false)
{

    // Initializing translation table:
    $subDecIndex = [
        'dev' => 1,
        'a' => 4,
        'b' => 5,
        'rc' => 8
    ];

    // Direction of conversion:
    if ($rev) {    // From double to string
        if (!$input) {
            $result = '';
        } else {
            list($int, $dev) = explode('.', $input);

            // Looking for decimals:
            $suffix = '';
            if (strlen($dev)) {
                $int++;    // Increase integer since that would have been decreased last time.
                $subDecIndex = array_flip($subDecIndex);
                $suffix = $subDecIndex[substr($dev, 0, 1)];
                if ($suffix) {
                    $suffix .= intval(substr($dev, 1));
                }
            }

            // Base part:
            $result = intval(substr($int, 0, -6)) . '.' . intval(substr($int, -6, -3)) . '.' . intval(substr($int, -3)) . $suffix;
        }
    } else {    // From string to double
484
        $result = \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger($input);
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
        if (preg_match('/(dev|a|b|rc)([0-9]*)$/', strtolower($input), $reg)) {
            $dec = intval($subDecIndex[$reg[1]]) . $reg[2];
            $result = (double)(($result - 1) . '.' . $dec);
        }
    }

    return $result;
}

/**
 * Updates the "extensions.xml" file which contains an index of all uploaded
 * extensions in the TER.
 *
 * @return    void
 * @access    public
 */
function writeExtensionIndexfile()
{
    global $TYPO3_DB;

    if (!@is_dir($GLOBALS['repositoryDir'])) {
Markus Sommer's avatar
Markus Sommer committed
506
507
508
        throw new tx_ter_exception_internalServerError(
            'Extension repository directory does not exist.',
            TX_TER_ERROR_GENERAL_EXTREPDIRDOESNTEXIST
509
510
511
512
513
514
        );
    }

    $trackTime = microtime();

    $res = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
515
516
517
        'uid,tstamp,extensionkey,version,title,description,state,category,t3xfilemd5',
        'tx_ter_extensions',
        '1'
518
519
520
521
522
523
524
    );

    // Read the extension records from the DB:
    $extensionsAndVersionsArr = [];
    $extensionsTotalDownloadsArr = [];
    while ($row = $TYPO3_DB->sql_fetch_assoc($res)) {
        $res2 = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
525
526
527
            'ownerusername,downloadcounter',
            'tx_ter_extensionkeys',
            'extensionkey="' . $row['extensionkey'] . '"'
528
529
530
531
532
533
        );
        $extensionKeyRow = $TYPO3_DB->sql_fetch_assoc($res2);
        $row['ownerusername'] = $extensionKeyRow['ownerusername'];
        $extensionsTotalDownloadsArr[$row['extensionkey']] = $extensionKeyRow['downloadcounter'];

        $res2 = $TYPO3_DB->exec_SELECTquery(
Markus Sommer's avatar
Markus Sommer committed
534
535
            'lastuploaddate,uploadcomment,dependencies,authorname,authoremail,authorcompany',
            'tx_ter_extensiondetails',
536
537
538
539
540
541
542
543
544
545
            'extensionuid=' . $row['uid']
        );
        $detailsRow = $TYPO3_DB->sql_fetch_assoc($res2);
        if (is_array($detailsRow)) {
            $row = $row + $detailsRow;
        }
        $extensionsAndVersionsArr [$row['extensionkey']][$row['version']] = $row;
    }

    // Prepare the DOM object:
Markus Sommer's avatar
Markus Sommer committed
546
    $dom = new DOMDocument('1.0', 'utf-8');
547
548
549
550
551
552
    $dom->formatOutput = false;
    $extensionsObj = $dom->appendChild(new DOMElement('extensions'));

    // Create the nested XML structure:
    foreach ($extensionsAndVersionsArr as $extensionKey => $extensionVersionsArr) {
        $extensionObj = $extensionsObj->appendChild(new DOMElement('extension'));
Markus Sommer's avatar
Markus Sommer committed
553
554
        $extensionObj->appendChild(new DOMAttr('extensionkey', $extensionKey));
        $extensionObj->appendChild(new DOMElement('downloadcounter', xmlentities($extensionsTotalDownloadsArr[$extensionKey])));
555
556
557

        foreach ($extensionVersionsArr as $versionNumber => $extensionVersionArr) {
            $versionObj = $extensionObj->appendChild(new DOMElement('version'));
Markus Sommer's avatar
Markus Sommer committed
558
            $versionObj->appendChild(new DOMAttr('version', $versionNumber));
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582

            $versionObj->appendChild(new DOMElement('title', xmlentities($extensionVersionArr['title'])));
            $versionObj->appendChild(new DOMElement('description', xmlentities($extensionVersionArr['description'])));
            $versionObj->appendChild(new DOMElement('state', xmlentities($extensionVersionArr['state'])));
            $versionObj->appendChild(new DOMElement('reviewstate', xmlentities($extensionVersionArr['reviewstate'])));
            $versionObj->appendChild(new DOMElement('category', xmlentities($extensionVersionArr['category'])));
            $versionObj->appendChild(new DOMElement('downloadcounter', xmlentities($extensionVersionArr['downloadcounter'])));
            $versionObj->appendChild(new DOMElement('lastuploaddate', $extensionVersionArr['lastuploaddate']));
            $versionObj->appendChild(new DOMElement('uploadcomment', xmlentities($extensionVersionArr['uploadcomment'])));
            $versionObj->appendChild(new DOMElement('dependencies', $extensionVersionArr['dependencies']));
            $versionObj->appendChild(new DOMElement('authorname', xmlentities($extensionVersionArr['authorname'])));
            $versionObj->appendChild(new DOMElement('authoremail', xmlentities($extensionVersionArr['authoremail'])));
            $versionObj->appendChild(new DOMElement('authorcompany', xmlentities($extensionVersionArr['authorcompany'])));
            $versionObj->appendChild(new DOMElement('ownerusername', xmlentities($extensionVersionArr['ownerusername'])));
            $versionObj->appendChild(new DOMElement('t3xfilemd5', $extensionVersionArr['t3xfilemd5']));
        }
    }

    $extensionsObj->appendChild(new DOMComment('Index created at ' . date("D M j G:i:s T Y")));
    $extensionsObj->appendChild(new DOMComment('Index created in ' . (microtime() - $trackTime) . ' ms'));

    // Write XML data to disc:
    $fh = fopen($GLOBALS['repositoryDir'] . 'cli-extensions.xml.gz', 'wb');
    if (!$fh) {
Markus Sommer's avatar
Markus Sommer committed
583
        throw new tx_ter_exception_internalServerError(
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
            'Write error while writing extensions index file: ' . $GLOBALS['repositoryDir'] . 'extensions.xml',
            TX_TER_ERROR_UPLOADEXTENSION_WRITEERRORWHILEWRITINGEXTENSIONSINDEX
        );
    }
    fwrite($fh, gzencode($dom->saveXML(), 9));
    fclose($fh);

#\TYPO3\CMS\Core\Utility\GeneralUtility::devLog('extensions','ter',0,$extensionsArr);
}

/**
 * Equivalent to htmlentities but for XML content
 *
 * @param    string $string : String to encode
 * @return    string        &,",',< and > replaced by entities
 * @access    public
 */
function xmlentities($string)
{
    return str_replace(['&', '"', "'", '<', '>'], ['&amp;', '&quot;', '&apos;', '&lt;', '&gt;'], $string);
}

606
?>