MediaWiki  master
LogPage.php
Go to the documentation of this file.
1 <?php
27 
37 class LogPage {
38  public const DELETED_ACTION = 1;
39  public const DELETED_COMMENT = 2;
40  public const DELETED_USER = 4;
41  public const DELETED_RESTRICTED = 8;
42 
43  // Convenience fields
44  public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
45  public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
46 
49 
51  public $sendToUDP;
52 
54  private $ircActionText;
55 
57  private $actionText;
58 
62  private $type;
63 
67  private $action;
68 
70  private $comment;
71 
73  private $params;
74 
76  private $doer;
77 
79  private $target;
80 
88  public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
89  $this->type = $type;
90  $this->updateRecentChanges = $rc;
91  $this->sendToUDP = ( $udp == 'UDP' );
92  }
93 
97  protected function saveContent() {
98  global $wgLogRestrictions;
99 
100  $dbw = wfGetDB( DB_MASTER );
101 
102  $now = wfTimestampNow();
103  $data = [
104  'log_type' => $this->type,
105  'log_action' => $this->action,
106  'log_timestamp' => $dbw->timestamp( $now ),
107  'log_namespace' => $this->target->getNamespace(),
108  'log_title' => $this->target->getDBkey(),
109  'log_page' => $this->target->getArticleID(),
110  'log_params' => $this->params
111  ];
112  $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
113  $dbw,
114  'log_comment',
115  $this->comment
116  );
117  $data += ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $this->doer );
118  $dbw->insert( 'logging', $data, __METHOD__ );
119  $newId = $dbw->insertId();
120 
121  # And update recentchanges
122  if ( $this->updateRecentChanges ) {
123  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
124 
126  $now, $titleObj, $this->doer, $this->getRcComment(), '',
127  $this->type, $this->action, $this->target, $this->comment,
128  $this->params, $newId, $this->getRcCommentIRC()
129  );
130  } elseif ( $this->sendToUDP ) {
131  # Don't send private logs to UDP
132  if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
133  return $newId;
134  }
135 
136  # Notify external application via UDP.
137  # We send this to IRC but do not want to add it the RC table.
138  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
140  $now, $titleObj, $this->doer, $this->getRcComment(), '',
141  $this->type, $this->action, $this->target, $this->comment,
142  $this->params, $newId, $this->getRcCommentIRC()
143  );
144  $rc->notifyRCFeeds();
145  }
146 
147  return $newId;
148  }
149 
155  public function getRcComment() {
156  $rcComment = $this->actionText;
157 
158  if ( $this->comment != '' ) {
159  if ( $rcComment == '' ) {
160  $rcComment = $this->comment;
161  } else {
162  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
164  }
165  }
166 
167  return $rcComment;
168  }
169 
175  public function getRcCommentIRC() {
176  $rcComment = $this->ircActionText;
177 
178  if ( $this->comment != '' ) {
179  if ( $rcComment == '' ) {
180  $rcComment = $this->comment;
181  } else {
182  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
184  }
185  }
186 
187  return $rcComment;
188  }
189 
194  public function getComment() {
195  return $this->comment;
196  }
197 
203  public static function validTypes() {
204  global $wgLogTypes;
205 
206  return $wgLogTypes;
207  }
208 
215  public static function isLogType( $type ) {
216  return in_array( $type, self::validTypes() );
217  }
218 
232  public static function actionText( $type, $action, $title = null, $skin = null,
233  $params = [], $filterWikilinks = false
234  ) {
235  global $wgLang, $wgLogActions;
236 
237  $key = "$type/$action";
238 
239  if ( isset( $wgLogActions[$key] ) ) {
240  if ( $skin === null ) {
241  $langObj = MediaWikiServices::getInstance()->getContentLanguage();
242  $langObjOrNull = null;
243  } else {
244  // TODO Is $skin->getLanguage() safe here?
246  $langObj = $wgLang;
247  $langObjOrNull = $wgLang;
248  }
249  if ( $title === null ) {
250  $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
251  } else {
252  $titleLink = self::getTitleLink( $title, $langObjOrNull );
253 
254  if ( count( $params ) == 0 ) {
255  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
256  ->inLanguage( $langObj )->escaped();
257  } else {
258  array_unshift( $params, $titleLink );
259 
260  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
261  ->inLanguage( $langObj )->escaped();
262  }
263  }
264  } else {
265  global $wgLogActionsHandlers;
266 
267  if ( isset( $wgLogActionsHandlers[$key] ) ) {
268  $args = func_get_args();
269  $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
270  } else {
271  wfDebug( "LogPage::actionText - unknown action $key" );
272  $rv = "$action";
273  }
274  }
275 
276  // For the perplexed, this feature was added in r7855 by Erik.
277  // The feature was added because we liked adding [[$1]] in our log entries
278  // but the log entries are parsed as Wikitext on RecentChanges but as HTML
279  // on Special:Log. The hack is essentially that [[$1]] represented a link
280  // to the title in question. The first parameter to the HTML version (Special:Log)
281  // is that link in HTML form, and so this just gets rid of the ugly [[]].
282  // However, this is a horrible hack and it doesn't work like you expect if, say,
283  // you want to link to something OTHER than the title of the log entry.
284  // The real problem, which Erik was trying to fix (and it sort-of works now) is
285  // that the same messages are being treated as both wikitext *and* HTML.
286  if ( $filterWikilinks ) {
287  $rv = str_replace( '[[', '', $rv );
288  $rv = str_replace( ']]', '', $rv );
289  }
290 
291  return $rv;
292  }
293 
299  private static function getTitleLink( Title $title, ?Language $lang ) : string {
300  if ( !$lang ) {
301  return $title->getPrefixedText();
302  }
303 
304  $services = MediaWikiServices::getInstance();
305  $linkRenderer = $services->getLinkRenderer();
306 
307  if ( $title->isSpecialPage() ) {
308  [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
309 
310  if ( $name === 'Log' ) {
311  $logPage = new LogPage( $par );
312  return wfMessage( 'parentheses' )
313  ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
314  ->inLanguage( $lang )
315  ->escaped();
316  }
317  }
318 
319  return $linkRenderer->makeLink( $title );
320  }
321 
334  public function addEntry( $action, $target, $comment, $params, $doer ) {
335  // FIXME $params is only documented to accept an array
336  if ( !is_array( $params ) ) {
337  $params = [ $params ];
338  }
339 
340  if ( $comment === null ) {
341  $comment = '';
342  }
343 
344  # Trim spaces on user supplied text
345  $comment = trim( $comment );
346 
347  $this->action = $action;
348  $this->target = $target;
349  $this->comment = $comment;
350  $this->params = self::makeParamBlob( $params );
351 
352  if ( !is_object( $doer ) ) {
354  }
355 
356  $this->doer = $doer;
357 
358  $logEntry = new ManualLogEntry( $this->type, $action );
359  $logEntry->setTarget( $target );
360  $logEntry->setPerformer( $doer );
361  $logEntry->setParameters( $params );
362  // All log entries using the LogPage to insert into the logging table
363  // are using the old logging system and therefore the legacy flag is
364  // needed to say the LogFormatter the parameters have numeric keys
365  $logEntry->setLegacy( true );
366 
367  $formatter = LogFormatter::newFromEntry( $logEntry );
369  $formatter->setContext( $context );
370 
371  $this->actionText = $formatter->getPlainActionText();
372  $this->ircActionText = $formatter->getIRCActionText();
373 
374  return $this->saveContent();
375  }
376 
385  public function addRelations( $field, $values, $logid ) {
386  if ( !strlen( $field ) || empty( $values ) ) {
387  return false;
388  }
389 
390  $data = [];
391 
392  foreach ( $values as $value ) {
393  $data[] = [
394  'ls_field' => $field,
395  'ls_value' => $value,
396  'ls_log_id' => $logid
397  ];
398  }
399 
400  $dbw = wfGetDB( DB_MASTER );
401  $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
402 
403  return true;
404  }
405 
412  public static function makeParamBlob( $params ) {
413  return implode( "\n", $params );
414  }
415 
422  public static function extractParams( $blob ) {
423  if ( $blob === '' ) {
424  return [];
425  } else {
426  return explode( "\n", $blob );
427  }
428  }
429 
435  public function getName() {
436  global $wgLogNames;
437 
438  // BC
439  $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
440 
441  return wfMessage( $key );
442  }
443 
449  public function getDescription() {
450  global $wgLogHeaders;
451  // BC
452  $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
453 
454  return wfMessage( $key );
455  }
456 
462  public function getRestriction() {
463  global $wgLogRestrictions;
464  // '' always returns true with $user->isAllowed()
465  return $wgLogRestrictions[$this->type] ?? '';
466  }
467 
473  public function isRestricted() {
474  $restriction = $this->getRestriction();
475 
476  return $restriction !== '' && $restriction !== '*';
477  }
478 }
LogPage\SUPPRESSED_ACTION
const SUPPRESSED_ACTION
Definition: LogPage.php:45
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:579
LogPage\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: LogPage.php:44
LogPage\validTypes
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:203
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:160
$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:8213
LogPage\extractParams
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:422
LogPage\isRestricted
isRestricted()
Tells if this log is not viewable by all.
Definition: LogPage.php:473
RecentChange\notifyLog
static notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='')
Definition: RecentChange.php:801
LogPage\$params
string $params
Blob made of a parameters array.
Definition: LogPage.php:73
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1220
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:624
$wgLogActions
$wgLogActions
Lists the message key string for formatting individual events of each type and action when listed in ...
Definition: DefaultSettings.php:8233
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:92
LogPage\actionText
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition: LogPage.php:232
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:140
LogPage\getTitleLink
static getTitleLink(Title $title, ?Language $lang)
Definition: LogPage.php:299
LogPage\__construct
__construct( $type, $rc=true, $udp='skipUDP')
Stable to call.
Definition: LogPage.php:88
LogPage\getRestriction
getRestriction()
Returns the right needed to read this log type.
Definition: LogPage.php:462
LogPage\getComment
getComment()
Get the comment from the last addEntry() call.
Definition: LogPage.php:194
LogPage\DELETED_COMMENT
const DELETED_COMMENT
Definition: LogPage.php:39
$blob
$blob
Definition: testCompression.php:70
LogPage\DELETED_USER
const DELETED_USER
Definition: LogPage.php:40
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2475
LogPage\isLogType
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:215
LogPage\getDescription
getDescription()
Description of this log type.
Definition: LogPage.php:449
LogPage\addEntry
addEntry( $action, $target, $comment, $params, $doer)
Add a log entry.
Definition: LogPage.php:334
$wgLang
$wgLang
Definition: Setup.php:776
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:37
LogPage\getName
getName()
Name of the log.
Definition: LogPage.php:435
$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:8128
$title
$title
Definition: testCompression.php:38
LogPage\getRcCommentIRC
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition: LogPage.php:175
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1844
DB_MASTER
const DB_MASTER
Definition: defines.php:26
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:910
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:38
LogPage\$action
string $action
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'.
Definition: LogPage.php:67
LogPage\$comment
string $comment
Comment associated with action.
Definition: LogPage.php:70
LogPage\makeParamBlob
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition: LogPage.php:412
$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:8152
LogPage\$doer
User $doer
The user doing the action.
Definition: LogPage.php:76
LogPage\$sendToUDP
bool $sendToUDP
Definition: LogPage.php:51
LogPage\getRcComment
getRcComment()
Get the RC comment from the last addEntry() call.
Definition: LogPage.php:155
LogPage\saveContent
saveContent()
Definition: LogPage.php:97
LogPage\$type
string $type
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move'.
Definition: LogPage.php:62
$wgLogActionsHandlers
$wgLogActionsHandlers
The same as above, but here values are names of classes, not messages.
Definition: DefaultSettings.php:8241
LogPage\addRelations
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition: LogPage.php:385
LogPage\$ircActionText
string $ircActionText
Plaintext version of the message for IRC.
Definition: LogPage.php:54
RecentChange\newLogEntry
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:834
Title
Represents a title within MediaWiki.
Definition: Title.php:41
LogPage\$actionText
string $actionText
Plaintext version of the message.
Definition: LogPage.php:57
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:42
$wgLogNames
$wgLogNames
Lists the message key string for each log type.
Definition: DefaultSettings.php:8190
LogPage\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: LogPage.php:41
LogPage\$updateRecentChanges
bool $updateRecentChanges
Definition: LogPage.php:48
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:97
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41
LogPage\$target
Title $target
Definition: LogPage.php:79
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:52