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