-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrabUserBlocks.php
126 lines (107 loc) · 3.96 KB
/
grabUserBlocks.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
<?php
/**
* Maintenance script to grab the user block data from a wiki (to which we have
* only read-only access instead of full database access).
*
* @file
* @ingroup Maintenance
* @author Jack Phoenix
* @author Jesús Martínez <[email protected]>
* @version 1.1
* @date 5 August 2019
* @note Based on code by:
* - Legoktm & Uncyclopedia development team, 2013 (blocks_table.py)
*/
use MediaWiki\MediaWikiServices;
require_once 'includes/ExternalWikiGrabber.php';
class GrabUserBlocks extends ExternalWikiGrabber {
public function __construct() {
parent::__construct();
$this->addDescription( 'Grabs user block data from a pre-existing wiki into a new wiki.' );
$this->addOption( 'startdate', 'Start point (20121222142317, 2012-12-22T14:23:17Z, etc).', false, true );
$this->addOption( 'enddate', 'End point (20121222142317, 2012-12-22T14:23:17Z, etc); defaults to current timestamp.', false, true );
}
public function execute() {
parent::execute();
$startDate = $this->getOption( 'startdate' );
if ( $startDate ) {
if ( !wfTimestamp( TS_ISO_8601, $startDate ) ) {
$this->fatalError( 'Invalid startdate format.' );
}
}
$endDate = $this->getOption( 'enddate' );
if ( $endDate ) {
if ( !wfTimestamp( TS_ISO_8601, $endDate ) ) {
$this->fatalError( 'Invalid enddate format.' );
}
} else {
$endDate = wfTimestampNow();
}
$params = [
'list' => 'blocks',
'bkdir' => 'newer',
'bkend' => $endDate,
'bklimit' => 'max',
'bkprop' => 'id|user|userid|by|byid|timestamp|expiry|reason|range|flags',
];
if ( $startDate !== null ) {
$params['bkstart'] = $startDate;
}
$more = true;
$i = 0;
$this->output( "Grabbing blocks...\n" );
do {
$result = $this->bot->query( $params );
if ( empty( $result['query']['blocks'] ) ) {
$this->fatalError( 'No blocks, hence nothing to do. Aborting the mission.' );
}
foreach ( $result['query']['blocks'] as $logEntry ) {
// Skip autoblocks, nothing we can do about 'em
if ( !isset( $logEntry['automatic'] ) ) {
$this->processEntry( $logEntry );
$i++;
}
if ( isset( $result['query-continue'] ) && isset( $result['query-continue']['blocks'] ) ) {
$params = array_merge( $params, $result['query-continue']['blocks'] );
$this->output( "{$i} entries processed.\n" );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
$this->output( "{$i} entries processed.\n" );
} else {
$more = false;
}
}
# Readd the AUTO_INCREMENT here
} while ( $more );
$this->output( "Done: $i entries processed\n" );
}
public function processEntry( $entry ) {
$ts = wfTimestamp( TS_MW, $entry['timestamp'] );
$commentStore = MediaWikiServices::getInstance()->getCommentStore();
$commentFields = $commentStore->insert( $this->dbw, 'ipb_reason', $entry['reason'] );
$data = [
'ipb_id' => $entry['id'],
'ipb_address' => $entry['user'],
'ipb_user' => $entry['userid'],
#'ipb_by' => $entry['byid'],
#'ipb_by_text' => $entry['by'],
#'ipb_reason' => $entry['reason'],
'ipb_by_actor' => $this->getActorFromUser( $entry['byid'], $entry['by'] ),
'ipb_timestamp' => $ts,
'ipb_auto' => 0,
'ipb_anon_only' => isset( $entry['anononly'] ),
'ipb_create_account' => isset( $entry['nocreate'] ),
'ipb_enable_autoblock' => isset( $entry['autoblock'] ),
'ipb_expiry' => ( $entry['expiry'] == 'infinity' ? $this->dbw->getInfinity() : wfTimestamp( TS_MW, $entry['expiry'] ) ),
'ipb_range_start' => ( isset( $entry['rangestart'] ) ? $entry['rangestart'] : false ),
'ipb_range_end' => ( isset( $entry['rangeend'] ) ? $entry['rangeend'] : false ),
'ipb_deleted' => isset( $entry['hidden'] ),
'ipb_block_email' => isset( $entry['noemail'] ),
'ipb_allow_usertalk' => isset( $entry['allowusertalk'] ),
] + $commentFields;
$this->dbw->insert( 'ipblocks', $data, __METHOD__ );
$this->dbw->commit();
}
}
$maintClass = 'GrabUserBlocks';
require_once RUN_MAINTENANCE_IF_MAIN;