MediaWiki  master
LogPage.php
Go to the documentation of this file.
1 <?php
28 
38 class LogPage {
39  public const DELETED_ACTION = 1;
40  public const DELETED_COMMENT = 2;
41  public const DELETED_USER = 4;
42  public const DELETED_RESTRICTED = 8;
43 
44  // Convenience fields
45  public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
46  public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
47 
50 
52  public $sendToUDP;
53 
55  private $ircActionText;
56 
58  private $actionText;
59 
63  private $type;
64 
68  private $action;
69 
71  private $comment;
72 
74  private $params;
75 
77  private $performer;
78 
80  private $target;
81 
89  public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
90  $this->type = $type;
91  $this->updateRecentChanges = $rc;
92  $this->sendToUDP = ( $udp == 'UDP' );
93  }
94 
98  protected function saveContent() {
99  global $wgLogRestrictions;
100 
101  $dbw = wfGetDB( DB_PRIMARY );
102 
103  $now = wfTimestampNow();
104  $actorId = MediaWikiServices::getInstance()->getActorNormalization()
105  ->acquireActorId( $this->performer, $dbw );
106  $data = [
107  'log_type' => $this->type,
108  'log_action' => $this->action,
109  'log_timestamp' => $dbw->timestamp( $now ),
110  'log_actor' => $actorId,
111  'log_namespace' => $this->target->getNamespace(),
112  'log_title' => $this->target->getDBkey(),
113  'log_page' => $this->target->getArticleID(),
114  'log_params' => $this->params
115  ];
116  $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
117  $dbw,
118  'log_comment',
119  $this->comment
120  );
121  $dbw->insert( 'logging', $data, __METHOD__ );
122  $newId = $dbw->insertId();
123 
124  # And update recentchanges
125  if ( $this->updateRecentChanges ) {
126  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
127 
129  $now, $titleObj, $this->performer, $this->getRcComment(), '',
130  $this->type, $this->action, $this->target, $this->comment,
131  $this->params, $newId, $this->getRcCommentIRC()
132  );
133  } elseif ( $this->sendToUDP ) {
134  # Don't send private logs to UDP
135  if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
136  return $newId;
137  }
138 
139  # Notify external application via UDP.
140  # We send this to IRC but do not want to add it the RC table.
141  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
143  $now, $titleObj, $this->performer, $this->getRcComment(), '',
144  $this->type, $this->action, $this->target, $this->comment,
145  $this->params, $newId, $this->getRcCommentIRC()
146  );
147  $rc->notifyRCFeeds();
148  }
149 
150  return $newId;
151  }
152 
158  public function getRcComment() {
159  $rcComment = $this->actionText;
160 
161  if ( $this->comment != '' ) {
162  if ( $rcComment == '' ) {
163  $rcComment = $this->comment;
164  } else {
165  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
167  }
168  }
169 
170  return $rcComment;
171  }
172 
178  public function getRcCommentIRC() {
179  $rcComment = $this->ircActionText;
180 
181  if ( $this->comment != '' ) {
182  if ( $rcComment == '' ) {
183  $rcComment = $this->comment;
184  } else {
185  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
187  }
188  }
189 
190  return $rcComment;
191  }
192 
197  public function getComment() {
198  return $this->comment;
199  }
200 
206  public static function validTypes() {
207  global $wgLogTypes;
208 
209  return $wgLogTypes;
210  }
211 
218  public static function isLogType( $type ) {
219  return in_array( $type, self::validTypes() );
220  }
221 
235  public static function actionText( $type, $action, $title = null, $skin = null,
236  $params = [], $filterWikilinks = false
237  ) {
238  global $wgLang, $wgLogActions;
239 
240  $key = "$type/$action";
241 
242  if ( isset( $wgLogActions[$key] ) ) {
243  if ( $skin === null ) {
244  $langObj = MediaWikiServices::getInstance()->getContentLanguage();
245  $langObjOrNull = null;
246  } else {
247  // TODO Is $skin->getLanguage() safe here?
249  $langObj = $wgLang;
250  $langObjOrNull = $wgLang;
251  }
252  if ( $title === null ) {
253  $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
254  } else {
255  $titleLink = self::getTitleLink( $title, $langObjOrNull );
256 
257  if ( count( $params ) == 0 ) {
258  // @phan-suppress-next-line SecurityCheck-XSS mixed between plaintext and html
259  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
260  ->inLanguage( $langObj )->escaped();
261  } else {
262  array_unshift( $params, $titleLink );
263 
264  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
265  ->inLanguage( $langObj )->escaped();
266  }
267  }
268  } else {
269  global $wgLogActionsHandlers;
270 
271  if ( isset( $wgLogActionsHandlers[$key] ) ) {
272  $args = func_get_args();
273  $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
274  } else {
275  wfDebug( "LogPage::actionText - unknown action $key" );
276  $rv = "$action";
277  }
278  }
279 
280  // For the perplexed, this feature was added in r7855 by Erik.
281  // The feature was added because we liked adding [[$1]] in our log entries
282  // but the log entries are parsed as Wikitext on RecentChanges but as HTML
283  // on Special:Log. The hack is essentially that [[$1]] represented a link
284  // to the title in question. The first parameter to the HTML version (Special:Log)
285  // is that link in HTML form, and so this just gets rid of the ugly [[]].
286  // However, this is a horrible hack and it doesn't work like you expect if, say,
287  // you want to link to something OTHER than the title of the log entry.
288  // The real problem, which Erik was trying to fix (and it sort-of works now) is
289  // that the same messages are being treated as both wikitext *and* HTML.
290  if ( $filterWikilinks ) {
291  $rv = str_replace( '[[', '', $rv );
292  $rv = str_replace( ']]', '', $rv );
293  }
294 
295  return $rv;
296  }
297 
303  private static function getTitleLink( Title $title, ?Language $lang ): string {
304  if ( !$lang ) {
305  return $title->getPrefixedText();
306  }
307 
308  $services = MediaWikiServices::getInstance();
309  $linkRenderer = $services->getLinkRenderer();
310 
311  if ( $title->isSpecialPage() ) {
312  [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
313 
314  if ( $name === 'Log' ) {
315  $logPage = new LogPage( $par );
316  return wfMessage( 'parentheses' )
317  ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
318  ->inLanguage( $lang )
319  ->escaped();
320  }
321  }
322 
323  return $linkRenderer->makeLink( $title );
324  }
325 
340  // FIXME $params is only documented to accept an array
341  if ( !is_array( $params ) ) {
342  $params = [ $params ];
343  }
344 
345  if ( $comment === null ) {
346  $comment = '';
347  }
348 
349  # Trim spaces on user supplied text
350  $comment = trim( $comment );
351 
352  $this->action = $action;
353  $this->target = $target;
354  $this->comment = $comment;
355  $this->params = self::makeParamBlob( $params );
356 
357  if ( !is_object( $performer ) ) {
359  }
360 
361  $this->performer = $performer;
362 
363  $logEntry = new ManualLogEntry( $this->type, $action );
364  $logEntry->setTarget( $target );
365  $logEntry->setPerformer( $performer );
366  $logEntry->setParameters( $params );
367  // All log entries using the LogPage to insert into the logging table
368  // are using the old logging system and therefore the legacy flag is
369  // needed to say the LogFormatter the parameters have numeric keys
370  $logEntry->setLegacy( true );
371 
372  $formatter = LogFormatter::newFromEntry( $logEntry );
374  $formatter->setContext( $context );
375 
376  $this->actionText = $formatter->getPlainActionText();
377  $this->ircActionText = $formatter->getIRCActionText();
378 
379  return $this->saveContent();
380  }
381 
390  public function addRelations( $field, $values, $logid ) {
391  if ( !strlen( $field ) || empty( $values ) ) {
392  return false;
393  }
394 
395  $data = [];
396 
397  foreach ( $values as $value ) {
398  $data[] = [
399  'ls_field' => $field,
400  'ls_value' => $value,
401  'ls_log_id' => $logid
402  ];
403  }
404 
405  $dbw = wfGetDB( DB_PRIMARY );
406  $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
407 
408  return true;
409  }
410 
417  public static function makeParamBlob( $params ) {
418  return implode( "\n", $params );
419  }
420 
427  public static function extractParams( $blob ) {
428  if ( $blob === '' ) {
429  return [];
430  } else {
431  return explode( "\n", $blob );
432  }
433  }
434 
440  public function getName() {
441  global $wgLogNames;
442 
443  // BC
444  $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
445 
446  return wfMessage( $key );
447  }
448 
454  public function getDescription() {
455  global $wgLogHeaders;
456  // BC
457  $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
458 
459  return wfMessage( $key );
460  }
461 
467  public function getRestriction() {
468  global $wgLogRestrictions;
469  // The empty string fallback will
470  // always return true in permission check
471  return $wgLogRestrictions[$this->type] ?? '';
472  }
473 
479  public function isRestricted() {
480  $restriction = $this->getRestriction();
481 
482  return $restriction !== '' && $restriction !== '*';
483  }
484 }
LogPage\SUPPRESSED_ACTION
const SUPPRESSED_ACTION
Definition: LogPage.php:46
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:643
LogPage\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: LogPage.php:45
LogPage\validTypes
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:206
RecentChange\newLogEntry
static newLogEntry( $timestamp, $logPage, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:919
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:186
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
$wgLogHeaders
$wgLogHeaders
Lists the message key string for descriptive text to be shown at the top of each log type.
Definition: DefaultSettings.php:8975
LogPage\extractParams
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:427
LogPage\isRestricted
isRestricted()
Tells if this log is not viewable by all.
Definition: LogPage.php:479
LogPage\$params
string $params
Blob made of a parameters array.
Definition: LogPage.php:74
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1182
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:658
$wgLogActions
$wgLogActions
Lists the message key string for formatting individual events of each type and action when listed in ...
Definition: DefaultSettings.php:8995
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:107
LogPage\actionText
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition: LogPage.php:235
$wgLang
$wgLang
Definition: Setup.php:807
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
LogPage\addEntry
addEntry( $action, $target, $comment, $params, $performer)
Add a log entry.
Definition: LogPage.php:339
LogPage\getTitleLink
static getTitleLink(Title $title, ?Language $lang)
Definition: LogPage.php:303
LogPage\__construct
__construct( $type, $rc=true, $udp='skipUDP')
Definition: LogPage.php:89
LogPage\getRestriction
getRestriction()
Returns the right needed to read this log type.
Definition: LogPage.php:467
LogPage\getComment
getComment()
Get the comment from the last addEntry() call.
Definition: LogPage.php:197
LogPage\DELETED_COMMENT
const DELETED_COMMENT
Definition: LogPage.php:40
$blob
$blob
Definition: testCompression.php:70
LogPage\DELETED_USER
const DELETED_USER
Definition: LogPage.php:41
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2226
LogPage\isLogType
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:218
LogPage\getDescription
getDescription()
Description of this log type.
Definition: LogPage.php:454
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:38
LogPage\getName
getName()
Name of the log.
Definition: LogPage.php:440
$args
if( $line===false) $args
Definition: mcc.php:124
$wgLogTypes
$wgLogTypes
The logging system has two levels: an event type, which describes the general category and can be vie...
Definition: DefaultSettings.php:8890
$title
$title
Definition: testCompression.php:38
LogPage\getRcCommentIRC
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition: LogPage.php:178
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1721
RecentChange\notifyLog
static notifyLog( $timestamp, $logPage, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='')
Definition: RecentChange.php:883
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:39
LogPage\$action
string $action
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'.
Definition: LogPage.php:68
LogPage\$comment
string $comment
Comment associated with action.
Definition: LogPage.php:71
LogPage\makeParamBlob
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition: LogPage.php:417
$wgLogRestrictions
$wgLogRestrictions
This restricts log access to those who have a certain right Users without this will not see it in the...
Definition: DefaultSettings.php:8914
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
LogPage\$sendToUDP
bool $sendToUDP
Definition: LogPage.php:52
LogPage\getRcComment
getRcComment()
Get the RC comment from the last addEntry() call.
Definition: LogPage.php:158
LogPage\saveContent
saveContent()
Definition: LogPage.php:98
LogPage\$type
string $type
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move'.
Definition: LogPage.php:63
$wgLogActionsHandlers
$wgLogActionsHandlers
The same as above, but here values are names of classes, not messages.
Definition: DefaultSettings.php:9003
LogPage\addRelations
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition: LogPage.php:390
LogPage\$ircActionText
string $ircActionText
Plaintext version of the message for IRC.
Definition: LogPage.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:49
LogPage\$actionText
string $actionText
Plaintext version of the message.
Definition: LogPage.php:58
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:44
$wgLogNames
$wgLogNames
Lists the message key string for each log type.
Definition: DefaultSettings.php:8952
LogPage\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: LogPage.php:42
LogPage\$updateRecentChanges
bool $updateRecentChanges
Definition: LogPage.php:49
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:101
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
LogPage\$performer
UserIdentity $performer
The user doing the action.
Definition: LogPage.php:77
LogPage\$target
Title $target
Definition: LogPage.php:80
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:54