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