forked from Nikerabbit/mediawiki-extensions-Termbank
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.php.save
167 lines (142 loc) · 4.83 KB
/
import.php.save
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
<?php
/**
* ...
*
* @author Niklas Laxstrom
* @copyright Copyright © 2011, 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" );
class TBImportExternalDatabase extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = '...';
$this->addOption( 'expressions', '.', true, true );
$this->addOption( 'concepts', ',', true, true );
$this->addOption( 'relations', '-', true, true );
}
public function execute() {
$desc = parse_ini_file( 'kasviaineisto.ini', true );
$namespace = $desc['database']['namespace'];
$concepts = $this->parseCSV( $this->getOption( 'concepts' ) );
$expressions = $this->parseCSV( $this->getOption( 'expressions' ) );
$relations = $this->parseCSV( $this->getOption( 'relations' ), false );
foreach ( $expressions as $i => $fields ) {
$käsite = $fields['käsite'];
if ( !$käsite ) {
continue;
}
$liittyvä = array(
'nimitys' => 'Nimitys:' . $fields['nimitys'],
'kieli' => $fields['kieli'],
'käännösvastaavuus' => $fields['käännösvastaavuus'],
'käyttösuositus' => $fields['käyttösuositus'],
'alkuperä' => $fields['alkuperä']
);
$liittyvä = array_filter( $liittyvä );
$concepts[$käsite]['_Liittyvä nimitys'][] = $liittyvä;
}
foreach ( $relations as $i => $fields ) {
$subject = $fields['-'];
$liittyvä = array(
'käsite' => 'Kasvitiede:' . $fields['käsite'],
'käsitesuhde' => $fields['käsitesuhde'],
);
$liittyvä = array_filter( $liittyvä );
if ( isset( $concepts[$subject] ) ) {
$concepts[$subject]['_Lähikäsite'][] = $liittyvä;
}
}
foreach ( $expressions as $i => $fields ) {
unset( $fields['käsite'] );
unset( $fields['etymologia'] );
unset( $fields['käännösvastaavuus'] );
unset( $fields['käyttösuositus'] );
$expressions[$i] = array_filter( $fields );
}
foreach ( $concepts as $i => $concept ) {
unset( $concept['-'] );
if ( !isset( $concept['lähteet'] ) ) { var_dump( $i, $concept ); die(); }
$lähteet = array_map( 'trim', explode( ',', $concept['lähteet'] ) );
$lähteet = array_flip( $lähteet );
unset( $lähteet['Väre'] );
unset( $lähteet['Piirainen'] );
ksort( $lähteet );
$concept['lähteet'] = implode( ', ', array_keys( $lähteet ) );
$concept = array_filter( $concept );
$concepts[$i] = $concept;
}
foreach ( $concepts as $concept ) {
if ( !isset( $concept['käsite'] ) ) continue;
$title = Title::makeTitleSafe( NS_KASVITIEDE, $concept['käsite'] );
if ( !$title ) {
echo "Invalid title for {$concept['käsite']}\n";
continue;
}
$concept['aineistotunnus'] = 'Kasvitiedetesti';
$this->insert( $title, 'Käsite', $concept );
}
foreach ( $expressions as $exp ) {
if ( !isset( $exp['nimitys'] ) ) continue;
$title = Title::makeTitleSafe( NS_NIMITYS, $exp['nimitys'] );
if ( !$title ) {
echo "Invalid title for {$exp['nimitys']}\n";
continue;
}
$exp['aineistotunnus'] = 'Kasvitiedetesti';
$this->insert( $title, 'Nimitys', $exp );
}
}
protected function parseCSV( $filename, $uniq = 0 ) {
$data = file_get_contents( $filename );
$rows = str_getcsv( $data, "\n" );
$headerRow = array_shift( $rows );
$headers = str_getcsv( $headerRow, ";" );
$output = array();
foreach ( $rows as $row ) {
$values = str_getcsv( $row, ";" );
if ( count( $values ) !== count( $headers ) ) {
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 ) {
$content = "{{" . "$mainTemplate\n";
foreach ( $fields as $key => $value ) {
if ( $key[0] === '_' ) continue;
$value = preg_replace( '/\[([^\]]+?)\|([^\]]+?)\]/', '[[Kasvitiede:$1|$2]]', $value );
$value = preg_replace( '/\[([^|\]]+?)\]/', '[[Kasvitiede:$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";
foreach ( $subitem as $subkey => $subvalue ) {
$content .= "|$subkey=$subvalue\n";
}
$content .= "}}\n";
}
}
$user = User::newFromName( 'Aineiston tuonti', false );
$page = new WikiPage( $title );
$page->doEdit( $content, 'Aineiston tuonti', 0, false, $user );
}
}
$maintClass = 'TBImportExternalDatabase';
require_once( DO_MAINTENANCE );