1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Erase a page record from the database |
4
|
|
|
* Irreversible (can't use standard undelete) and does not update link tables |
5
|
|
|
* |
6
|
|
|
* This program is free software; you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along |
17
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
18
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
20
|
|
|
* |
21
|
|
|
* @file |
22
|
|
|
* @ingroup Maintenance |
23
|
|
|
* @author Rob Church <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
require_once __DIR__ . '/Maintenance.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Maintenance script that erases a page record from the database. |
30
|
|
|
* |
31
|
|
|
* @ingroup Maintenance |
32
|
|
|
*/ |
33
|
|
|
class NukePage extends Maintenance { |
34
|
|
|
public function __construct() { |
35
|
|
|
parent::__construct(); |
36
|
|
|
$this->addDescription( 'Remove a page record from the database' ); |
37
|
|
|
$this->addOption( 'delete', "Actually delete the page" ); |
38
|
|
|
$this->addArg( 'title', 'Title to delete' ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function execute() { |
42
|
|
|
|
43
|
|
|
$name = $this->getArg(); |
44
|
|
|
$delete = $this->getOption( 'delete', false ); |
45
|
|
|
|
46
|
|
|
$dbw = $this->getDB( DB_MASTER ); |
47
|
|
|
$this->beginTransaction( $dbw, __METHOD__ ); |
48
|
|
|
|
49
|
|
|
$tbl_pag = $dbw->tableName( 'page' ); |
50
|
|
|
$tbl_rec = $dbw->tableName( 'recentchanges' ); |
51
|
|
|
$tbl_rev = $dbw->tableName( 'revision' ); |
52
|
|
|
|
53
|
|
|
# Get page ID |
54
|
|
|
$this->output( "Searching for \"$name\"..." ); |
55
|
|
|
$title = Title::newFromText( $name ); |
56
|
|
|
if ( $title ) { |
57
|
|
|
$id = $title->getArticleID(); |
58
|
|
|
$real = $title->getPrefixedText(); |
59
|
|
|
$isGoodArticle = $title->isContentPage(); |
60
|
|
|
$this->output( "found \"$real\" with ID $id.\n" ); |
61
|
|
|
|
62
|
|
|
# Get corresponding revisions |
63
|
|
|
$this->output( "Searching for revisions..." ); |
64
|
|
|
$res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" ); |
65
|
|
|
$revs = []; |
66
|
|
|
foreach ( $res as $row ) { |
67
|
|
|
$revs[] = $row->rev_id; |
68
|
|
|
} |
69
|
|
|
$count = count( $revs ); |
70
|
|
|
$this->output( "found $count.\n" ); |
71
|
|
|
|
72
|
|
|
# Delete the page record and associated recent changes entries |
73
|
|
|
if ( $delete ) { |
74
|
|
|
$this->output( "Deleting page record..." ); |
75
|
|
|
$dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" ); |
76
|
|
|
$this->output( "done.\n" ); |
77
|
|
|
$this->output( "Cleaning up recent changes..." ); |
78
|
|
|
$dbw->query( "DELETE FROM $tbl_rec WHERE rc_cur_id = $id" ); |
79
|
|
|
$this->output( "done.\n" ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); |
83
|
|
|
|
84
|
|
|
# Delete revisions as appropriate |
85
|
|
|
if ( $delete && $count ) { |
86
|
|
|
$this->output( "Deleting revisions..." ); |
87
|
|
|
$this->deleteRevisions( $revs ); |
88
|
|
|
$this->output( "done.\n" ); |
89
|
|
|
$this->purgeRedundantText( true ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
# Update stats as appropriate |
93
|
|
|
if ( $delete ) { |
94
|
|
|
$this->output( "Updating site stats..." ); |
95
|
|
|
$ga = $isGoodArticle ? -1 : 0; // if it was good, decrement that too |
96
|
|
|
$stats = new SiteStatsUpdate( 0, -$count, $ga, -1 ); |
97
|
|
|
$stats->doUpdate(); |
98
|
|
|
$this->output( "done.\n" ); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
$this->output( "not found in database.\n" ); |
102
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function deleteRevisions( $ids ) { |
107
|
|
|
$dbw = $this->getDB( DB_MASTER ); |
108
|
|
|
$this->beginTransaction( $dbw, __METHOD__ ); |
109
|
|
|
|
110
|
|
|
$tbl_rev = $dbw->tableName( 'revision' ); |
111
|
|
|
|
112
|
|
|
$set = implode( ', ', $ids ); |
113
|
|
|
$dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" ); |
114
|
|
|
|
115
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$maintClass = "NukePage"; |
120
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN; |
121
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.