/
manageInactiveWikis.php
279 lines (244 loc) · 8.4 KB
/
manageInactiveWikis.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
* @author Southparkfan
* @author John Lewis
* @version 2.0
*/
require_once( __DIR__ . '/../../../maintenance/Maintenance.php' );
class ManageInactiveWikis extends Maintenance {
public function __construct() {
parent::__construct();
$this->addOption( 'write', 'Actually make changes to wikis which are considered for the next stage in dormancy', false, false );
$this->mDescription = 'A script to find inactive wikis in a farm.';
}
public function execute() {
global $wgCreateWikiDatabase, $wgCreateWikiStateDays;
$dbw = wfGetDB( DB_MASTER, [], $wgCreateWikiDatabase );
$res = $dbw->select(
'cw_wikis',
[
'wiki_dbname',
'wiki_inactive',
'wiki_inactive_timestamp',
'wiki_closed',
'wiki_closed_timestamp',
'wiki_creation',
'wiki_deleted'
],
[
'wiki_inactive_exempt' => 0,
'wiki_deleted' => 0
],
__METHOD__
);
foreach ( $res as $row ) {
$dbName = $row->wiki_dbname;
$inactive = $row->wiki_inactive;
$inactiveDate = $row->wiki_inactive_timestamp;
$closed = $row->wiki_closed;
$closedDate = $row->wiki_closed_timestamp;
$deleted = $row->wiki_deleted;
$wikiCreation = $row->wiki_creation;
$inactiveDays = (int)$wgCreateWikiStateDays['inactive'];
if ( !$deleted && $wikiCreation < date( "YmdHis", strtotime( "-{$inactiveDays} days" ) ) ) {
$this->checkLastActivity( $dbName, $inactive, $inactiveDate, $closed, $closedDate, $dbw );
}
}
}
public function checkLastActivity( $dbName, $inactive, $inactiveDate, $closed, $closedDate, $dbw ) {
global $wgCreateWikiStateDays, $wgCreateWikiDatabase;
$dbw->selectDomain( $dbName );
$lastEntryObj = $dbw->selectRow(
'recentchanges',
'rc_timestamp',
[
"rc_log_action != 'renameuser'"
],
__METHOD__,
[
'ORDER BY' => 'rc_timestamp DESC',
]
);
$dbw->selectDomain( $wgCreateWikiDatabase );
$inactiveDays = (int)$wgCreateWikiStateDays['inactive'];
$closeDays = (int)$wgCreateWikiStateDays['closed'];
$removeDays = (int)$wgCreateWikiStateDays['removed'];
$canWrite = $this->hasOption( 'write' );
// Wiki doesn't seem inactive: go on to the next wiki.
if ( isset( $lastEntryObj->rc_timestamp ) && $lastEntryObj->rc_timestamp > date( "YmdHis", strtotime( "-{$inactiveDays} days" ) ) ) {
if ( $canWrite && $inactive ) {
$this->unWarnWiki( $dbName, $dbw );
}
return true;
}
if ( !$closed ) {
// Wiki is NOT closed yet
$closeTime = $inactiveDays + $closeDays;
if ( isset( $lastEntryObj->rc_timestamp ) && $lastEntryObj->rc_timestamp < date( "YmdHis", strtotime( "-{$closeTime} days" ) ) ) {
// Last RC entry older than allowed time
if ( $canWrite ) {
$this->closeWiki( $dbName, $dbw );
$this->emailBureaucrats( $dbName, $dbw );
$this->output( "{$dbName} was eligible for closing and has been closed now.\n" );
} else {
$this->output( "{$dbName} should be closed. Timestamp of last recent changes entry: {$lastEntryObj->rc_timestamp}\n" );
}
} elseif ( isset( $lastEntryObj->rc_timestamp ) && $lastEntryObj->rc_timestamp < date( "YmdHis", strtotime( "-45 days" ) ) ) {
// Meets inactivity
if ( $canWrite ) {
$this->warnWiki( $dbName, $dbw );
$this->output( "{$dbName} was eligible for a warning notice and one was given.\n" );
} else {
$this->output( "{$dbName} should get a warning notice. Timestamp of last recent changes entry: {$lastEntryObj->rc_timestamp}\n" );
}
} else {
// No RC entries
if ( !$inactive ) {
// Wiki not marked inactive yet, warning should be given
if ( $canWrite ) {
$this->warnWiki( $dbName, $dbw );
$this->output( "{$dbName} does not seem to contain recentchanges entries, therefore warning.\n" );
} else {
$this->output( "{$dbName} does not seem to contain recentchanges entries, eligible for warning.\n" );
}
} elseif ( $inactiveDate && $inactiveDate < date( "YmdHis", strtotime( "-{$closeDays} days" ) ) ) {
// Wiki already warned, eligible for closure
if ( $canWrite ) {
$this->closeWiki( $dbName, $dbw );
$this->output( "{$dbName} does not seem to contain recentchanges entries after {$closeDays}+ days warning, therefore closing.\n" );
} else {
$this->output( "{$dbName} does not seem to contain recentchanges entries after {$closeDays}+ days warning, eligible for closure.\n" );
}
} else {
// Wiki warned recently
$this->output( "{$dbName} does not seem to contain recentchanges entries, warned recently.\n" );
}
}
} else {
// Wiki already has been closed
if ( $closedDate && $closedDate < date( "YmdHis", strtotime( "-{$removeDays} days" ) ) ) {
// Wiki closed, eligible for deletion
if ( $canWrite ) {
$this->removeWiki( $dbName, $dbw );
$this->output( "{$dbName} is eligible to be removed from public viewing and has been.\n" );
} else {
$this->output( "{$dbName} is eligible for public removal, was closed on {$closedDate}.\n" );
}
} elseif ( $closedDate && $closedDate > date( "YmdHis", strtotime( "-{$removeDays} days" ) ) ) {
// Wiki closed but not yet eligible for removal
$this->output( "{$dbName} is not eligible for public removal yet, but has already been closed on {$closedDate}.\n" );
} else {
// Could not determine closure date, fallback
$this->output( "{$dbName} has already been closed but its closure date could not be determined. Please check!\n" );
}
}
return true;
}
public function removeWiki( $wikiDb, $dbw ) {
$dbw->update(
'cw_wikis',
[
'wiki_closed' => 0,
'wiki_closed_timestamp' => null,
'wiki_deleted' => 1,
'wiki_deleted_timestamp' => $dbw->timestamp()
],
[
'wiki_dbname' => $wikiDb
]
);
}
public function closeWiki( $wikiDb, $dbw ) {
$dbw->update(
'cw_wikis',
[
'wiki_closed' => '1',
'wiki_closed_timestamp' => $dbw->timestamp(),
'wiki_inactive' => '0',
'wiki_inactive_timestamp' => null, // Consistency
],
[
'wiki_dbname' => $wikiDb
],
__METHOD__
);
Hooks::run( 'CreateWikiStateClosed', [ $wikiDb ] );
return true;
}
public function warnWiki( $wikiDb, $dbw ) {
$dbw->update(
'cw_wikis',
[
'wiki_inactive' => '1',
'wiki_inactive_timestamp' => $dbw->timestamp(),
],
[
'wiki_dbname' => $wikiDb
],
__METHOD__
);
Hooks::run( 'CreateWikiStateInactive', [ $wikiDb ] );
return true;
}
public function unWarnWiki( $wikiDb, $dbw ) {
$dbw->update(
'cw_wikis',
[
'wiki_closed' => '0',
'wiki_closed_timestamp' => null, // Consistency
'wiki_inactive' => '0',
'wiki_inactive_timestamp' => null, // Consistency
],
[
'wiki_dbname' => $wikiDb
],
__METHOD__
);
Hooks::run( 'CreateWikiStateActive', [ $wikiDb ] );
return true;
}
public function emailBureaucrats( $wikiDb, $dbw ) {
global $wgPasswordSender, $wgSitename, $wgCreateWikiDatabase;
$dbw->selectDomain( $wikiDb );
$bureaucrats = $dbw->select(
[ 'user', 'user_groups' ],
[ 'user_email', 'user_name' ],
[ 'ug_group' => 'bureaucrat' ],
__METHOD__,
[],
[
'user_groups' => [
'INNER JOIN',
[ 'user_id=ug_user' ]
]
]
);
$dbw->selectDomain( $wgCreateWikiDatabase );
foreach ( $bureaucrats as $users ) {
$emails[] = new MailAddress( $users->user_email, $users->user_name );
}
$from = new MailAddress( $wgPasswordSender, wfMessage( 'createwiki-close-email-sender' ) );
$subject = wfMessage( 'miraheze-close-email-subject', $wikiDb )->inContentLanguage()->text();
$body = wfMessage( 'miraheze-close-email-body' )->inContentLanguage()->text();
return UserMailer::send( $emails, $from, $subject, $body );
}
}
$maintClass = 'ManageInactiveWikis';
require_once RUN_MAINTENANCE_IF_MAIN;