MediaWiki
master
|
00001 <?php 00065 class RecentChange { 00066 var $mAttribs = array(), $mExtra = array(); 00067 00071 var $mTitle = false; 00072 00076 private $mPerformer = false; 00077 00081 var $mMovedToTitle = false; 00082 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked 00083 var $notificationtimestamp; 00084 00085 # Factory methods 00086 00091 public static function newFromRow( $row ) { 00092 $rc = new RecentChange; 00093 $rc->loadFromRow( $row ); 00094 return $rc; 00095 } 00096 00101 public static function newFromCurRow( $row ) { 00102 $rc = new RecentChange; 00103 $rc->loadFromCurRow( $row ); 00104 $rc->notificationtimestamp = false; 00105 $rc->numberofWatchingusers = false; 00106 return $rc; 00107 } 00108 00115 public static function newFromId( $rcid ) { 00116 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ ); 00117 } 00118 00126 public static function newFromConds( $conds, $fname = __METHOD__ ) { 00127 $dbr = wfGetDB( DB_SLAVE ); 00128 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname ); 00129 if ( $row !== false ) { 00130 return self::newFromRow( $row ); 00131 } else { 00132 return null; 00133 } 00134 } 00135 00141 public static function selectFields() { 00142 return array( 00143 'rc_id', 00144 'rc_timestamp', 00145 'rc_cur_time', 00146 'rc_user', 00147 'rc_user_text', 00148 'rc_namespace', 00149 'rc_title', 00150 'rc_comment', 00151 'rc_minor', 00152 'rc_bot', 00153 'rc_new', 00154 'rc_cur_id', 00155 'rc_this_oldid', 00156 'rc_last_oldid', 00157 'rc_type', 00158 'rc_patrolled', 00159 'rc_ip', 00160 'rc_old_len', 00161 'rc_new_len', 00162 'rc_deleted', 00163 'rc_logid', 00164 'rc_log_type', 00165 'rc_log_action', 00166 'rc_params', 00167 ); 00168 } 00169 00170 # Accessors 00171 00175 public function setAttribs( $attribs ) { 00176 $this->mAttribs = $attribs; 00177 } 00178 00182 public function setExtra( $extra ) { 00183 $this->mExtra = $extra; 00184 } 00185 00190 public function &getTitle() { 00191 if ( $this->mTitle === false ) { 00192 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] ); 00193 # Make sure the correct page ID is process cached 00194 $this->mTitle->resetArticleID( $this->mAttribs['rc_cur_id'] ); 00195 } 00196 return $this->mTitle; 00197 } 00198 00204 public function getPerformer() { 00205 if ( $this->mPerformer === false ) { 00206 if ( $this->mAttribs['rc_user'] ) { 00207 $this->mPerformer = User::newFromID( $this->mAttribs['rc_user'] ); 00208 } else { 00209 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false ); 00210 } 00211 } 00212 return $this->mPerformer; 00213 } 00214 00219 public function save( $noudp = false ) { 00220 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang; 00221 00222 $dbw = wfGetDB( DB_MASTER ); 00223 if ( !is_array( $this->mExtra ) ) { 00224 $this->mExtra = array(); 00225 } 00226 $this->mExtra['lang'] = $wgLocalInterwiki; 00227 00228 if ( !$wgPutIPinRC ) { 00229 $this->mAttribs['rc_ip'] = ''; 00230 } 00231 00232 # If our database is strict about IP addresses, use NULL instead of an empty string 00233 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) { 00234 unset( $this->mAttribs['rc_ip'] ); 00235 } 00236 00237 # Make sure summary is truncated (whole multibyte characters) 00238 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 ); 00239 00240 # Fixup database timestamps 00241 $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] ); 00242 $this->mAttribs['rc_cur_time'] = $dbw->timestamp( $this->mAttribs['rc_cur_time'] ); 00243 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' ); 00244 00245 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL 00246 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id'] == 0 ) { 00247 unset( $this->mAttribs['rc_cur_id'] ); 00248 } 00249 00250 # Insert new row 00251 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ ); 00252 00253 # Set the ID 00254 $this->mAttribs['rc_id'] = $dbw->insertId(); 00255 00256 # Notify extensions 00257 wfRunHooks( 'RecentChange_save', array( &$this ) ); 00258 00259 # Notify external application via UDP 00260 if ( !$noudp ) { 00261 $this->notifyRC2UDP(); 00262 } 00263 00264 # E-mail notifications 00265 if ( $wgUseEnotif || $wgShowUpdatedMarker ) { 00266 $editor = $this->getPerformer(); 00267 $title = $this->getTitle(); 00268 00269 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) { 00270 # @todo FIXME: This would be better as an extension hook 00271 $enotif = new EmailNotification(); 00272 $enotif->notifyOnPageChange( $editor, $title, 00273 $this->mAttribs['rc_timestamp'], 00274 $this->mAttribs['rc_comment'], 00275 $this->mAttribs['rc_minor'], 00276 $this->mAttribs['rc_last_oldid'] ); 00277 } 00278 } 00279 } 00280 00281 public function notifyRC2UDP() { 00282 global $wgRC2UDPAddress, $wgRC2UDPOmitBots; 00283 # Notify external application via UDP 00284 if ( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) { 00285 self::sendToUDP( $this->getIRCLine() ); 00286 } 00287 } 00288 00298 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) { 00299 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort; 00300 # Assume default for standard RC case 00301 $address = $address ? $address : $wgRC2UDPAddress; 00302 $prefix = $prefix ? $prefix : $wgRC2UDPPrefix; 00303 $port = $port ? $port : $wgRC2UDPPort; 00304 # Notify external application via UDP 00305 if ( $address ) { 00306 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ); 00307 if ( $conn ) { 00308 $line = $prefix . $line; 00309 wfDebug( __METHOD__ . ": sending UDP line: $line\n" ); 00310 socket_sendto( $conn, $line, strlen( $line ), 0, $address, $port ); 00311 socket_close( $conn ); 00312 return true; 00313 } else { 00314 wfDebug( __METHOD__ . ": failed to create UDP socket\n" ); 00315 } 00316 } 00317 return false; 00318 } 00319 00325 public static function cleanupForIRC( $text ) { 00326 return Sanitizer::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) ); 00327 } 00328 00336 public static function markPatrolled( $change, $auto = false ) { 00337 global $wgUser; 00338 00339 $change = $change instanceof RecentChange 00340 ? $change 00341 : RecentChange::newFromId( $change ); 00342 00343 if ( !$change instanceof RecentChange ) { 00344 return null; 00345 } 00346 return $change->doMarkPatrolled( $wgUser, $auto ); 00347 } 00348 00357 public function doMarkPatrolled( User $user, $auto = false ) { 00358 global $wgUseRCPatrol, $wgUseNPPatrol; 00359 $errors = array(); 00360 // If recentchanges patrol is disabled, only new pages 00361 // can be patrolled 00362 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) { 00363 $errors[] = array( 'rcpatroldisabled' ); 00364 } 00365 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol" 00366 $right = $auto ? 'autopatrol' : 'patrol'; 00367 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) ); 00368 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) { 00369 $errors[] = array( 'hookaborted' ); 00370 } 00371 // Users without the 'autopatrol' right can't patrol their 00372 // own revisions 00373 if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) { 00374 $errors[] = array( 'markedaspatrollederror-noautopatrol' ); 00375 } 00376 if ( $errors ) { 00377 return $errors; 00378 } 00379 // If the change was patrolled already, do nothing 00380 if ( $this->getAttribute( 'rc_patrolled' ) ) { 00381 return array(); 00382 } 00383 // Actually set the 'patrolled' flag in RC 00384 $this->reallyMarkPatrolled(); 00385 // Log this patrol event 00386 PatrolLog::record( $this, $auto, $user ); 00387 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) ); 00388 return array(); 00389 } 00390 00395 public function reallyMarkPatrolled() { 00396 $dbw = wfGetDB( DB_MASTER ); 00397 $dbw->update( 00398 'recentchanges', 00399 array( 00400 'rc_patrolled' => 1 00401 ), 00402 array( 00403 'rc_id' => $this->getAttribute( 'rc_id' ) 00404 ), 00405 __METHOD__ 00406 ); 00407 return $dbw->affectedRows(); 00408 } 00409 00428 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId, 00429 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) { 00430 $rc = new RecentChange; 00431 $rc->mTitle = $title; 00432 $rc->mPerformer = $user; 00433 $rc->mAttribs = array( 00434 'rc_timestamp' => $timestamp, 00435 'rc_cur_time' => $timestamp, 00436 'rc_namespace' => $title->getNamespace(), 00437 'rc_title' => $title->getDBkey(), 00438 'rc_type' => RC_EDIT, 00439 'rc_minor' => $minor ? 1 : 0, 00440 'rc_cur_id' => $title->getArticleID(), 00441 'rc_user' => $user->getId(), 00442 'rc_user_text' => $user->getName(), 00443 'rc_comment' => $comment, 00444 'rc_this_oldid' => $newId, 00445 'rc_last_oldid' => $oldId, 00446 'rc_bot' => $bot ? 1 : 0, 00447 'rc_ip' => self::checkIPAddress( $ip ), 00448 'rc_patrolled' => intval( $patrol ), 00449 'rc_new' => 0, # obsolete 00450 'rc_old_len' => $oldSize, 00451 'rc_new_len' => $newSize, 00452 'rc_deleted' => 0, 00453 'rc_logid' => 0, 00454 'rc_log_type' => null, 00455 'rc_log_action' => '', 00456 'rc_params' => '' 00457 ); 00458 00459 $rc->mExtra = array( 00460 'prefixedDBkey' => $title->getPrefixedDBkey(), 00461 'lastTimestamp' => $lastTimestamp, 00462 'oldSize' => $oldSize, 00463 'newSize' => $newSize, 00464 ); 00465 $rc->save(); 00466 return $rc; 00467 } 00468 00486 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot, 00487 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) { 00488 $rc = new RecentChange; 00489 $rc->mTitle = $title; 00490 $rc->mPerformer = $user; 00491 $rc->mAttribs = array( 00492 'rc_timestamp' => $timestamp, 00493 'rc_cur_time' => $timestamp, 00494 'rc_namespace' => $title->getNamespace(), 00495 'rc_title' => $title->getDBkey(), 00496 'rc_type' => RC_NEW, 00497 'rc_minor' => $minor ? 1 : 0, 00498 'rc_cur_id' => $title->getArticleID(), 00499 'rc_user' => $user->getId(), 00500 'rc_user_text' => $user->getName(), 00501 'rc_comment' => $comment, 00502 'rc_this_oldid' => $newId, 00503 'rc_last_oldid' => 0, 00504 'rc_bot' => $bot ? 1 : 0, 00505 'rc_ip' => self::checkIPAddress( $ip ), 00506 'rc_patrolled' => intval( $patrol ), 00507 'rc_new' => 1, # obsolete 00508 'rc_old_len' => 0, 00509 'rc_new_len' => $size, 00510 'rc_deleted' => 0, 00511 'rc_logid' => 0, 00512 'rc_log_type' => null, 00513 'rc_log_action' => '', 00514 'rc_params' => '' 00515 ); 00516 00517 $rc->mExtra = array( 00518 'prefixedDBkey' => $title->getPrefixedDBkey(), 00519 'lastTimestamp' => 0, 00520 'oldSize' => 0, 00521 'newSize' => $size 00522 ); 00523 $rc->save(); 00524 return $rc; 00525 } 00526 00542 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type, 00543 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) 00544 { 00545 global $wgLogRestrictions; 00546 # Don't add private logs to RC! 00547 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) { 00548 return false; 00549 } 00550 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, 00551 $target, $logComment, $params, $newId, $actionCommentIRC ); 00552 $rc->save(); 00553 return true; 00554 } 00555 00571 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip, 00572 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) { 00573 global $wgRequest; 00574 00575 $rc = new RecentChange; 00576 $rc->mTitle = $target; 00577 $rc->mPerformer = $user; 00578 $rc->mAttribs = array( 00579 'rc_timestamp' => $timestamp, 00580 'rc_cur_time' => $timestamp, 00581 'rc_namespace' => $target->getNamespace(), 00582 'rc_title' => $target->getDBkey(), 00583 'rc_type' => RC_LOG, 00584 'rc_minor' => 0, 00585 'rc_cur_id' => $target->getArticleID(), 00586 'rc_user' => $user->getId(), 00587 'rc_user_text' => $user->getName(), 00588 'rc_comment' => $logComment, 00589 'rc_this_oldid' => 0, 00590 'rc_last_oldid' => 0, 00591 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0, 00592 'rc_ip' => self::checkIPAddress( $ip ), 00593 'rc_patrolled' => 1, 00594 'rc_new' => 0, # obsolete 00595 'rc_old_len' => null, 00596 'rc_new_len' => null, 00597 'rc_deleted' => 0, 00598 'rc_logid' => $newId, 00599 'rc_log_type' => $type, 00600 'rc_log_action' => $action, 00601 'rc_params' => $params 00602 ); 00603 00604 $rc->mExtra = array( 00605 'prefixedDBkey' => $title->getPrefixedDBkey(), 00606 'lastTimestamp' => 0, 00607 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage 00608 'actionCommentIRC' => $actionCommentIRC 00609 ); 00610 return $rc; 00611 } 00612 00618 public function loadFromRow( $row ) { 00619 $this->mAttribs = get_object_vars( $row ); 00620 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] ); 00621 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set 00622 } 00623 00629 public function loadFromCurRow( $row ) { 00630 $this->mAttribs = array( 00631 'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ), 00632 'rc_cur_time' => $row->rev_timestamp, 00633 'rc_user' => $row->rev_user, 00634 'rc_user_text' => $row->rev_user_text, 00635 'rc_namespace' => $row->page_namespace, 00636 'rc_title' => $row->page_title, 00637 'rc_comment' => $row->rev_comment, 00638 'rc_minor' => $row->rev_minor_edit ? 1 : 0, 00639 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT, 00640 'rc_cur_id' => $row->page_id, 00641 'rc_this_oldid' => $row->rev_id, 00642 'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0, 00643 'rc_bot' => 0, 00644 'rc_ip' => '', 00645 'rc_id' => $row->rc_id, 00646 'rc_patrolled' => $row->rc_patrolled, 00647 'rc_new' => $row->page_is_new, # obsolete 00648 'rc_old_len' => $row->rc_old_len, 00649 'rc_new_len' => $row->rc_new_len, 00650 'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '', 00651 'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null, 00652 'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null, 00653 'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0, 00654 'rc_deleted' => $row->rc_deleted // MUST be set 00655 ); 00656 } 00657 00664 public function getAttribute( $name ) { 00665 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null; 00666 } 00667 00671 public function getAttributes() { 00672 return $this->mAttribs; 00673 } 00674 00681 public function diffLinkTrail( $forceCur ) { 00682 if ( $this->mAttribs['rc_type'] == RC_EDIT ) { 00683 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) . 00684 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] ); 00685 if ( $forceCur ) { 00686 $trail .= '&diff=0' ; 00687 } else { 00688 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] ); 00689 } 00690 } else { 00691 $trail = ''; 00692 } 00693 return $trail; 00694 } 00695 00699 public function getIRCLine() { 00700 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki, 00701 $wgCanonicalServer, $wgScript; 00702 00703 if ( $this->mAttribs['rc_type'] == RC_LOG ) { 00704 // Don't use SpecialPage::getTitleFor, backwards compatibility with 00705 // IRC API which expects "Log". 00706 $titleObj = Title::newFromText( 'Log/' . $this->mAttribs['rc_log_type'], NS_SPECIAL ); 00707 } else { 00708 $titleObj =& $this->getTitle(); 00709 } 00710 $title = $titleObj->getPrefixedText(); 00711 $title = self::cleanupForIRC( $title ); 00712 00713 if ( $this->mAttribs['rc_type'] == RC_LOG ) { 00714 $url = ''; 00715 } else { 00716 $url = $wgCanonicalServer . $wgScript; 00717 if ( $this->mAttribs['rc_type'] == RC_NEW ) { 00718 $query = '?oldid=' . $this->mAttribs['rc_this_oldid']; 00719 } else { 00720 $query = '?diff=' . $this->mAttribs['rc_this_oldid'] . '&oldid=' . $this->mAttribs['rc_last_oldid']; 00721 } 00722 if ( $wgUseRCPatrol || ( $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) { 00723 $query .= '&rcid=' . $this->mAttribs['rc_id']; 00724 } 00725 // HACK: We need this hook for WMF's secure server setup 00726 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) ); 00727 $url .= $query; 00728 } 00729 00730 if ( $this->mAttribs['rc_old_len'] !== null && $this->mAttribs['rc_new_len'] !== null ) { 00731 $szdiff = $this->mAttribs['rc_new_len'] - $this->mAttribs['rc_old_len']; 00732 if ( $szdiff < -500 ) { 00733 $szdiff = "\002$szdiff\002"; 00734 } elseif ( $szdiff >= 0 ) { 00735 $szdiff = '+' . $szdiff ; 00736 } 00737 // @todo i18n with parentheses in content language? 00738 $szdiff = '(' . $szdiff . ')' ; 00739 } else { 00740 $szdiff = ''; 00741 } 00742 00743 $user = self::cleanupForIRC( $this->mAttribs['rc_user_text'] ); 00744 00745 if ( $this->mAttribs['rc_type'] == RC_LOG ) { 00746 $targetText = $this->getTitle()->getPrefixedText(); 00747 $comment = self::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra['actionCommentIRC'] ) ); 00748 $flag = $this->mAttribs['rc_log_action']; 00749 } else { 00750 $comment = self::cleanupForIRC( $this->mAttribs['rc_comment'] ); 00751 $flag = ''; 00752 if ( !$this->mAttribs['rc_patrolled'] && ( $wgUseRCPatrol || $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) { 00753 $flag .= '!'; 00754 } 00755 $flag .= ( $this->mAttribs['rc_type'] == RC_NEW ? "N" : "" ) . ( $this->mAttribs['rc_minor'] ? "M" : "" ) . ( $this->mAttribs['rc_bot'] ? "B" : "" ); 00756 } 00757 00758 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) { 00759 $prefix = $wgLocalInterwiki; 00760 } elseif ( $wgRC2UDPInterwikiPrefix ) { 00761 $prefix = $wgRC2UDPInterwikiPrefix; 00762 } else { 00763 $prefix = false; 00764 } 00765 if ( $prefix !== false ) { 00766 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]"; 00767 } else { 00768 $titleString = "\00314[[\00307$title\00314]]"; 00769 } 00770 00771 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003, 00772 # no colour (\003) switches back to the term default 00773 $fullString = "$titleString\0034 $flag\00310 " . 00774 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n"; 00775 00776 return $fullString; 00777 } 00778 00786 public function getCharacterDifference( $old = 0, $new = 0 ) { 00787 if ( $old === 0 ) { 00788 $old = $this->mAttribs['rc_old_len']; 00789 } 00790 if ( $new === 0 ) { 00791 $new = $this->mAttribs['rc_new_len']; 00792 } 00793 if ( $old === null || $new === null ) { 00794 return ''; 00795 } 00796 return ChangesList::showCharacterDifference( $old, $new ); 00797 } 00798 00799 private static function checkIPAddress( $ip ) { 00800 global $wgRequest; 00801 if ( $ip ) { 00802 if ( !IP::isIPAddress( $ip ) ) { 00803 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" ); 00804 } 00805 } else { 00806 $ip = $wgRequest->getIP(); 00807 if ( !$ip ) { 00808 $ip = ''; 00809 } 00810 } 00811 return $ip; 00812 } 00813 }