MediaWiki  REL1_21
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         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                 # Trim spaces on user supplied text
00446                 $comment = trim( $this->getComment() );
00447 
00448                 # Truncate for whole multibyte characters.
00449                 $comment = $wgContLang->truncate( $comment, 255 );
00450 
00451                 $data = array(
00452                         'log_id' => $id,
00453                         'log_type' => $this->getType(),
00454                         'log_action' => $this->getSubtype(),
00455                         'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
00456                         'log_user' => $this->getPerformer()->getId(),
00457                         'log_user_text' => $this->getPerformer()->getName(),
00458                         'log_namespace' => $this->getTarget()->getNamespace(),
00459                         'log_title' => $this->getTarget()->getDBkey(),
00460                         'log_page' => $this->getTarget()->getArticleID(),
00461                         'log_comment' => $comment,
00462                         'log_params' => serialize( (array) $this->getParameters() ),
00463                 );
00464                 $dbw->insert( 'logging', $data, __METHOD__ );
00465                 $this->id = !is_null( $id ) ? $id : $dbw->insertId();
00466                 return $this->id;
00467         }
00468 
00474         public function publish( $newId, $to = 'rcandudp' ) {
00475                 $log = new LogPage( $this->getType() );
00476                 if ( $log->isRestricted() ) {
00477                         return;
00478                 }
00479 
00480                 $formatter = LogFormatter::newFromEntry( $this );
00481                 $context = RequestContext::newExtraneousContext( $this->getTarget() );
00482                 $formatter->setContext( $context );
00483 
00484                 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
00485                 $user = $this->getPerformer();
00486                 $ip = "";
00487                 if ( $user->isAnon() ) {
00488                         /*
00489                          * "MediaWiki default" and friends may have
00490                          * no IP address in their name
00491                          */
00492                         if ( IP::isIPAddress( $user->getName() ) ) {
00493                                 $ip = $user->getName();
00494                         }
00495                 }
00496                 $rc = RecentChange::newLogEntry(
00497                         $this->getTimestamp(),
00498                         $logpage,
00499                         $user,
00500                         $formatter->getPlainActionText(),
00501                         $ip,
00502                         $this->getType(),
00503                         $this->getSubtype(),
00504                         $this->getTarget(),
00505                         $this->getComment(),
00506                         serialize( (array) $this->getParameters() ),
00507                         $newId,
00508                         $formatter->getIRCActionComment() // Used for IRC feeds
00509                 );
00510 
00511                 if ( $to === 'rc' || $to === 'rcandudp' ) {
00512                         $rc->save( 'pleasedontudp' );
00513                 }
00514 
00515                 if ( $to === 'udp' || $to === 'rcandudp' ) {
00516                         $rc->notifyRC2UDP();
00517                 }
00518         }
00519 
00520         // LogEntry->
00521 
00522         public function getType() {
00523                 return $this->type;
00524         }
00525 
00526         public function getSubtype() {
00527                 return $this->subtype;
00528         }
00529 
00530         public function getParameters() {
00531                 return $this->parameters;
00532         }
00533 
00537         public function getPerformer() {
00538                 return $this->performer;
00539         }
00540 
00544         public function getTarget() {
00545                 return $this->target;
00546         }
00547 
00548         public function getTimestamp() {
00549                 $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
00550                 return wfTimestamp( TS_MW, $ts );
00551         }
00552 
00553         public function getComment() {
00554                 return $this->comment;
00555         }
00556 
00557         public function getDeleted() {
00558                 return (int) $this->deleted;
00559         }
00560 
00561 }