MediaWiki REL1_41
LogPage.php
Go to the documentation of this file.
1<?php
33
43class LogPage {
44 public const DELETED_ACTION = 1;
45 public const DELETED_COMMENT = 2;
46 public const DELETED_USER = 4;
47 public const DELETED_RESTRICTED = 8;
48
49 // Convenience fields
50 public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
51 public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
52
55
57 public $sendToUDP;
58
60 private $ircActionText;
61
63 private $actionText;
64
68 private $type;
69
73 private $action;
74
76 private $comment;
77
79 private $params;
80
82 private $performer;
83
85 private $target;
86
94 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
95 $this->type = $type;
96 $this->updateRecentChanges = $rc;
97 $this->sendToUDP = ( $udp == 'UDP' );
98 }
99
103 protected function saveContent() {
104 $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
105
106 $dbw = wfGetDB( DB_PRIMARY );
107
108 $now = wfTimestampNow();
109 $actorId = MediaWikiServices::getInstance()->getActorNormalization()
110 ->acquireActorId( $this->performer, $dbw );
111 $data = [
112 'log_type' => $this->type,
113 'log_action' => $this->action,
114 'log_timestamp' => $dbw->timestamp( $now ),
115 'log_actor' => $actorId,
116 'log_namespace' => $this->target->getNamespace(),
117 'log_title' => $this->target->getDBkey(),
118 'log_page' => $this->target->getArticleID(),
119 'log_params' => $this->params
120 ];
121 $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
122 $dbw,
123 'log_comment',
124 $this->comment
125 );
126 $dbw->insert( 'logging', $data, __METHOD__ );
127 $newId = $dbw->insertId();
128
129 # And update recentchanges
130 if ( $this->updateRecentChanges ) {
131 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
132
133 RecentChange::notifyLog(
134 $now, $titleObj, $this->performer, $this->getRcComment(), '',
135 $this->type, $this->action, $this->target, $this->comment,
136 $this->params, $newId, $this->getRcCommentIRC()
137 );
138 } elseif ( $this->sendToUDP ) {
139 # Don't send private logs to UDP
140 if ( isset( $logRestrictions[$this->type] ) && $logRestrictions[$this->type] != '*' ) {
141 return $newId;
142 }
143
144 // Notify external application via UDP.
145 // We send this to IRC but do not want to add it the RC table.
146 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
147 $rc = RecentChange::newLogEntry(
148 $now, $titleObj, $this->performer, $this->getRcComment(), '',
149 $this->type, $this->action, $this->target, $this->comment,
150 $this->params, $newId, $this->getRcCommentIRC()
151 );
152 $rc->notifyRCFeeds();
153 }
154
155 return $newId;
156 }
157
163 public function getRcComment() {
164 $rcComment = $this->actionText;
165
166 if ( $this->comment != '' ) {
167 if ( $rcComment == '' ) {
168 $rcComment = $this->comment;
169 } else {
170 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
171 $this->comment;
172 }
173 }
174
175 return $rcComment;
176 }
177
183 public function getRcCommentIRC() {
184 $rcComment = $this->ircActionText;
185
186 if ( $this->comment != '' ) {
187 if ( $rcComment == '' ) {
188 $rcComment = $this->comment;
189 } else {
190 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
191 $this->comment;
192 }
193 }
194
195 return $rcComment;
196 }
197
202 public function getComment() {
203 return $this->comment;
204 }
205
211 public static function validTypes() {
212 $logTypes = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogTypes );
213
214 return $logTypes;
215 }
216
223 public static function isLogType( $type ) {
224 return in_array( $type, self::validTypes() );
225 }
226
240 public static function actionText( $type, $action, $title = null, $skin = null,
241 $params = [], $filterWikilinks = false
242 ) {
243 global $wgLang;
244 $config = MediaWikiServices::getInstance()->getMainConfig();
245 $logActionsHandlers = $config->get( MainConfigNames::LogActionsHandlers );
246 $key = "$type/$action";
247
248 if ( isset( $logActionsHandlers[$key] ) ) {
249 $args = func_get_args();
250 $rv = call_user_func_array( $logActionsHandlers[$key], $args );
251 } else {
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
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 = LogFormatter::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
399 $data = [];
400
401 foreach ( $values as $value ) {
402 $data[] = [
403 'ls_field' => $field,
404 'ls_value' => $value,
405 'ls_log_id' => $logid
406 ];
407 }
408
409 $dbw = wfGetDB( DB_PRIMARY );
410 $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
411
412 return true;
413 }
414
421 public static function makeParamBlob( $params ) {
422 return implode( "\n", $params );
423 }
424
431 public static function extractParams( $blob ) {
432 if ( $blob === '' ) {
433 return [];
434 } else {
435 return explode( "\n", $blob );
436 }
437 }
438
444 public function getName() {
445 $logNames = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogNames );
446
447 // BC
448 $key = $logNames[$this->type] ?? 'log-name-' . $this->type;
449
450 return wfMessage( $key );
451 }
452
458 public function getDescription() {
459 $logHeaders = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogHeaders );
460 // BC
461 $key = $logHeaders[$this->type] ?? 'log-description-' . $this->type;
462
463 return wfMessage( $key );
464 }
465
471 public function getRestriction() {
472 $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
473 // The empty string fallback will
474 // always return true in permission check
475 return $logRestrictions[$this->type] ?? '';
476 }
477
483 public function isRestricted() {
484 $restriction = $this->getRestriction();
485
486 return $restriction !== '' && $restriction !== '*';
487 }
488}
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.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
Definition Setup.php:535
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:88
Base class for language-specific code.
Definition Language.php:63
Class to simplify the use of log pages.
Definition LogPage.php:43
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition LogPage.php:240
getRestriction()
Returns the right needed to read this log type.
Definition LogPage.php:471
const SUPPRESSED_USER
Definition LogPage.php:50
__construct( $type, $rc=true, $udp='skipUDP')
Definition LogPage.php:94
const DELETED_USER
Definition LogPage.php:46
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition LogPage.php:421
const DELETED_RESTRICTED
Definition LogPage.php:47
bool $sendToUDP
Definition LogPage.php:57
isRestricted()
Tells if this log is not viewable by all.
Definition LogPage.php:483
static extractParams( $blob)
Extract a parameter array from a blob.
Definition LogPage.php:431
static isLogType( $type)
Is $type a valid log type.
Definition LogPage.php:223
getName()
Name of the log.
Definition LogPage.php:444
saveContent()
Definition LogPage.php:103
const DELETED_COMMENT
Definition LogPage.php:45
getComment()
Get the comment from the last addEntry() call.
Definition LogPage.php:202
bool $updateRecentChanges
Definition LogPage.php:54
getRcComment()
Get the RC comment from the last addEntry() call.
Definition LogPage.php:163
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:211
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition LogPage.php:183
getDescription()
Description of this log type.
Definition LogPage.php:458
const DELETED_ACTION
Definition LogPage.php:44
const SUPPRESSED_ACTION
Definition LogPage.php:51
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.
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:76
getDBkey()
Get the main part with underscores.
Definition Title.php:1049
isSpecialPage()
Returns true if this is a special page.
Definition Title.php:1267
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1885
internal since 1.36
Definition User.php:98
Interface for objects representing user identity.
const DB_PRIMARY
Definition defines.php:28