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