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