-
Notifications
You must be signed in to change notification settings - Fork 3
/
EditWarning.php
422 lines (366 loc) · 13.9 KB
/
EditWarning.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
418
419
420
421
422
<?php
/**
* Implementation of EditWarning and EditWarning_Lock class.
*
* This file is part of the MediaWiki extension EditWarning. It contains
* the implementation of EditWarning and EditWarning_Lock class with
* functions to add, edit, delete and check for article locks.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* @author Thomas David <[email protected]>
* @copyright 2007-2011 Thomas David <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2.0 or later
* @version 0.4-rc1
* @category Extensions
* @package EditWarning
*/
if ( !defined( 'MEDIAWIKI' ) && !defined( 'EDITWARNING_UNITTEST' ) ) {
echo <<<EOT
<p>To install this extension, put the following line in LocalSettings.php:\n
<pre>require_once "\$IP/extensions/EditWarning/EditWarning.php";</pre></p>\n\n
<p>See <a href="http://www.mediawiki.org/wiki/Extension:EditWarning/0.4">http://www.mediawiki.org/wiki/Extension:EditWarning/0.4</a> for more information.</p>
EOT;
exit(1);
}
$extension_dir = dirname(__FILE__) . "/";
$wgExtensionCredits['other'][] = array(
'name' => "EditWarning",
'author' => "Thomas David",
'url' => "http://www.mediawiki.org/wiki/Extension:EditWarning/0.4",
'version' => "0.4-rc",
'descriptionmsg' => "editwarning-desc"
);
$wgAutoloadClasses['EditWarning'] = $extension_dir . 'EditWarning.class.php';
$wgAutoloadClasses['EditWarningMsg'] = $extension_dir . 'EditWarningMsg.class.php';
$wgExtensionMessagesFiles['EditWarning'] = $extension_dir . 'Messages.i18n.php';
$wgExtensionFunctions[] = 'fnEditWarning_init';
$editwarning = null;
if ( !defined( 'EDITWARNING_UNITTEST' ) ) {
$editwarning = new EditWarning();
}
// Assign hooks to functions
$wgHooks['AlternateEdit'][] = array( 'fnEditWarning_edit', $editwarning ); // On article edit.
$wgHooks['ArticleSave'][] = array( 'fnEditWarning_remove', $editwarning ); // On article save.
$wgHooks['UserLogout'][] = array( 'fnEditWarning_logout', $editwarning ); // On user logout.
$wgHooks['ArticleViewHeader'][] = array( 'fnEditWarning_abort', $editwarning ); // On editing abort.
/**
* Gets the section id from GET or POST
*
* @return int Section id.
*/
function fnEditWarning_getSection() {
if ( defined( 'EDITWARNING_UNITTEST' ) ) {
return $GLOBALS['unitGetSection'];
}
if ( isset( $_GET['section'] ) && !isset( $_POST['wpSection'] ) ) {
return intval( $_GET['section'] );
} else {
return intval( $_POST['wpSection'] );
}
}
/**
* Setup EditWarning extension
*
* @return boolean Returns always true.
*/
function fnEditWarning_init() {
global $wgRequest, $wgOut, $wgScriptPath, $wgUser, $EditWarning_OnlyEditor;
// Add CSS styles to header
if ( ( $wgRequest->getVal('action') == 'edit' || $wgRequest->getVal('action') == 'submit' )
&& $EditWarning_OnlyEditor != "false"
&& $wgUser->getID() >= 1 ) {
$wgOut->addHeadItem('edit_css', ' <link href="' . $wgScriptPath . '/extensions/EditWarning/article_edit.css" rel="stylesheet" type="text/css" />');
}
$wgOut->addHeadItem('EditWarning', ' <link href="' . $wgScriptPath . '/extensions/EditWarning/style.css" rel="stylesheet" type="text/css" />');
return true;
}
/*
* Functions and definitions for fnEditWarning_edit
*/
// Used for messages to indicate edit type.
define('TYPE_ARTICLE', 1);
define('TYPE_ARTICLE_SECTIONCONFLICT', 2);
define('TYPE_SECTION', 3);
/**
* Function to show info message about created or updated locks for sections
* or articles.
*
* @param int $msgtype Type of edit (article or section).
*/
function showInfoMsg($msgtype, $timestamp, $cancel_url) {
$type = ($msgtype == TYPE_ARTICLE) ? "ArticleNotice" : "SectionNotice";
// Show info message with updated timestamp.
$msg_params[] = date("Y-m-d", $timestamp);
$msg_params[] = date("H:i", $timestamp);
$msg = EditWarningMsg::getInstance($type, $cancel_url, $msg_params);
$msg->show();
unset($msg);
}
/**
* Function to show warning message about existing locks for sections or
* articles.
*
* @param <type> $lockobj EditWarningLock object.
*/
function showWarningMsg($msgtype, $lockobj, $cancel_url) {
switch ($msgtype) {
case TYPE_ARTICLE:
$type = "ArticleWarning";
break;
case TYPE_ARTICLE_SECTIONCONFLICT:
$type = "ArticleSectionWarning";
break;
case TYPE_SECTION:
$type = "SectionWarning";
break;
}
// Calculate time to wait
$difference = floatval(abs(time() - $lockobj->getTimestamp()));
$time_to_wait = bcdiv($difference, 60, 0);
// Parameters for message string
if ($msgtype == TYPE_ARTICLE || $msgtype == TYPE_SECTION) {
$msg_params[] = $lockobj->getUserName();
$msg_params[] = date("Y-m-d", $lockobj->getTimestamp());
$msg_params[] = date("H:i", $lockobj->getTimestamp());
}
$msg_params[] = $time_to_wait;
// Use minutes or seconds string?
if ($time_to_wait > 1 || $difference > 60) {
$msg_params[] = wfMessage( 'ew-minutes' )->text();
} else {
$msg_params[] = wfMessage( 'ew-seconds' )->text();
}
$msg = EditWarningMsg::getInstance($type, $cancel_url, $msg_params);
$msg->show();
unset($msg);
}
/**
* Action on article editing
*
* @hook AlternateEdit
* @param object $editpage Editpage object.
* @param object $ew EditWarning object
* @return boolean|int It returns a constant int if it runs in unit test
* environment, else true.
*/
function fnEditWarning_edit($ew, $editpage) {
global $wgUser, $wgScriptPath, $wgScriptExtension, $PHP_SELF;
$dbr = null;
$dbw = null;
// Abort on nonexisting pages
if ( $editpage->mArticle->getID() < 1 ) {
return true;
}
if (! defined( 'EDITWARNING_UNITTEST' ) ) {
$dbr =& wfGetDB( DB_SLAVE );
$dbw =& wfGetDB( DB_MASTER );
}
$ew->setUserID( $wgUser->getID() );
$ew->setUserName($wgUser->getName());
$ew->setArticleID( $editpage->mArticle->getID() );
$section = fnEditWarning_getSection();
$msg_params = array();
// Get article title for cancel button
if ($editpage->mArticle->mTitle->getNamespace() == 'NS_MAIN') {
$article_title = $editpage->mArticle->mTitle->getPartialURL();
} else {
$article_title = $editpage->mArticle->mTitle->getNsText() . ":" . $editpage->mArticle->mTitle->getPartialURL();
}
$url = $PHP_SELF . "?title=" . $article_title . "&cancel=true";
// Check request values
if ( $section > 0 ) {
// Section editing
$ew->setSection( $section );
$ew->load( $dbr );
// Is the whole article locked?
if ($ew->isArticleLocked()) {
// Is it by the user?
if ($ew->isArticleLockedByUser()) {
// The user has already a lock on the whole article, but
// edits now a single section. Change article lock to
// section lock.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_SECTION_NEW;
}
$ew->removeLock($dbw);
$ew->saveLock($dbw, $section);
showInfoMsg(TYPE_SECTION, $ew->getTimestamp(TIMESTAMP_NEW), $url);
unset($ew);
return true;
} else {
// Someone else has a lock on the whole article. Show warning.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_ARTICLE_OTHER;
}
showWarningMsg(TYPE_ARTICLE, $ew->getArticleLock(), $url);
unset($ew);
return true;
}
} elseif ($ew->isSectionLocked($section)) {
$sectionLock = $ew->getSectionLock($section);
// Is the section locked by the user?
if ($ew->isSectionLockedByUser($sectionLock)) {
// The section is locked by the user. Update lock.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_SECTION_USER;
}
$ew->updateLock($dbw, $section);
showInfoMsg(TYPE_SECTION, $ew->getTimestamp(TIMESTAMP_NEW), $url);
unset($ew);
return true;
} else {
// The section is locked by someone else. Show warning.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_SECTION_OTHER;
}
showWarningMsg(TYPE_SECTION, $sectionLock, $url);
unset($ew);
return true;
}
} else {
// No locks. Create section lock for user.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_SECTION_NEW;
}
// Don't save locks for anonymous users.
if ($ew->getUserID() < 1) {
return true;
}
$ew->saveLock($dbw, $section);
showInfoMsg(TYPE_SECTION, $ew->getTimestamp(TIMESTAMP_NEW), $url);
unset($ew);
return true;
}
} else {
// Article editing
$ew->load($dbr);
// Is the article locked?
if ($ew->isArticleLocked()) {
if ($ew->isArticleLockedByUser()) {
// The article is locked by the user. Update lock.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_ARTICLE_USER;
}
$ew->updateLock($dbw);
showInfoMsg(TYPE_ARTICLE, $ew->getTimestamp(TIMESTAMP_NEW), $url);
unset($ew);
return true;
} else {
// The article is locked by someone else. Show warning.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_ARTICLE_OTHER;
}
$article_lock = $ew->getArticleLock();
showWarningMsg(TYPE_ARTICLE, $article_lock, $url);
unset($ew);
return true;
}
} elseif ($ew->anySectionLocks()) {
// There is at least one section lock
if ($ew->anySectionLocksByOthers()) {
// At least one section lock by another user.
// So an article lock is not possible. Show warning.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_SECTION_OTHER;
}
$sectionLocks = $ew->getSectionLocksByOther();
// Get the newest lock of a section for the warning message.
$lock = $sectionLocks[$ew->getSectionLocksByOtherCount() - 1];
showWarningMsg(TYPE_ARTICLE_SECTIONCONFLICT, $lock, $url);
unset($ew);
return true;
} else {
// The user has exclusively one or more locks on sections
// of the article, but now wants to edit the whole article.
// Change sections locks to article lock.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_ARTICLE_NEW;
}
$ew->removeUserLocks($dbw);
$ew->saveLock($dbw);
showInfoMsg(TYPE_ARTICLE, $ew->getTimestamp(TIMESTAMP_NEW), $url);
unset($ew);
return true;
}
} else {
// No locks. Create new article lock.
if (defined('EDITWARNING_UNITTEST')) {
return EDIT_ARTICLE_NEW;
}
// Don't save locks for anonymous users.
if ($ew->getUserID() < 1) {
return true;
}
$ew->saveLock($dbw);
showInfoMsg(TYPE_ARTICLE, $ew->getTimestamp(TIMESTAMP_NEW), $url);
unset($ew);
return true;
}
}
}
/**
* Action if article is saved.
*
* @hook ArticleSave
* @param
* @return boolean Returns always true.
*/
function fnEditWarning_remove( $ew, $article, $user, $text, $summary, $minor, $watch, $sectionanchor, $flags ) {
global $wgUser;
// Abort on nonexisting pages or anonymous users.
if ( $article->getID() < 1 || $user->getID() < 1 ) {
return true;
}
$dbw =& wfGetDB(DB_MASTER);
$ew->setUserID($wgUser->getID());
$ew->setArticleID($article->getID());
$ew->removeLock($dbw);
return true;
}
/**
* Action if editing is aborted.
*
* @hook ArticleViewHeader
* @param
* @return boolean Returns always true.
*/
function fnEditWarning_abort( $ew, $article, $outputDone, $pcache ) {
global $wgRequest, $wgUser;
if( $wgRequest->getVal('cancel' ) == "true") {
$dbw =& wfGetDB(DB_MASTER);
$ew->setUserID($wgUser->getID());
$ew->setArticleID($article->getID());
$ew->removeLock($dbw);
$msg = EditWarningMsg::getInstance( "Cancel" );
$msg->show();
unset( $ew );
unset( $msg );
}
return true;
}
/**
* Action on user logout.
*
* @hook UserLogout
* @param user User object.
* @return boolean Returns always true.
*
*/
function fnEditWarning_logout($ew, $user) {
$dbw =& wfGetDB( DB_MASTER );
$ew->setUserID( $user->getID() );
$ew->removeUserLocks( $dbw );
return true;
}