MediaWiki REL1_40
LogPage.php
Go to the documentation of this file.
1<?php
31
41class LogPage {
42 public const DELETED_ACTION = 1;
43 public const DELETED_COMMENT = 2;
44 public const DELETED_USER = 4;
45 public const DELETED_RESTRICTED = 8;
46
47 // Convenience fields
48 public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
49 public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
50
53
55 public $sendToUDP;
56
58 private $ircActionText;
59
61 private $actionText;
62
66 private $type;
67
71 private $action;
72
74 private $comment;
75
77 private $params;
78
80 private $performer;
81
83 private $target;
84
92 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
93 $this->type = $type;
94 $this->updateRecentChanges = $rc;
95 $this->sendToUDP = ( $udp == 'UDP' );
96 }
97
101 protected function saveContent() {
102 $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
103
104 $dbw = wfGetDB( DB_PRIMARY );
105
106 $now = wfTimestampNow();
107 $actorId = MediaWikiServices::getInstance()->getActorNormalization()
108 ->acquireActorId( $this->performer, $dbw );
109 $data = [
110 'log_type' => $this->type,
111 'log_action' => $this->action,
112 'log_timestamp' => $dbw->timestamp( $now ),
113 'log_actor' => $actorId,
114 'log_namespace' => $this->target->getNamespace(),
115 'log_title' => $this->target->getDBkey(),
116 'log_page' => $this->target->getArticleID(),
117 'log_params' => $this->params
118 ];
119 $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
120 $dbw,
121 'log_comment',
122 $this->comment
123 );
124 $dbw->insert( 'logging', $data, __METHOD__ );
125 $newId = $dbw->insertId();
126
127 # And update recentchanges
128 if ( $this->updateRecentChanges ) {
129 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
130
131 RecentChange::notifyLog(
132 $now, $titleObj, $this->performer, $this->getRcComment(), '',
133 $this->type, $this->action, $this->target, $this->comment,
134 $this->params, $newId, $this->getRcCommentIRC()
135 );
136 } elseif ( $this->sendToUDP ) {
137 # Don't send private logs to UDP
138 if ( isset( $logRestrictions[$this->type] ) && $logRestrictions[$this->type] != '*' ) {
139 return $newId;
140 }
141
142 // Notify external application via UDP.
143 // We send this to IRC but do not want to add it the RC table.
144 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
145 $rc = RecentChange::newLogEntry(
146 $now, $titleObj, $this->performer, $this->getRcComment(), '',
147 $this->type, $this->action, $this->target, $this->comment,
148 $this->params, $newId, $this->getRcCommentIRC()
149 );
150 $rc->notifyRCFeeds();
151 }
152
153 return $newId;
154 }
155
161 public function getRcComment() {
162 $rcComment = $this->actionText;
163
164 if ( $this->comment != '' ) {
165 if ( $rcComment == '' ) {
166 $rcComment = $this->comment;
167 } else {
168 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
169 $this->comment;
170 }
171 }
172
173 return $rcComment;
174 }
175
181 public function getRcCommentIRC() {
182 $rcComment = $this->ircActionText;
183
184 if ( $this->comment != '' ) {
185 if ( $rcComment == '' ) {
186 $rcComment = $this->comment;
187 } else {
188 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
189 $this->comment;
190 }
191 }
192
193 return $rcComment;
194 }
195
200 public function getComment() {
201 return $this->comment;
202 }
203
209 public static function validTypes() {
210 $logTypes = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogTypes );
211
212 return $logTypes;
213 }
214
221 public static function isLogType( $type ) {
222 return in_array( $type, self::validTypes() );
223 }
224
238 public static function actionText( $type, $action, $title = null, $skin = null,
239 $params = [], $filterWikilinks = false
240 ) {
241 global $wgLang;
242 $config = MediaWikiServices::getInstance()->getMainConfig();
243 $logActions = $config->get( MainConfigNames::LogActions );
244 $key = "$type/$action";
245
246 if ( isset( $logActions[$key] ) ) {
247 if ( $skin === null ) {
248 $langObj = MediaWikiServices::getInstance()->getContentLanguage();
249 $langObjOrNull = null;
250 } else {
251 // TODO Is $skin->getLanguage() safe here?
252 StubUserLang::unstub( $wgLang );
253 $langObj = $wgLang;
254 $langObjOrNull = $wgLang;
255 }
256 if ( $title === null ) {
257 $rv = wfMessage( $logActions[$key] )->inLanguage( $langObj )->escaped();
258 } else {
259 $titleLink = self::getTitleLink( $title, $langObjOrNull );
260
261 if ( count( $params ) == 0 ) {
262 $rv = wfMessage( $logActions[$key] )->rawParams( $titleLink )
263 ->inLanguage( $langObj )->escaped();
264 } else {
265 array_unshift( $params, $titleLink );
266
267 $rv = wfMessage( $logActions[$key] )->rawParams( $params )
268 ->inLanguage( $langObj )->escaped();
269 }
270 }
271 } else {
272 $logActionsHandlers = $config->get( MainConfigNames::LogActionsHandlers );
273
274 if ( isset( $logActionsHandlers[$key] ) ) {
275 $args = func_get_args();
276 $rv = call_user_func_array( $logActionsHandlers[$key], $args );
277 } else {
278 wfDebug( "LogPage::actionText - unknown action $key" );
279 $rv = "$action";
280 }
281 }
282
283 // For the perplexed, this feature was added in r7855 by Erik.
284 // The feature was added because we liked adding [[$1]] in our log entries
285 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
286 // on Special:Log. The hack is essentially that [[$1]] represented a link
287 // to the title in question. The first parameter to the HTML version (Special:Log)
288 // is that link in HTML form, and so this just gets rid of the ugly [[]].
289 // However, this is a horrible hack and it doesn't work like you expect if, say,
290 // you want to link to something OTHER than the title of the log entry.
291 // The real problem, which Erik was trying to fix (and it sort-of works now) is
292 // that the same messages are being treated as both wikitext *and* HTML.
293 if ( $filterWikilinks ) {
294 $rv = str_replace( '[[', '', $rv );
295 $rv = str_replace( ']]', '', $rv );
296 }
297
298 return $rv;
299 }
300
306 private static function getTitleLink( Title $title, ?Language $lang ): string {
307 if ( !$lang ) {
308 return $title->getPrefixedText();
309 }
310
311 $services = MediaWikiServices::getInstance();
312 $linkRenderer = $services->getLinkRenderer();
313
314 if ( $title->isSpecialPage() ) {
315 [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
316
317 if ( $name === 'Log' ) {
318 $logPage = new LogPage( $par );
319 return wfMessage( 'parentheses' )
320 ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
321 ->inLanguage( $lang )
322 ->escaped();
323 }
324 }
325
326 return $linkRenderer->makeLink( $title );
327 }
328
342 public function addEntry( $action, $target, $comment, $params, $performer ) {
343 // FIXME $params is only documented to accept an array
344 if ( !is_array( $params ) ) {
345 $params = [ $params ];
346 }
347
348 # Trim spaces on user supplied text
349 $comment = trim( $comment ?? '' );
350
351 $this->action = $action;
352 $this->target = $target;
353 $this->comment = $comment;
354 $this->params = self::makeParamBlob( $params );
355
356 if ( !is_object( $performer ) ) {
357 $performer = User::newFromId( $performer );
358 }
359
360 $this->performer = $performer;
361
362 $logEntry = new ManualLogEntry( $this->type, $action );
363 $logEntry->setTarget( $target );
364 $logEntry->setPerformer( $performer );
365 $logEntry->setParameters( $params );
366 // All log entries using the LogPage to insert into the logging table
367 // are using the old logging system and therefore the legacy flag is
368 // needed to say the LogFormatter the parameters have numeric keys
369 $logEntry->setLegacy( true );
370
371 $formatter = LogFormatter::newFromEntry( $logEntry );
372 $context = RequestContext::newExtraneousContext( $target );
373 $formatter->setContext( $context );
374
375 $this->actionText = $formatter->getPlainActionText();
376 $this->ircActionText = $formatter->getIRCActionText();
377
378 return $this->saveContent();
379 }
380
389 public function addRelations( $field, $values, $logid ) {
390 if ( !strlen( $field ) || empty( $values ) ) {
391 return false;
392 }
393
394 $data = [];
395
396 foreach ( $values as $value ) {
397 $data[] = [
398 'ls_field' => $field,
399 'ls_value' => $value,
400 'ls_log_id' => $logid
401 ];
402 }
403
404 $dbw = wfGetDB( DB_PRIMARY );
405 $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
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.
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:527
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
Base class for language-specific code.
Definition Language.php:56
Class to simplify the use of log pages.
Definition LogPage.php:41
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition LogPage.php:238
getRestriction()
Returns the right needed to read this log type.
Definition LogPage.php:466
const SUPPRESSED_USER
Definition LogPage.php:48
__construct( $type, $rc=true, $udp='skipUDP')
Definition LogPage.php:92
const DELETED_USER
Definition LogPage.php:44
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition LogPage.php:416
const DELETED_RESTRICTED
Definition LogPage.php:45
bool $sendToUDP
Definition LogPage.php:55
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:221
getName()
Name of the log.
Definition LogPage.php:439
saveContent()
Definition LogPage.php:101
const DELETED_COMMENT
Definition LogPage.php:43
getComment()
Get the comment from the last addEntry() call.
Definition LogPage.php:200
bool $updateRecentChanges
Definition LogPage.php:52
getRcComment()
Get the RC comment from the last addEntry() call.
Definition LogPage.php:161
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition LogPage.php:389
static validTypes()
Get the list of valid log types.
Definition LogPage.php:209
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition LogPage.php:181
getDescription()
Description of this log type.
Definition LogPage.php:453
const DELETED_ACTION
Definition LogPage.php:42
const SUPPRESSED_ACTION
Definition LogPage.php:49
addEntry( $action, $target, $comment, $params, $performer)
Add a log entry.
Definition LogPage.php:342
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.
Stub object for the user language.
Represents a title within MediaWiki.
Definition Title.php:82
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:626
Interface for objects representing user identity.
const DB_PRIMARY
Definition defines.php:28
if(!isset( $args[0])) $lang