forked from Nikerabbit/mediawiki-extensions-Termbank
-
Notifications
You must be signed in to change notification settings - Fork 1
/
importNotes.php
114 lines (87 loc) · 2.69 KB
/
importNotes.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
<?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 = 'Adds hidden notes only shown to experts';
$this->addOption( 'namespace', 'Working group namespace', true, true );
$this->addOption( 'notes', 'File containing the notes', true, true );
}
public function execute() {
$namespace = $this->getOption( 'namespace' );
global $wgContLang;
$namespaceId = $wgContLang->getNsIndex( $namespace );
if ( $namespaceId === false ) {
echo "EIN3: Unknown namespace: $namespace\n";
die();
}
$notes = $this->parseCSV( $this->getOption( 'notes' ), 1 );
foreach ( $notes as $i => $fields ) {
$käsite = $fields['käsite'];
if ( !$käsite ) {
continue;
}
$note = $fields['teksti'];
$note = str_replace( '\n', "\n", $note);
$title = Title::makeTitle( $namespaceId, $käsite );
if ( !$title ) {
echo "EIN1: Invalid title for {$käsite['käsite']}\n";
continue;
}
if ( !$title->exists() ) {
$name = $title->getPrefixedText();
echo "EIN2: Page does not exists: $name\n";
continue;
}
$this->insert( $title, $note );
}
}
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 );
$contents = $values;
$content = $values[1];
unset($contents[0]);
unset($contents[1]);
foreach ($contents as $element) {
$content = $content+'%'+$element;
}
$result[0] = $values[0];
$result[1] = $content;
if ( is_int($uniq) ) {
//echo '$uniq:'+$uniq;
$output[$values[$uniq]] = array_combine( $headers, $result );
} else {
$output[] = array_combine( $headers, $result );
}
}
return $output;
}
protected function insert( Title $title, $note ) {
$dbw = wfGetDB( DB_MASTER );
$fields = array( 'pd_page' => $title->getArticleId(), 'pd_text' => $note );
$dbw->replace( 'privatedata', array( array( 'pd_page' ) ), $fields, __METHOD__ );
}
}
$maintClass = 'TBImportExternalDatabase';
require_once( DO_MAINTENANCE );