MediaWiki  master
LogPage.php
Go to the documentation of this file.
1 <?php
31 
41 class 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 
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 );
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  $logActionsHandlers = $config->get( MainConfigNames::LogActionsHandlers );
244  $key = "$type/$action";
245 
246  if ( isset( $logActionsHandlers[$key] ) ) {
247  $args = func_get_args();
248  $rv = call_user_func_array( $logActionsHandlers[$key], $args );
249  } else {
250  $logActions = $config->get( MainConfigNames::LogActions );
251 
252  if ( isset( $logActions[$key] ) ) {
253  $message = $logActions[$key];
254  } else {
255  wfDebug( "LogPage::actionText - unknown action $key" );
256  $message = "log-unknown-action";
257  $params = [ $key ];
258  }
259 
260  if ( $skin === null ) {
261  $langObj = MediaWikiServices::getInstance()->getContentLanguage();
262  $langObjOrNull = null;
263  } else {
264  // TODO Is $skin->getLanguage() safe here?
265  StubUserLang::unstub( $wgLang );
266  $langObj = $wgLang;
267  $langObjOrNull = $wgLang;
268  }
269  if ( $title === null ) {
270  $rv = wfMessage( $message )->inLanguage( $langObj )->escaped();
271  } else {
272  $titleLink = self::getTitleLink( $title, $langObjOrNull );
273 
274  if ( count( $params ) == 0 ) {
275  $rv = wfMessage( $message )->rawParams( $titleLink )
276  ->inLanguage( $langObj )->escaped();
277  } else {
278  array_unshift( $params, $titleLink );
279 
280  $rv = wfMessage( $message )->rawParams( $params )
281  ->inLanguage( $langObj )->escaped();
282  }
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 ) || empty( $values ) ) {
394  return false;
395  }
396 
397  $data = [];
398 
399  foreach ( $values as $value ) {
400  $data[] = [
401  'ls_field' => $field,
402  'ls_value' => $value,
403  'ls_log_id' => $logid
404  ];
405  }
406 
407  $dbw = wfGetDB( DB_PRIMARY );
408  $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
409 
410  return true;
411  }
412 
419  public static function makeParamBlob( $params ) {
420  return implode( "\n", $params );
421  }
422 
429  public static function extractParams( $blob ) {
430  if ( $blob === '' ) {
431  return [];
432  } else {
433  return explode( "\n", $blob );
434  }
435  }
436 
442  public function getName() {
443  $logNames = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogNames );
444 
445  // BC
446  $key = $logNames[$this->type] ?? 'log-name-' . $this->type;
447 
448  return wfMessage( $key );
449  }
450 
456  public function getDescription() {
457  $logHeaders = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogHeaders );
458  // BC
459  $key = $logHeaders[$this->type] ?? 'log-description-' . $this->type;
460 
461  return wfMessage( $key );
462  }
463 
469  public function getRestriction() {
470  $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
471  // The empty string fallback will
472  // always return true in permission check
473  return $logRestrictions[$this->type] ?? '';
474  }
475 
481  public function isRestricted() {
482  $restriction = $this->getRestriction();
483 
484  return $restriction !== '' && $restriction !== '*';
485  }
486 }
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:528
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
Base class for language-specific code.
Definition: Language.php:57
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
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:469
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:419
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:481
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:429
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:221
getName()
Name of the log.
Definition: LogPage.php:442
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:392
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:456
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:345
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 newLogEntry( $timestamp, $logPage, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false, $forceBotFlag=null)
static notifyLog( $timestamp, $logPage, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='')
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
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