forked from Nikerabbit/mediawiki-extensions-Termbank
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.php
255 lines (214 loc) · 7.27 KB
/
import.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
<?php
/**
* ...
*
* @author Niklas Laxstrom
* @copyright Copyright © 2011-2012, Niklas Laxström
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
* @file
*/
// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$dir = dirname( __FILE__ ); $IP = "$dir/../..";
}
require_once( "$IP/maintenance/Maintenance.php" );
const SEPARATOR = '%';
const NIMIAVARUUS = 'Kielitiede';
const CONCEPT_TEMPLATE = 'Käsite';
const EXPRESSION_TEMPLATE = 'Nimitys';
const REF_EXPRESSION_TEMPLATE = 'Liittyvä nimitys';
const RELATED_CONCEPT_TEMPLATE = 'Lähikäsite';
const _REF_EXPRESSION_TEMPLATE = '_Liittyvä nimitys';
const _RELATED_CONCEPT_TEMPLATE = '_Lähikäsite';
const IMPORTING_USER = "Aineiston tuonti";
const CONCEPT_FIELD = 'käsite';
const EXPRESSION_FIELD = 'nimitys';
const SOURCE = 'lähdeaineisto';
const LANGUAGE = 'kieli';
const EQUIVALENCE = 'käännösvastaavuus';
const STATUS = 'käyttösuositus';
const ORIGIN = 'alkuperä';
const NOTE = 'käyttöhuomautus';
const ETYMOLOGY = 'etymologia';
const RELATED_CONCEPT = 'lähikäsite';
const RELATION = 'käsitesuhde';
class TBImportExternalDatabase extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = '...';
$this->addOption( 'filecode', '.', true, true );
$this->addOption( 'namespace', '.', true, true );
$this->addOption( 'source', '.', true, true );
$this->addOption( 'overwrite', '.', true, true );
$this->addOption( 'checked', '.', true, true );
}
public function execute() {
$overwrite = $this->getOption( 'overwrite' );
$namespace = $this->getOption( 'namespace' );
$checked = $this->getOption( 'checked' );
$source = $this->getOption( 'source' );
global $wgContLang;
$namespaceId = $wgContLang->getNsIndex( $namespace );
if ( $namespaceId == false) {
echo "Namespace has no id";
die();
}
if ( $this->getOption( 'checked' ) === 'y' OR $this->getOption( 'checked' ) === 'n') {
$checked == $this->getOption( 'checked' );
}
else {
echo "Invalid value in checked (y/n)";
die();
}
if ( $this->getOption( 'overwrite' ) === 'y' OR $this->getOption( 'overwrite' ) === 'n') {
$overwrite == $this->getOption( 'overwrite' );
}
else {
echo "Invalid value in overwrite (y/n)";
die();
}
$confile = $this->getOption( 'filecode' );
$confile .= "_con.csv";
$expfile = $this->getOption( 'filecode' );
$expfile .= "_exp.csv";
$relfile = $this->getOption( 'filecode' );
$relfile .= "_rel.csv";
echo "$confile, $expfile, $relfile";
$concepts = $this->parseCSV( $confile, 1 );
$expressions = $this->parseCSV( $expfile, false );
$relations = $this->parseCSV( $relfile, false );
foreach ( $expressions as $fields ) {
$concept = $fields[CONCEPT_FIELD];
if ( !$concept ) {
continue;
}
$referring = array(
EXPRESSION_FIELD => $fields[EXPRESSION_FIELD],
LANGUAGE => $fields[LANGUAGE],
EQUIVALENCE => $fields[EQUIVALENCE],
STATUS => $fields[STATUS],
ORIGIN => $fields[ORIGIN],
NOTE => $fields[NOTE]
);
$referring = array_filter( $referring );
if ( !isset( $concepts[$concept] ) ) {
echo "Expression refers to an unidentified concept: $concept\n";
}
$concepts[$concept][_REF_EXPRESSION_TEMPLATE][] = $referring;
}
foreach ( $relations as $fields ) {
$subject = $fields[CONCEPT_FIELD];
$referring = array(
CONCEPT_FIELD => $fields[RELATED_CONCEPT],
RELATION => $fields[RELATION],
);
$referring = array_filter( $referring );
if ( isset( $concepts[$subject] ) ) {
$concepts[$subject][_RELATED_CONCEPT][] = $referring;
}
}
foreach ( $expressions as $i => $fields ) {
unset( $fields[CONCEPT_FIELD] );
unset( $fields[ETYMOLOGY] );
unset( $fields[EQUIVALENCE] );
unset( $fields[NOTE] );
$expressions[$i] = array_filter( $fields );
}
foreach ( $concepts as $i => $concept ) {
unset( $concept['-'] );
if ( !isset( $concept['lähteet'] ) ) { echo "Apua1\n"; var_dump( $i, $concept ); die(); }
$lahteet = array_map( 'trim', explode( ',', $concept['lähteet'] ) );
$lahteet = array_flip( $lahteet );
unset( $lahteet['Väre'] );
unset( $lahteet['Piirainen'] );
ksort( $lahteet );
$concept['lähteet'] = implode( ', ', array_keys( $lahteet ) );
$concept = array_filter( $concept );
$concepts[$i] = $concept;
}
foreach ( $concepts as $concept ) {
if ( !isset( $concept[CONCEPT_FIELD] ) ) continue;
$title = Title::makeTitleSafe( $namespaceId, $concept[CONCEPT_FIELD] );
if ( !$title ) {
echo "Invalid title for $concept[CONCEPT_FIELD]\n";
continue;
}
$concept[SOURCE] = $source;
$this->insert( $title, CONCEPT_TEMPLATE, $concept, $namespace, $overwrite, $checked );
}
foreach ( $expressions as $exp ) {
if ( !isset( $exp[EXPRESSION_FIELD] ) ) continue;
$title = Title::makeTitleSafe( NS_NIMITYS, $exp[EXPRESSION_FIELD] );
if ( !$title ) {
echo "Invalid title for {$exp['nimitys']}\n";
continue;
}
$exp[SOURCE] = $source;
$this->insert( $title, EXPRESSION_TEMPLATE, $exp, $namespace, $overwrite, $checked );
}
}
protected function parseCSV( $filename, $uniq = 0 ) {
$data = file_get_contents( $filename );
$rows = str_getcsv( $data, "\n" );
$headerRow = array_shift( $rows );
$headers = str_getcsv( $headerRow, SEPARATOR );
$output = array();
foreach ( $rows as $row ) {
$values = str_getcsv( $row, SEPARATOR );
if ( count( $values ) !== count( $headers ) ) {
echo "Row length not matching to headers\n";
var_dump( $headers, $values ); die();
}
if ( is_int($uniq) ) {
$output[$values[$uniq]] = array_combine( $headers, $values );
} else {
$output[] = array_combine( $headers, $values );
}
}
return $output;
}
protected function insert( Title $title, $mainTemplate, $fields, $namespace, $overwrite, $checked ) {
$content = "{{" . "$mainTemplate\n";
if ( $mainTemplate === CONCEPT_TEMPLATE ) $content .= "|tarkistettu=$checked\n";
foreach ( $fields as $key => $value ) {
if ( $key[0] === '_' ) continue;
$value = preg_replace( '/\[([^\]]+?)\|([^\]]+?)\]/', "[[$namespace:$1|$2]]", $value );
$value = preg_replace( '/\[([^|\]]+?)\]/', "[[$namespace:$1|$1]]", $value );
$content .= "|$key=$value\n";
}
$content .= "}}\n";
foreach ( $fields as $key => $value ) {
if ( $key[0] !== '_' ) continue;
$key = ltrim( $key, '_' );
foreach ( $value as $subitem ) {
$content .= "{{" . "$key\n";
if ($key == 'Liittyvä nimitys' ) $content .= "|otsikossa=Y\n";
foreach ( $subitem as $subkey => $subvalue ) {
$content .= "|$subkey=$subvalue\n";
}
$content .= "}}\n";
}
}
$user = User::newFromName( IMPORTING_USER, false );
$page = new WikiPage( $title );
echo "Importing $title";
if ($page->exists() == true) {
echo " --> Wiki already has page: $title";
if ($overwrite == 'y' ) {
echo " --> Replacing\n$content\n";
$page->doEdit( $content, IMPORTING_USER, 0, false, $user );
}
if ($overwrite == 'n' ) {
echo " --> Not replaced\n";
}
}
else {
echo "-->Saved\n";
$page->doEdit( $content, IMPORTING_USER, 0, false, $user );
}
}
}
$maintClass = 'TBImportExternalDatabase';
require_once( DO_MAINTENANCE );