MediaWiki  REL1_20
LogEntry.php
Go to the documentation of this file.
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 
00179         protected function __construct( $row ) {
00180                 $this->row = $row;
00181         }
00182 
00187         public function getId() {
00188                 return (int)$this->row->log_id;
00189         }
00190 
00195         protected function getRawParameters() {
00196                 return $this->row->log_params;
00197         }
00198 
00199         // LogEntryBase->
00200 
00201         public function isLegacy() {
00202                 // This does the check
00203                 $this->getParameters();
00204                 return $this->legacy;
00205         }
00206 
00207         // LogEntry->
00208 
00209         public function getType() {
00210                 return $this->row->log_type;
00211         }
00212 
00213         public function getSubtype() {
00214                 return $this->row->log_action;
00215         }
00216 
00217         public function getParameters() {
00218                 if ( !isset( $this->params ) ) {
00219                         $blob = $this->getRawParameters();
00220                         wfSuppressWarnings();
00221                         $params = unserialize( $blob );
00222                         wfRestoreWarnings();
00223                         if ( $params !== false ) {
00224                                 $this->params = $params;
00225                                 $this->legacy = false;
00226                         } else {
00227                                 $this->params = $blob === '' ? array() : explode( "\n", $blob );
00228                                 $this->legacy = true;
00229                         }
00230                 }
00231                 return $this->params;
00232         }
00233 
00234         public function getPerformer() {
00235                 $userId = (int) $this->row->log_user;
00236                 if ( $userId !== 0 ) { // logged-in users
00237                         if ( isset( $this->row->user_name ) ) {
00238                                 return User::newFromRow( $this->row );
00239                         } else {
00240                                 return User::newFromId( $userId );
00241                         }
00242                 } else { // IP users
00243                         $userText = $this->row->log_user_text;
00244                         return User::newFromName( $userText, false );
00245                 }
00246         }
00247 
00248         public function getTarget() {
00249                 $namespace = $this->row->log_namespace;
00250                 $page = $this->row->log_title;
00251                 $title = Title::makeTitle( $namespace, $page );
00252                 return $title;
00253         }
00254 
00255         public function getTimestamp() {
00256                 return wfTimestamp( TS_MW, $this->row->log_timestamp );
00257         }
00258 
00259         public function getComment() {
00260                 return $this->row->log_comment;
00261         }
00262 
00263         public function getDeleted() {
00264                 return $this->row->log_deleted;
00265         }
00266 
00267 }
00268 
00269 class RCDatabaseLogEntry extends DatabaseLogEntry {
00270 
00271         public function getId() {
00272                 return $this->row->rc_logid;
00273         }
00274 
00275         protected function getRawParameters() {
00276                 return $this->row->rc_params;
00277         }
00278 
00279         // LogEntry->
00280 
00281         public function getType() {
00282                 return $this->row->rc_log_type;
00283         }
00284 
00285         public function getSubtype() {
00286                 return $this->row->rc_log_action;
00287         }
00288 
00289         public function getPerformer() {
00290                 $userId = (int) $this->row->rc_user;
00291                 if ( $userId !== 0 ) {
00292                         return User::newFromId( $userId );
00293                 } else {
00294                         $userText = $this->row->rc_user_text;
00295                         // Might be an IP, don't validate the username
00296                         return User::newFromName( $userText, false );
00297                 }
00298         }
00299 
00300         public function getTarget() {
00301                 $namespace = $this->row->rc_namespace;
00302                 $page = $this->row->rc_title;
00303                 $title = Title::makeTitle( $namespace, $page );
00304                 return $title;
00305         }
00306 
00307         public function getTimestamp() {
00308                 return wfTimestamp( TS_MW, $this->row->rc_timestamp );
00309         }
00310 
00311         public function getComment() {
00312                 return $this->row->rc_comment;
00313         }
00314 
00315         public function getDeleted() {
00316                 return $this->row->rc_deleted;
00317         }
00318 
00319 }
00320 
00326 class ManualLogEntry extends LogEntryBase {
00327         protected $type; 
00328         protected $subtype; 
00329         protected $parameters = array(); 
00330         protected $performer; 
00331         protected $target; 
00332         protected $timestamp; 
00333         protected $comment = ''; 
00334         protected $deleted; 
00335 
00344         public function __construct( $type, $subtype ) {
00345                 $this->type = $type;
00346                 $this->subtype = $subtype;
00347         }
00348 
00365         public function setParameters( $parameters ) {
00366                 $this->parameters = $parameters;
00367         }
00368 
00376         public function setPerformer( User $performer ) {
00377                 $this->performer = $performer;
00378         }
00379 
00387         public function setTarget( Title $target ) {
00388                 $this->target = $target;
00389         }
00390 
00398         public function setTimestamp( $timestamp ) {
00399                 $this->timestamp = $timestamp;
00400         }
00401 
00409         public function setComment( $comment ) {
00410                 $this->comment = $comment;
00411         }
00412 
00420         public function setDeleted( $deleted ) {
00421                 $this->deleted = $deleted;
00422         }
00423 
00428         public function insert() {
00429                 global $wgContLang;
00430 
00431                 $dbw = wfGetDB( DB_MASTER );
00432                 $id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
00433 
00434                 if ( $this->timestamp === null ) {
00435                         $this->timestamp = wfTimestampNow();
00436                 }
00437 
00438                 # Truncate for whole multibyte characters.
00439                 $comment = $wgContLang->truncate( $this->getComment(), 255 );
00440 
00441                 $data = array(
00442                         'log_id' => $id,
00443                         'log_type' => $this->getType(),
00444                         'log_action' => $this->getSubtype(),
00445                         'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
00446                         'log_user' => $this->getPerformer()->getId(),
00447                         'log_user_text' => $this->getPerformer()->getName(),
00448                         'log_namespace' => $this->getTarget()->getNamespace(),
00449                         'log_title' => $this->getTarget()->getDBkey(),
00450                         'log_page' => $this->getTarget()->getArticleID(),
00451                         'log_comment' => $comment,
00452                         'log_params' => serialize( (array) $this->getParameters() ),
00453                 );
00454                 $dbw->insert( 'logging', $data, __METHOD__ );
00455                 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
00456                 return $this->id;
00457         }
00458 
00464         public function publish( $newId, $to = 'rcandudp' ) {
00465                 $log = new LogPage( $this->getType() );
00466                 if ( $log->isRestricted() ) {
00467                         return;
00468                 }
00469 
00470                 $formatter = LogFormatter::newFromEntry( $this );
00471                 $context = RequestContext::newExtraneousContext( $this->getTarget() );
00472                 $formatter->setContext( $context );
00473 
00474                 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
00475                 $user = $this->getPerformer();
00476                 $ip = "";
00477                 if ( $user->isAnon() ) {
00478                         /*
00479                          * "MediaWiki default" and friends may have
00480                          * no IP address in their name
00481                          */
00482                         if ( IP::isIPAddress( $user->getName() ) ) {
00483                                 $ip = $user->getName();
00484                         }
00485                 }
00486                 $rc = RecentChange::newLogEntry(
00487                         $this->getTimestamp(),
00488                         $logpage,
00489                         $user,
00490                         $formatter->getPlainActionText(),
00491                         $ip,
00492                         $this->getType(),
00493                         $this->getSubtype(),
00494                         $this->getTarget(),
00495                         $this->getComment(),
00496                         serialize( (array) $this->getParameters() ),
00497                         $newId,
00498                         $formatter->getIRCActionComment() // Used for IRC feeds
00499                 );
00500 
00501                 if ( $to === 'rc' || $to === 'rcandudp' ) {
00502                         $rc->save( 'pleasedontudp' );
00503                 }
00504 
00505                 if ( $to === 'udp' || $to === 'rcandudp' ) {
00506                         $rc->notifyRC2UDP();
00507                 }
00508         }
00509 
00510         // LogEntry->
00511 
00512         public function getType() {
00513                 return $this->type;
00514         }
00515 
00516         public function getSubtype() {
00517                 return $this->subtype;
00518         }
00519 
00520         public function getParameters() {
00521                 return $this->parameters;
00522         }
00523 
00527         public function getPerformer() {
00528                 return $this->performer;
00529         }
00530 
00534         public function getTarget() {
00535                 return $this->target;
00536         }
00537 
00538         public function getTimestamp() {
00539                 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
00540                 return wfTimestamp( TS_MW, $ts );
00541         }
00542 
00543         public function getComment() {
00544                 return $this->comment;
00545         }
00546 
00547         public function getDeleted() {
00548                 return (int) $this->deleted;
00549         }
00550 
00551 }