|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\SsPak\DataExtractor; |
|
4
|
|
|
|
|
5
|
|
|
use DB; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Connects to the SilverStripe Database object of a given SilverStripe project, |
|
9
|
|
|
* in order to bulk save/load data |
|
10
|
|
|
*/ |
|
11
|
|
|
class DatabaseConnector |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
private $basePath; |
|
15
|
|
|
private $isConnected = false; |
|
16
|
|
|
|
|
17
|
|
|
function __construct($basePath) { |
|
|
|
|
|
|
18
|
|
|
$this->basePath = $basePath; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
function connect() { |
|
|
|
|
|
|
22
|
|
|
if ($this->isConnected) { |
|
23
|
|
|
return; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->isConnected = true; |
|
27
|
|
|
|
|
28
|
|
|
// Necessary for SilverStripe's _ss_environment.php loader to work |
|
29
|
|
|
$_SERVER['SCRIPT_FILENAME'] = $this->basePath . '/dummy.php'; |
|
30
|
|
|
|
|
31
|
|
|
global $databaseConfig; |
|
32
|
|
|
|
|
33
|
|
|
// require composers autoloader |
|
34
|
|
|
if (file_exists($this->basePath . '/vendor/autoload.php')) { |
|
35
|
|
|
require_once $this->basePath . '/vendor/autoload.php'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (file_exists($this->basePath . '/framework/core/Core.php')) { |
|
39
|
|
|
require_once($this->basePath . '/framework/core/Core.php'); |
|
40
|
|
|
} elseif (file_exists($this->basePath . '/sapphire/core/Core.php')) { |
|
41
|
|
|
require_once($this->basePath . '/sapphire/core/Core.php'); |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new \LogicException("No framework/core/Core.php or sapphire/core/Core.php included in project. Perhaps $this->basePath is not a SilverStripe project?"); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Connect to database |
|
47
|
|
|
require_once('model/DB.php'); |
|
48
|
|
|
|
|
49
|
|
|
if ($databaseConfig) { |
|
50
|
|
|
DB::connect($databaseConfig); |
|
51
|
|
|
} else { |
|
52
|
|
|
throw new \LogicException("No \$databaseConfig found"); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
function getDatabase() { |
|
|
|
|
|
|
57
|
|
|
$this->connect(); |
|
58
|
|
|
|
|
59
|
|
|
if(method_exists('DB', 'get_conn')) { |
|
60
|
|
|
return DB::get_conn(); |
|
61
|
|
|
} else { |
|
62
|
|
|
return DB::getConn(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get a list of tables from the database |
|
68
|
|
|
*/ |
|
69
|
|
|
function getTables() { |
|
|
|
|
|
|
70
|
|
|
$this->connect(); |
|
71
|
|
|
|
|
72
|
|
|
if(method_exists('DB', 'table_list')) { |
|
73
|
|
|
return DB::table_list(); |
|
74
|
|
|
} else { |
|
75
|
|
|
return DB::tableList(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get a list of tables from the database |
|
81
|
|
|
*/ |
|
82
|
|
|
function getFieldsForTable($tableName) { |
|
|
|
|
|
|
83
|
|
|
$this->connect(); |
|
84
|
|
|
|
|
85
|
|
|
if(method_exists('DB', 'field_list')) { |
|
86
|
|
|
return DB::field_list($tableName); |
|
87
|
|
|
} else { |
|
88
|
|
|
return DB::fieldList($tableName); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Save the named table to the given table write |
|
94
|
|
|
*/ |
|
95
|
|
|
function saveTable($tableName, TableWriter $writer) { |
|
|
|
|
|
|
96
|
|
|
$query = $this->getDatabase()->query("SELECT * FROM \"$tableName\""); |
|
97
|
|
|
|
|
98
|
|
|
foreach ($query as $record) { |
|
99
|
|
|
$writer->writeRecord($record); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$writer->finish(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Save the named table to the given table write |
|
107
|
|
|
*/ |
|
108
|
|
|
function loadTable($tableName, TableReader $reader) { |
|
|
|
|
|
|
109
|
|
|
$this->getDatabase()->clearTable($tableName); |
|
110
|
|
|
|
|
111
|
|
|
$fields = $this->getFieldsForTable($tableName); |
|
112
|
|
|
|
|
113
|
|
|
foreach($reader as $record) { |
|
114
|
|
|
foreach ($record as $k => $v) { |
|
115
|
|
|
if (!isset($fields[$k])) { |
|
116
|
|
|
unset($record[$k]); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
// TODO: Batch records |
|
120
|
|
|
$manipulation = [ |
|
121
|
|
|
$tableName => [ |
|
122
|
|
|
'command' => 'insert', |
|
123
|
|
|
'fields' => $record, |
|
124
|
|
|
], |
|
125
|
|
|
]; |
|
126
|
|
|
DB::manipulate($manipulation); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.