-
Notifications
You must be signed in to change notification settings - Fork 23
/
sqlidoimport.php
110 lines (99 loc) · 4.35 KB
/
sqlidoimport.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
<?php
/**
* Main import script
* @copyright Copyright (C) 2010 - SQLi Agency. All rights reserved
* @licence http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2
* @author Jerome Vieilledent
* @version @@@VERSION@@@
* @package sqliimport
*/
require 'autoload.php';
$cli = eZCLI::instance();
$cli->setUseStyles(true);
$script = eZScript::instance( array( 'description' => ( "SQLIImport import script\n"),
'use-session' => false,
'use-modules' => true,
'use-extensions' => true ) );
$script->startup();
// Options processing
$options = $script->getOptions(
'[source-handlers:][list-source-handlers][options:]',
'',
array(
'source-handlers' => 'Comma separated source handlers identifiers. If not provided, all source handlers will be processed.',
'list-source-handlers' => 'Lists all available handlers',
'options' => 'Options for import handlers. Should be something like --options="handler1::foo=bar,foo2=baz|handler2::someoption=biz"'
)
);
$script->initialize();
$script->setUseDebugAccumulators( true );
include_once __DIR__ . '/../../modules/sqliimport/sigtermhandler.php';
include_once __DIR__ . '/../../modules/sqliimport/fatalerrorhandler.php';
try
{
$importINI = eZINI::instance( 'sqliimport.ini' );
$aAvailableSourceHandlers = $importINI->variable( 'ImportSettings', 'AvailableSourceHandlers' );
/*
* List all available source handlers if requested, then exit
*/
if( isset( $options['list-source-handlers'] ) )
{
if( $aAvailableSourceHandlers )
{
$cli->notice( 'Available source handlers :' );
$cli->warning( implode( ', ', $aAvailableSourceHandlers ) );
}
else
{
$cli->error( 'No source handler defined !' );
}
$script->shutdown();
}
else
{
/*
* Process requested import handlers
* An SQLIImportItem object will be created and stored in DB for each handler
*/
$requestedHandlers = $options['source-handlers'] ? $options['source-handlers'] : '';
$aRequestedHandlers = $requestedHandlers ? explode( ',', $requestedHandlers ) : $importINI->variable( 'ImportSettings', 'AvailableSourceHandlers');
$areValidHandlers = SQLIImportFactory::checkExistingHandlers( $aRequestedHandlers ); // An exception may be thrown if a handler is not defined in sqliimport.ini
if( $aRequestedHandlers )
{
$aHandlersOptions = SQLIImportHandlerOptions::decodeHandlerOptionLine( $options['options'] );
$importUser = eZUser::fetchByName( 'admin' ); // As this is a manual script, "Admin" user will be used to import
$aImportItems = array();
// First stores an SQLIImportItem for each handler to process
foreach( $aRequestedHandlers as $handler )
{
$handlerOptions = isset( $aHandlersOptions[$handler] ) ? $aHandlersOptions[$handler] : null;
$pendingImport = new SQLIImportItem( array(
'handler' => $handler,
'user_id' => $importUser->attribute( 'contentobject_id' ),
) );
if ( $handlerOptions instanceof SQLIImportHandlerOptions )
$pendingImport->setAttribute( 'options', $handlerOptions );
$pendingImport->store();
$aImportItems[] = $pendingImport;
}
$importFactory = SQLIImportFactory::instance();
$importFactory->runImport( $aImportItems );
$importFactory->cleanup();
$cli->notice( 'Import is over :)' );
}
else
{
$cli->warning( 'No import handler to process ! Check sqliimport.ini to define handlers' );
}
$memoryMax = memory_get_peak_usage();
$memoryMax = round( $memoryMax / 1024 / 1024, 2 ); // Convert in Megabytes
$cli->notice( 'Peak memory usage : '.$memoryMax.'M' );
}
$script->shutdown();
}
catch( Exception $e )
{
$errCode = $e->getCode();
$errCode = $errCode != 0 ? $errCode : 1; // If an error has occured, script must terminate with a status other than 0
$script->shutdown( $errCode, $e->getMessage() );
}