123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814 |
- <?php
- /**
- * Contain classes for dealing with individual log entries
- *
- * This is how I see the log system history:
- * - appending to plain wiki pages
- * - formatting log entries based on database fields
- * - user is now part of the action message
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @author Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- * @since 1.19
- */
- use Wikimedia\Rdbms\IDatabase;
- /**
- * Interface for log entries. Every log entry has these methods.
- *
- * @since 1.19
- */
- interface LogEntry {
- /**
- * The main log type.
- *
- * @return string
- */
- public function getType();
- /**
- * The log subtype.
- *
- * @return string
- */
- public function getSubtype();
- /**
- * The full logtype in format maintype/subtype.
- *
- * @return string
- */
- public function getFullType();
- /**
- * Get the extra parameters stored for this message.
- *
- * @return array
- */
- public function getParameters();
- /**
- * Get the user for performed this action.
- *
- * @return User
- */
- public function getPerformer();
- /**
- * Get the target page of this action.
- *
- * @return Title
- */
- public function getTarget();
- /**
- * Get the timestamp when the action was executed.
- *
- * @return string
- */
- public function getTimestamp();
- /**
- * Get the user provided comment.
- *
- * @return string
- */
- public function getComment();
- /**
- * Get the access restriction.
- *
- * @return string
- */
- public function getDeleted();
- /**
- * @param int $field One of LogPage::DELETED_* bitfield constants
- * @return bool
- */
- public function isDeleted( $field );
- }
- /**
- * Extends the LogEntryInterface with some basic functionality
- *
- * @since 1.19
- */
- abstract class LogEntryBase implements LogEntry {
- public function getFullType() {
- return $this->getType() . '/' . $this->getSubtype();
- }
- public function isDeleted( $field ) {
- return ( $this->getDeleted() & $field ) === $field;
- }
- /**
- * Whether the parameters for this log are stored in new or
- * old format.
- *
- * @return bool
- */
- public function isLegacy() {
- return false;
- }
- /**
- * Create a blob from a parameter array
- *
- * @since 1.26
- * @param array $params
- * @return string
- */
- public static function makeParamBlob( $params ) {
- return serialize( (array)$params );
- }
- /**
- * Extract a parameter array from a blob
- *
- * @since 1.26
- * @param string $blob
- * @return array
- */
- public static function extractParams( $blob ) {
- return unserialize( $blob );
- }
- }
- /**
- * This class wraps around database result row.
- *
- * @since 1.19
- */
- class DatabaseLogEntry extends LogEntryBase {
- /**
- * Returns array of information that is needed for querying
- * log entries. Array contains the following keys:
- * tables, fields, conds, options and join_conds
- *
- * @return array
- */
- public static function getSelectQueryData() {
- $commentQuery = CommentStore::newKey( 'log_comment' )->getJoin();
- $tables = [ 'logging', 'user' ] + $commentQuery['tables'];
- $fields = [
- 'log_id', 'log_type', 'log_action', 'log_timestamp',
- 'log_user', 'log_user_text',
- 'log_namespace', 'log_title', // unused log_page
- 'log_params', 'log_deleted',
- 'user_id', 'user_name', 'user_editcount',
- ] + $commentQuery['fields'];
- $joins = [
- // IPs don't have an entry in user table
- 'user' => [ 'LEFT JOIN', 'log_user=user_id' ],
- ] + $commentQuery['joins'];
- return [
- 'tables' => $tables,
- 'fields' => $fields,
- 'conds' => [],
- 'options' => [],
- 'join_conds' => $joins,
- ];
- }
- /**
- * Constructs new LogEntry from database result row.
- * Supports rows from both logging and recentchanges table.
- *
- * @param stdClass|array $row
- * @return DatabaseLogEntry
- */
- public static function newFromRow( $row ) {
- $row = (object)$row;
- if ( isset( $row->rc_logid ) ) {
- return new RCDatabaseLogEntry( $row );
- } else {
- return new self( $row );
- }
- }
- /** @var stdClass Database result row. */
- protected $row;
- /** @var User */
- protected $performer;
- /** @var array Parameters for log entry */
- protected $params;
- /** @var int A rev id associated to the log entry */
- protected $revId = null;
- /** @var bool Whether the parameters for this log entry are stored in new or old format. */
- protected $legacy;
- protected function __construct( $row ) {
- $this->row = $row;
- }
- /**
- * Returns the unique database id.
- *
- * @return int
- */
- public function getId() {
- return (int)$this->row->log_id;
- }
- /**
- * Returns whatever is stored in the database field.
- *
- * @return string
- */
- protected function getRawParameters() {
- return $this->row->log_params;
- }
- public function isLegacy() {
- // This extracts the property
- $this->getParameters();
- return $this->legacy;
- }
- public function getType() {
- return $this->row->log_type;
- }
- public function getSubtype() {
- return $this->row->log_action;
- }
- public function getParameters() {
- if ( !isset( $this->params ) ) {
- $blob = $this->getRawParameters();
- MediaWiki\suppressWarnings();
- $params = LogEntryBase::extractParams( $blob );
- MediaWiki\restoreWarnings();
- if ( $params !== false ) {
- $this->params = $params;
- $this->legacy = false;
- } else {
- $this->params = LogPage::extractParams( $blob );
- $this->legacy = true;
- }
- if ( isset( $this->params['associated_rev_id'] ) ) {
- $this->revId = $this->params['associated_rev_id'];
- unset( $this->params['associated_rev_id'] );
- }
- }
- return $this->params;
- }
- public function getAssociatedRevId() {
- // This extracts the property
- $this->getParameters();
- return $this->revId;
- }
- public function getPerformer() {
- if ( !$this->performer ) {
- $userId = (int)$this->row->log_user;
- if ( $userId !== 0 ) {
- // logged-in users
- if ( isset( $this->row->user_name ) ) {
- $this->performer = User::newFromRow( $this->row );
- } else {
- $this->performer = User::newFromId( $userId );
- }
- } else {
- // IP users
- $userText = $this->row->log_user_text;
- $this->performer = User::newFromName( $userText, false );
- }
- }
- return $this->performer;
- }
- public function getTarget() {
- $namespace = $this->row->log_namespace;
- $page = $this->row->log_title;
- $title = Title::makeTitle( $namespace, $page );
- return $title;
- }
- public function getTimestamp() {
- return wfTimestamp( TS_MW, $this->row->log_timestamp );
- }
- public function getComment() {
- return CommentStore::newKey( 'log_comment' )->getComment( $this->row )->text;
- }
- public function getDeleted() {
- return $this->row->log_deleted;
- }
- }
- class RCDatabaseLogEntry extends DatabaseLogEntry {
- public function getId() {
- return $this->row->rc_logid;
- }
- protected function getRawParameters() {
- return $this->row->rc_params;
- }
- public function getAssociatedRevId() {
- return $this->row->rc_this_oldid;
- }
- public function getType() {
- return $this->row->rc_log_type;
- }
- public function getSubtype() {
- return $this->row->rc_log_action;
- }
- public function getPerformer() {
- if ( !$this->performer ) {
- $userId = (int)$this->row->rc_user;
- if ( $userId !== 0 ) {
- $this->performer = User::newFromId( $userId );
- } else {
- $userText = $this->row->rc_user_text;
- // Might be an IP, don't validate the username
- $this->performer = User::newFromName( $userText, false );
- }
- }
- return $this->performer;
- }
- public function getTarget() {
- $namespace = $this->row->rc_namespace;
- $page = $this->row->rc_title;
- $title = Title::makeTitle( $namespace, $page );
- return $title;
- }
- public function getTimestamp() {
- return wfTimestamp( TS_MW, $this->row->rc_timestamp );
- }
- public function getComment() {
- return CommentStore::newKey( 'rc_comment' )
- // Legacy because the row probably used RecentChange::selectFields()
- ->getCommentLegacy( wfGetDB( DB_REPLICA ), $this->row )->text;
- }
- public function getDeleted() {
- return $this->row->rc_deleted;
- }
- }
- /**
- * Class for creating log entries manually, to inject them into the database.
- *
- * @since 1.19
- */
- class ManualLogEntry extends LogEntryBase {
- /** @var string Type of log entry */
- protected $type;
- /** @var string Sub type of log entry */
- protected $subtype;
- /** @var array Parameters for log entry */
- protected $parameters = [];
- /** @var array */
- protected $relations = [];
- /** @var User Performer of the action for the log entry */
- protected $performer;
- /** @var Title Target title for the log entry */
- protected $target;
- /** @var string Timestamp of creation of the log entry */
- protected $timestamp;
- /** @var string Comment for the log entry */
- protected $comment = '';
- /** @var int A rev id associated to the log entry */
- protected $revId = 0;
- /** @var array Change tags add to the log entry */
- protected $tags = null;
- /** @var int Deletion state of the log entry */
- protected $deleted;
- /** @var int ID of the log entry */
- protected $id;
- /** @var bool Can this log entry be patrolled? */
- protected $isPatrollable = false;
- /** @var bool Whether this is a legacy log entry */
- protected $legacy = false;
- /**
- * @since 1.19
- * @param string $type
- * @param string $subtype
- */
- public function __construct( $type, $subtype ) {
- $this->type = $type;
- $this->subtype = $subtype;
- }
- /**
- * Set extra log parameters.
- *
- * You can pass params to the log action message by prefixing the keys with
- * a number and optional type, using colons to separate the fields. The
- * numbering should start with number 4, the first three parameters are
- * hardcoded for every message.
- *
- * If you want to store stuff that should not be available in messages, don't
- * prefix the array key with a number and don't use the colons.
- *
- * Example:
- * $entry->setParameters(
- * '4::color' => 'blue',
- * '5:number:count' => 3000,
- * 'animal' => 'dog'
- * );
- *
- * @since 1.19
- * @param array $parameters Associative array
- */
- public function setParameters( $parameters ) {
- $this->parameters = $parameters;
- }
- /**
- * Declare arbitrary tag/value relations to this log entry.
- * These can be used to filter log entries later on.
- *
- * @param array $relations Map of (tag => (list of values|value))
- * @since 1.22
- */
- public function setRelations( array $relations ) {
- $this->relations = $relations;
- }
- /**
- * Set the user that performed the action being logged.
- *
- * @since 1.19
- * @param User $performer
- */
- public function setPerformer( User $performer ) {
- $this->performer = $performer;
- }
- /**
- * Set the title of the object changed.
- *
- * @since 1.19
- * @param Title $target
- */
- public function setTarget( Title $target ) {
- $this->target = $target;
- }
- /**
- * Set the timestamp of when the logged action took place.
- *
- * @since 1.19
- * @param string $timestamp
- */
- public function setTimestamp( $timestamp ) {
- $this->timestamp = $timestamp;
- }
- /**
- * Set a comment associated with the action being logged.
- *
- * @since 1.19
- * @param string $comment
- */
- public function setComment( $comment ) {
- $this->comment = $comment;
- }
- /**
- * Set an associated revision id.
- *
- * For example, the ID of the revision that was inserted to mark a page move
- * or protection, file upload, etc.
- *
- * @since 1.27
- * @param int $revId
- */
- public function setAssociatedRevId( $revId ) {
- $this->revId = $revId;
- }
- /**
- * Set change tags for the log entry.
- *
- * @since 1.27
- * @param string|string[] $tags
- */
- public function setTags( $tags ) {
- if ( is_string( $tags ) ) {
- $tags = [ $tags ];
- }
- $this->tags = $tags;
- }
- /**
- * Set whether this log entry should be made patrollable
- * This shouldn't depend on config, only on whether there is full support
- * in the software for patrolling this log entry.
- * False by default
- *
- * @since 1.27
- * @param bool $patrollable
- */
- public function setIsPatrollable( $patrollable ) {
- $this->isPatrollable = (bool)$patrollable;
- }
- /**
- * Set the 'legacy' flag
- *
- * @since 1.25
- * @param bool $legacy
- */
- public function setLegacy( $legacy ) {
- $this->legacy = $legacy;
- }
- /**
- * Set the 'deleted' flag.
- *
- * @since 1.19
- * @param int $deleted One of LogPage::DELETED_* bitfield constants
- */
- public function setDeleted( $deleted ) {
- $this->deleted = $deleted;
- }
- /**
- * Insert the entry into the `logging` table.
- *
- * @param IDatabase $dbw
- * @return int ID of the log entry
- * @throws MWException
- */
- public function insert( IDatabase $dbw = null ) {
- $dbw = $dbw ?: wfGetDB( DB_MASTER );
- if ( $this->timestamp === null ) {
- $this->timestamp = wfTimestampNow();
- }
- // Trim spaces on user supplied text
- $comment = trim( $this->getComment() );
- $params = $this->getParameters();
- $relations = $this->relations;
- // Additional fields for which there's no space in the database table schema
- $revId = $this->getAssociatedRevId();
- if ( $revId ) {
- $params['associated_rev_id'] = $revId;
- $relations['associated_rev_id'] = $revId;
- }
- $data = [
- 'log_type' => $this->getType(),
- 'log_action' => $this->getSubtype(),
- 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
- 'log_user' => $this->getPerformer()->getId(),
- 'log_user_text' => $this->getPerformer()->getName(),
- 'log_namespace' => $this->getTarget()->getNamespace(),
- 'log_title' => $this->getTarget()->getDBkey(),
- 'log_page' => $this->getTarget()->getArticleID(),
- 'log_params' => LogEntryBase::makeParamBlob( $params ),
- ];
- if ( isset( $this->deleted ) ) {
- $data['log_deleted'] = $this->deleted;
- }
- $data += CommentStore::newKey( 'log_comment' )->insert( $dbw, $comment );
- $dbw->insert( 'logging', $data, __METHOD__ );
- $this->id = $dbw->insertId();
- $rows = [];
- foreach ( $relations as $tag => $values ) {
- if ( !strlen( $tag ) ) {
- throw new MWException( "Got empty log search tag." );
- }
- if ( !is_array( $values ) ) {
- $values = [ $values ];
- }
- foreach ( $values as $value ) {
- $rows[] = [
- 'ls_field' => $tag,
- 'ls_value' => $value,
- 'ls_log_id' => $this->id
- ];
- }
- }
- if ( count( $rows ) ) {
- $dbw->insert( 'log_search', $rows, __METHOD__, 'IGNORE' );
- }
- return $this->id;
- }
- /**
- * Get a RecentChanges object for the log entry
- *
- * @param int $newId
- * @return RecentChange
- * @since 1.23
- */
- public function getRecentChange( $newId = 0 ) {
- $formatter = LogFormatter::newFromEntry( $this );
- $context = RequestContext::newExtraneousContext( $this->getTarget() );
- $formatter->setContext( $context );
- $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
- $user = $this->getPerformer();
- $ip = "";
- if ( $user->isAnon() ) {
- // "MediaWiki default" and friends may have
- // no IP address in their name
- if ( IP::isIPAddress( $user->getName() ) ) {
- $ip = $user->getName();
- }
- }
- return RecentChange::newLogEntry(
- $this->getTimestamp(),
- $logpage,
- $user,
- $formatter->getPlainActionText(),
- $ip,
- $this->getType(),
- $this->getSubtype(),
- $this->getTarget(),
- $this->getComment(),
- LogEntryBase::makeParamBlob( $this->getParameters() ),
- $newId,
- $formatter->getIRCActionComment(), // Used for IRC feeds
- $this->getAssociatedRevId(), // Used for e.g. moves and uploads
- $this->getIsPatrollable()
- );
- }
- /**
- * Publish the log entry.
- *
- * @param int $newId Id of the log entry.
- * @param string $to One of: rcandudp (default), rc, udp
- */
- public function publish( $newId, $to = 'rcandudp' ) {
- DeferredUpdates::addCallableUpdate(
- function () use ( $newId, $to ) {
- $log = new LogPage( $this->getType() );
- if ( !$log->isRestricted() ) {
- $rc = $this->getRecentChange( $newId );
- if ( $to === 'rc' || $to === 'rcandudp' ) {
- // save RC, passing tags so they are applied there
- $tags = $this->getTags();
- if ( is_null( $tags ) ) {
- $tags = [];
- }
- $rc->addTags( $tags );
- $rc->save( 'pleasedontudp' );
- }
- if ( $to === 'udp' || $to === 'rcandudp' ) {
- $rc->notifyRCFeeds();
- }
- // Log the autopatrol if the log entry is patrollable
- if ( $this->getIsPatrollable() &&
- $rc->getAttribute( 'rc_patrolled' ) === 1
- ) {
- PatrolLog::record( $rc, true, $this->getPerformer() );
- }
- }
- },
- DeferredUpdates::POSTSEND,
- wfGetDB( DB_MASTER )
- );
- }
- public function getType() {
- return $this->type;
- }
- public function getSubtype() {
- return $this->subtype;
- }
- public function getParameters() {
- return $this->parameters;
- }
- /**
- * @return User
- */
- public function getPerformer() {
- return $this->performer;
- }
- /**
- * @return Title
- */
- public function getTarget() {
- return $this->target;
- }
- public function getTimestamp() {
- $ts = $this->timestamp !== null ? $this->timestamp : wfTimestampNow();
- return wfTimestamp( TS_MW, $ts );
- }
- public function getComment() {
- return $this->comment;
- }
- /**
- * @since 1.27
- * @return int
- */
- public function getAssociatedRevId() {
- return $this->revId;
- }
- /**
- * @since 1.27
- * @return array
- */
- public function getTags() {
- return $this->tags;
- }
- /**
- * Whether this log entry is patrollable
- *
- * @since 1.27
- * @return bool
- */
- public function getIsPatrollable() {
- return $this->isPatrollable;
- }
- /**
- * @since 1.25
- * @return bool
- */
- public function isLegacy() {
- return $this->legacy;
- }
- public function getDeleted() {
- return (int)$this->deleted;
- }
- }
|