forked from kannera/TermBank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpImport.php
68 lines (53 loc) · 1.9 KB
/
expImport.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
<?php
/*
* This file imports a csv file given in the expressions command line
* variable to termwiki
*
* The first field in the csv file is the expression
* The second field in the csv file is the content of the expression page
*/
// 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 = '%';
class TBImportExternalDatabase extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = '...';
$this->addOption('expressions', '.', true, true);
}
public function execute() {
foreach($this->parseCSV($this->getOption('expressions')) as $expression => $content) {
$title = Title::makeTitleSafe(NS_EXPRESSION, $expression);
if (!$title) {
echo "Invalid title for {$expression}\n";
continue;
}
$this->insert($title, $content);
}
}
protected function parseCSV($filename, $uniq = 0) {
$data = file_get_contents($filename);
$rows = str_getcsv($data, "\n");
$output = array();
foreach ($rows as $row) {
$values = str_getcsv($row, "\t");
$output[$values[0]] = $values[1];
}
return $output;
}
protected function insert(Title $title, $content) {
$content = str_replace("|language", "\n|language", $content);
$content = str_replace("|pos", "\n|pos", $content);
$content = str_replace("}}", "\n}}\n", $content);
$user = User::newFromName('SD-expression importer', false);
$page = new WikiPage($title);
$page->doEdit($content, 'SD-expression importer', 0, false, $user);
}
}
$maintClass = 'TBImportExternalDatabase';
require_once(DO_MAINTENANCE);