MediaWiki  REL1_19
LogEntry.php
Go to the documentation of this file.
00001 <?php
00020 interface LogEntry {
00021 
00026         public function getType();
00027 
00032         public function getSubtype();
00033 
00038         public function getFullType();
00039 
00044         public function getParameters();
00045 
00050         public function getPerformer();
00051 
00056         public function getTarget();
00057 
00062         public function getTimestamp();
00063 
00068         public function getComment();
00069 
00074         public function getDeleted();
00075 
00080         public function isDeleted( $field );
00081 }
00082 
00087 abstract class LogEntryBase implements LogEntry {
00088 
00089         public function getFullType() {
00090                 return $this->getType() . '/' . $this->getSubtype();
00091         }
00092 
00093         public function isDeleted( $field ) {
00094                 return ( $this->getDeleted() & $field ) === $field;
00095         }
00096 
00101         public function isLegacy() {
00102                 return false;
00103         }
00104 
00105 }
00106 
00111 class DatabaseLogEntry extends LogEntryBase {
00112         // Static->
00113 
00120         public static function getSelectQueryData() {
00121                 $tables = array( 'logging', 'user' );
00122                 $fields = array(
00123                         'log_id', 'log_type', 'log_action', 'log_timestamp',
00124                         'log_user', 'log_user_text',
00125                         'log_namespace', 'log_title', // unused log_page
00126                         'log_comment', 'log_params', 'log_deleted',
00127                         'user_id', 'user_name', 'user_editcount',
00128                 );
00129 
00130                 $joins = array(
00131                         // IP's don't have an entry in user table
00132                         'user' => array( 'LEFT JOIN', 'log_user=user_id' ),
00133                 );
00134 
00135                 return array(
00136                         'tables' => $tables,
00137                         'fields' => $fields,
00138                         'conds'  => array(),
00139                         'options' => array(),
00140                         'join_conds' => $joins,
00141                 );
00142         }
00143 
00150         public static function newFromRow( $row ) {
00151                 if ( is_array( $row ) && isset( $row['rc_logid'] ) ) {
00152                         return new RCDatabaseLogEntry( (object) $row );
00153                 } else {
00154                         return new self( $row );
00155                 }
00156         }
00157 
00158         // Non-static->
00159 
00161         protected $row;
00162 
00163         protected function __construct( $row ) {
00164                 $this->row = $row;
00165         }
00166 
00171         public function getId() {
00172                 return (int)$this->row->log_id;
00173         }
00174 
00179         protected function getRawParameters() {
00180                 return $this->row->log_params;
00181         }
00182 
00183         // LogEntryBase->
00184 
00185         public function isLegacy() {
00186                 // This does the check
00187                 $this->getParameters();
00188                 return $this->legacy;
00189         }
00190 
00191         // LogEntry->
00192 
00193         public function getType() {
00194                 return $this->row->log_type;
00195         }
00196 
00197         public function getSubtype() {
00198                 return $this->row->log_action;
00199         }
00200 
00201         public function getParameters() {
00202                 if ( !isset( $this->params ) ) {
00203                         $blob = $this->getRawParameters();
00204                         wfSuppressWarnings();
00205                         $params = unserialize( $blob );
00206                         wfRestoreWarnings();
00207                         if ( $params !== false ) {
00208                                 $this->params = $params;
00209                                 $this->legacy = false;
00210                         } else {
00211                                 $this->params = $blob === '' ? array() : explode( "\n", $blob );
00212                                 $this->legacy = true;
00213                         }
00214                 }
00215                 return $this->params;
00216         }
00217 
00218         public function getPerformer() {
00219                 $userId = (int) $this->row->log_user;
00220                 if ( $userId !== 0 ) { // logged-in users
00221                         if ( isset( $this->row->user_name ) ) {
00222                                 return User::newFromRow( $this->row );
00223                         } else {
00224                                 return User::newFromId( $userId );
00225                         }
00226                 } else { // IP users
00227                         $userText = $this->row->log_user_text;
00228                         return User::newFromName( $userText, false );
00229                 }
00230         }
00231 
00232         public function getTarget() {
00233                 $namespace = $this->row->log_namespace;
00234                 $page = $this->row->log_title;
00235                 $title = Title::makeTitle( $namespace, $page );
00236                 return $title;
00237         }
00238 
00239         public function getTimestamp() {
00240                 return wfTimestamp( TS_MW, $this->row->log_timestamp );
00241         }
00242 
00243         public function getComment() {
00244                 return $this->row->log_comment;
00245         }
00246 
00247         public function getDeleted() {
00248                 return $this->row->log_deleted;
00249         }
00250 
00251 }
00252 
00253 class RCDatabaseLogEntry extends DatabaseLogEntry {
00254 
00255         public function getId() {
00256                 return $this->row->rc_logid;
00257         }
00258 
00259         protected function getRawParameters() {
00260                 return $this->row->rc_params;
00261         }
00262 
00263         // LogEntry->
00264 
00265         public function getType() {
00266                 return $this->row->rc_log_type;
00267         }
00268 
00269         public function getSubtype() {
00270                 return $this->row->rc_log_action;
00271         }
00272 
00273         public function getPerformer() {
00274                 $userId = (int) $this->row->rc_user;
00275                 if ( $userId !== 0 ) {
00276                         return User::newFromId( $userId );
00277                 } else {
00278                         $userText = $this->row->rc_user_text;
00279                         // Might be an IP, don't validate the username
00280                         return User::newFromName( $userText, false );
00281                 }
00282         }
00283 
00284         public function getTarget() {
00285                 $namespace = $this->row->rc_namespace;
00286                 $page = $this->row->rc_title;
00287                 $title = Title::makeTitle( $namespace, $page );
00288                 return $title;
00289         }
00290 
00291         public function getTimestamp() {
00292                 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
00293         }
00294 
00295         public function getComment() {
00296                 return $this->row->rc_comment;
00297         }
00298 
00299         public function getDeleted() {
00300                 return $this->row->rc_deleted;
00301         }
00302 
00303 }
00304 
00310 class ManualLogEntry extends LogEntryBase {
00311         protected $type; 
00312         protected $subtype; 
00313         protected $parameters = array(); 
00314         protected $performer; 
00315         protected $target; 
00316         protected $timestamp; 
00317         protected $comment = ''; 
00318         protected $deleted; 
00319 
00328         public function __construct( $type, $subtype ) {
00329                 $this->type = $type;
00330                 $this->subtype = $subtype;
00331         }
00332 
00349         public function setParameters( $parameters ) {
00350                 $this->parameters = $parameters;
00351         }
00352 
00360         public function setPerformer( User $performer ) {
00361                 $this->performer = $performer;
00362         }
00363 
00371         public function setTarget( Title $target ) {
00372                 $this->target = $target;
00373         }
00374 
00382         public function setTimestamp( $timestamp ) {
00383                 $this->timestamp = $timestamp;
00384         }
00385 
00393         public function setComment( $comment ) {
00394                 $this->comment = $comment;
00395         }
00396 
00404         public function setDeleted( $deleted ) {
00405                 $this->deleted = $deleted;
00406         }
00407 
00412         public function insert() {
00413                 global $wgContLang;
00414 
00415                 $dbw = wfGetDB( DB_MASTER );
00416                 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
00417 
00418                 if ( $this->timestamp === null ) {
00419                         $this->timestamp = wfTimestampNow();
00420                 }
00421 
00422                 # Truncate for whole multibyte characters.
00423                 $comment = $wgContLang->truncate( $this->getComment(), 255 );
00424 
00425                 $data = array(
00426                         'log_id' => $id,
00427                         'log_type' => $this->getType(),
00428                         'log_action' => $this->getSubtype(),
00429                         'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
00430                         'log_user' => $this->getPerformer()->getId(),
00431                         'log_user_text' => $this->getPerformer()->getName(),
00432                         'log_namespace' => $this->getTarget()->getNamespace(),
00433                         'log_title' => $this->getTarget()->getDBkey(),
00434                         'log_page' => $this->getTarget()->getArticleId(),
00435                         'log_comment' => $comment,
00436                         'log_params' => serialize( (array) $this->getParameters() ),
00437                 );
00438                 $dbw->insert( 'logging', $data, __METHOD__ );
00439                 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
00440                 return $this->id;
00441         }
00442 
00448         public function publish( $newId, $to = 'rcandudp' ) {
00449                 $log = new LogPage( $this->getType() );
00450                 if ( $log->isRestricted() ) {
00451                         return;
00452                 }
00453 
00454                 $formatter = LogFormatter::newFromEntry( $this );
00455                 $context = RequestContext::newExtraneousContext( $this->getTarget() );
00456                 $formatter->setContext( $context );
00457 
00458                 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
00459                 $user = $this->getPerformer();
00460                 $rc = RecentChange::newLogEntry(
00461                         $this->getTimestamp(),
00462                         $logpage,
00463                         $user,
00464                         $formatter->getIRCActionText(), // Used for IRC feeds
00465                         $user->isAnon() ? $user->getName() : '',
00466                         $this->getType(),
00467                         $this->getSubtype(),
00468                         $this->getTarget(),
00469                         $this->getComment(),
00470                         serialize( (array) $this->getParameters() ),
00471                         $newId
00472                 );
00473 
00474                 if ( $to === 'rc' || $to === 'rcandudp' ) {
00475                         $rc->save( 'pleasedontudp' );
00476                 }
00477 
00478                 if ( $to === 'udp' || $to === 'rcandudp' ) {
00479                         $rc->notifyRC2UDP();
00480                 }
00481         }
00482 
00483         // LogEntry->
00484 
00485         public function getType() {
00486                 return $this->type;
00487         }
00488 
00489         public function getSubtype() {
00490                 return $this->subtype;
00491         }
00492 
00493         public function getParameters() {
00494                 return $this->parameters;
00495         }
00496 
00497         public function getPerformer() {
00498                 return $this->performer;
00499         }
00500 
00501         public function getTarget() {
00502                 return $this->target;
00503         }
00504 
00505         public function getTimestamp() {
00506                 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
00507                 return wfTimestamp( TS_MW, $ts );
00508         }
00509 
00510         public function getComment() {
00511                 return $this->comment;
00512         }
00513 
00514         public function getDeleted() {
00515                 return (int) $this->deleted;
00516         }
00517 
00518 }