forked from kannera/TermBank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importPages.php
171 lines (130 loc) · 4.26 KB
/
importPages.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
<?php
/**
* ...
*
* @author Antti Kanner
* @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 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( 'source', '.', true, true );
$this->addOption( 'overwrite', '.', true, true );
$this->addOption( 'checked', '.', true, true );
}
public function execute() {
$overwrite = $this->getOption( 'overwrite' );
$checked = $this->getOption( 'checked' );
$source = $this->getOption( 'source' );
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();
}
$file = $this->getOption( 'filecode' );
$pages = $this->parseCSV( $file, 1 );
foreach ( $pages as $page ) {
$namespace = $page['namespace'];
$pagename = $page['pagename'];
$content = $page['content'];
global $wgContLang;
$namespaceId = $wgContLang->getNsIndex( $namespace );
if ( $namespaceId == false) {
echo "Namespace has no id";
die();
}
if ( !isset( $pagename ) ) continue;
$title = Title::makeTitleSafe( $namespaceId, $pagename );
if ( !$title ) {
echo "Invalid title for $pagename\n";
continue;
}
$this->insert( $title, $content, $namespace, $overwrite, $checked );
}
}
protected function parseCSV( $filename, $uniq = 0 ) {
$data = file_get_contents( $filename );
$rows = str_getcsv( $data, "\n" );
$output = array();
foreach ( $rows as $row ) {
$headers = array("namespace", "pagename", "content");
$values = str_getcsv( $row, "\t" );
if ( count( $values ) !== 3 ) {
echo "Row length not matching to headers\n";
var_dump( $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, $content, $namespace, $overwrite, $checked ) {
$templateSwitch = "}}{{";
$callSwitch = "|";
$content = preg_replace( '/}}{{/', "}}\n{{", $content );
$content = preg_replace( '/\|/', "\n|", $content );
$content = preg_replace( '/<putki>/', "|", $content );
if ($checked == 'y' ) $content = preg_replace( '/{{Käsite/', "{{Käsite|tarkistettu=y", $content );
echo $namespace, $content;
$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";
$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 );