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