MediaWiki  master
LogPage.php
Go to the documentation of this file.
1 <?php
27 
33 class LogPage {
34  public const DELETED_ACTION = 1;
35  public const DELETED_COMMENT = 2;
36  public const DELETED_USER = 4;
37  public const DELETED_RESTRICTED = 8;
38 
39  // Convenience fields
40  public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
41  public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
42 
45 
47  public $sendToUDP;
48 
50  private $ircActionText;
51 
53  private $actionText;
54 
58  private $type;
59 
63  private $action;
64 
66  private $comment;
67 
69  private $params;
70 
72  private $doer;
73 
75  private $target;
76 
83  public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
84  $this->type = $type;
85  $this->updateRecentChanges = $rc;
86  $this->sendToUDP = ( $udp == 'UDP' );
87  }
88 
92  protected function saveContent() {
93  global $wgLogRestrictions;
94 
95  $dbw = wfGetDB( DB_MASTER );
96 
97  $now = wfTimestampNow();
98  $data = [
99  'log_type' => $this->type,
100  'log_action' => $this->action,
101  'log_timestamp' => $dbw->timestamp( $now ),
102  'log_namespace' => $this->target->getNamespace(),
103  'log_title' => $this->target->getDBkey(),
104  'log_page' => $this->target->getArticleID(),
105  'log_params' => $this->params
106  ];
107  $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
108  $dbw,
109  'log_comment',
110  $this->comment
111  );
112  $data += ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $this->doer );
113  $dbw->insert( 'logging', $data, __METHOD__ );
114  $newId = $dbw->insertId();
115 
116  # And update recentchanges
117  if ( $this->updateRecentChanges ) {
118  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
119 
121  $now, $titleObj, $this->doer, $this->getRcComment(), '',
122  $this->type, $this->action, $this->target, $this->comment,
123  $this->params, $newId, $this->getRcCommentIRC()
124  );
125  } elseif ( $this->sendToUDP ) {
126  # Don't send private logs to UDP
127  if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
128  return $newId;
129  }
130 
131  # Notify external application via UDP.
132  # We send this to IRC but do not want to add it the RC table.
133  $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
135  $now, $titleObj, $this->doer, $this->getRcComment(), '',
136  $this->type, $this->action, $this->target, $this->comment,
137  $this->params, $newId, $this->getRcCommentIRC()
138  );
139  $rc->notifyRCFeeds();
140  }
141 
142  return $newId;
143  }
144 
150  public function getRcComment() {
151  $rcComment = $this->actionText;
152 
153  if ( $this->comment != '' ) {
154  if ( $rcComment == '' ) {
155  $rcComment = $this->comment;
156  } else {
157  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
159  }
160  }
161 
162  return $rcComment;
163  }
164 
170  public function getRcCommentIRC() {
171  $rcComment = $this->ircActionText;
172 
173  if ( $this->comment != '' ) {
174  if ( $rcComment == '' ) {
175  $rcComment = $this->comment;
176  } else {
177  $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
179  }
180  }
181 
182  return $rcComment;
183  }
184 
189  public function getComment() {
190  return $this->comment;
191  }
192 
198  public static function validTypes() {
199  global $wgLogTypes;
200 
201  return $wgLogTypes;
202  }
203 
210  public static function isLogType( $type ) {
211  return in_array( $type, self::validTypes() );
212  }
213 
227  public static function actionText( $type, $action, $title = null, $skin = null,
228  $params = [], $filterWikilinks = false
229  ) {
230  global $wgLang, $wgLogActions;
231 
232  $key = "$type/$action";
233 
234  if ( isset( $wgLogActions[$key] ) ) {
235  if ( $skin === null ) {
236  $langObj = MediaWikiServices::getInstance()->getContentLanguage();
237  $langObjOrNull = null;
238  } else {
239  // TODO Is $skin->getLanguage() safe here?
241  $langObj = $wgLang;
242  $langObjOrNull = $wgLang;
243  }
244  if ( $title === null ) {
245  $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
246  } else {
247  $titleLink = self::getTitleLink( $title, $langObjOrNull );
248 
249  if ( count( $params ) == 0 ) {
250  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
251  ->inLanguage( $langObj )->escaped();
252  } else {
253  array_unshift( $params, $titleLink );
254 
255  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
256  ->inLanguage( $langObj )->escaped();
257  }
258  }
259  } else {
260  global $wgLogActionsHandlers;
261 
262  if ( isset( $wgLogActionsHandlers[$key] ) ) {
263  $args = func_get_args();
264  $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
265  } else {
266  wfDebug( "LogPage::actionText - unknown action $key" );
267  $rv = "$action";
268  }
269  }
270 
271  // For the perplexed, this feature was added in r7855 by Erik.
272  // The feature was added because we liked adding [[$1]] in our log entries
273  // but the log entries are parsed as Wikitext on RecentChanges but as HTML
274  // on Special:Log. The hack is essentially that [[$1]] represented a link
275  // to the title in question. The first parameter to the HTML version (Special:Log)
276  // is that link in HTML form, and so this just gets rid of the ugly [[]].
277  // However, this is a horrible hack and it doesn't work like you expect if, say,
278  // you want to link to something OTHER than the title of the log entry.
279  // The real problem, which Erik was trying to fix (and it sort-of works now) is
280  // that the same messages are being treated as both wikitext *and* HTML.
281  if ( $filterWikilinks ) {
282  $rv = str_replace( '[[', '', $rv );
283  $rv = str_replace( ']]', '', $rv );
284  }
285 
286  return $rv;
287  }
288 
294  private static function getTitleLink( Title $title, ?Language $lang ) : string {
295  if ( !$lang ) {
296  return $title->getPrefixedText();
297  }
298 
299  $services = MediaWikiServices::getInstance();
300  $linkRenderer = $services->getLinkRenderer();
301 
302  if ( $title->isSpecialPage() ) {
303  [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
304 
305  if ( $name === 'Log' ) {
306  $logPage = new LogPage( $par );
307  return wfMessage( 'parentheses' )
308  ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
309  ->inLanguage( $lang )
310  ->escaped();
311  }
312  }
313 
314  return $linkRenderer->makeLink( $title );
315  }
316 
330  public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
331  if ( !is_array( $params ) ) {
332  $params = [ $params ];
333  }
334 
335  if ( $comment === null ) {
336  $comment = '';
337  }
338 
339  # Trim spaces on user supplied text
340  $comment = trim( $comment );
341 
342  $this->action = $action;
343  $this->target = $target;
344  $this->comment = $comment;
345  $this->params = self::makeParamBlob( $params );
346 
347  if ( $doer === null ) {
348  wfDeprecated( __METHOD__ . ' without passing a $user parameter', '1.35' );
349  global $wgUser;
350  $doer = $wgUser;
351  } elseif ( !is_object( $doer ) ) {
353  }
354 
355  $this->doer = $doer;
356 
357  $logEntry = new ManualLogEntry( $this->type, $action );
358  $logEntry->setTarget( $target );
359  $logEntry->setPerformer( $doer );
360  $logEntry->setParameters( $params );
361  // All log entries using the LogPage to insert into the logging table
362  // are using the old logging system and therefore the legacy flag is
363  // needed to say the LogFormatter the parameters have numeric keys
364  $logEntry->setLegacy( true );
365 
366  $formatter = LogFormatter::newFromEntry( $logEntry );
368  $formatter->setContext( $context );
369 
370  $this->actionText = $formatter->getPlainActionText();
371  $this->ircActionText = $formatter->getIRCActionText();
372 
373  return $this->saveContent();
374  }
375 
384  public function addRelations( $field, $values, $logid ) {
385  if ( !strlen( $field ) || empty( $values ) ) {
386  return false;
387  }
388 
389  $data = [];
390 
391  foreach ( $values as $value ) {
392  $data[] = [
393  'ls_field' => $field,
394  'ls_value' => $value,
395  'ls_log_id' => $logid
396  ];
397  }
398 
399  $dbw = wfGetDB( DB_MASTER );
400  $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
401 
402  return true;
403  }
404 
411  public static function makeParamBlob( $params ) {
412  return implode( "\n", $params );
413  }
414 
421  public static function extractParams( $blob ) {
422  if ( $blob === '' ) {
423  return [];
424  } else {
425  return explode( "\n", $blob );
426  }
427  }
428 
434  public function getName() {
435  global $wgLogNames;
436 
437  // BC
438  $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
439 
440  return wfMessage( $key );
441  }
442 
448  public function getDescription() {
449  global $wgLogHeaders;
450  // BC
451  $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
452 
453  return wfMessage( $key );
454  }
455 
461  public function getRestriction() {
462  global $wgLogRestrictions;
463  // '' always returns true with $user->isAllowed()
464  return $wgLogRestrictions[$this->type] ?? '';
465  }
466 
472  public function isRestricted() {
473  $restriction = $this->getRestriction();
474 
475  return $restriction !== '' && $restriction !== '*';
476  }
477 }
LogPage\SUPPRESSED_ACTION
const SUPPRESSED_ACTION
Definition: LogPage.php:41
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:557
LogPage\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: LogPage.php:40
LogPage\validTypes
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:198
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
$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:8203
LogPage\extractParams
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:421
LogPage\isRestricted
isRestricted()
Tells if this log is not viewable by all.
Definition: LogPage.php:472
RecentChange\notifyLog
static notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='')
Definition: RecentChange.php:788
LogPage\$params
string $params
Blob made of a parameters array.
Definition: LogPage.php:69
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1222
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:621
$wgLogActions
$wgLogActions
Lists the message key string for formatting individual events of each type and action when listed in ...
Definition: DefaultSettings.php:8223
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:90
LogPage\actionText
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition: LogPage.php:227
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:139
LogPage\getTitleLink
static getTitleLink(Title $title, ?Language $lang)
Definition: LogPage.php:294
LogPage\__construct
__construct( $type, $rc=true, $udp='skipUDP')
Definition: LogPage.php:83
LogPage\getRestriction
getRestriction()
Returns the right needed to read this log type.
Definition: LogPage.php:461
LogPage\getComment
getComment()
Get the comment from the last addEntry() call.
Definition: LogPage.php:189
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1029
LogPage\DELETED_COMMENT
const DELETED_COMMENT
Definition: LogPage.php:35
$blob
$blob
Definition: testCompression.php:70
LogPage\DELETED_USER
const DELETED_USER
Definition: LogPage.php:36
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2464
LogPage\isLogType
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:210
LogPage\getDescription
getDescription()
Description of this log type.
Definition: LogPage.php:448
$wgLang
$wgLang
Definition: Setup.php:782
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
LogPage\getName
getName()
Name of the log.
Definition: LogPage.php:434
$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:8118
$title
$title
Definition: testCompression.php:38
LogPage\getRcCommentIRC
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition: LogPage.php:170
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1840
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:912
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:34
LogPage\$action
string $action
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'.
Definition: LogPage.php:63
LogPage\$comment
string $comment
Comment associated with action.
Definition: LogPage.php:66
LogPage\makeParamBlob
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition: LogPage.php:411
$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:8142
LogPage\$doer
User $doer
The user doing the action.
Definition: LogPage.php:72
LogPage\$sendToUDP
bool $sendToUDP
Definition: LogPage.php:47
LogPage\getRcComment
getRcComment()
Get the RC comment from the last addEntry() call.
Definition: LogPage.php:150
LogPage\saveContent
saveContent()
Definition: LogPage.php:92
LogPage\$type
string $type
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move'.
Definition: LogPage.php:58
$wgLogActionsHandlers
$wgLogActionsHandlers
The same as above, but here values are names of classes, not messages.
Definition: DefaultSettings.php:8231
LogPage\addRelations
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition: LogPage.php:384
LogPage\$ircActionText
string $ircActionText
Plaintext version of the message for IRC.
Definition: LogPage.php:50
RecentChange\newLogEntry
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:821
Title
Represents a title within MediaWiki.
Definition: Title.php:42
LogPage\$actionText
string $actionText
Plaintext version of the message.
Definition: LogPage.php:53
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:38
$wgLogNames
$wgLogNames
Lists the message key string for each log type.
Definition: DefaultSettings.php:8180
LogPage\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: LogPage.php:37
LogPage\$updateRecentChanges
bool $updateRecentChanges
Definition: LogPage.php:44
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:54
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:93
LogPage\addEntry
addEntry( $action, $target, $comment, $params=[], $doer=null)
Add a log entry.
Definition: LogPage.php:330
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:75
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:50