MediaWiki master
LogPage.php
Go to the documentation of this file.
1<?php
36
46class LogPage {
47 public const DELETED_ACTION = 1;
48 public const DELETED_COMMENT = 2;
49 public const DELETED_USER = 4;
50 public const DELETED_RESTRICTED = 8;
51
52 // Convenience fields
53 public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
54 public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
55
58
60 public $sendToUDP;
61
63 private $ircActionText;
64
66 private $actionText;
67
71 private $type;
72
76 private $action;
77
79 private $comment;
80
82 private $params;
83
85 private $performer;
86
88 private $target;
89
97 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
98 $this->type = $type;
99 $this->updateRecentChanges = $rc;
100 $this->sendToUDP = ( $udp == 'UDP' );
101 }
102
106 protected function saveContent() {
107 $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
108
109 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
110
111 $now = wfTimestampNow();
112 $actorId = MediaWikiServices::getInstance()->getActorNormalization()
113 ->acquireActorId( $this->performer, $dbw );
114 $data = [
115 'log_type' => $this->type,
116 'log_action' => $this->action,
117 'log_timestamp' => $dbw->timestamp( $now ),
118 'log_actor' => $actorId,
119 'log_namespace' => $this->target->getNamespace(),
120 'log_title' => $this->target->getDBkey(),
121 'log_page' => $this->target->getArticleID(),
122 'log_params' => $this->params
123 ];
124 $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
125 $dbw,
126 'log_comment',
127 $this->comment
128 );
129 $dbw->newInsertQueryBuilder()
130 ->insertInto( 'logging' )
131 ->row( $data )
132 ->caller( __METHOD__ )->execute();
133 $newId = $dbw->insertId();
134
135 # And update recentchanges
136 if ( $this->updateRecentChanges ) {
137 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
138
139 RecentChange::notifyLog(
140 $now, $titleObj, $this->performer, $this->getRcComment(), '',
141 $this->type, $this->action, $this->target, $this->comment,
142 $this->params, $newId, $this->getRcCommentIRC()
143 );
144 } elseif ( $this->sendToUDP ) {
145 # Don't send private logs to UDP
146 if ( isset( $logRestrictions[$this->type] ) && $logRestrictions[$this->type] != '*' ) {
147 return $newId;
148 }
149
150 // Notify external application via UDP.
151 // We send this to IRC but do not want to add it the RC table.
152 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
153 $rc = RecentChange::newLogEntry(
154 $now, $titleObj, $this->performer, $this->getRcComment(), '',
155 $this->type, $this->action, $this->target, $this->comment,
156 $this->params, $newId, $this->getRcCommentIRC()
157 );
158 $rc->notifyRCFeeds();
159 }
160
161 return $newId;
162 }
163
169 public function getRcComment() {
170 $rcComment = $this->actionText;
171
172 if ( $this->comment != '' ) {
173 if ( $rcComment == '' ) {
174 $rcComment = $this->comment;
175 } else {
176 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
177 $this->comment;
178 }
179 }
180
181 return $rcComment;
182 }
183
189 public function getRcCommentIRC() {
190 $rcComment = $this->ircActionText;
191
192 if ( $this->comment != '' ) {
193 if ( $rcComment == '' ) {
194 $rcComment = $this->comment;
195 } else {
196 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
197 $this->comment;
198 }
199 }
200
201 return $rcComment;
202 }
203
208 public function getComment() {
209 return $this->comment;
210 }
211
217 public static function validTypes() {
218 $logTypes = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogTypes );
219
220 return $logTypes;
221 }
222
229 public static function isLogType( $type ) {
230 return in_array( $type, self::validTypes() );
231 }
232
246 public static function actionText( $type, $action, $title = null, $skin = null,
247 $params = [], $filterWikilinks = false
248 ) {
249 global $wgLang;
250 $config = MediaWikiServices::getInstance()->getMainConfig();
251 $key = "$type/$action";
252
253 $logActions = $config->get( MainConfigNames::LogActions );
254
255 if ( isset( $logActions[$key] ) ) {
256 $message = $logActions[$key];
257 } else {
258 wfDebug( "LogPage::actionText - unknown action $key" );
259 $message = "log-unknown-action";
260 $params = [ $key ];
261 }
262
263 if ( $skin === null ) {
264 $langObj = MediaWikiServices::getInstance()->getContentLanguage();
265 $langObjOrNull = null;
266 } else {
267 // TODO Is $skin->getLanguage() safe here?
268 StubUserLang::unstub( $wgLang );
269 $langObj = $wgLang;
270 $langObjOrNull = $wgLang;
271 }
272 if ( $title === null ) {
273 $rv = wfMessage( $message )->inLanguage( $langObj )->escaped();
274 } else {
275 $titleLink = self::getTitleLink( $title, $langObjOrNull );
276
277 if ( count( $params ) == 0 ) {
278 $rv = wfMessage( $message )->rawParams( $titleLink )
279 ->inLanguage( $langObj )->escaped();
280 } else {
281 array_unshift( $params, $titleLink );
282
283 $rv = wfMessage( $message )->rawParams( $params )
284 ->inLanguage( $langObj )->escaped();
285 }
286 }
287
288 // For the perplexed, this feature was added in r7855 by Erik.
289 // The feature was added because we liked adding [[$1]] in our log entries
290 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
291 // on Special:Log. The hack is essentially that [[$1]] represented a link
292 // to the title in question. The first parameter to the HTML version (Special:Log)
293 // is that link in HTML form, and so this just gets rid of the ugly [[]].
294 // However, this is a horrible hack and it doesn't work like you expect if, say,
295 // you want to link to something OTHER than the title of the log entry.
296 // The real problem, which Erik was trying to fix (and it sort-of works now) is
297 // that the same messages are being treated as both wikitext *and* HTML.
298 if ( $filterWikilinks ) {
299 $rv = str_replace( '[[', '', $rv );
300 $rv = str_replace( ']]', '', $rv );
301 }
302
303 return $rv;
304 }
305
311 private static function getTitleLink( Title $title, ?Language $lang ): string {
312 if ( !$lang ) {
313 return $title->getPrefixedText();
314 }
315
316 $services = MediaWikiServices::getInstance();
317 $linkRenderer = $services->getLinkRenderer();
318
319 if ( $title->isSpecialPage() ) {
320 [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
321
322 if ( $name === 'Log' ) {
323 $logPage = new LogPage( $par );
324 return wfMessage( 'parentheses' )
325 ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
326 ->inLanguage( $lang )
327 ->escaped();
328 }
329 }
330
331 return $linkRenderer->makeLink( $title );
332 }
333
347 public function addEntry( $action, $target, $comment, $params, $performer ) {
348 // FIXME $params is only documented to accept an array
349 if ( !is_array( $params ) ) {
350 $params = [ $params ];
351 }
352
353 # Trim spaces on user supplied text
354 $comment = trim( $comment ?? '' );
355
356 $this->action = $action;
357 $this->target = $target;
358 $this->comment = $comment;
359 $this->params = self::makeParamBlob( $params );
360
361 if ( !is_object( $performer ) ) {
362 $performer = User::newFromId( $performer );
363 }
364
365 $this->performer = $performer;
366
367 $logEntry = new ManualLogEntry( $this->type, $action );
368 $logEntry->setTarget( $target );
369 $logEntry->setPerformer( $performer );
370 $logEntry->setParameters( $params );
371 // All log entries using the LogPage to insert into the logging table
372 // are using the old logging system and therefore the legacy flag is
373 // needed to say the LogFormatter the parameters have numeric keys
374 $logEntry->setLegacy( true );
375
376 $formatter = MediaWikiServices::getInstance()->getLogFormatterFactory()->newFromEntry( $logEntry );
377 $context = RequestContext::newExtraneousContext( $target );
378 $formatter->setContext( $context );
379
380 $this->actionText = $formatter->getPlainActionText();
381 $this->ircActionText = $formatter->getIRCActionText();
382
383 return $this->saveContent();
384 }
385
394 public function addRelations( $field, $values, $logid ) {
395 if ( !strlen( $field ) || !$values ) {
396 return false;
397 }
398 $insert = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase()
399 ->newInsertQueryBuilder()
400 ->insertInto( 'log_search' )
401 ->ignore();
402 foreach ( $values as $value ) {
403 $insert->row( [ 'ls_field' => $field, 'ls_value' => $value, 'ls_log_id' => $logid ] );
404 }
405 $insert->caller( __METHOD__ )->execute();
406
407 return true;
408 }
409
416 public static function makeParamBlob( $params ) {
417 return implode( "\n", $params );
418 }
419
426 public static function extractParams( $blob ) {
427 if ( $blob === '' ) {
428 return [];
429 } else {
430 return explode( "\n", $blob );
431 }
432 }
433
439 public function getName() {
440 $logNames = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogNames );
441
442 // BC
443 $key = $logNames[$this->type] ?? 'log-name-' . $this->type;
444
445 return wfMessage( $key );
446 }
447
453 public function getDescription() {
454 $logHeaders = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogHeaders );
455 // BC
456 $key = $logHeaders[$this->type] ?? 'log-description-' . $this->type;
457
458 return wfMessage( $key );
459 }
460
466 public function getRestriction() {
467 $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
468 // The empty string fallback will
469 // always return true in permission check
470 return $logRestrictions[$this->type] ?? '';
471 }
472
478 public function isRestricted() {
479 $restriction = $this->getRestriction();
480
481 return $restriction !== '' && $restriction !== '*';
482 }
483}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:541
array $params
The job parameters.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Class to simplify the use of log pages.
Definition LogPage.php:46
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition LogPage.php:246
getRestriction()
Returns the right needed to read this log type.
Definition LogPage.php:466
const SUPPRESSED_USER
Definition LogPage.php:53
__construct( $type, $rc=true, $udp='skipUDP')
Definition LogPage.php:97
const DELETED_USER
Definition LogPage.php:49
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition LogPage.php:416
const DELETED_RESTRICTED
Definition LogPage.php:50
bool $sendToUDP
Definition LogPage.php:60
isRestricted()
Tells if this log is not viewable by all.
Definition LogPage.php:478
static extractParams( $blob)
Extract a parameter array from a blob.
Definition LogPage.php:426
static isLogType( $type)
Is $type a valid log type.
Definition LogPage.php:229
getName()
Name of the log.
Definition LogPage.php:439
saveContent()
Definition LogPage.php:106
const DELETED_COMMENT
Definition LogPage.php:48
getComment()
Get the comment from the last addEntry() call.
Definition LogPage.php:208
bool $updateRecentChanges
Definition LogPage.php:57
getRcComment()
Get the RC comment from the last addEntry() call.
Definition LogPage.php:169
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition LogPage.php:394
static validTypes()
Get the list of valid log types.
Definition LogPage.php:217
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition LogPage.php:189
getDescription()
Description of this log type.
Definition LogPage.php:453
const DELETED_ACTION
Definition LogPage.php:47
const SUPPRESSED_ACTION
Definition LogPage.php:54
addEntry( $action, $target, $comment, $params, $performer)
Add a log entry.
Definition LogPage.php:347
Class for creating new log entries and inserting them into the database.
Group all the pieces relevant to the context of a request into one instance.
Base class for language-specific code.
Definition Language.php:80
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
Parent class for all special pages.
Stub object for the user language.
Represents a title within MediaWiki.
Definition Title.php:78
getDBkey()
Get the main part with underscores.
Definition Title.php:1034
isSpecialPage()
Returns true if this is a special page.
Definition Title.php:1252
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1858
internal since 1.36
Definition User.php:93
Interface for objects representing user identity.