1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../../..'; |
4
|
|
|
require_once $basePath . '/maintenance/Maintenance.php'; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class ImportJSONData extends Maintenance { |
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
public function __construct() { |
11
|
|
|
parent::__construct(); |
12
|
|
|
$this->addDescription( "\n" . |
13
|
|
|
"Script for importing data stored in JSON stores\n" |
14
|
|
|
); |
15
|
|
|
$this->addDefaultParams(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see Maintenance::addDefaultParams |
20
|
|
|
*/ |
21
|
|
|
protected function addDefaultParams() { |
22
|
|
|
$this->addOption( 'delimiter', 'The delimiter parameter sets the field delimiter (a single character)', false, true, "d" ); |
23
|
|
|
$this->addOption( 'separator', 'The separator parameter sets the surrounding character of each field (a single character)', false, true, "s" ); |
24
|
|
|
$this->addOption( 'namespace', 'Namespace where to store data (main namespace, empty one, by default)', false, true, "n" ); |
25
|
|
|
$this->addOption( 'rowfields', 'Comma-separated list of fields to consider', false, true, "f" ); |
26
|
|
|
$this->addOption( 'rowobject', 'Subobject row property', false, true, "r" ); |
27
|
|
|
$this->addOption( 'user', 'Username to which edits should be attributed. ' .'Default: "Maintenance script"', false, true, 'u' ); |
28
|
|
|
$this->addOption( 'single', 'Enable single mode import', false, false, 'i' ); |
29
|
|
|
// $this->addArg( 'file', 'Data files to be imported' ); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @see Maintenance::execute |
34
|
|
|
*/ |
35
|
|
|
public function execute() { |
36
|
|
|
|
37
|
|
|
$delimiter = $this->getOption( "delimiter", '"' ); |
38
|
|
|
$separator = $this->getOption( "separator", ',' ); |
39
|
|
|
$namespace = $this->getOption( "namespace", '' ); |
40
|
|
|
$rowobject = $this->getOption( "rowobject", null ); |
41
|
|
|
$rowfields = $this->getOption( "rowfields", null ); |
42
|
|
|
$userName = $this->getOption( 'user', false ); |
43
|
|
|
$single = $this->getOption( 'single', false ); |
44
|
|
|
|
45
|
|
|
// Get all the arguments. A loop is required since Maintenance doesn't |
46
|
|
|
// suppport an arbitrary number of arguments. |
47
|
|
|
$files = []; |
48
|
|
|
$i = 0; |
49
|
|
|
while ( $arg = $this->getArg( $i++ ) ) { |
50
|
|
|
if ( file_exists( $arg ) ) { |
51
|
|
|
$files[$arg] = file_get_contents( $arg ); |
52
|
|
|
} else { |
53
|
|
|
$this->error( "Fatal error: The file '$arg' does not exist!", 1 ); |
54
|
|
|
} |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$count = count( $files ); |
58
|
|
|
$this->output( "Importing $count pages...\n" ); |
59
|
|
|
|
60
|
|
|
if ( $userName === false ) { |
61
|
|
|
$user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); |
62
|
|
|
} else { |
63
|
|
|
$user = User::newFromName( $userName ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ( !$user ) { |
67
|
|
|
$this->error( "Invalid username\n", true ); |
68
|
|
|
} |
69
|
|
|
if ( $user->isAnon() ) { |
70
|
|
|
$user->addToDatabase(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// TODO: Need to review this in a more efficient way |
74
|
|
|
foreach ( $files as $file => $text ) { |
75
|
|
|
|
76
|
|
|
if( $this->isJSON($rowfields) ){ |
77
|
|
|
$row=json_decode($rowfields); |
78
|
|
|
$rowfields=""; |
|
|
|
|
79
|
|
|
$rowfields=$row; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$data = $this->csv_to_array( $text, trim( $separator ), trim( $delimiter ) ); |
83
|
|
|
|
84
|
|
|
$dataObj = $this->arraySort( $data ); |
85
|
|
|
|
86
|
|
|
for( $i=0; $i<count($dataObj); $i++ ){ |
|
|
|
|
87
|
|
|
|
88
|
|
|
$title = ""; |
89
|
|
|
|
90
|
|
|
for( $j=0; $j<count($dataObj[$i]); $j++ ){ |
|
|
|
|
91
|
|
|
$title = array_shift( $dataObj[$i][$j] ); |
92
|
|
|
} |
93
|
|
|
//print_r($dataObj[$i]); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$metaObj = array( 'app' => 'SDI','version' => 0.1 ); |
96
|
|
|
|
97
|
|
|
if ( $rowobject ) { |
98
|
|
|
$metaObj["rowobject"] = $rowobject; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ( $rowfields ) { |
102
|
|
|
$metaObj["rowfields"] = $rowfields; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ( $single ) { |
106
|
|
|
$metaObj["single"] = true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$obj = array('data' => $dataObj[$i],'meta' => $metaObj ); |
110
|
|
|
|
111
|
|
|
//print_r($obj); |
112
|
|
|
$jsonStr = json_encode( $obj ); |
113
|
|
|
//print_r($jsonStr); |
114
|
|
|
if ( ! empty( $title ) ) { |
115
|
|
|
|
116
|
|
|
$fulltitle = $title; |
117
|
|
|
if ( $namespace !== "" ) { |
118
|
|
|
$fulltitle = $namespace.":".$title; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$status = SDImportData::importJSON( $jsonStr, $fulltitle, true ); |
|
|
|
|
122
|
|
|
echo "Data ".$title." completed\n"; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
echo "\nHas been successfully completed\n"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
This function parse the array with a delimiter and a separator given by the user |
132
|
|
|
**/ |
133
|
|
|
private function csv_to_array( $text, $delimiter, $separator ){ |
134
|
|
|
|
135
|
|
|
// Splitting lines |
136
|
|
|
$lines = preg_split( '/$\R?^/m', $text ); |
137
|
|
|
|
138
|
|
|
foreach ( $lines as $line ) { |
139
|
|
|
$data[] = str_getcsv( $text, $delimiter, $separator ); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $data; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
/** |
145
|
|
|
Sort the array and group it by title |
146
|
|
|
**/ |
147
|
|
|
private function arraySort($input){ |
148
|
|
|
|
149
|
|
|
foreach ($input as $key=>$val) $output[$val[0]][]=$val; |
|
|
|
|
150
|
|
|
$output = $this->removeKeys( $output ); |
|
|
|
|
151
|
|
|
return $output; |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
/** |
155
|
|
|
Remove the keys from the array |
156
|
|
|
**/ |
157
|
|
|
private function removeKeys( array $array ){ |
158
|
|
|
|
159
|
|
|
$array = array_values( $array ); |
160
|
|
|
foreach ( $array as &$value ){ |
161
|
|
|
if ( is_array( $value ) ){ |
162
|
|
|
$value = removeKeys( $value ); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $array; |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function isJSON($string){ |
171
|
|
|
return is_string($string) && is_array(json_decode($string, true)) ? true : false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
$maintClass = 'ImportJSONData'; |
178
|
|
|
require_once( DO_MAINTENANCE ); |
179
|
|
|
|
180
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.