MediaWiki  master
LogPage.php
Go to the documentation of this file.
1 <?php
29 
39 class LogPage {
40  public const DELETED_ACTION = 1;
41  public const DELETED_COMMENT = 2;
42  public const DELETED_USER = 4;
43  public const DELETED_RESTRICTED = 8;
44 
45  // Convenience fields
46  public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
47  public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
48 
51 
53  public $sendToUDP;
54 
56  private $ircActionText;
57 
59  private $actionText;
60 
64  private $type;
65 
69  private $action;
70 
72  private $comment;
73 
75  private $params;
76 
78  private $performer;
79 
81  private $target;
82 
90  public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
91  $this->type = $type;
92  $this->updateRecentChanges = $rc;
93  $this->sendToUDP = ( $udp == 'UDP' );
94  }
95 
99  protected function saveContent() {
100  $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
101 
102  $dbw = wfGetDB( DB_PRIMARY );
103 
104  $now = wfTimestampNow();
105  $actorId = MediaWikiServices::getInstance()->getActorNormalization()
106  ->acquireActorId( $this->performer, $dbw );
107  $data = [
108  'log_type' => $this->type,
109  'log_action' => $this->action,
110  'log_timestamp' => $dbw->timestamp( $now ),
111  'log_actor' => $actorId,
112  'log_namespace' => $this->target->getNamespace(),
113  'log_title' => $this->target->getDBkey(),
114  'log_page' => $this->target->getArticleID(),
115  'log_params' => $this->params
116  ];
117  $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
118  $dbw,
119  'log_comment',
120  $this->comment
121  );
122  $dbw->insert( 'logging', $data, __METHOD__ );
123  $newId = $dbw->insertId();
124 
125  # And update recentchanges
126  if ( $this->updateRecentChanges ) {
127  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
128 
130  $now, $titleObj, $this->performer, $this->getRcComment(), '',
131  $this->type, $this->action, $this->target, $this->comment,
132  $this->params, $newId, $this->getRcCommentIRC()
133  );
134  } elseif ( $this->sendToUDP ) {
135  # Don't send private logs to UDP
136  if ( isset( $logRestrictions[$this->type] ) && $logRestrictions[$this->type] != '*' ) {
137  return $newId;
138  }
139 
140  // Notify external application via UDP.
141  // We send this to IRC but do not want to add it the RC table.
142  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
144  $now, $titleObj, $this->performer, $this->getRcComment(), '',
145  $this->type, $this->action, $this->target, $this->comment,
146  $this->params, $newId, $this->getRcCommentIRC()
147  );
148  $rc->notifyRCFeeds();
149  }
150 
151  return $newId;
152  }
153 
159  public function getRcComment() {
160  $rcComment = $this->actionText;
161 
162  if ( $this->comment != '' ) {
163  if ( $rcComment == '' ) {
164  $rcComment = $this->comment;
165  } else {
166  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
167  $this->comment;
168  }
169  }
170 
171  return $rcComment;
172  }
173 
179  public function getRcCommentIRC() {
180  $rcComment = $this->ircActionText;
181 
182  if ( $this->comment != '' ) {
183  if ( $rcComment == '' ) {
184  $rcComment = $this->comment;
185  } else {
186  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
187  $this->comment;
188  }
189  }
190 
191  return $rcComment;
192  }
193 
198  public function getComment() {
199  return $this->comment;
200  }
201 
207  public static function validTypes() {
208  $logTypes = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogTypes );
209 
210  return $logTypes;
211  }
212 
219  public static function isLogType( $type ) {
220  return in_array( $type, self::validTypes() );
221  }
222 
236  public static function actionText( $type, $action, $title = null, $skin = null,
237  $params = [], $filterWikilinks = false
238  ) {
239  global $wgLang;
240  $config = MediaWikiServices::getInstance()->getMainConfig();
241  $logActions = $config->get( MainConfigNames::LogActions );
242  $key = "$type/$action";
243 
244  if ( isset( $logActions[$key] ) ) {
245  if ( $skin === null ) {
246  $langObj = MediaWikiServices::getInstance()->getContentLanguage();
247  $langObjOrNull = null;
248  } else {
249  // TODO Is $skin->getLanguage() safe here?
251  $langObj = $wgLang;
252  $langObjOrNull = $wgLang;
253  }
254  if ( $title === null ) {
255  $rv = wfMessage( $logActions[$key] )->inLanguage( $langObj )->escaped();
256  } else {
257  $titleLink = self::getTitleLink( $title, $langObjOrNull );
258 
259  if ( count( $params ) == 0 ) {
260  $rv = wfMessage( $logActions[$key] )->rawParams( $titleLink )
261  ->inLanguage( $langObj )->escaped();
262  } else {
263  array_unshift( $params, $titleLink );
264 
265  $rv = wfMessage( $logActions[$key] )->rawParams( $params )
266  ->inLanguage( $langObj )->escaped();
267  }
268  }
269  } else {
270  $logActionsHandlers = $config->get( MainConfigNames::LogActionsHandlers );
271 
272  if ( isset( $logActionsHandlers[$key] ) ) {
273  $args = func_get_args();
274  $rv = call_user_func_array( $logActionsHandlers[$key], $args );
275  } else {
276  wfDebug( "LogPage::actionText - unknown action $key" );
277  $rv = "$action";
278  }
279  }
280 
281  // For the perplexed, this feature was added in r7855 by Erik.
282  // The feature was added because we liked adding [[$1]] in our log entries
283  // but the log entries are parsed as Wikitext on RecentChanges but as HTML
284  // on Special:Log. The hack is essentially that [[$1]] represented a link
285  // to the title in question. The first parameter to the HTML version (Special:Log)
286  // is that link in HTML form, and so this just gets rid of the ugly [[]].
287  // However, this is a horrible hack and it doesn't work like you expect if, say,
288  // you want to link to something OTHER than the title of the log entry.
289  // The real problem, which Erik was trying to fix (and it sort-of works now) is
290  // that the same messages are being treated as both wikitext *and* HTML.
291  if ( $filterWikilinks ) {
292  $rv = str_replace( '[[', '', $rv );
293  $rv = str_replace( ']]', '', $rv );
294  }
295 
296  return $rv;
297  }
298 
304  private static function getTitleLink( Title $title, ?Language $lang ): string {
305  if ( !$lang ) {
306  return $title->getPrefixedText();
307  }
308 
309  $services = MediaWikiServices::getInstance();
310  $linkRenderer = $services->getLinkRenderer();
311 
312  if ( $title->isSpecialPage() ) {
313  [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
314 
315  if ( $name === 'Log' ) {
316  $logPage = new LogPage( $par );
317  return wfMessage( 'parentheses' )
318  ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
319  ->inLanguage( $lang )
320  ->escaped();
321  }
322  }
323 
324  return $linkRenderer->makeLink( $title );
325  }
326 
340  public function addEntry( $action, $target, $comment, $params, $performer ) {
341  // FIXME $params is only documented to accept an array
342  if ( !is_array( $params ) ) {
343  $params = [ $params ];
344  }
345 
346  if ( $comment === null ) {
347  $comment = '';
348  }
349 
350  # Trim spaces on user supplied text
351  $comment = trim( $comment );
352 
353  $this->action = $action;
354  $this->target = $target;
355  $this->comment = $comment;
356  $this->params = self::makeParamBlob( $params );
357 
358  if ( !is_object( $performer ) ) {
359  $performer = User::newFromId( $performer );
360  }
361 
362  $this->performer = $performer;
363 
364  $logEntry = new ManualLogEntry( $this->type, $action );
365  $logEntry->setTarget( $target );
366  $logEntry->setPerformer( $performer );
367  $logEntry->setParameters( $params );
368  // All log entries using the LogPage to insert into the logging table
369  // are using the old logging system and therefore the legacy flag is
370  // needed to say the LogFormatter the parameters have numeric keys
371  $logEntry->setLegacy( true );
372 
373  $formatter = LogFormatter::newFromEntry( $logEntry );
374  $context = RequestContext::newExtraneousContext( $target );
375  $formatter->setContext( $context );
376 
377  $this->actionText = $formatter->getPlainActionText();
378  $this->ircActionText = $formatter->getIRCActionText();
379 
380  return $this->saveContent();
381  }
382 
391  public function addRelations( $field, $values, $logid ) {
392  if ( !strlen( $field ) || empty( $values ) ) {
393  return false;
394  }
395 
396  $data = [];
397 
398  foreach ( $values as $value ) {
399  $data[] = [
400  'ls_field' => $field,
401  'ls_value' => $value,
402  'ls_log_id' => $logid
403  ];
404  }
405 
406  $dbw = wfGetDB( DB_PRIMARY );
407  $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
408 
409  return true;
410  }
411 
418  public static function makeParamBlob( $params ) {
419  return implode( "\n", $params );
420  }
421 
428  public static function extractParams( $blob ) {
429  if ( $blob === '' ) {
430  return [];
431  } else {
432  return explode( "\n", $blob );
433  }
434  }
435 
441  public function getName() {
442  $logNames = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogNames );
443 
444  // BC
445  $key = $logNames[$this->type] ?? 'log-name-' . $this->type;
446 
447  return wfMessage( $key );
448  }
449 
455  public function getDescription() {
456  $logHeaders = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogHeaders );
457  // BC
458  $key = $logHeaders[$this->type] ?? 'log-description-' . $this->type;
459 
460  return wfMessage( $key );
461  }
462 
468  public function getRestriction() {
469  $logRestrictions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LogRestrictions );
470  // The empty string fallback will
471  // always return true in permission check
472  return $logRestrictions[$this->type] ?? '';
473  }
474 
480  public function isRestricted() {
481  $restriction = $this->getRestriction();
482 
483  return $restriction !== '' && $restriction !== '*';
484  }
485 }
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:497
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:82
Base class for language-specific code.
Definition: Language.php:53
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Class to simplify the use of log pages.
Definition: LogPage.php:39
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition: LogPage.php:236
getRestriction()
Returns the right needed to read this log type.
Definition: LogPage.php:468
const SUPPRESSED_USER
Definition: LogPage.php:46
__construct( $type, $rc=true, $udp='skipUDP')
Definition: LogPage.php:90
const DELETED_USER
Definition: LogPage.php:42
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition: LogPage.php:418
const DELETED_RESTRICTED
Definition: LogPage.php:43
bool $sendToUDP
Definition: LogPage.php:53
isRestricted()
Tells if this log is not viewable by all.
Definition: LogPage.php:480
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:428
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:219
getName()
Name of the log.
Definition: LogPage.php:441
saveContent()
Definition: LogPage.php:99
const DELETED_COMMENT
Definition: LogPage.php:41
getComment()
Get the comment from the last addEntry() call.
Definition: LogPage.php:198
bool $updateRecentChanges
Definition: LogPage.php:50
getRcComment()
Get the RC comment from the last addEntry() call.
Definition: LogPage.php:159
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition: LogPage.php:391
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:207
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition: LogPage.php:179
getDescription()
Description of this log type.
Definition: LogPage.php:455
const DELETED_ACTION
Definition: LogPage.php:40
const SUPPRESSED_ACTION
Definition: LogPage.php:47
addEntry( $action, $target, $comment, $params, $performer)
Add a log entry.
Definition: LogPage.php:340
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.
static newLogEntry( $timestamp, $logPage, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
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 unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:101
Represents a title within MediaWiki.
Definition: Title.php:49
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:639
Interface for objects representing user identity.
if( $line===false) $args
Definition: mcc.php:124
const DB_PRIMARY
Definition: defines.php:28
if(!isset( $args[0])) $lang