3 * Contains a class for dealing with manual log entries
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Niklas Laxström
22 * @license GPL-2.0-or-later
26 use MediaWiki\ChangeTags\Taggable
;
27 use MediaWiki\Linker\LinkTarget
;
28 use MediaWiki\User\UserIdentity
;
29 use Wikimedia\Rdbms\IDatabase
;
30 use Wikimedia\Assert\Assert
;
33 * Class for creating new log entries and inserting them into the database.
37 class ManualLogEntry
extends LogEntryBase
implements Taggable
{
38 /** @var string Type of log entry */
41 /** @var string Sub type of log entry */
44 /** @var array Parameters for log entry */
45 protected $parameters = [];
48 protected $relations = [];
50 /** @var User Performer of the action for the log entry */
53 /** @var Title Target title for the log entry */
56 /** @var string Timestamp of creation of the log entry */
59 /** @var string Comment for the log entry */
60 protected $comment = '';
62 /** @var int A rev id associated to the log entry */
65 /** @var string[] Change tags add to the log entry */
68 /** @var int Deletion state of the log entry */
71 /** @var int ID of the log entry */
74 /** @var bool Can this log entry be patrolled? */
75 protected $isPatrollable = false;
77 /** @var bool Whether this is a legacy log entry */
78 protected $legacy = false;
83 * @param string $subtype
85 public function __construct( $type, $subtype ) {
87 $this->subtype
= $subtype;
91 * Set extra log parameters.
93 * You can pass params to the log action message by prefixing the keys with
94 * a number and optional type, using colons to separate the fields. The
95 * numbering should start with number 4, the first three parameters are
96 * hardcoded for every message.
98 * If you want to store stuff that should not be available in messages, don't
99 * prefix the array key with a number and don't use the colons.
102 * $entry->setParameters(
103 * '4::color' => 'blue',
104 * '5:number:count' => 3000,
109 * @param array $parameters Associative array
111 public function setParameters( $parameters ) {
112 $this->parameters
= $parameters;
116 * Declare arbitrary tag/value relations to this log entry.
117 * These can be used to filter log entries later on.
119 * @param array $relations Map of (tag => (list of values|value))
122 public function setRelations( array $relations ) {
123 $this->relations
= $relations;
127 * Set the user that performed the action being logged.
130 * @param UserIdentity $performer
132 public function setPerformer( UserIdentity
$performer ) {
133 $this->performer
= User
::newFromIdentity( $performer );
137 * Set the title of the object changed.
140 * @param LinkTarget $target
142 public function setTarget( LinkTarget
$target ) {
143 $this->target
= Title
::newFromLinkTarget( $target );
147 * Set the timestamp of when the logged action took place.
150 * @param string $timestamp
152 public function setTimestamp( $timestamp ) {
153 $this->timestamp
= $timestamp;
157 * Set a comment associated with the action being logged.
160 * @param string $comment
162 public function setComment( $comment ) {
163 $this->comment
= $comment;
167 * Set an associated revision id.
169 * For example, the ID of the revision that was inserted to mark a page move
170 * or protection, file upload, etc.
175 public function setAssociatedRevId( $revId ) {
176 $this->revId
= $revId;
180 * Set change tags for the log entry.
182 * Passing `null` means the same as empty array,
183 * for compatibility with WikiPage::doUpdateRestrictions().
186 * @param string|string[]|null $tags
187 * @deprecated since 1.33 Please use addTags() instead
189 public function setTags( $tags ) {
191 wfDebug( 'Overwriting existing ManualLogEntry tags' );
194 $this->addTags( $tags );
198 * Add change tags for the log entry
201 * @param string|string[]|null $tags Tags to apply
203 public function addTags( $tags ) {
204 if ( $tags === null ) {
208 if ( is_string( $tags ) ) {
211 Assert
::parameterElementType( 'string', $tags, 'tags' );
212 $this->tags
= array_unique( array_merge( $this->tags
, $tags ) );
216 * Set whether this log entry should be made patrollable
217 * This shouldn't depend on config, only on whether there is full support
218 * in the software for patrolling this log entry.
222 * @param bool $patrollable
224 public function setIsPatrollable( $patrollable ) {
225 $this->isPatrollable
= (bool)$patrollable;
229 * Set the 'legacy' flag
232 * @param bool $legacy
234 public function setLegacy( $legacy ) {
235 $this->legacy
= $legacy;
239 * Set the 'deleted' flag.
242 * @param int $deleted One of LogPage::DELETED_* bitfield constants
244 public function setDeleted( $deleted ) {
245 $this->deleted
= $deleted;
249 * Insert the entry into the `logging` table.
251 * @param IDatabase|null $dbw
252 * @return int ID of the log entry
253 * @throws MWException
255 public function insert( IDatabase
$dbw = null ) {
256 global $wgActorTableSchemaMigrationStage;
258 $dbw = $dbw ?
: wfGetDB( DB_MASTER
);
260 if ( $this->timestamp
=== null ) {
261 $this->timestamp
= wfTimestampNow();
264 // Trim spaces on user supplied text
265 $comment = trim( $this->getComment() );
267 $params = $this->getParameters();
268 $relations = $this->relations
;
270 // Ensure actor relations are set
271 if ( ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) &&
272 empty( $relations['target_author_actor'] )
275 if ( !empty( $relations['target_author_id'] ) ) {
276 foreach ( $relations['target_author_id'] as $id ) {
277 $actorIds[] = User
::newFromId( $id )->getActorId( $dbw );
280 if ( !empty( $relations['target_author_ip'] ) ) {
281 foreach ( $relations['target_author_ip'] as $ip ) {
282 $actorIds[] = User
::newFromName( $ip, false )->getActorId( $dbw );
286 $relations['target_author_actor'] = $actorIds;
287 $params['authorActors'] = $actorIds;
290 if ( !( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) ) {
291 unset( $relations['target_author_id'], $relations['target_author_ip'] );
292 unset( $params['authorIds'], $params['authorIPs'] );
295 // Additional fields for which there's no space in the database table schema
296 $revId = $this->getAssociatedRevId();
298 $params['associated_rev_id'] = $revId;
299 $relations['associated_rev_id'] = $revId;
303 'log_type' => $this->getType(),
304 'log_action' => $this->getSubtype(),
305 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
306 'log_namespace' => $this->getTarget()->getNamespace(),
307 'log_title' => $this->getTarget()->getDBkey(),
308 'log_page' => $this->getTarget()->getArticleID(),
309 'log_params' => LogEntryBase
::makeParamBlob( $params ),
311 if ( isset( $this->deleted
) ) {
312 $data['log_deleted'] = $this->deleted
;
314 $data +
= CommentStore
::getStore()->insert( $dbw, 'log_comment', $comment );
315 $data +
= ActorMigration
::newMigration()
316 ->getInsertValues( $dbw, 'log_user', $this->getPerformer() );
318 $dbw->insert( 'logging', $data, __METHOD__
);
319 $this->id
= $dbw->insertId();
322 foreach ( $relations as $tag => $values ) {
323 if ( !strlen( $tag ) ) {
324 throw new MWException( "Got empty log search tag." );
327 if ( !is_array( $values ) ) {
328 $values = [ $values ];
331 foreach ( $values as $value ) {
334 'ls_value' => $value,
335 'ls_log_id' => $this->id
339 if ( count( $rows ) ) {
340 $dbw->insert( 'log_search', $rows, __METHOD__
, [ 'IGNORE' ] );
347 * Get a RecentChanges object for the log entry
350 * @return RecentChange
353 public function getRecentChange( $newId = 0 ) {
354 $formatter = LogFormatter
::newFromEntry( $this );
355 $context = RequestContext
::newExtraneousContext( $this->getTarget() );
356 $formatter->setContext( $context );
358 $logpage = SpecialPage
::getTitleFor( 'Log', $this->getType() );
359 $user = $this->getPerformer();
361 if ( $user->isAnon() ) {
362 // "MediaWiki default" and friends may have
363 // no IP address in their name
364 if ( IP
::isIPAddress( $user->getName() ) ) {
365 $ip = $user->getName();
369 return RecentChange
::newLogEntry(
370 $this->getTimestamp(),
373 $formatter->getPlainActionText(),
379 LogEntryBase
::makeParamBlob( $this->getParameters() ),
381 $formatter->getIRCActionComment(), // Used for IRC feeds
382 $this->getAssociatedRevId(), // Used for e.g. moves and uploads
383 $this->getIsPatrollable()
388 * Publish the log entry.
390 * @param int $newId Id of the log entry.
391 * @param string $to One of: rcandudp (default), rc, udp
393 public function publish( $newId, $to = 'rcandudp' ) {
395 // FIXME: this code should be removed once all callers properly call publish()
396 if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
397 \MediaWiki\Logger\LoggerFactory
::getInstance( 'logging' )->warning(
398 'newId and/or revId must be set when calling ManualLogEntry::publish()',
402 'revId' => $this->getAssociatedRevId(),
403 // pass a new exception to register the stack trace
404 'exception' => new RuntimeException()
410 DeferredUpdates
::addCallableUpdate(
411 function () use ( $newId, $to, $canAddTags ) {
412 $log = new LogPage( $this->getType() );
413 if ( !$log->isRestricted() ) {
414 Hooks
::runWithoutAbort( 'ManualLogEntryBeforePublish', [ $this ] );
415 $rc = $this->getRecentChange( $newId );
417 if ( $to === 'rc' ||
$to === 'rcandudp' ) {
418 // save RC, passing tags so they are applied there
419 $rc->addTags( $this->getTags() );
420 $rc->save( $rc::SEND_NONE
);
422 $tags = $this->getTags();
423 if ( $tags && $canAddTags ) {
424 $revId = $this->getAssociatedRevId();
428 $revId > 0 ?
$revId : null,
429 $newId > 0 ?
$newId : null
434 if ( $to === 'udp' ||
$to === 'rcandudp' ) {
435 $rc->notifyRCFeeds();
439 DeferredUpdates
::POSTSEND
,
444 public function getType() {
448 public function getSubtype() {
449 return $this->subtype
;
452 public function getParameters() {
453 return $this->parameters
;
459 public function getPerformer() {
460 return $this->performer
;
466 public function getTarget() {
467 return $this->target
;
470 public function getTimestamp() {
471 $ts = $this->timestamp ??
wfTimestampNow();
473 return wfTimestamp( TS_MW
, $ts );
476 public function getComment() {
477 return $this->comment
;
484 public function getAssociatedRevId() {
492 public function getTags() {
497 * Whether this log entry is patrollable
502 public function getIsPatrollable() {
503 return $this->isPatrollable
;
510 public function isLegacy() {
511 return $this->legacy
;
514 public function getDeleted() {
515 return (int)$this->deleted
;