MediaWiki
master
|
00001 <?php 00035 interface LogEntry { 00036 00041 public function getType(); 00042 00047 public function getSubtype(); 00048 00053 public function getFullType(); 00054 00059 public function getParameters(); 00060 00065 public function getPerformer(); 00066 00071 public function getTarget(); 00072 00077 public function getTimestamp(); 00078 00083 public function getComment(); 00084 00089 public function getDeleted(); 00090 00095 public function isDeleted( $field ); 00096 } 00097 00102 abstract class LogEntryBase implements LogEntry { 00103 00104 public function getFullType() { 00105 return $this->getType() . '/' . $this->getSubtype(); 00106 } 00107 00108 public function isDeleted( $field ) { 00109 return ( $this->getDeleted() & $field ) === $field; 00110 } 00111 00117 public function isLegacy() { 00118 return false; 00119 } 00120 00121 } 00122 00127 class DatabaseLogEntry extends LogEntryBase { 00128 // Static-> 00129 00136 public static function getSelectQueryData() { 00137 $tables = array( 'logging', 'user' ); 00138 $fields = array( 00139 'log_id', 'log_type', 'log_action', 'log_timestamp', 00140 'log_user', 'log_user_text', 00141 'log_namespace', 'log_title', // unused log_page 00142 'log_comment', 'log_params', 'log_deleted', 00143 'user_id', 'user_name', 'user_editcount', 00144 ); 00145 00146 $joins = array( 00147 // IP's don't have an entry in user table 00148 'user' => array( 'LEFT JOIN', 'log_user=user_id' ), 00149 ); 00150 00151 return array( 00152 'tables' => $tables, 00153 'fields' => $fields, 00154 'conds' => array(), 00155 'options' => array(), 00156 'join_conds' => $joins, 00157 ); 00158 } 00159 00166 public static function newFromRow( $row ) { 00167 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) { 00168 return new RCDatabaseLogEntry( (object) $row ); 00169 } else { 00170 return new self( $row ); 00171 } 00172 } 00173 00174 // Non-static-> 00175 00177 protected $row; 00178 protected $performer; 00179 00180 protected function __construct( $row ) { 00181 $this->row = $row; 00182 } 00183 00188 public function getId() { 00189 return (int)$this->row->log_id; 00190 } 00191 00196 protected function getRawParameters() { 00197 return $this->row->log_params; 00198 } 00199 00200 // LogEntryBase-> 00201 00202 public function isLegacy() { 00203 // This does the check 00204 $this->getParameters(); 00205 return $this->legacy; 00206 } 00207 00208 // LogEntry-> 00209 00210 public function getType() { 00211 return $this->row->log_type; 00212 } 00213 00214 public function getSubtype() { 00215 return $this->row->log_action; 00216 } 00217 00218 public function getParameters() { 00219 if ( !isset( $this->params ) ) { 00220 $blob = $this->getRawParameters(); 00221 wfSuppressWarnings(); 00222 $params = unserialize( $blob ); 00223 wfRestoreWarnings(); 00224 if ( $params !== false ) { 00225 $this->params = $params; 00226 $this->legacy = false; 00227 } else { 00228 $this->params = $blob === '' ? array() : explode( "\n", $blob ); 00229 $this->legacy = true; 00230 } 00231 } 00232 return $this->params; 00233 } 00234 00235 public function getPerformer() { 00236 if( !$this->performer ) { 00237 $userId = (int) $this->row->log_user; 00238 if ( $userId !== 0 ) { // logged-in users 00239 if ( isset( $this->row->user_name ) ) { 00240 $this->performer = User::newFromRow( $this->row ); 00241 } else { 00242 $this->performer = User::newFromId( $userId ); 00243 } 00244 } else { // IP users 00245 $userText = $this->row->log_user_text; 00246 $this->performer = User::newFromName( $userText, false ); 00247 } 00248 } 00249 return $this->performer; 00250 } 00251 00252 public function getTarget() { 00253 $namespace = $this->row->log_namespace; 00254 $page = $this->row->log_title; 00255 $title = Title::makeTitle( $namespace, $page ); 00256 return $title; 00257 } 00258 00259 public function getTimestamp() { 00260 return wfTimestamp( TS_MW, $this->row->log_timestamp ); 00261 } 00262 00263 public function getComment() { 00264 return $this->row->log_comment; 00265 } 00266 00267 public function getDeleted() { 00268 return $this->row->log_deleted; 00269 } 00270 00271 } 00272 00273 class RCDatabaseLogEntry extends DatabaseLogEntry { 00274 00275 public function getId() { 00276 return $this->row->rc_logid; 00277 } 00278 00279 protected function getRawParameters() { 00280 return $this->row->rc_params; 00281 } 00282 00283 // LogEntry-> 00284 00285 public function getType() { 00286 return $this->row->rc_log_type; 00287 } 00288 00289 public function getSubtype() { 00290 return $this->row->rc_log_action; 00291 } 00292 00293 public function getPerformer() { 00294 if( !$this->performer ) { 00295 $userId = (int) $this->row->rc_user; 00296 if ( $userId !== 0 ) { 00297 $this->performer = User::newFromId( $userId ); 00298 } else { 00299 $userText = $this->row->rc_user_text; 00300 // Might be an IP, don't validate the username 00301 $this->performer = User::newFromName( $userText, false ); 00302 } 00303 } 00304 return $this->performer; 00305 } 00306 00307 public function getTarget() { 00308 $namespace = $this->row->rc_namespace; 00309 $page = $this->row->rc_title; 00310 $title = Title::makeTitle( $namespace, $page ); 00311 return $title; 00312 } 00313 00314 public function getTimestamp() { 00315 return wfTimestamp( TS_MW, $this->row->rc_timestamp ); 00316 } 00317 00318 public function getComment() { 00319 return $this->row->rc_comment; 00320 } 00321 00322 public function getDeleted() { 00323 return $this->row->rc_deleted; 00324 } 00325 00326 } 00327 00333 class ManualLogEntry extends LogEntryBase { 00334 protected $type; 00335 protected $subtype; 00336 protected $parameters = array(); 00337 protected $performer; 00338 protected $target; 00339 protected $timestamp; 00340 protected $comment = ''; 00341 protected $deleted; 00342 00351 public function __construct( $type, $subtype ) { 00352 $this->type = $type; 00353 $this->subtype = $subtype; 00354 } 00355 00372 public function setParameters( $parameters ) { 00373 $this->parameters = $parameters; 00374 } 00375 00383 public function setPerformer( User $performer ) { 00384 $this->performer = $performer; 00385 } 00386 00394 public function setTarget( Title $target ) { 00395 $this->target = $target; 00396 } 00397 00405 public function setTimestamp( $timestamp ) { 00406 $this->timestamp = $timestamp; 00407 } 00408 00416 public function setComment( $comment ) { 00417 $this->comment = $comment; 00418 } 00419 00427 public function setDeleted( $deleted ) { 00428 $this->deleted = $deleted; 00429 } 00430 00435 public function insert() { 00436 global $wgContLang; 00437 00438 $dbw = wfGetDB( DB_MASTER ); 00439 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' ); 00440 00441 if ( $this->timestamp === null ) { 00442 $this->timestamp = wfTimestampNow(); 00443 } 00444 00445 # Truncate for whole multibyte characters. 00446 $comment = $wgContLang->truncate( $this->getComment(), 255 ); 00447 00448 $data = array( 00449 'log_id' => $id, 00450 'log_type' => $this->getType(), 00451 'log_action' => $this->getSubtype(), 00452 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ), 00453 'log_user' => $this->getPerformer()->getId(), 00454 'log_user_text' => $this->getPerformer()->getName(), 00455 'log_namespace' => $this->getTarget()->getNamespace(), 00456 'log_title' => $this->getTarget()->getDBkey(), 00457 'log_page' => $this->getTarget()->getArticleID(), 00458 'log_comment' => $comment, 00459 'log_params' => serialize( (array) $this->getParameters() ), 00460 ); 00461 $dbw->insert( 'logging', $data, __METHOD__ ); 00462 $this->id = !is_null( $id ) ? $id : $dbw->insertId(); 00463 return $this->id; 00464 } 00465 00471 public function publish( $newId, $to = 'rcandudp' ) { 00472 $log = new LogPage( $this->getType() ); 00473 if ( $log->isRestricted() ) { 00474 return; 00475 } 00476 00477 $formatter = LogFormatter::newFromEntry( $this ); 00478 $context = RequestContext::newExtraneousContext( $this->getTarget() ); 00479 $formatter->setContext( $context ); 00480 00481 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() ); 00482 $user = $this->getPerformer(); 00483 $ip = ""; 00484 if ( $user->isAnon() ) { 00485 /* 00486 * "MediaWiki default" and friends may have 00487 * no IP address in their name 00488 */ 00489 if ( IP::isIPAddress( $user->getName() ) ) { 00490 $ip = $user->getName(); 00491 } 00492 } 00493 $rc = RecentChange::newLogEntry( 00494 $this->getTimestamp(), 00495 $logpage, 00496 $user, 00497 $formatter->getPlainActionText(), 00498 $ip, 00499 $this->getType(), 00500 $this->getSubtype(), 00501 $this->getTarget(), 00502 $this->getComment(), 00503 serialize( (array) $this->getParameters() ), 00504 $newId, 00505 $formatter->getIRCActionComment() // Used for IRC feeds 00506 ); 00507 00508 if ( $to === 'rc' || $to === 'rcandudp' ) { 00509 $rc->save( 'pleasedontudp' ); 00510 } 00511 00512 if ( $to === 'udp' || $to === 'rcandudp' ) { 00513 $rc->notifyRC2UDP(); 00514 } 00515 } 00516 00517 // LogEntry-> 00518 00519 public function getType() { 00520 return $this->type; 00521 } 00522 00523 public function getSubtype() { 00524 return $this->subtype; 00525 } 00526 00527 public function getParameters() { 00528 return $this->parameters; 00529 } 00530 00534 public function getPerformer() { 00535 return $this->performer; 00536 } 00537 00541 public function getTarget() { 00542 return $this->target; 00543 } 00544 00545 public function getTimestamp() { 00546 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow(); 00547 return wfTimestamp( TS_MW, $ts ); 00548 } 00549 00550 public function getComment() { 00551 return $this->comment; 00552 } 00553 00554 public function getDeleted() { 00555 return (int) $this->deleted; 00556 } 00557 00558 }