Merge "wpUndidRevision should be passed along during 'preview' and 'diff'"
[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 $wgRC2UDPPrefix, $wgRC2UDPInterwikiPrefix;
301 wfDeprecated( __METHOD__, '1.22' );
302
303 $engine = new UDPRCFeedEngine();
304 $feed = array(
305 'uri' => "udp://$address:$port/$wgRC2UDPPrefix",
306 'formatter' => 'IRCColourfulRCFeedFormatter',
307 'add_interwiki_prefix' => $wgRC2UDPInterwikiPrefix,
308 );
309
310 return $engine->send( $feed, $line );
311 }
312
313 /**
314 * Notify all the feeds about the change.
315 */
316 public function notifyRCFeeds() {
317 global $wgRCFeeds;
318
319 foreach ( $wgRCFeeds as $feed ) {
320 $engine = self::getStreamEngine( $feed['uri'] );
321
322 if ( isset( $this->mExtras['actionCommentIRC'] ) ) {
323 $actionComment = $this->mExtras['actionCommentIRC'];
324 } else {
325 $actionComment = null;
326 }
327
328 $omitBots = isset( $feed['omit_bots'] ) ? $feed['omit_bots'] : false;
329
330 if (
331 ( $omitBots && $this->mAttribs['rc_bot'] ) ||
332 $this->mAttribs['rc_type'] == RC_EXTERNAL
333 ) {
334 continue;
335 }
336
337 $formatter = new $feed['formatter']();
338 $line = $formatter->getLine( $feed, $this, $actionComment );
339
340 $engine->send( $feed, $line );
341 }
342 }
343
344 /**
345 * Gets the stream engine object for a given URI from $wgStreamLoggers
346 *
347 * @param $uri string URI to get the engine object for
348 * @return object The engine object
349 */
350 private static function getStreamEngine( $uri ) {
351 global $wgStreamLoggers;
352
353 $scheme = parse_url( $uri, PHP_URL_SCHEME );
354 if ( !$scheme ) {
355 throw new MWException( __FUNCTION__ . ": Invalid stream logger URI: '$uri'" );
356 }
357
358 if ( !isset( $wgStreamLoggers[$scheme] ) ) {
359 throw new MWException( __FUNCTION__ . ": Unknown stream logger URI scheme: $scheme" );
360 }
361
362 return new $wgStreamLoggers[$scheme];
363 }
364
365 /**
366 * @deprecated since 1.22, moved to IRCColourfulRCFeedFormatter
367 */
368 public static function cleanupForIRC( $text ) {
369 wfDeprecated( __METHOD__, '1.22' );
370 return IRCColourfulRCFeedFormatter::cleanupForIRC( $text );
371 }
372
373 /**
374 * Mark a given change as patrolled
375 *
376 * @param $change Mixed: RecentChange or corresponding rc_id
377 * @param $auto Boolean: for automatic patrol
378 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
379 */
380 public static function markPatrolled( $change, $auto = false ) {
381 global $wgUser;
382
383 $change = $change instanceof RecentChange
384 ? $change
385 : RecentChange::newFromId( $change );
386
387 if ( !$change instanceof RecentChange ) {
388 return null;
389 }
390 return $change->doMarkPatrolled( $wgUser, $auto );
391 }
392
393 /**
394 * Mark this RecentChange as patrolled
395 *
396 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
397 * @param $user User object doing the action
398 * @param $auto Boolean: for automatic patrol
399 * @return array of permissions errors, see Title::getUserPermissionsErrors()
400 */
401 public function doMarkPatrolled( User $user, $auto = false ) {
402 global $wgUseRCPatrol, $wgUseNPPatrol;
403 $errors = array();
404 // If recentchanges patrol is disabled, only new pages
405 // can be patrolled
406 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
407 $errors[] = array( 'rcpatroldisabled' );
408 }
409 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
410 $right = $auto ? 'autopatrol' : 'patrol';
411 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
412 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
413 $errors[] = array( 'hookaborted' );
414 }
415 // Users without the 'autopatrol' right can't patrol their
416 // own revisions
417 if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) {
418 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
419 }
420 if ( $errors ) {
421 return $errors;
422 }
423 // If the change was patrolled already, do nothing
424 if ( $this->getAttribute( 'rc_patrolled' ) ) {
425 return array();
426 }
427 // Actually set the 'patrolled' flag in RC
428 $this->reallyMarkPatrolled();
429 // Log this patrol event
430 PatrolLog::record( $this, $auto, $user );
431 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
432 return array();
433 }
434
435 /**
436 * Mark this RecentChange patrolled, without error checking
437 * @return Integer: number of affected rows
438 */
439 public function reallyMarkPatrolled() {
440 $dbw = wfGetDB( DB_MASTER );
441 $dbw->update(
442 'recentchanges',
443 array(
444 'rc_patrolled' => 1
445 ),
446 array(
447 'rc_id' => $this->getAttribute( 'rc_id' )
448 ),
449 __METHOD__
450 );
451 // Invalidate the page cache after the page has been patrolled
452 // to make sure that the Patrol link isn't visible any longer!
453 $this->getTitle()->invalidateCache();
454 return $dbw->affectedRows();
455 }
456
457 /**
458 * Makes an entry in the database corresponding to an edit
459 *
460 * @param $timestamp
461 * @param $title Title
462 * @param $minor
463 * @param $user User
464 * @param $comment
465 * @param $oldId
466 * @param $lastTimestamp
467 * @param $bot
468 * @param $ip string
469 * @param $oldSize int
470 * @param $newSize int
471 * @param $newId int
472 * @param $patrol int
473 * @return RecentChange
474 */
475 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
476 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
477 $rc = new RecentChange;
478 $rc->mTitle = $title;
479 $rc->mPerformer = $user;
480 $rc->mAttribs = array(
481 'rc_timestamp' => $timestamp,
482 'rc_cur_time' => $timestamp,
483 'rc_namespace' => $title->getNamespace(),
484 'rc_title' => $title->getDBkey(),
485 'rc_type' => RC_EDIT,
486 'rc_minor' => $minor ? 1 : 0,
487 'rc_cur_id' => $title->getArticleID(),
488 'rc_user' => $user->getId(),
489 'rc_user_text' => $user->getName(),
490 'rc_comment' => $comment,
491 'rc_this_oldid' => $newId,
492 'rc_last_oldid' => $oldId,
493 'rc_bot' => $bot ? 1 : 0,
494 'rc_ip' => self::checkIPAddress( $ip ),
495 'rc_patrolled' => intval( $patrol ),
496 'rc_new' => 0, # obsolete
497 'rc_old_len' => $oldSize,
498 'rc_new_len' => $newSize,
499 'rc_deleted' => 0,
500 'rc_logid' => 0,
501 'rc_log_type' => null,
502 'rc_log_action' => '',
503 'rc_params' => ''
504 );
505
506 $rc->mExtra = array(
507 'prefixedDBkey' => $title->getPrefixedDBkey(),
508 'lastTimestamp' => $lastTimestamp,
509 'oldSize' => $oldSize,
510 'newSize' => $newSize,
511 'pageStatus' => 'changed'
512 );
513 $rc->save();
514 return $rc;
515 }
516
517 /**
518 * Makes an entry in the database corresponding to page creation
519 * Note: the title object must be loaded with the new id using resetArticleID()
520 * @todo Document parameters and return
521 *
522 * @param $timestamp
523 * @param $title Title
524 * @param $minor
525 * @param $user User
526 * @param $comment
527 * @param $bot
528 * @param $ip string
529 * @param $size int
530 * @param $newId int
531 * @param $patrol int
532 * @return RecentChange
533 */
534 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
535 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
536 $rc = new RecentChange;
537 $rc->mTitle = $title;
538 $rc->mPerformer = $user;
539 $rc->mAttribs = array(
540 'rc_timestamp' => $timestamp,
541 'rc_cur_time' => $timestamp,
542 'rc_namespace' => $title->getNamespace(),
543 'rc_title' => $title->getDBkey(),
544 'rc_type' => RC_NEW,
545 'rc_minor' => $minor ? 1 : 0,
546 'rc_cur_id' => $title->getArticleID(),
547 'rc_user' => $user->getId(),
548 'rc_user_text' => $user->getName(),
549 'rc_comment' => $comment,
550 'rc_this_oldid' => $newId,
551 'rc_last_oldid' => 0,
552 'rc_bot' => $bot ? 1 : 0,
553 'rc_ip' => self::checkIPAddress( $ip ),
554 'rc_patrolled' => intval( $patrol ),
555 'rc_new' => 1, # obsolete
556 'rc_old_len' => 0,
557 'rc_new_len' => $size,
558 'rc_deleted' => 0,
559 'rc_logid' => 0,
560 'rc_log_type' => null,
561 'rc_log_action' => '',
562 'rc_params' => ''
563 );
564
565 $rc->mExtra = array(
566 'prefixedDBkey' => $title->getPrefixedDBkey(),
567 'lastTimestamp' => 0,
568 'oldSize' => 0,
569 'newSize' => $size,
570 'pageStatus' => 'created'
571 );
572 $rc->save();
573 return $rc;
574 }
575
576 /**
577 * @param $timestamp
578 * @param $title
579 * @param $user
580 * @param $actionComment
581 * @param $ip string
582 * @param $type
583 * @param $action
584 * @param $target
585 * @param $logComment
586 * @param $params
587 * @param $newId int
588 * @param $actionCommentIRC string
589 * @return bool
590 */
591 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
592 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' )
593 {
594 global $wgLogRestrictions;
595 # Don't add private logs to RC!
596 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
597 return false;
598 }
599 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
600 $target, $logComment, $params, $newId, $actionCommentIRC );
601 $rc->save();
602 return true;
603 }
604
605 /**
606 * @param $timestamp
607 * @param $title Title
608 * @param $user User
609 * @param $actionComment
610 * @param $ip string
611 * @param $type
612 * @param $action
613 * @param $target Title
614 * @param $logComment
615 * @param $params
616 * @param $newId int
617 * @param $actionCommentIRC string
618 * @return RecentChange
619 */
620 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
621 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
622 global $wgRequest;
623
624 ## Get pageStatus for email notification
625 switch ( $type . '-' . $action ) {
626 case 'delete-delete':
627 $pageStatus = 'deleted';
628 break;
629 case 'move-move':
630 case 'move-move_redir':
631 $pageStatus = 'moved';
632 break;
633 case 'delete-restore':
634 $pageStatus = 'restored';
635 break;
636 case 'upload-upload':
637 $pageStatus = 'created';
638 break;
639 case 'upload-overwrite':
640 default:
641 $pageStatus = 'changed';
642 break;
643 }
644
645 $rc = new RecentChange;
646 $rc->mTitle = $target;
647 $rc->mPerformer = $user;
648 $rc->mAttribs = array(
649 'rc_timestamp' => $timestamp,
650 'rc_cur_time' => $timestamp,
651 'rc_namespace' => $target->getNamespace(),
652 'rc_title' => $target->getDBkey(),
653 'rc_type' => RC_LOG,
654 'rc_minor' => 0,
655 'rc_cur_id' => $target->getArticleID(),
656 'rc_user' => $user->getId(),
657 'rc_user_text' => $user->getName(),
658 'rc_comment' => $logComment,
659 'rc_this_oldid' => 0,
660 'rc_last_oldid' => 0,
661 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
662 'rc_ip' => self::checkIPAddress( $ip ),
663 'rc_patrolled' => 1,
664 'rc_new' => 0, # obsolete
665 'rc_old_len' => null,
666 'rc_new_len' => null,
667 'rc_deleted' => 0,
668 'rc_logid' => $newId,
669 'rc_log_type' => $type,
670 'rc_log_action' => $action,
671 'rc_params' => $params
672 );
673
674 $rc->mExtra = array(
675 'prefixedDBkey' => $title->getPrefixedDBkey(),
676 'lastTimestamp' => 0,
677 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
678 'pageStatus' => $pageStatus,
679 'actionCommentIRC' => $actionCommentIRC
680 );
681 return $rc;
682 }
683
684 /**
685 * Initialises the members of this object from a mysql row object
686 *
687 * @param $row
688 */
689 public function loadFromRow( $row ) {
690 $this->mAttribs = get_object_vars( $row );
691 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
692 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
693 }
694
695 /**
696 * Makes a pseudo-RC entry from a cur row
697 *
698 * @deprected in 1.22
699 * @param $row
700 */
701 public function loadFromCurRow( $row ) {
702 wfDeprecated( __METHOD__, '1.22' );
703 $this->mAttribs = array(
704 'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ),
705 'rc_cur_time' => $row->rev_timestamp,
706 'rc_user' => $row->rev_user,
707 'rc_user_text' => $row->rev_user_text,
708 'rc_namespace' => $row->page_namespace,
709 'rc_title' => $row->page_title,
710 'rc_comment' => $row->rev_comment,
711 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
712 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
713 'rc_cur_id' => $row->page_id,
714 'rc_this_oldid' => $row->rev_id,
715 'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0,
716 'rc_bot' => 0,
717 'rc_ip' => '',
718 'rc_id' => $row->rc_id,
719 'rc_patrolled' => $row->rc_patrolled,
720 'rc_new' => $row->page_is_new, # obsolete
721 'rc_old_len' => $row->rc_old_len,
722 'rc_new_len' => $row->rc_new_len,
723 'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '',
724 'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null,
725 'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null,
726 'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0,
727 'rc_deleted' => $row->rc_deleted // MUST be set
728 );
729 }
730
731 /**
732 * Get an attribute value
733 *
734 * @param string $name Attribute name
735 * @return mixed
736 */
737 public function getAttribute( $name ) {
738 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
739 }
740
741 /**
742 * @return array
743 */
744 public function getAttributes() {
745 return $this->mAttribs;
746 }
747
748 /**
749 * Gets the end part of the diff URL associated with this object
750 * Blank if no diff link should be displayed
751 * @param $forceCur
752 * @return string
753 */
754 public function diffLinkTrail( $forceCur ) {
755 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
756 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
757 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
758 if ( $forceCur ) {
759 $trail .= '&diff=0';
760 } else {
761 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
762 }
763 } else {
764 $trail = '';
765 }
766 return $trail;
767 }
768
769 /**
770 * Returns the change size (HTML).
771 * The lengths can be given optionally.
772 * @param $old int
773 * @param $new int
774 * @return string
775 */
776 public function getCharacterDifference( $old = 0, $new = 0 ) {
777 if ( $old === 0 ) {
778 $old = $this->mAttribs['rc_old_len'];
779 }
780 if ( $new === 0 ) {
781 $new = $this->mAttribs['rc_new_len'];
782 }
783 if ( $old === null || $new === null ) {
784 return '';
785 }
786 return ChangesList::showCharacterDifference( $old, $new );
787 }
788
789 /**
790 * Purge expired changes from the recentchanges table
791 * @since 1.22
792 */
793 public static function purgeExpiredChanges() {
794 if ( wfReadOnly() ) {
795 return;
796 }
797
798 $method = __METHOD__;
799 $dbw = wfGetDB( DB_MASTER );
800 $dbw->onTransactionIdle( function() use ( $dbw, $method ) {
801 global $wgRCMaxAge;
802
803 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
804 $dbw->delete(
805 'recentchanges',
806 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
807 $method
808 );
809 } );
810 }
811
812 private static function checkIPAddress( $ip ) {
813 global $wgRequest;
814 if ( $ip ) {
815 if ( !IP::isIPAddress( $ip ) ) {
816 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
817 }
818 } else {
819 $ip = $wgRequest->getIP();
820 if ( !$ip ) {
821 $ip = '';
822 }
823 }
824 return $ip;
825 }
826
827 /**
828 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
829 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
830 * or rows which will be deleted soon shouldn't be included.
831 *
832 * @param $timestamp mixed MWTimestamp compatible timestamp
833 * @param $tolerance integer Tolerance in seconds
834 * @return bool
835 */
836 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
837 global $wgRCMaxAge;
838 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
839 }
840 }