MediaWiki
REL1_20
|
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', '*', $conds, $fname ); 00129 if ( $row !== false ) { 00130 return self::newFromRow( $row ); 00131 } else { 00132 return null; 00133 } 00134 } 00135 00136 # Accessors 00137 00141 public function setAttribs( $attribs ) { 00142 $this->mAttribs = $attribs; 00143 } 00144 00148 public function setExtra( $extra ) { 00149 $this->mExtra = $extra; 00150 } 00151 00156 public function &getTitle() { 00157 if( $this->mTitle === false ) { 00158 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] ); 00159 # Make sure the correct page ID is process cached 00160 $this->mTitle->resetArticleID( $this->mAttribs['rc_cur_id'] ); 00161 } 00162 return $this->mTitle; 00163 } 00164 00168 public function getMovedToTitle() { 00169 if( $this->mMovedToTitle === false ) { 00170 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'], 00171 $this->mAttribs['rc_moved_to_title'] ); 00172 } 00173 return $this->mMovedToTitle; 00174 } 00175 00181 public function getPerformer() { 00182 if ( $this->mPerformer === false ) { 00183 if ( $this->mAttribs['rc_user'] ) { 00184 $this->mPerformer = User::newFromID( $this->mAttribs['rc_user'] ); 00185 } else { 00186 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false ); 00187 } 00188 } 00189 return $this->mPerformer; 00190 } 00191 00196 public function save( $noudp = false ) { 00197 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang; 00198 00199 $dbw = wfGetDB( DB_MASTER ); 00200 if( !is_array($this->mExtra) ) { 00201 $this->mExtra = array(); 00202 } 00203 $this->mExtra['lang'] = $wgLocalInterwiki; 00204 00205 if( !$wgPutIPinRC ) { 00206 $this->mAttribs['rc_ip'] = ''; 00207 } 00208 00209 # If our database is strict about IP addresses, use NULL instead of an empty string 00210 if( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) { 00211 unset( $this->mAttribs['rc_ip'] ); 00212 } 00213 00214 # Make sure summary is truncated (whole multibyte characters) 00215 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 ); 00216 00217 # Fixup database timestamps 00218 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']); 00219 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']); 00220 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' ); 00221 00222 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL 00223 if( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) { 00224 unset( $this->mAttribs['rc_cur_id'] ); 00225 } 00226 00227 # Insert new row 00228 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ ); 00229 00230 # Set the ID 00231 $this->mAttribs['rc_id'] = $dbw->insertId(); 00232 00233 # Notify extensions 00234 wfRunHooks( 'RecentChange_save', array( &$this ) ); 00235 00236 # Notify external application via UDP 00237 if ( !$noudp ) { 00238 $this->notifyRC2UDP(); 00239 } 00240 00241 # E-mail notifications 00242 if( $wgUseEnotif || $wgShowUpdatedMarker ) { 00243 $editor = $this->getPerformer(); 00244 $title = $this->getTitle(); 00245 00246 if ( wfRunHooks( 'AbortEmailNotification', array($editor, $title) ) ) { 00247 # @todo FIXME: This would be better as an extension hook 00248 $enotif = new EmailNotification(); 00249 $enotif->notifyOnPageChange( $editor, $title, 00250 $this->mAttribs['rc_timestamp'], 00251 $this->mAttribs['rc_comment'], 00252 $this->mAttribs['rc_minor'], 00253 $this->mAttribs['rc_last_oldid'] ); 00254 } 00255 } 00256 } 00257 00258 public function notifyRC2UDP() { 00259 global $wgRC2UDPAddress, $wgRC2UDPOmitBots; 00260 # Notify external application via UDP 00261 if( $wgRC2UDPAddress && ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) { 00262 self::sendToUDP( $this->getIRCLine() ); 00263 } 00264 } 00265 00275 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) { 00276 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort; 00277 # Assume default for standard RC case 00278 $address = $address ? $address : $wgRC2UDPAddress; 00279 $prefix = $prefix ? $prefix : $wgRC2UDPPrefix; 00280 $port = $port ? $port : $wgRC2UDPPort; 00281 # Notify external application via UDP 00282 if( $address ) { 00283 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP ); 00284 if( $conn ) { 00285 $line = $prefix . $line; 00286 wfDebug( __METHOD__ . ": sending UDP line: $line\n" ); 00287 socket_sendto( $conn, $line, strlen($line), 0, $address, $port ); 00288 socket_close( $conn ); 00289 return true; 00290 } else { 00291 wfDebug( __METHOD__ . ": failed to create UDP socket\n" ); 00292 } 00293 } 00294 return false; 00295 } 00296 00302 public static function cleanupForIRC( $text ) { 00303 return Sanitizer::decodeCharReferences( str_replace( array( "\n", "\r" ), array( "", "" ), $text ) ); 00304 } 00305 00313 public static function markPatrolled( $change, $auto = false ) { 00314 global $wgUser; 00315 00316 $change = $change instanceof RecentChange 00317 ? $change 00318 : RecentChange::newFromId($change); 00319 00320 if( !$change instanceof RecentChange ) { 00321 return null; 00322 } 00323 return $change->doMarkPatrolled( $wgUser, $auto ); 00324 } 00325 00334 public function doMarkPatrolled( User $user, $auto = false ) { 00335 global $wgUseRCPatrol, $wgUseNPPatrol; 00336 $errors = array(); 00337 // If recentchanges patrol is disabled, only new pages 00338 // can be patrolled 00339 if( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute('rc_type') != RC_NEW ) ) { 00340 $errors[] = array('rcpatroldisabled'); 00341 } 00342 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol" 00343 $right = $auto ? 'autopatrol' : 'patrol'; 00344 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) ); 00345 if( !wfRunHooks('MarkPatrolled', array($this->getAttribute('rc_id'), &$user, false)) ) { 00346 $errors[] = array('hookaborted'); 00347 } 00348 // Users without the 'autopatrol' right can't patrol their 00349 // own revisions 00350 if( $user->getName() == $this->getAttribute('rc_user_text') && !$user->isAllowed('autopatrol') ) { 00351 $errors[] = array('markedaspatrollederror-noautopatrol'); 00352 } 00353 if( $errors ) { 00354 return $errors; 00355 } 00356 // If the change was patrolled already, do nothing 00357 if( $this->getAttribute('rc_patrolled') ) { 00358 return array(); 00359 } 00360 // Actually set the 'patrolled' flag in RC 00361 $this->reallyMarkPatrolled(); 00362 // Log this patrol event 00363 PatrolLog::record( $this, $auto, $user ); 00364 wfRunHooks( 'MarkPatrolledComplete', array($this->getAttribute('rc_id'), &$user, false) ); 00365 return array(); 00366 } 00367 00372 public function reallyMarkPatrolled() { 00373 $dbw = wfGetDB( DB_MASTER ); 00374 $dbw->update( 00375 'recentchanges', 00376 array( 00377 'rc_patrolled' => 1 00378 ), 00379 array( 00380 'rc_id' => $this->getAttribute('rc_id') 00381 ), 00382 __METHOD__ 00383 ); 00384 return $dbw->affectedRows(); 00385 } 00386 00405 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId, 00406 $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) { 00407 $rc = new RecentChange; 00408 $rc->mTitle = $title; 00409 $rc->mPerformer = $user; 00410 $rc->mAttribs = array( 00411 'rc_timestamp' => $timestamp, 00412 'rc_cur_time' => $timestamp, 00413 'rc_namespace' => $title->getNamespace(), 00414 'rc_title' => $title->getDBkey(), 00415 'rc_type' => RC_EDIT, 00416 'rc_minor' => $minor ? 1 : 0, 00417 'rc_cur_id' => $title->getArticleID(), 00418 'rc_user' => $user->getId(), 00419 'rc_user_text' => $user->getName(), 00420 'rc_comment' => $comment, 00421 'rc_this_oldid' => $newId, 00422 'rc_last_oldid' => $oldId, 00423 'rc_bot' => $bot ? 1 : 0, 00424 'rc_moved_to_ns' => 0, 00425 'rc_moved_to_title' => '', 00426 'rc_ip' => self::checkIPAddress( $ip ), 00427 'rc_patrolled' => intval($patrol), 00428 'rc_new' => 0, # obsolete 00429 'rc_old_len' => $oldSize, 00430 'rc_new_len' => $newSize, 00431 'rc_deleted' => 0, 00432 'rc_logid' => 0, 00433 'rc_log_type' => null, 00434 'rc_log_action' => '', 00435 'rc_params' => '' 00436 ); 00437 00438 $rc->mExtra = array( 00439 'prefixedDBkey' => $title->getPrefixedDBkey(), 00440 'lastTimestamp' => $lastTimestamp, 00441 'oldSize' => $oldSize, 00442 'newSize' => $newSize, 00443 ); 00444 $rc->save(); 00445 return $rc; 00446 } 00447 00465 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot, 00466 $ip='', $size=0, $newId=0, $patrol=0 ) { 00467 $rc = new RecentChange; 00468 $rc->mTitle = $title; 00469 $rc->mPerformer = $user; 00470 $rc->mAttribs = array( 00471 'rc_timestamp' => $timestamp, 00472 'rc_cur_time' => $timestamp, 00473 'rc_namespace' => $title->getNamespace(), 00474 'rc_title' => $title->getDBkey(), 00475 'rc_type' => RC_NEW, 00476 'rc_minor' => $minor ? 1 : 0, 00477 'rc_cur_id' => $title->getArticleID(), 00478 'rc_user' => $user->getId(), 00479 'rc_user_text' => $user->getName(), 00480 'rc_comment' => $comment, 00481 'rc_this_oldid' => $newId, 00482 'rc_last_oldid' => 0, 00483 'rc_bot' => $bot ? 1 : 0, 00484 'rc_moved_to_ns' => 0, 00485 'rc_moved_to_title' => '', 00486 'rc_ip' => self::checkIPAddress( $ip ), 00487 'rc_patrolled' => intval($patrol), 00488 'rc_new' => 1, # obsolete 00489 'rc_old_len' => 0, 00490 'rc_new_len' => $size, 00491 'rc_deleted' => 0, 00492 'rc_logid' => 0, 00493 'rc_log_type' => null, 00494 'rc_log_action' => '', 00495 'rc_params' => '' 00496 ); 00497 00498 $rc->mExtra = array( 00499 'prefixedDBkey' => $title->getPrefixedDBkey(), 00500 'lastTimestamp' => 0, 00501 'oldSize' => 0, 00502 'newSize' => $size 00503 ); 00504 $rc->save(); 00505 return $rc; 00506 } 00507 00523 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type, 00524 $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) 00525 { 00526 global $wgLogRestrictions; 00527 # Don't add private logs to RC! 00528 if( isset($wgLogRestrictions[$type]) && $wgLogRestrictions[$type] != '*' ) { 00529 return false; 00530 } 00531 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, 00532 $target, $logComment, $params, $newId, $actionCommentIRC ); 00533 $rc->save(); 00534 return true; 00535 } 00536 00552 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip, 00553 $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) { 00554 global $wgRequest; 00555 00556 $rc = new RecentChange; 00557 $rc->mTitle = $target; 00558 $rc->mPerformer = $user; 00559 $rc->mAttribs = array( 00560 'rc_timestamp' => $timestamp, 00561 'rc_cur_time' => $timestamp, 00562 'rc_namespace' => $target->getNamespace(), 00563 'rc_title' => $target->getDBkey(), 00564 'rc_type' => RC_LOG, 00565 'rc_minor' => 0, 00566 'rc_cur_id' => $target->getArticleID(), 00567 'rc_user' => $user->getId(), 00568 'rc_user_text' => $user->getName(), 00569 'rc_comment' => $logComment, 00570 'rc_this_oldid' => 0, 00571 'rc_last_oldid' => 0, 00572 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0, 00573 'rc_moved_to_ns' => 0, 00574 'rc_moved_to_title' => '', 00575 'rc_ip' => self::checkIPAddress( $ip ), 00576 'rc_patrolled' => 1, 00577 'rc_new' => 0, # obsolete 00578 'rc_old_len' => null, 00579 'rc_new_len' => null, 00580 'rc_deleted' => 0, 00581 'rc_logid' => $newId, 00582 'rc_log_type' => $type, 00583 'rc_log_action' => $action, 00584 'rc_params' => $params 00585 ); 00586 00587 $rc->mExtra = array( 00588 'prefixedDBkey' => $title->getPrefixedDBkey(), 00589 'lastTimestamp' => 0, 00590 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage 00591 'actionCommentIRC' => $actionCommentIRC 00592 ); 00593 return $rc; 00594 } 00595 00601 public function loadFromRow( $row ) { 00602 $this->mAttribs = get_object_vars( $row ); 00603 $this->mAttribs['rc_timestamp'] = wfTimestamp(TS_MW, $this->mAttribs['rc_timestamp']); 00604 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set 00605 } 00606 00612 public function loadFromCurRow( $row ) { 00613 $this->mAttribs = array( 00614 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp), 00615 'rc_cur_time' => $row->rev_timestamp, 00616 'rc_user' => $row->rev_user, 00617 'rc_user_text' => $row->rev_user_text, 00618 'rc_namespace' => $row->page_namespace, 00619 'rc_title' => $row->page_title, 00620 'rc_comment' => $row->rev_comment, 00621 'rc_minor' => $row->rev_minor_edit ? 1 : 0, 00622 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT, 00623 'rc_cur_id' => $row->page_id, 00624 'rc_this_oldid' => $row->rev_id, 00625 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0, 00626 'rc_bot' => 0, 00627 'rc_moved_to_ns' => 0, 00628 'rc_moved_to_title' => '', 00629 'rc_ip' => '', 00630 'rc_id' => $row->rc_id, 00631 'rc_patrolled' => $row->rc_patrolled, 00632 'rc_new' => $row->page_is_new, # obsolete 00633 'rc_old_len' => $row->rc_old_len, 00634 'rc_new_len' => $row->rc_new_len, 00635 'rc_params' => isset($row->rc_params) ? $row->rc_params : '', 00636 'rc_log_type' => isset($row->rc_log_type) ? $row->rc_log_type : null, 00637 'rc_log_action' => isset($row->rc_log_action) ? $row->rc_log_action : null, 00638 'rc_log_id' => isset($row->rc_log_id) ? $row->rc_log_id: 0, 00639 'rc_deleted' => $row->rc_deleted // MUST be set 00640 ); 00641 } 00642 00649 public function getAttribute( $name ) { 00650 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null; 00651 } 00652 00656 public function getAttributes() { 00657 return $this->mAttribs; 00658 } 00659 00666 public function diffLinkTrail( $forceCur ) { 00667 if( $this->mAttribs['rc_type'] == RC_EDIT ) { 00668 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) . 00669 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']); 00670 if( $forceCur ) { 00671 $trail .= '&diff=0' ; 00672 } else { 00673 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']); 00674 } 00675 } else { 00676 $trail = ''; 00677 } 00678 return $trail; 00679 } 00680 00684 public function getIRCLine() { 00685 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki, 00686 $wgCanonicalServer, $wgScript; 00687 00688 if( $this->mAttribs['rc_type'] == RC_LOG ) { 00689 // Don't use SpecialPage::getTitleFor, backwards compatibility with 00690 // IRC API which expects "Log". 00691 $titleObj = Title::newFromText( 'Log/' . $this->mAttribs['rc_log_type'], NS_SPECIAL ); 00692 } else { 00693 $titleObj =& $this->getTitle(); 00694 } 00695 $title = $titleObj->getPrefixedText(); 00696 $title = self::cleanupForIRC( $title ); 00697 00698 if( $this->mAttribs['rc_type'] == RC_LOG ) { 00699 $url = ''; 00700 } else { 00701 $url = $wgCanonicalServer . $wgScript; 00702 if( $this->mAttribs['rc_type'] == RC_NEW ) { 00703 $query = '?oldid=' . $this->mAttribs['rc_this_oldid']; 00704 } else { 00705 $query = '?diff=' . $this->mAttribs['rc_this_oldid'] . '&oldid=' . $this->mAttribs['rc_last_oldid']; 00706 } 00707 if ( $wgUseRCPatrol || ( $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) { 00708 $query .= '&rcid=' . $this->mAttribs['rc_id']; 00709 } 00710 // HACK: We need this hook for WMF's secure server setup 00711 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) ); 00712 $url .= $query; 00713 } 00714 00715 if( $this->mAttribs['rc_old_len'] !== null && $this->mAttribs['rc_new_len'] !== null ) { 00716 $szdiff = $this->mAttribs['rc_new_len'] - $this->mAttribs['rc_old_len']; 00717 if($szdiff < -500) { 00718 $szdiff = "\002$szdiff\002"; 00719 } elseif($szdiff >= 0) { 00720 $szdiff = '+' . $szdiff ; 00721 } 00722 // @todo i18n with parentheses in content language? 00723 $szdiff = '(' . $szdiff . ')' ; 00724 } else { 00725 $szdiff = ''; 00726 } 00727 00728 $user = self::cleanupForIRC( $this->mAttribs['rc_user_text'] ); 00729 00730 if ( $this->mAttribs['rc_type'] == RC_LOG ) { 00731 $targetText = $this->getTitle()->getPrefixedText(); 00732 $comment = self::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra['actionCommentIRC'] ) ); 00733 $flag = $this->mAttribs['rc_log_action']; 00734 } else { 00735 $comment = self::cleanupForIRC( $this->mAttribs['rc_comment'] ); 00736 $flag = ''; 00737 if ( !$this->mAttribs['rc_patrolled'] && ( $wgUseRCPatrol || $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) { 00738 $flag .= '!'; 00739 } 00740 $flag .= ( $this->mAttribs['rc_type'] == RC_NEW ? "N" : "" ) . ( $this->mAttribs['rc_minor'] ? "M" : "" ) . ( $this->mAttribs['rc_bot'] ? "B" : "" ); 00741 } 00742 00743 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) { 00744 $prefix = $wgLocalInterwiki; 00745 } elseif ( $wgRC2UDPInterwikiPrefix ) { 00746 $prefix = $wgRC2UDPInterwikiPrefix; 00747 } else { 00748 $prefix = false; 00749 } 00750 if ( $prefix !== false ) { 00751 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]"; 00752 } else { 00753 $titleString = "\00314[[\00307$title\00314]]"; 00754 } 00755 00756 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003, 00757 # no colour (\003) switches back to the term default 00758 $fullString = "$titleString\0034 $flag\00310 " . 00759 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n"; 00760 00761 return $fullString; 00762 } 00763 00771 public function getCharacterDifference( $old = 0, $new = 0 ) { 00772 if( $old === 0 ) { 00773 $old = $this->mAttribs['rc_old_len']; 00774 } 00775 if( $new === 0 ) { 00776 $new = $this->mAttribs['rc_new_len']; 00777 } 00778 if( $old === null || $new === null ) { 00779 return ''; 00780 } 00781 return ChangesList::showCharacterDifference( $old, $new ); 00782 } 00783 00784 private static function checkIPAddress( $ip ) { 00785 global $wgRequest; 00786 if ( $ip ) { 00787 if ( !IP::isIPAddress( $ip ) ) { 00788 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" ); 00789 } 00790 } else { 00791 $ip = $wgRequest->getIP(); 00792 if( !$ip ) 00793 $ip = ''; 00794 } 00795 return $ip; 00796 } 00797 }