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