-
Notifications
You must be signed in to change notification settings - Fork 6
/
grabPageRestrictions.php
153 lines (127 loc) · 4.01 KB
/
grabPageRestrictions.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
<?php
/**
* Maintenance script to grab the page restrictions from a wiki (to which we have only read-only access instead of
* full database access). It's worth noting that grabText.php and grabNewText.php already import page restrictions,
* so this script is only really useful if you're using an XML dump that doesn't include page restrictions.
*
* @file
* @ingroup Maintenance
* @author Jayden Bailey <[email protected]>
* @version 1.0
* @date 10 September 2023
*/
use MediaWiki\MediaWikiServices;
require_once 'includes/ExternalWikiGrabber.php';
class GrabPageRestrictions extends ExternalWikiGrabber {
/**
* Current number of pages retrieved
*
* @var int
*/
protected int $pageCount = 0;
public function __construct() {
parent::__construct();
$this->addDescription( 'Grabs page restrictions from a pre-existing wiki into a new wiki.' );
$this->addOption( 'namespaces', 'Pipe-separated namespaces (ID) to grab. Defaults to all namespaces', false, true );
}
public function execute() {
parent::execute();
$this->output( "\n" );
# Get all pages as a list, start by getting namespace numbers...
$this->output( "Retrieving namespaces list...\n" );
$params = [
'meta' => 'siteinfo',
'siprop' => 'namespaces'
];
$result = $this->bot->query( $params );
$siteinfo = $result['query'];
# No data - bail out early
if ( empty( $siteinfo ) ) {
$this->fatalError( 'No siteinfo data found' );
}
$textNamespaces = [];
if ( $this->hasOption( 'namespaces' ) ) {
$textNamespaces = explode( '|', $this->getOption( 'namespaces', '' ) );
} else {
foreach ( array_keys( $siteinfo['namespaces'] ) as $ns ) {
# Ignore special
if ( $ns >= 0 ) {
$textNamespaces[] = $ns;
}
}
}
if ( !$textNamespaces ) {
$this->fatalError( 'Got no namespaces' );
}
foreach ( $textNamespaces as $ns ) {
$this->processNamespace( $ns );
}
$this->output( "Done: $this->pageCount entries processed\n" );
}
public function processNamespace( $ns ) {
$params = [
'generator' => 'allpages',
'gaplimit' => 'max',
'prop' => 'info',
'inprop' => 'protection',
'gapprtype' => 'edit|move|upload',
'gapnamespace' => $ns
];
$more = true;
$this->output( "Grabbing pages with restrictions for namespace $ns...\n" );
do {
$result = $this->bot->query( $params );
if ( empty( $result['query']['pages'] ) ) {
return;
}
foreach ( $result['query']['pages'] as $page ) {
$this->processPage( $page );
$this->pageCount++;
}
if ( isset( $result['query-continue']['pages'] ) ) {
$params = array_merge( $params, $result['query-continue']['pages'] );
$this->output( "$this->pageCount entries processed.\n" );
} elseif ( isset( $result['continue'] ) ) {
$params = array_merge( $params, $result['continue'] );
$this->output( "$this->pageCount entries processed.\n" );
} else {
$more = false;
}
} while ( $more );
}
public function processPage( $page ) {
$pageStore = MediaWikiServices::getInstance()->getPageStore();
$ourPage = $pageStore->getPageById( $page['pageid'] );
if ( is_null( $ourPage ) ) {
// This page doesn't exist in our database, so ignore it.
return;
}
// Delete first any existing protection
$this->dbw->delete(
'page_restrictions',
[ 'pr_page' => $page['pageid'] ],
__METHOD__
);
$this->output( "Setting page_restrictions on page_id {$page['pageid']}.\n" );
// insert current restrictions
foreach ( $page['protection'] as $prot ) {
// Skip protections inherited from cascade protections
if ( !isset( $prot['source'] ) ) {
$expiry = $prot['expiry'] == 'infinity' ? 'infinity' : wfTimestamp( TS_MW, $prot['expiry'] );
$this->dbw->insert(
'page_restrictions',
[
'pr_page' => $page['pageid'],
'pr_type' => $prot['type'],
'pr_level' => $prot['level'],
'pr_cascade' => (int)isset( $prot['cascade'] ),
'pr_expiry' => $expiry
],
__METHOD__
);
}
}
}
}
$maintClass = 'GrabPageRestrictions';
require_once RUN_MAINTENANCE_IF_MAIN;