This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
/
DeleteHistory_body.php
219 lines (195 loc) · 7.62 KB
/
DeleteHistory_body.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
<?php
class DeleteHistory extends SpecialPage
{
function __construct()
{
global $wgVersion;
// Need to belong to Administor group
parent::__construct( 'DeleteHistory', 'editinterface' );
if ($wgVersion <= 1.16) {
wfLoadExtensionMessages('DeleteHistory');
}
}
function execute( $par )
{
// Get database size function
function get_db_size()
{
global $wgRequest, $wgDBname;
$dbw =& wfGetDB( DB_MASTER );
$mw_dbname = $dbw->buildLike($wgDBname);
$dbsize = $dbw->query( "select table_schema " . $mw_dbname . ", sum( data_length + index_length ) / 1024 / 1024 'DB sze in MB' from information_schema.TABLES where TABLE_SCHEMA " . $mw_dbname . " GROUP BY table_schema ;");
while ($row = $dbw->fetchRow($dbsize))
{
$size = $row[1];
}
return $size;
}
// Get tables engine and collation
function get_engine_col($table_name)
{
global $wgRequest, $wgDBname;
$dbw =& wfGetDB( DB_MASTER );
$engine_collation = $dbw->query( "select ENGINE,TABLE_COLLATION from information_schema.TABLES where TABLE_SCHEMA = '" . $wgDBname . "' and TABLE_NAME = '" . $table_name . "';");
while ($row = $dbw->fetchRow($engine_collation))
{
$engine = $row[0];
$collation = $row[1];
}
return array('engine' => $engine, 'collation' => $collation);
}
// Load required globals
global $wgRequest, $wgOut, $wgUser, $wgDBname, $wgVersion;
$this->setHeaders();
// Check if user is Admin
if ( !$this->userCanExecute($wgUser) )
{
$this->displayRestrictionError();
return;
}
$dbw =& wfGetDB( DB_MASTER );
// Get request data from, e.g.
$param = $wgRequest->getText('param');
// Localisation messages
$check_only = wfMessage('check_only')->text();
$del_hist = wfMessage('del_hist')->text();
$del_hist_opt = wfMessage('del_hist_opt')->text();
// Escaping URI
$current_uri = htmlentities($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8');
// User choice
$wgOut->addWikiText(wfMessage('what_to_do')->text() . ' :');
$wgOut->addHTML("<form action=\"$current_uri\" method=\"post\">
<input type=\"radio\" name=\"choice\" value=\"0\" checked>$check_only<br />
<input type=\"radio\" name=\"choice\" value=\"1\">$del_hist<br />
<input type=\"radio\" name=\"choice\" value=\"2\">$del_hist_opt (MySQL)<br /><br />
<input type=\"submit\" value=\"" . wfMessage('validate')->text() . "\">
</form>
");
// Choosen action choice
$show_db_size=0;
$pwd = getcwd();
$command = 'php ' . $pwd . '/maintenance/deleteOldRevisions.php --delete';
if ((isset($_POST['choice'])) and ($_POST['choice'] == "0"))
{
$command = 'php ' . $pwd . '/maintenance/deleteOldRevisions.php';
$out_logs = shell_exec(escapeshellcmd($command));
}
elseif ((isset($_POST['choice'])) and ($_POST['choice'] == "1"))
{
$out_logs = shell_exec(escapeshellcmd($command));
}
elseif ((isset($_POST['choice'])) and ($_POST['choice'] == "2"))
{
$show_db_size=1;
// Change deprecated function
if ($wgVersion >= 1.18)
{
$dbw->begin();
}
else
{
$dbw->immediateBegin();
}
// Get actual database size
$db_size_old = get_db_size( );
// Delete old history
$out_logs = shell_exec(escapeshellcmd($command));
// Get all tables
$alltables = $dbw->query( "SHOW TABLES" );
// Show optimize status
$out_opt="<table border=1 style='border-style: double;'>
<tr>
<th>Tables Names</th>
<th>Engine</th>
<th>Collation</th>
<th>Status</th>
</tr>
";
while ($row = $dbw->fetchRow($alltables))
{
// Run and show output
$out_opt .= "<tr>\n<td>";
// Database name
$out_opt .= $row[0] . " </td>\n<td>";
// Get engine and collation
extract(get_engine_col($row[0]));
$out_opt .= $engine . " </td>\n<td>";
$out_opt .= $collation . " </td>\n<td>";
// Optimize
$opt_res = $dbw->query("OPTIMIZE TABLE {$row[0]};");
while ($res = $dbw->fetchRow( $opt_res ))
{
// Set green color on OK and if table is already up to date
if (preg_match ("/OK/i", $res[3]))
{
$out_opt .= '<span style="font-weight: bold; color: green"> ' . $res[3] . ' </span>';
}
elseif (preg_match ("/Table is already up to date/i", $res[3]))
{
$out_opt .= '<span style="color: green">' . $res[3] . ' </span>';
}
else
{
$out_opt .= $res[3] . ' ';
}
}
}
$out_opt.="</table>";
// Get size after
$db_size_new = get_db_size( );
}
// Fix display pre problem
if ((isset($_POST['choice'])) and ($_POST['choice'] != ""))
{
$wgOut->addHTML("<br />");
$wgOut->addWikiText('=' . wfMessage('result')->text() . '=');
// Show DB size if ask to optimize
if ($show_db_size == 1)
{
function kb_or_mb($db_size_old,$db_size_new)
{
$spacewon=($db_size_old - $db_size_new);
if ($spacewon < 1)
{
$spacewon = ($spacewon * 1024) . " KB";
}
else
{
$spacewon = $spacewon . " MB";
}
return $spacewon;
}
// Show result
$wgOut->addHTML("<table border=1'>
<tr>
<td></td>
<th>" . wfMessage('size')->text() . "</th>
</tr><tr>
<th>" . wfMessage('db_size_old')->text() . "</th>
<td>$db_size_old MB</td>
</tr><tr>
<th>" . wfMessage('db_size_new')->text() . "</th>
<td>$db_size_new MB</td>
</tr><tr>
<th>" . wfMessage('db_space_won')->text() . "</th>
<td align='center'><b>" . kb_or_mb($db_size_old,$db_size_new) . "</b></td>
</tr></table><br />
");
}
if ($out_opt)
{
$wgOut->addWikiText('=' . wfMessage('opt_stat')->text() . '=');
$wgOut->addHTML($out_opt . '<br />');
}
if ($out_logs)
{
if ($_POST['choice'] == "2")
{
$wgOut->addWikiText('=' . wfMessage('logs')->text() . '=');
}
$wgOut->addHTML("<pre>".$out_logs."</pre>");
}
}
return true;
}
}