forked from bovender/LinkTitles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialLinkTitles.php
291 lines (262 loc) · 8.94 KB
/
SpecialLinkTitles.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
<?php
/*
* Copyright 2012-2014 Daniel Kraus <krada@gmx.net> ('bovender')
*
* 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.
*/
/// @defgroup batch Batch processing
/// @cond
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'Not an entry point.' );
}
/// @endcond
/// Provides a special page that can be used to batch-process all pages in
/// the wiki. By default, this can only be performed by sysops.
/// @ingroup batch
class SpecialLinkTitles extends SpecialPage {
/// Constructor. Announces the special page title and required user right
/// to the parent constructor.
function __construct() {
// the second parameter in the following function call ensures that only
// users who have the 'linktitles-batch' right get to see this page (by
// default, this are all sysop users).
parent::__construct('LinkTitles', 'linktitles-batch');
}
/// Entry function of the special page class. Will abort if the user does
/// not have appropriate permissions ('linktitles-batch').
/// @returns undefined
function execute($par) {
// Prevent non-authorized users from executing the batch processing.
if ( !$this->userCanExecute( $this->getUser() ) ) {
$this->displayRestrictionError();
return;
}
$request = $this->getRequest();
$output = $this->getOutput();
$this->setHeaders();
// Determine whether this page was requested via GET or POST.
// If GET, display information and a button to start linking.
// If POST, start or continue the linking process.
if ( $request->wasPosted() ) {
if ( array_key_exists('s', $request->getValues()) ) {
$this->process($request, $output);
}
else
{
$this->buildInfoPage($request, $output);
}
}
else
{
$this->buildInfoPage($request, $output);
}
}
/// Processes wiki articles, starting at the page indicated by
/// $startTitle. If $wgLinkTitlesTimeLimit is reached before all pages are
/// processed, returns the title of the next page that needs processing.
/// @param WebRequest $request WebRequest object that is associated with the special
/// page.
/// @param OutputPage $output Output page for the special page.
private function process( WebRequest &$request, OutputPage &$output) {
global $wgLinkTitlesTimeLimit;
// Start the stopwatch
$startTime = microtime(true);
// Connect to the database
$dbr = wfGetDB( DB_SLAVE );
// Fetch the start index and max number of records from the POST
// request.
$postValues = $request->getValues();
// Convert the start index to an integer; this helps preventing
// SQL injection attacks via forged POST requests.
$start = intval($postValues['s']);
// If an end index was given, we don't need to query the database
if ( array_key_exists('e', $postValues) ) {
$end = intval($postValues['e']);
}
else
{
// No end index was given. Therefore, count pages now.
$end = $this->countPages($dbr);
};
array_key_exists('r', $postValues) ?
$reloads = $postValues['r'] :
$reloads = 0;
// Retrieve page names from the database.
$res = $dbr->select(
'page',
'page_title',
array(
'page_namespace = 0',
),
__METHOD__,
array(
'LIMIT' => 999999999,
'OFFSET' => $start
)
);
// Iterate through the pages; break if a time limit is exceeded.
foreach ( $res as $row ) {
$curTitle = $row->page_title;
LinkTitles::processPage($curTitle, $this->getContext());
$start += 1;
// Check if the time limit is exceeded
if ( microtime(true)-$startTime > $wgLinkTitlesTimeLimit )
{
break;
}
}
$this->addProgressInfo($output, $curTitle, $start, $end);
// If we have not reached the last page yet, produce code to reload
// the extension's special page.
if ( $start < $end )
{
$reloads += 1;
// Build a form with hidden values and output JavaScript code that
// immediately submits the form in order to continue the process.
$output->addHTML($this->getReloaderForm($request->getRequestURL(),
$start, $end, $reloads));
}
else // Last page has been processed
{
$this->addCompletedInfo($output, $start, $end, $reloads);
}
}
/// Adds WikiText to the output containing information about the extension
/// and a form and button to start linking.
private function buildInfoPage(&$request, &$output) {
$url = $request->getRequestURL();
// TODO: Put the page contents in messages in the i18n file.
$output->addWikiText(
<<<EOF
LinkTitles extension: http://www.mediawiki.org/wiki/Extension:LinkTitles
Source code: http://github.com/bovender/LinkTitles
== Batch Linking ==
You can start a batch linking process by clicking on the button below.
This will go through every page in the normal namespace of your Wiki and
insert links automatically. This page will repeatedly reload itself, in
order to prevent blocking the server. To interrupt the process, simply
close this page.
EOF
);
$output->addHTML(
<<<EOF
<form method="post" action="${url}">
<input type="submit" value="Start linking" />
<input type="hidden" name="s" value="0" />
</form>
EOF
);
}
/// Produces informative output in WikiText format to show while working.
/// @param $output Output object.
/// @param $curTitle Title of the currently processed page.
/// @param $index Index of the currently processed page.
/// @param $end Last index that will be processed (i.e., number of
/// pages).
private function addProgressInfo(&$output, $curTitle, $index, $end) {
$progress = $index / $end * 100;
$percent = sprintf("%01.1f", $progress);
$output->addWikiText(
<<<EOF
== Processing pages... ==
The [http://www.mediawiki.org/wiki/Extension:LinkTitles LinkTitles]
extension is currently going through every page of your wiki, adding links to
existing pages as appropriate.
=== Current page: $curTitle ===
EOF
);
$output->addHTML(
<<<EOF
<p>Page ${index} of ${end}.</p>
<div style="width:100%; padding:2px; border:1px solid #000; position: relative;
margin-bottom:16px;">
<span style="position: absolute; left: 50%; font-weight:bold; color:#555;">
${percent}%
</span>
<div style="width:${progress}%; background-color:#bbb; height:20px; margin:0;"></div>
</div>
EOF
);
$output->addWikiText(
<<<EOF
=== To abort, close this page, or hit the 'Stop' button in your browser ===
[[Special:LinkTitles|Return to Special:LinkTitles.]]
EOF
);
}
/// Generates an HTML form and JavaScript to automatically submit the
/// form.
/// @param $url URL to reload with a POST request.
/// @param $start Index of the next page that shall be processed.
/// @param $end Index of the last page to be processed.
/// @param $reloads Counter that holds the number of reloads so far.
/// @returns String that holds the HTML for a form and a
/// JavaScript command.
private function getReloaderForm($url, $start, $end, $reloads) {
return
<<<EOF
<form method="post" name="linktitles" action="${url}">
<input type="hidden" name="s" value="${start}" />
<input type="hidden" name="e" value="${end}" />
<input type="hidden" name="r" value="${reloads}" />
</form>
<script type="text/javascript">
document.linktitles.submit();
</script>
EOF
;
}
/// Adds statistics to the page when all processing is done.
/// @param $output Output object
/// @param $start Index of the first page that was processed.
/// @param $end Index of the last processed page.
/// @param $reloads Number of reloads of the page.
/// @returns undefined
private function addCompletedInfo(&$output, $start, $end, $reloads) {
global $wgLinkTitlesTimeLimit;
$pagesPerReload = sprintf('%0.1f', $end / $reloads);
$output->addWikiText(
<<<EOF
== Batch processing completed! ==
{| class="wikitable"
|-
| total number of pages: || ${end}
|-
| timeout setting [s]: || ${wgLinkTitlesTimeLimit}
|-
| webpage reloads: || ${reloads}
|-
| pages scanned per reload interval: || ${pagesPerReload}
|}
EOF
);
}
/// Counts the number of pages in a read-access wiki database ($dbr).
/// @param $dbr Read-only `Database` object.
/// @returns Number of pages in the default namespace (0) of the wiki.
private function countPages(&$dbr) {
$res = $dbr->select(
'page',
'page_id',
array(
'page_namespace = 0',
),
__METHOD__
);
return $res->numRows();
}
}
// vim: ts=2:sw=2:noet:comments^=\:///