forked from Nikerabbit/mediawiki-extensions-Termbank
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddMissingTerms.php
156 lines (130 loc) · 4.26 KB
/
addMissingTerms.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
<?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 = '%';
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 );
$this->addOption( 'namespace', '.', true, true );
$this->addOption( 'source', '.', true, true );*/
}
public function execute() {
$full = $this->parseCSV( 'n.txt', false );
$concepts = array();
foreach ( $full as $row ) {
$belongsTo = $row['käsite'];
$row = array_filter( $row );
if ( !isset( $concepts[$belongsTo] ) ) {
$concept[$belongsTo][] = $row;
}
}
############ Käsitesivut
foreach ( $concept as $name => $expressions ) {
$title = Title::makeTitle( NS_MIKROBIOLOGIA, $name );
if ( !$title->exists() ) {
echo $title->getPrefixedText() . " doesn't exist\n";
}
$page = TemplateParser::newFromTitle( $title );
$parsed = $page->extractTemplates();
foreach ( $parsed as $template ) {
if ( $template['name'] !== 'Liittyvä nimitys' ) continue;
foreach ( $expressions as $index => $exp ) {
if ( $template['params']['kieli'] === $exp['kieli'] &&
trim( $template['params']['nimitys'] ) === "Nimitys:" . $exp['nimitys'] ) {
echo "Found {$template['params']['nimitys']} {$template['params']['kieli']}\n";
unset( $expressions[$index] );
}
}
}
foreach ( $expressions as $exp ) {
echo "Addinng Nimitys:{$exp['nimitys']} {$exp['kieli']}\n";
$parsed[] = array(
'name' => 'Liittyvä nimitys',
'params' => array(
'kieli' => $exp['kieli'],
'nimitys' => "Nimitys:" . $exp['nimitys'],
)
);
}
$text = $page->updateText( $parsed );
if ( $text ) {
echo "Going to save page " . $title->getPrefixedText() . "\n";
# var_dump( $text );
# yay readline( "Ok?" );
$user = User::newFromName( 'Aineiston tuonti', false );
$wikipage = new WikiPage( $title );
$wikipage->doEdit( $text, 'Lisätään puuttuvia nimityksiä', 0, false, $user );
}
}
############ Nimityssivut
foreach ( $full as $exp ) {
$title = Title::makeTitle( NS_NIMITYS, $exp['nimitys'] );
if ( !$title->exists() ) {
echo $title->getPrefixedText() . " doesn't exist\n";
}
$page = TemplateParser::newFromTitle( $title );
$parsed = $page->extractTemplates();
foreach ( $parsed as $template ) {
if ( $template['name'] !== 'Nimitys' ) continue;
if ( $template['params']['kieli'] === $exp['kieli'] &&
trim( $template['params']['nimitys'] ) === $exp['nimitys'] ) {
continue 2;
}
}
$exp = array_filter( $exp );
$exp['lähdeaineisto'] = 'MikroMBS20-12';
$parsed[] = array(
'name' => 'Nimitys',
'params' => $exp,
);
$text = $page->updateText( $parsed );
if ( $text ) {
echo "Going to save page " . $title->getPrefixedText() . "\n";
#var_dump( $text );
#readline( "Ok?" );
$user = User::newFromName( 'Aineiston tuonti', false );
$wikipage = new WikiPage( $title );
$wikipage->doEdit( $text, 'Lisätään puuttuvia nimityksiä', 0, false, $user );
}
}
}
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 "Apua2\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;
}
}
$maintClass = 'TBImportExternalDatabase';
require_once( DO_MAINTENANCE );