-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimportPages.php
136 lines (113 loc) · 3.93 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
<?php
/**
* ...
*
* @author Antti Kanner
* @copyright Copyright © 2011-2024, Niklas Laxström
* @license GPL-2.0-or-later
* @file
*/
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
$env = getenv( 'MW_INSTALL_PATH' );
$IP = $env !== false ? $env : __DIR__ . '/../..';
require_once "$IP/maintenance/Maintenance.php";
class TermbankImportPages extends Maintenance {
private const IMPORTING_USER = 'Aineiston tuonti';
public function __construct() {
parent::__construct();
$this->addOption( 'filecode', '.', true, true );
$this->addOption( 'overwrite', '.', true, true );
$this->addOption( 'checked', '.', true, true );
$this->addOption( 'extend', '.', true, true );
}
public function execute(): void {
$overwrite = $this->getOption( 'overwrite' );
$checked = $this->getOption( 'checked' );
$extend = $this->getOption( 'extend' );
$file = $this->getOption( 'filecode' );
$pages = $this->parseCSV( $file );
foreach ( $pages as $page ) {
$namespace = $page['namespace'] ?? '';
$pagename = $page['pagename'] ?? '';
$content = $page['content'] ?? '';
$content = UtfNormal\Validator::cleanUp( $content );
$title = Title::newFromText( UtfNormal\Validator::cleanUp( "$namespace:$pagename" ) );
if ( !$title || $title->inNamespace( NS_MAIN ) ) {
echo "Invalid title for $pagename\n";
continue;
}
$this->insert( $title, $content, $overwrite, $checked, $extend );
}
}
/** Eats a filename, returns a list of dicts(ns, title, content) */
protected function parseCSV( $filename ): array {
$data = file_get_contents( $filename );
$rows = str_getcsv( $data, "\n" );
$output = [];
foreach ( $rows as $row ) {
$headers = [ "namespace", "pagename", "content" ];
$values = str_getcsv( $row, "\t" );
if ( count( $values ) !== 3 ) {
echo "Row length not matching to headers\n";
var_dump( $values );
die();
}
$output[] = array_combine( $headers, $values );
}
print_r( $output );
return $output;
}
protected function insert( Title $title, $content, $overwrite, $checked, $extend ): void {
$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 );
}
if ( $checked === 'n' ) {
$content = preg_replace( '/{{Käsite\|/', "{{Käsite|tarkistettu=N|", $content );
}
$wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
$userFactory = MediaWikiServices::getInstance()->getUserFactory();
$user = $userFactory->newFromName( self::IMPORTING_USER );
if ( !$user ) {
$this->fatalError( "Invalid user name: " . self::IMPORTING_USER );
}
$content = UtfNormal\Validator::cleanUp( $content );
$contentObj = ContentHandler::makeContent( $content, $title );
$page = $wikiPageFactory->newFromTitle( $title );
echo "Importing $title";
if ( $page->exists() ) {
echo " --> Wiki already has page: $title\n";
if ( $overwrite === 'y' ) {
echo " --> Replacing\n";
if ( $extend === 'y' ) {
if ( strlen( $content ) > strlen( $page->getUserText() ) ) {
$page->newPageUpdater( $user )
->setContent( SlotRecord::MAIN, $contentObj )
->saveRevision( CommentStoreComment::newUnsavedComment(
self::IMPORTING_USER
) );
} else {
echo "--> not extended";
}
} else {
$page->newPageUpdater( $user )
->setContent( SlotRecord::MAIN, $contentObj )
->saveRevision( CommentStoreComment::newUnsavedComment(
self::IMPORTING_USER
) );
}
}
// else: not replaced
} else {
echo " --> Saved\n";
$page->newPageUpdater( $user )
->setContent( SlotRecord::MAIN, $contentObj )
->saveRevision( CommentStoreComment::newUnsavedComment( self::IMPORTING_USER ) );
}
}
}
$maintClass = TermbankImportPages::class;
require_once RUN_MAINTENANCE_IF_MAIN;