MediaWiki
REL1_22
|
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 $relations = array(); 00338 protected $performer; 00339 protected $target; 00340 protected $timestamp; 00341 protected $comment = ''; 00342 protected $deleted; 00343 00352 public function __construct( $type, $subtype ) { 00353 $this->type = $type; 00354 $this->subtype = $subtype; 00355 } 00356 00373 public function setParameters( $parameters ) { 00374 $this->parameters = $parameters; 00375 } 00376 00384 public function setRelations( array $relations ) { 00385 $this->relations = $relations; 00386 } 00387 00395 public function setPerformer( User $performer ) { 00396 $this->performer = $performer; 00397 } 00398 00406 public function setTarget( Title $target ) { 00407 $this->target = $target; 00408 } 00409 00417 public function setTimestamp( $timestamp ) { 00418 $this->timestamp = $timestamp; 00419 } 00420 00428 public function setComment( $comment ) { 00429 $this->comment = $comment; 00430 } 00431 00439 public function setDeleted( $deleted ) { 00440 $this->deleted = $deleted; 00441 } 00442 00448 public function insert( IDatabase $dbw = null ) { 00449 global $wgContLang; 00450 00451 $dbw = $dbw ?: wfGetDB( DB_MASTER ); 00452 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' ); 00453 00454 if ( $this->timestamp === null ) { 00455 $this->timestamp = wfTimestampNow(); 00456 } 00457 00458 # Trim spaces on user supplied text 00459 $comment = trim( $this->getComment() ); 00460 00461 # Truncate for whole multibyte characters. 00462 $comment = $wgContLang->truncate( $comment, 255 ); 00463 00464 $data = array( 00465 'log_id' => $id, 00466 'log_type' => $this->getType(), 00467 'log_action' => $this->getSubtype(), 00468 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ), 00469 'log_user' => $this->getPerformer()->getId(), 00470 'log_user_text' => $this->getPerformer()->getName(), 00471 'log_namespace' => $this->getTarget()->getNamespace(), 00472 'log_title' => $this->getTarget()->getDBkey(), 00473 'log_page' => $this->getTarget()->getArticleID(), 00474 'log_comment' => $comment, 00475 'log_params' => serialize( (array)$this->getParameters() ), 00476 ); 00477 $dbw->insert( 'logging', $data, __METHOD__ ); 00478 $this->id = !is_null( $id ) ? $id : $dbw->insertId(); 00479 00480 $rows = array(); 00481 foreach ( $this->relations as $tag => $values ) { 00482 if ( !strlen( $tag ) ) { 00483 throw new MWException( "Got empty log search tag." ); 00484 } 00485 foreach ( $values as $value ) { 00486 $rows[] = array( 00487 'ls_field' => $tag, 00488 'ls_value' => $value, 00489 'ls_log_id' => $this->id 00490 ); 00491 } 00492 } 00493 if ( count( $rows ) ) { 00494 $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' ); 00495 } 00496 00497 return $this->id; 00498 } 00499 00505 public function publish( $newId, $to = 'rcandudp' ) { 00506 $log = new LogPage( $this->getType() ); 00507 if ( $log->isRestricted() ) { 00508 return; 00509 } 00510 00511 $formatter = LogFormatter::newFromEntry( $this ); 00512 $context = RequestContext::newExtraneousContext( $this->getTarget() ); 00513 $formatter->setContext( $context ); 00514 00515 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() ); 00516 $user = $this->getPerformer(); 00517 $ip = ""; 00518 if ( $user->isAnon() ) { 00519 /* 00520 * "MediaWiki default" and friends may have 00521 * no IP address in their name 00522 */ 00523 if ( IP::isIPAddress( $user->getName() ) ) { 00524 $ip = $user->getName(); 00525 } 00526 } 00527 $rc = RecentChange::newLogEntry( 00528 $this->getTimestamp(), 00529 $logpage, 00530 $user, 00531 $formatter->getPlainActionText(), 00532 $ip, 00533 $this->getType(), 00534 $this->getSubtype(), 00535 $this->getTarget(), 00536 $this->getComment(), 00537 serialize( (array)$this->getParameters() ), 00538 $newId, 00539 $formatter->getIRCActionComment() // Used for IRC feeds 00540 ); 00541 00542 if ( $to === 'rc' || $to === 'rcandudp' ) { 00543 $rc->save( 'pleasedontudp' ); 00544 } 00545 00546 if ( $to === 'udp' || $to === 'rcandudp' ) { 00547 $rc->notifyRCFeeds(); 00548 } 00549 } 00550 00551 // LogEntry-> 00552 00553 public function getType() { 00554 return $this->type; 00555 } 00556 00557 public function getSubtype() { 00558 return $this->subtype; 00559 } 00560 00561 public function getParameters() { 00562 return $this->parameters; 00563 } 00564 00568 public function getPerformer() { 00569 return $this->performer; 00570 } 00571 00575 public function getTarget() { 00576 return $this->target; 00577 } 00578 00579 public function getTimestamp() { 00580 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow(); 00581 return wfTimestamp( TS_MW, $ts ); 00582 } 00583 00584 public function getComment() { 00585 return $this->comment; 00586 } 00587 00588 public function getDeleted() { 00589 return (int)$this->deleted; 00590 } 00591 00592 }