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