Merge "Remove unused $entryPoint from Preferences::tryFormSubmit"
[lhc/web/wiklou.git] / includes / changes / RecentChange.php
1 <?php
2 /**
3 * Utility class for creating and accessing recent change entries.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Utility class for creating new RC entries
25 *
26 * mAttribs:
27 * rc_id id of the row in the recentchanges table
28 * rc_timestamp time the entry was made
29 * rc_namespace namespace #
30 * rc_title non-prefixed db key
31 * rc_type is new entry, used to determine whether updating is necessary
32 * rc_source string representation of change source
33 * rc_minor is minor
34 * rc_cur_id page_id of associated page entry
35 * rc_user user id who made the entry
36 * rc_user_text user name who made the entry
37 * rc_comment edit summary
38 * rc_this_oldid rev_id associated with this entry (or zero)
39 * rc_last_oldid rev_id associated with the entry before this one (or zero)
40 * rc_bot is bot, hidden
41 * rc_ip IP address of the user in dotted quad notation
42 * rc_new obsolete, use rc_type==RC_NEW
43 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
44 * rc_old_len integer byte length of the text before the edit
45 * rc_new_len the same after the edit
46 * rc_deleted partial deletion
47 * rc_logid the log_id value for this log entry (or zero)
48 * rc_log_type the log type (or null)
49 * rc_log_action the log action (or null)
50 * rc_params log params
51 *
52 * mExtra:
53 * prefixedDBkey prefixed db key, used by external app via msg queue
54 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
55 * lang the interwiki prefix, automatically set in save()
56 * oldSize text size before the change
57 * newSize text size after the change
58 * pageStatus status of the page: created, deleted, moved, restored, changed
59 *
60 * temporary: not stored in the database
61 * notificationtimestamp
62 * numberofWatchingusers
63 */
64 class RecentChange {
65 // Constants for the rc_source field. Extensions may also have
66 // their own source constants.
67 const SRC_EDIT = 'mw.edit';
68 const SRC_NEW = 'mw.new';
69 const SRC_LOG = 'mw.log';
70 const SRC_EXTERNAL = 'mw.external'; // obsolete
71
72 public $mAttribs = array();
73 public $mExtra = array();
74
75 /**
76 * @var Title
77 */
78 public $mTitle = false;
79
80 /**
81 * @var User
82 */
83 private $mPerformer = false;
84
85 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
86 public $notificationtimestamp;
87
88 /**
89 * @var int Line number of recent change. Default -1.
90 */
91 public $counter = -1;
92
93 # Factory methods
94
95 /**
96 * @param $row
97 * @return RecentChange
98 */
99 public static function newFromRow( $row ) {
100 $rc = new RecentChange;
101 $rc->loadFromRow( $row );
102
103 return $rc;
104 }
105
106 /**
107 * No uses left in Gerrit on 2013-11-19.
108 * @deprecated in 1.22
109 * @param $row
110 * @return RecentChange
111 */
112 public static function newFromCurRow( $row ) {
113 wfDeprecated( __METHOD__, '1.22' );
114 $rc = new RecentChange;
115 $rc->loadFromCurRow( $row );
116 $rc->notificationtimestamp = false;
117 $rc->numberofWatchingusers = false;
118
119 return $rc;
120 }
121
122 /**
123 * Obtain the recent change with a given rc_id value
124 *
125 * @param int $rcid rc_id value to retrieve
126 * @return RecentChange
127 */
128 public static function newFromId( $rcid ) {
129 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ );
130 }
131
132 /**
133 * Find the first recent change matching some specific conditions
134 *
135 * @param array $conds of conditions
136 * @param $fname Mixed: override the method name in profiling/logs
137 * @param $options Array Query options
138 * @return RecentChange
139 */
140 public static function newFromConds( $conds, $fname = __METHOD__, $options = array() ) {
141 $dbr = wfGetDB( DB_SLAVE );
142 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname, $options );
143 if ( $row !== false ) {
144 return self::newFromRow( $row );
145 } else {
146 return null;
147 }
148 }
149
150 /**
151 * Return the list of recentchanges fields that should be selected to create
152 * a new recentchanges object.
153 * @return array
154 */
155 public static function selectFields() {
156 return array(
157 'rc_id',
158 'rc_timestamp',
159 'rc_user',
160 'rc_user_text',
161 'rc_namespace',
162 'rc_title',
163 'rc_comment',
164 'rc_minor',
165 'rc_bot',
166 'rc_new',
167 'rc_cur_id',
168 'rc_this_oldid',
169 'rc_last_oldid',
170 'rc_type',
171 'rc_source',
172 'rc_patrolled',
173 'rc_ip',
174 'rc_old_len',
175 'rc_new_len',
176 'rc_deleted',
177 'rc_logid',
178 'rc_log_type',
179 'rc_log_action',
180 'rc_params',
181 );
182 }
183
184 # Accessors
185
186 /**
187 * @param $attribs array
188 */
189 public function setAttribs( $attribs ) {
190 $this->mAttribs = $attribs;
191 }
192
193 /**
194 * @param $extra array
195 */
196 public function setExtra( $extra ) {
197 $this->mExtra = $extra;
198 }
199
200 /**
201 *
202 * @return Title
203 */
204 public function &getTitle() {
205 if ( $this->mTitle === false ) {
206 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
207 }
208
209 return $this->mTitle;
210 }
211
212 /**
213 * Get the User object of the person who performed this change.
214 *
215 * @return User
216 */
217 public function getPerformer() {
218 if ( $this->mPerformer === false ) {
219 if ( $this->mAttribs['rc_user'] ) {
220 $this->mPerformer = User::newFromID( $this->mAttribs['rc_user'] );
221 } else {
222 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false );
223 }
224 }
225
226 return $this->mPerformer;
227 }
228
229 /**
230 * Writes the data in this object to the database
231 * @param $noudp bool
232 */
233 public function save( $noudp = false ) {
234 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
235
236 $dbw = wfGetDB( DB_MASTER );
237 if ( !is_array( $this->mExtra ) ) {
238 $this->mExtra = array();
239 }
240 $this->mExtra['lang'] = $wgLocalInterwiki;
241
242 if ( !$wgPutIPinRC ) {
243 $this->mAttribs['rc_ip'] = '';
244 }
245
246 # If our database is strict about IP addresses, use NULL instead of an empty string
247 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
248 unset( $this->mAttribs['rc_ip'] );
249 }
250
251 # Trim spaces on user supplied text
252 $this->mAttribs['rc_comment'] = trim( $this->mAttribs['rc_comment'] );
253
254 # Make sure summary is truncated (whole multibyte characters)
255 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
256
257 # Fixup database timestamps
258 $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] );
259 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
260
261 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
262 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id'] == 0 ) {
263 unset( $this->mAttribs['rc_cur_id'] );
264 }
265
266 # Insert new row
267 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ );
268
269 # Set the ID
270 $this->mAttribs['rc_id'] = $dbw->insertId();
271
272 # Notify extensions
273 wfRunHooks( 'RecentChange_save', array( &$this ) );
274
275 # Notify external application via UDP
276 if ( !$noudp ) {
277 $this->notifyRCFeeds();
278 }
279
280 # E-mail notifications
281 if ( $wgUseEnotif || $wgShowUpdatedMarker ) {
282 $editor = $this->getPerformer();
283 $title = $this->getTitle();
284
285 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
286 # @todo FIXME: This would be better as an extension hook
287 $enotif = new EmailNotification();
288 $enotif->notifyOnPageChange( $editor, $title,
289 $this->mAttribs['rc_timestamp'],
290 $this->mAttribs['rc_comment'],
291 $this->mAttribs['rc_minor'],
292 $this->mAttribs['rc_last_oldid'],
293 $this->mExtra['pageStatus'] );
294 }
295 }
296 }
297
298 /**
299 * @deprecated since 1.22, use notifyRCFeeds instead.
300 */
301 public function notifyRC2UDP() {
302 wfDeprecated( __METHOD__, '1.22' );
303 $this->notifyRCFeeds();
304 }
305
306 /**
307 * Send some text to UDP.
308 * @deprecated since 1.22
309 */
310 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
311 global $wgRC2UDPAddress, $wgRC2UDPInterwikiPrefix, $wgRC2UDPPort, $wgRC2UDPPrefix;
312
313 wfDeprecated( __METHOD__, '1.22' );
314
315 # Assume default for standard RC case
316 $address = $address ? $address : $wgRC2UDPAddress;
317 $prefix = $prefix ? $prefix : $wgRC2UDPPrefix;
318 $port = $port ? $port : $wgRC2UDPPort;
319
320 $engine = new UDPRCFeedEngine();
321 $feed = array(
322 'uri' => "udp://$address:$port/$prefix",
323 'formatter' => 'IRCColourfulRCFeedFormatter',
324 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
325 );
326
327 $engine->send( $feed, $line );
328 }
329
330 /**
331 * Notify all the feeds about the change.
332 */
333 public function notifyRCFeeds() {
334 global $wgRCFeeds;
335
336 foreach ( $wgRCFeeds as $feed ) {
337 $omitBots = isset( $feed['omit_bots'] ) ? $feed['omit_bots'] : false;
338
339 if (
340 ( $omitBots && $this->mAttribs['rc_bot'] ) ||
341 $this->mAttribs['rc_type'] == RC_EXTERNAL
342 ) {
343 continue;
344 }
345
346 $engine = self::getEngine( $feed['uri'] );
347
348 if ( isset( $this->mExtra['actionCommentIRC'] ) ) {
349 $actionComment = $this->mExtra['actionCommentIRC'];
350 } else {
351 $actionComment = null;
352 }
353
354 /** @var $formatter RCFeedFormatter */
355 $formatter = new $feed['formatter']();
356 $line = $formatter->getLine( $feed, $this, $actionComment );
357
358 $engine->send( $feed, $line );
359 }
360 }
361
362 /**
363 * Gets the stream engine object for a given URI from $wgRCEngines
364 *
365 * @param string $uri URI to get the engine object for
366 * @throws MWException
367 * @return RCFeedEngine The engine object
368 */
369 public static function getEngine( $uri ) {
370 global $wgRCEngines;
371
372 $scheme = parse_url( $uri, PHP_URL_SCHEME );
373 if ( !$scheme ) {
374 throw new MWException( __FUNCTION__ . ": Invalid stream logger URI: '$uri'" );
375 }
376
377 if ( !isset( $wgRCEngines[$scheme] ) ) {
378 throw new MWException( __FUNCTION__ . ": Unknown stream logger URI scheme: $scheme" );
379 }
380
381 return new $wgRCEngines[$scheme];
382 }
383
384 /**
385 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
386 */
387 public static function cleanupForIRC( $text ) {
388 wfDeprecated( __METHOD__, '1.22' );
389
390 return IRCColourfulRCFeedFormatter::cleanupForIRC( $text );
391 }
392
393 /**
394 * Mark a given change as patrolled
395 *
396 * @param $change Mixed: RecentChange or corresponding rc_id
397 * @param $auto Boolean: for automatic patrol
398 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
399 */
400 public static function markPatrolled( $change, $auto = false ) {
401 global $wgUser;
402
403 $change = $change instanceof RecentChange
404 ? $change
405 : RecentChange::newFromId( $change );
406
407 if ( !$change instanceof RecentChange ) {
408 return null;
409 }
410
411 return $change->doMarkPatrolled( $wgUser, $auto );
412 }
413
414 /**
415 * Mark this RecentChange as patrolled
416 *
417 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
418 * 'markedaspatrollederror-noautopatrol' as errors
419 * @param $user User object doing the action
420 * @param $auto Boolean: for automatic patrol
421 * @return array of permissions errors, see Title::getUserPermissionsErrors()
422 */
423 public function doMarkPatrolled( User $user, $auto = false ) {
424 global $wgUseRCPatrol, $wgUseNPPatrol;
425 $errors = array();
426 // If recentchanges patrol is disabled, only new pages
427 // can be patrolled
428 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
429 $errors[] = array( 'rcpatroldisabled' );
430 }
431 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
432 $right = $auto ? 'autopatrol' : 'patrol';
433 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
434 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
435 $errors[] = array( 'hookaborted' );
436 }
437 // Users without the 'autopatrol' right can't patrol their
438 // own revisions
439 if ( $user->getName() == $this->getAttribute( 'rc_user_text' )
440 && !$user->isAllowed( 'autopatrol' )
441 ) {
442 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
443 }
444 if ( $errors ) {
445 return $errors;
446 }
447 // If the change was patrolled already, do nothing
448 if ( $this->getAttribute( 'rc_patrolled' ) ) {
449 return array();
450 }
451 // Actually set the 'patrolled' flag in RC
452 $this->reallyMarkPatrolled();
453 // Log this patrol event
454 PatrolLog::record( $this, $auto, $user );
455 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
456
457 return array();
458 }
459
460 /**
461 * Mark this RecentChange patrolled, without error checking
462 * @return Integer: number of affected rows
463 */
464 public function reallyMarkPatrolled() {
465 $dbw = wfGetDB( DB_MASTER );
466 $dbw->update(
467 'recentchanges',
468 array(
469 'rc_patrolled' => 1
470 ),
471 array(
472 'rc_id' => $this->getAttribute( 'rc_id' )
473 ),
474 __METHOD__
475 );
476 // Invalidate the page cache after the page has been patrolled
477 // to make sure that the Patrol link isn't visible any longer!
478 $this->getTitle()->invalidateCache();
479
480 return $dbw->affectedRows();
481 }
482
483 /**
484 * Makes an entry in the database corresponding to an edit
485 *
486 * @param $timestamp
487 * @param $title Title
488 * @param $minor
489 * @param $user User
490 * @param $comment
491 * @param $oldId
492 * @param $lastTimestamp
493 * @param $bot
494 * @param $ip string
495 * @param $oldSize int
496 * @param $newSize int
497 * @param $newId int
498 * @param $patrol int
499 * @return RecentChange
500 */
501 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
502 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
503 $rc = new RecentChange;
504 $rc->mTitle = $title;
505 $rc->mPerformer = $user;
506 $rc->mAttribs = array(
507 'rc_timestamp' => $timestamp,
508 'rc_namespace' => $title->getNamespace(),
509 'rc_title' => $title->getDBkey(),
510 'rc_type' => RC_EDIT,
511 'rc_source' => self::SRC_EDIT,
512 'rc_minor' => $minor ? 1 : 0,
513 'rc_cur_id' => $title->getArticleID(),
514 'rc_user' => $user->getId(),
515 'rc_user_text' => $user->getName(),
516 'rc_comment' => $comment,
517 'rc_this_oldid' => $newId,
518 'rc_last_oldid' => $oldId,
519 'rc_bot' => $bot ? 1 : 0,
520 'rc_ip' => self::checkIPAddress( $ip ),
521 'rc_patrolled' => intval( $patrol ),
522 'rc_new' => 0, # obsolete
523 'rc_old_len' => $oldSize,
524 'rc_new_len' => $newSize,
525 'rc_deleted' => 0,
526 'rc_logid' => 0,
527 'rc_log_type' => null,
528 'rc_log_action' => '',
529 'rc_params' => ''
530 );
531
532 $rc->mExtra = array(
533 'prefixedDBkey' => $title->getPrefixedDBkey(),
534 'lastTimestamp' => $lastTimestamp,
535 'oldSize' => $oldSize,
536 'newSize' => $newSize,
537 'pageStatus' => 'changed'
538 );
539 $rc->save();
540
541 return $rc;
542 }
543
544 /**
545 * Makes an entry in the database corresponding to page creation
546 * Note: the title object must be loaded with the new id using resetArticleID()
547 *
548 * @param $timestamp
549 * @param $title Title
550 * @param $minor
551 * @param $user User
552 * @param $comment
553 * @param $bot
554 * @param $ip string
555 * @param $size int
556 * @param $newId int
557 * @param $patrol int
558 * @return RecentChange
559 */
560 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
561 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
562 $rc = new RecentChange;
563 $rc->mTitle = $title;
564 $rc->mPerformer = $user;
565 $rc->mAttribs = array(
566 'rc_timestamp' => $timestamp,
567 'rc_namespace' => $title->getNamespace(),
568 'rc_title' => $title->getDBkey(),
569 'rc_type' => RC_NEW,
570 'rc_source' => self::SRC_NEW,
571 'rc_minor' => $minor ? 1 : 0,
572 'rc_cur_id' => $title->getArticleID(),
573 'rc_user' => $user->getId(),
574 'rc_user_text' => $user->getName(),
575 'rc_comment' => $comment,
576 'rc_this_oldid' => $newId,
577 'rc_last_oldid' => 0,
578 'rc_bot' => $bot ? 1 : 0,
579 'rc_ip' => self::checkIPAddress( $ip ),
580 'rc_patrolled' => intval( $patrol ),
581 'rc_new' => 1, # obsolete
582 'rc_old_len' => 0,
583 'rc_new_len' => $size,
584 'rc_deleted' => 0,
585 'rc_logid' => 0,
586 'rc_log_type' => null,
587 'rc_log_action' => '',
588 'rc_params' => ''
589 );
590
591 $rc->mExtra = array(
592 'prefixedDBkey' => $title->getPrefixedDBkey(),
593 'lastTimestamp' => 0,
594 'oldSize' => 0,
595 'newSize' => $size,
596 'pageStatus' => 'created'
597 );
598 $rc->save();
599
600 return $rc;
601 }
602
603 /**
604 * @param $timestamp
605 * @param $title
606 * @param $user
607 * @param $actionComment
608 * @param $ip string
609 * @param $type
610 * @param $action
611 * @param $target
612 * @param $logComment
613 * @param $params
614 * @param $newId int
615 * @param $actionCommentIRC string
616 * @return bool
617 */
618 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
619 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
620 ) {
621 global $wgLogRestrictions;
622
623 # Don't add private logs to RC!
624 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
625 return false;
626 }
627 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
628 $target, $logComment, $params, $newId, $actionCommentIRC );
629 $rc->save();
630
631 return true;
632 }
633
634 /**
635 * @param $timestamp
636 * @param $title Title
637 * @param $user User
638 * @param $actionComment
639 * @param $ip string
640 * @param $type
641 * @param $action
642 * @param $target Title
643 * @param $logComment
644 * @param $params
645 * @param $newId int
646 * @param $actionCommentIRC string
647 * @return RecentChange
648 */
649 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
650 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
651 global $wgRequest;
652
653 ## Get pageStatus for email notification
654 switch ( $type . '-' . $action ) {
655 case 'delete-delete':
656 $pageStatus = 'deleted';
657 break;
658 case 'move-move':
659 case 'move-move_redir':
660 $pageStatus = 'moved';
661 break;
662 case 'delete-restore':
663 $pageStatus = 'restored';
664 break;
665 case 'upload-upload':
666 $pageStatus = 'created';
667 break;
668 case 'upload-overwrite':
669 default:
670 $pageStatus = 'changed';
671 break;
672 }
673
674 $rc = new RecentChange;
675 $rc->mTitle = $target;
676 $rc->mPerformer = $user;
677 $rc->mAttribs = array(
678 'rc_timestamp' => $timestamp,
679 'rc_namespace' => $target->getNamespace(),
680 'rc_title' => $target->getDBkey(),
681 'rc_type' => RC_LOG,
682 'rc_source' => self::SRC_LOG,
683 'rc_minor' => 0,
684 'rc_cur_id' => $target->getArticleID(),
685 'rc_user' => $user->getId(),
686 'rc_user_text' => $user->getName(),
687 'rc_comment' => $logComment,
688 'rc_this_oldid' => 0,
689 'rc_last_oldid' => 0,
690 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
691 'rc_ip' => self::checkIPAddress( $ip ),
692 'rc_patrolled' => 1,
693 'rc_new' => 0, # obsolete
694 'rc_old_len' => null,
695 'rc_new_len' => null,
696 'rc_deleted' => 0,
697 'rc_logid' => $newId,
698 'rc_log_type' => $type,
699 'rc_log_action' => $action,
700 'rc_params' => $params
701 );
702
703 $rc->mExtra = array(
704 'prefixedDBkey' => $title->getPrefixedDBkey(),
705 'lastTimestamp' => 0,
706 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
707 'pageStatus' => $pageStatus,
708 'actionCommentIRC' => $actionCommentIRC
709 );
710
711 return $rc;
712 }
713
714 /**
715 * Initialises the members of this object from a mysql row object
716 *
717 * @param $row
718 */
719 public function loadFromRow( $row ) {
720 $this->mAttribs = get_object_vars( $row );
721 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
722 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
723 }
724
725 /**
726 * Makes a pseudo-RC entry from a cur row
727 *
728 * @deprecated in 1.22
729 * @param $row
730 */
731 public function loadFromCurRow( $row ) {
732 wfDeprecated( __METHOD__, '1.22' );
733 $this->mAttribs = array(
734 'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ),
735 'rc_user' => $row->rev_user,
736 'rc_user_text' => $row->rev_user_text,
737 'rc_namespace' => $row->page_namespace,
738 'rc_title' => $row->page_title,
739 'rc_comment' => $row->rev_comment,
740 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
741 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
742 'rc_source' => $row->page_is_new ? self::SRC_NEW : self::SRC_EDIT,
743 'rc_cur_id' => $row->page_id,
744 'rc_this_oldid' => $row->rev_id,
745 'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0,
746 'rc_bot' => 0,
747 'rc_ip' => '',
748 'rc_id' => $row->rc_id,
749 'rc_patrolled' => $row->rc_patrolled,
750 'rc_new' => $row->page_is_new, # obsolete
751 'rc_old_len' => $row->rc_old_len,
752 'rc_new_len' => $row->rc_new_len,
753 'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '',
754 'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null,
755 'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null,
756 'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0,
757 'rc_deleted' => $row->rc_deleted // MUST be set
758 );
759 }
760
761 /**
762 * Get an attribute value
763 *
764 * @param string $name Attribute name
765 * @return mixed
766 */
767 public function getAttribute( $name ) {
768 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
769 }
770
771 /**
772 * @return array
773 */
774 public function getAttributes() {
775 return $this->mAttribs;
776 }
777
778 /**
779 * Gets the end part of the diff URL associated with this object
780 * Blank if no diff link should be displayed
781 * @param $forceCur
782 * @return string
783 */
784 public function diffLinkTrail( $forceCur ) {
785 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
786 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
787 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
788 if ( $forceCur ) {
789 $trail .= '&diff=0';
790 } else {
791 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
792 }
793 } else {
794 $trail = '';
795 }
796
797 return $trail;
798 }
799
800 /**
801 * Returns the change size (HTML).
802 * The lengths can be given optionally.
803 * @param $old int
804 * @param $new int
805 * @return string
806 */
807 public function getCharacterDifference( $old = 0, $new = 0 ) {
808 if ( $old === 0 ) {
809 $old = $this->mAttribs['rc_old_len'];
810 }
811 if ( $new === 0 ) {
812 $new = $this->mAttribs['rc_new_len'];
813 }
814 if ( $old === null || $new === null ) {
815 return '';
816 }
817
818 return ChangesList::showCharacterDifference( $old, $new );
819 }
820
821 /**
822 * Purge expired changes from the recentchanges table
823 * @since 1.22
824 */
825 public static function purgeExpiredChanges() {
826 if ( wfReadOnly() ) {
827 return;
828 }
829
830 $method = __METHOD__;
831 $dbw = wfGetDB( DB_MASTER );
832 $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
833 global $wgRCMaxAge;
834
835 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
836 $dbw->delete(
837 'recentchanges',
838 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
839 $method
840 );
841 } );
842 }
843
844 private static function checkIPAddress( $ip ) {
845 global $wgRequest;
846 if ( $ip ) {
847 if ( !IP::isIPAddress( $ip ) ) {
848 throw new MWException( "Attempt to write \"" . $ip .
849 "\" as an IP address into recent changes" );
850 }
851 } else {
852 $ip = $wgRequest->getIP();
853 if ( !$ip ) {
854 $ip = '';
855 }
856 }
857
858 return $ip;
859 }
860
861 /**
862 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
863 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
864 * or rows which will be deleted soon shouldn't be included.
865 *
866 * @param $timestamp mixed MWTimestamp compatible timestamp
867 * @param $tolerance integer Tolerance in seconds
868 * @return bool
869 */
870 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
871 global $wgRCMaxAge;
872
873 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
874 }
875 }