-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimportNotes.php
86 lines (74 loc) · 2.25 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
<?php
/**
* ...
*
* @author Niklas Laxstrom
* @copyright Copyright © 2011-2012, Niklas Laxström
* @license GPL-2.0-or-later
* @file
*/
use MediaWiki\MediaWikiServices;
$env = getenv( 'MW_INSTALL_PATH' );
$IP = $env !== false ? $env : __DIR__ . '/../..';
require_once "$IP/maintenance/Maintenance.php";
const SEPARATOR = '\t';
class TermbankImportNotes extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = 'Adds hidden notes only shown to experts';
$this->addOption( 'notes', 'File containing the notes', true, true );
}
public function execute() {
$contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();
$notes = $this->parseCSV( $this->getOption( 'notes' ) );
foreach ( $notes as $fields ) {
$käsite = $fields['käsite'];
if ( !$käsite ) {
continue;
}
$namespace = $fields['alue'];
$namespaceId = $contentLanguage->getNsIndex( $namespace );
if ( $namespaceId === false ) {
echo "EIN3: Unknown namespace: $namespace\n";
} else {
$note = $fields['teksti'];
$note = str_replace( '\n', "\n", $note );
$title = Title::makeTitle( $namespaceId, $käsite );
if ( !$title ) {
echo "EIN1: Invalid title for {$käsite}\n";
continue;
} elseif ( !$title->exists() ) {
$name = $title->getPrefixedText();
echo "EIN2: Page does not exists: $name\n";
continue;
} else {
$this->insert( $title, $note );
}
}
}
}
protected function parseCSV( $filename ): array {
$data = file_get_contents( $filename );
$rows = str_getcsv( $data, "\n" );
$headerRow = array_shift( $rows );
$headers = str_getcsv( $headerRow, "\t" );
$output = [];
foreach ( $rows as $row ) {
$outputRow = str_getcsv( $row, "\t" );
$rowcount = count( $outputRow );
$concept = $outputRow[1];
if ( $rowcount != 3 ) {
echo "$concept\n";
}
$output[] = array_combine( $headers, $outputRow );
}
return $output;
}
protected function insert( Title $title, $note ) {
$dbw = wfGetDB( DB_PRIMARY );
$fields = [ 'pd_page' => $title->getArticleId(), 'pd_text' => $note ];
$dbw->replace( 'privatedata', [ [ 'pd_page' ] ], $fields, __METHOD__ );
}
}
$maintClass = TermbankImportNotes::class;
require_once RUN_MAINTENANCE_IF_MAIN;