-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionDeletePagePermanently.php
417 lines (354 loc) · 11.3 KB
/
ActionDeletePagePermanently.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Storage\BlobStore;
use Wikimedia\Rdbms\IDatabase;
class ActionDeletePagePermanently extends FormAction {
/**
* @param SkinTemplate $sktemplate
* @param array &$links
*/
public static function onAddSkinHook( SkinTemplate $sktemplate, array &$links ) {
if ( $sktemplate->getUser()->isAllowed( 'deleteperm' ) ) {
$title = $sktemplate->getRelevantTitle();
$action = self::getActionName( $sktemplate );
if ( self::canDeleteTitle( $title ) ) {
$links['actions']['delete_page_permanently'] = [
'class' => ( $action === 'delete_page_permanently' ) ? 'selected' : false,
'text' => $sktemplate->msg( 'deletepagesforgood-delete_permanently' )->text(),
'href' => $title->getLocalUrl( 'action=delete_page_permanently' )
];
}
}
}
/** @inheritDoc */
public function getName() {
return 'delete_page_permanently';
}
/** @inheritDoc */
public function doesWrites() {
return true;
}
/** @inheritDoc */
public function getDescription() {
return '';
}
/** @inheritDoc */
protected function usesOOUI() {
return true;
}
/**
* @param Title $title
* @return bool
*/
public static function canDeleteTitle( Title $title ) {
global $wgDeletePagesForGoodNamespaces;
if ( $title->exists() && $title->getArticleID() !== 0 &&
$title->getDBkey() !== '' &&
$title->getNamespace() !== NS_SPECIAL &&
isset( $wgDeletePagesForGoodNamespaces[ $title->getNamespace() ] ) &&
$wgDeletePagesForGoodNamespaces[ $title->getNamespace() ]
) {
return true;
} else {
return false;
}
}
/**
* @param mixed $data
* @return bool|string[]
*/
public function onSubmit( $data ) {
if ( self::canDeleteTitle( $this->getTitle() ) ) {
$this->deletePermanently( $this->getTitle() );
return true;
} else {
# $output->addHTML( $this->msg( 'deletepagesforgood-del_impossible' )->escaped() );
return [ 'deletepagesforgood-del_impossible' ];
}
}
/**
* @param Title $title
* @return bool|string
*/
public function deletePermanently( Title $title ) {
$ns = $title->getNamespace();
$t = $title->getDBkey();
$id = $title->getArticleID();
$cats = $title->getParentCategories();
$dbw = wfGetDB( DB_MASTER );
$dbw->startAtomic( __METHOD__ );
/*
* First delete entries, which are in direct relation with the page:
*/
# Delete redirect...
$dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
# Delete external links...
$dbw->delete( 'externallinks', [ 'el_from' => $id ], __METHOD__ );
# Delete language links...
$dbw->delete( 'langlinks', [ 'll_from' => $id ], __METHOD__ );
if ( $GLOBALS['wgDBtype'] !== "postgres" && $GLOBALS['wgDBtype'] !== "sqlite" ) {
# Delete search index...
$dbw->delete( 'searchindex', [ 'si_page' => $id ], __METHOD__ );
}
# Delete restrictions for the page
$dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
# Delete page links
$dbw->delete( 'pagelinks', [ 'pl_from' => $id ], __METHOD__ );
# Delete category links
$dbw->delete( 'categorylinks', [ 'cl_from' => $id ], __METHOD__ );
# Delete template links
$dbw->delete( 'templatelinks', [ 'tl_from' => $id ], __METHOD__ );
// $wgMultiContentRevisionSchemaMigrationStage existed between 1.32 (included) and 1.39 (excluded)
$mcrSchemaMigrationStage = isset( $GLOBALS['wgMultiContentRevisionSchemaMigrationStage'] )
? $GLOBALS['wgMultiContentRevisionSchemaMigrationStage']
: 0;
// Before 1.32, revision.rev_text_id existed, but $mcrSchemaMigrationStage === 0
if ( ( $mcrSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) ||
$dbw->fieldExists( 'revision', 'rev_text_id', __METHOD__ )
) {
# Read text entries for all revisions and delete them.
$res = $dbw->select( 'revision', 'rev_text_id', "rev_page=$id" );
foreach ( $res as $row ) {
$value = $row->rev_text_id;
$dbw->delete( 'text', [ 'old_id' => $value ], __METHOD__ );
}
# Read text entries for all archived pages and delete them.
$arRes = $dbw->select( 'archive', 'ar_text_id', [
'ar_namespace' => $ns,
'ar_title' => $t
] );
foreach ( $arRes as $arRow ) {
$value = $arRow->ar_text_id;
$dbw->delete( 'text', [ 'old_id' => $value ], __METHOD__ );
}
}
// From 1.35, revision.rev_text_id does not exist, and from 1.39 $mcrSchemaMigrationStage === 0
if ( ( $mcrSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) ||
!$dbw->fieldExists( 'revision', 'rev_text_id', __METHOD__ )
) {
# Delete slot, content, and text entries for all revisions.
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
$blobStore = MediaWikiServices::getInstance()->getBlobStore();
$revQuery = $revisionStore->getQueryInfo();
$res = $dbw->select(
$revQuery['tables'],
$revQuery['fields'],
"rev_page=$id",
__METHOD__,
[],
$revQuery['joins']
);
foreach ( $res as $row ) {
$rev = $revisionStore->newRevisionFromRow( $row );
$this->deleteSlotsPermanently( $dbw,
$rev->getSlots()->getSlots(),
$rev->getId(),
$blobStore
);
}
$arRevQuery = $revisionStore->getArchiveQueryInfo();
$arRes = $dbw->select(
$arRevQuery['tables'],
$arRevQuery['fields'],
[
'ar_namespace' => $ns,
'ar_title' => $t
],
__METHOD__,
[],
$arRevQuery['joins']
);
foreach ( $arRes as $arRow ) {
$rev = $revisionStore->newRevisionFromArchiveRow( $arRow );
$this->deleteSlotsPermanently( $dbw,
$rev->getSlots()->getSlots(),
$rev->getId(),
$blobStore
);
}
}
# In the table 'revision' : Delete all the revision of the page where 'rev_page' = $id
$dbw->delete( 'revision', [ 'rev_page' => $id ], __METHOD__ );
# Delete image links
$dbw->delete( 'imagelinks', [ 'il_from' => $id ], __METHOD__ );
/*
* then delete entries which are not in direct relation with the page:
*/
# Clean up recentchanges entries...
$dbw->delete( 'recentchanges', [
'rc_namespace' => $ns,
'rc_title' => $t
], __METHOD__ );
# Clean up archive entries...
$dbw->delete( 'archive', [
'ar_namespace' => $ns,
'ar_title' => $t
], __METHOD__ );
# Clean up log entries...
$dbw->delete( 'logging', [
'log_namespace' => $ns,
'log_title' => $t
], __METHOD__ );
# Clean up watchlist...
$dbw->delete( 'watchlist', [
'wl_namespace' => $ns,
'wl_title' => $t
], __METHOD__ );
$dbw->delete( 'watchlist', [
'wl_namespace' => MediaWikiServices::getInstance()
->getNamespaceInfo()
->getAssociated( $ns ),
'wl_title' => $t
], __METHOD__ );
# In the table 'page' : Delete the page entry
$dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
/*
* If the article belongs to a category, update category counts
*/
if ( !empty( $cats ) ) {
foreach ( $cats as $parentcat => $currentarticle ) {
$catname = preg_split( '/:/', $parentcat, 2 );
$cat = Category::newFromName( $catname[1] );
if ( !is_object( $cat ) ) {
// Blank error to allow us to continue
} else {
$cat->refreshCounts();
}
}
}
/*
* If an image is being deleted, some extra work needs to be done
*/
if ( $ns == NS_FILE ) {
if ( method_exists( MediaWikiServices::class, 'getRepoGroup' ) ) {
// MediaWiki 1.34+
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $t );
} else {
$file = wfFindFile( $t );
}
if ( $file ) {
# Get all filenames of old versions:
$fields = OldLocalFile::selectFields();
$res = $dbw->select( 'oldimage', $fields, [ 'oi_name' => $t ] );
foreach ( $res as $row ) {
$oldLocalFile = OldLocalFile::newFromRow( $row, $file->repo );
$path = $oldLocalFile->getArchivePath() . '/' . $oldLocalFile->getArchiveName();
try {
unlink( $path );
} catch ( Exception $e ) {
return $e->getMessage();
}
}
$path = $file->getLocalRefPath();
try {
$file->purgeThumbnails();
unlink( $path );
} catch ( Exception $e ) {
return $e->getMessage();
}
}
# Clean the filearchive for the given filename:
$dbw->delete( 'filearchive', [ 'fa_name' => $t ], __METHOD__ );
# Delete old db entries of the image:
$dbw->delete( 'oldimage', [ 'oi_name' => $t ], __METHOD__ );
# Delete archive entries of the image:
$dbw->delete( 'filearchive', [ 'fa_name' => $t ], __METHOD__ );
# Delete image entry:
$dbw->delete( 'image', [ 'img_name' => $t ], __METHOD__ );
// $dbw->endAtomic( __METHOD__ );
$linkCache = MediaWikiServices::getInstance()->getLinkCache();
$linkCache->clear();
}
$dbw->endAtomic( __METHOD__ );
return true;
}
/**
* In MCR schema, delete the slots corresponding to some revision.
*
* @param IDatabase $dbw Database handle
* @param SlotRecord[] $slots Slots
* @param int $revId Revision ID
* @param BlobStore $blobStore MediaWiki service BlobStore
* @return bool true if the content can be deleted, false otherwise
*/
private function deleteSlotsPermanently( $dbw, $slots, $revId, $blobStore ) {
foreach ( $slots as $role => $slot ) {
if ( $this->shouldDeleteContent( $dbw, $revId, $slot->getContentId() ) ) {
$textId = $blobStore->getTextIdFromAddress( $slot->getAddress() );
if ( $textId ) {
$dbw->delete( 'text', [ 'old_id' => $textId ], __METHOD__ );
}
$dbw->delete( 'content',
[ 'content_id' => $slot->getContentId() ],
__METHOD__
);
}
}
// This may orphan content types other than text
$dbw->delete( 'slots',
[ 'slot_revision_id' => $revId ],
__METHOD__
);
}
/**
* Determines if a particular piece of content should be deleted. Deleting requires querying
* if the content is used in any other revisions. This can be slow, and the caller will have
* a transaction open on the master database. Setting $wgDeletePagesForGoodDeleteContent to
* false is faster, because it skips the query and leaves the content alone. But it leaves
* orphaned content in storage.
*
* @param IDatabase $dbw Database handle
* @param int $revId Revision ID
* @param int $contentId Content ID to consider
* @return bool true if the content can be deleted, false otherwise
*/
private function shouldDeleteContent( $dbw, $revId, $contentId ) {
global $wgDeletePagesForGoodDeleteContent;
if ( !$wgDeletePagesForGoodDeleteContent ) {
return false;
}
$count = $dbw->selectRowCount(
'slots',
'*',
[
'slot_content_id' => $contentId,
"slot_revision_id != $revId"
]
);
return $count == 0;
}
/**
* Returns the name that goes in the \<h1\> page title
*
* @return string
*/
protected function getPageTitle() {
return $this->msg( 'deletepagesforgood-deletepagetitle', $this->getTitle()->getPrefixedText() );
}
/**
* @param HTMLForm $form
*/
protected function alterForm( HTMLForm $form ) {
$title = $this->getTitle();
$output = $this->getOutput();
$output->addBacklinkSubtitle( $title );
$form->addPreText( $this->msg( 'confirmdeletetext' )->parseAsBlock() );
$form->addPreText(
$this->msg( 'deletepagesforgood-ask_deletion' )->parseAsBlock()
);
$form->setSubmitTextMsg( 'deletepagesforgood-yes' );
}
/** @inheritDoc */
public function getRestriction() {
return 'deleteperm';
}
/**
* @return bool
*/
public function onSuccess() {
$output = $this->getOutput();
$output->addHTML( $this->msg( 'deletepagesforgood-del_done' )->escaped() );
return false;
}
}