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