MediaWiki  master
LogPage.php
Go to the documentation of this file.
1 <?php
27 
33 class LogPage {
34  const DELETED_ACTION = 1;
35  const DELETED_COMMENT = 2;
36  const DELETED_USER = 4;
37  const DELETED_RESTRICTED = 8;
38 
39  // Convenience fields
40  const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
41  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  if ( is_null( $skin ) ) {
233  $langObj = MediaWikiServices::getInstance()->getContentLanguage();
234  $langObjOrNull = null;
235  } else {
236  $langObj = $wgLang;
237  $langObjOrNull = $wgLang;
238  }
239 
240  $key = "$type/$action";
241 
242  if ( isset( $wgLogActions[$key] ) ) {
243  if ( is_null( $title ) ) {
244  $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
245  } else {
246  $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
247 
248  if ( count( $params ) == 0 ) {
249  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
250  ->inLanguage( $langObj )->escaped();
251  } else {
252  array_unshift( $params, $titleLink );
253 
254  $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
255  ->inLanguage( $langObj )->escaped();
256  }
257  }
258  } else {
259  global $wgLogActionsHandlers;
260 
261  if ( isset( $wgLogActionsHandlers[$key] ) ) {
262  $args = func_get_args();
263  $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
264  } else {
265  wfDebug( "LogPage::actionText - unknown action $key\n" );
266  $rv = "$action";
267  }
268  }
269 
270  // For the perplexed, this feature was added in r7855 by Erik.
271  // The feature was added because we liked adding [[$1]] in our log entries
272  // but the log entries are parsed as Wikitext on RecentChanges but as HTML
273  // on Special:Log. The hack is essentially that [[$1]] represented a link
274  // to the title in question. The first parameter to the HTML version (Special:Log)
275  // is that link in HTML form, and so this just gets rid of the ugly [[]].
276  // However, this is a horrible hack and it doesn't work like you expect if, say,
277  // you want to link to something OTHER than the title of the log entry.
278  // The real problem, which Erik was trying to fix (and it sort-of works now) is
279  // that the same messages are being treated as both wikitext *and* HTML.
280  if ( $filterWikilinks ) {
281  $rv = str_replace( '[[', '', $rv );
282  $rv = str_replace( ']]', '', $rv );
283  }
284 
285  return $rv;
286  }
287 
296  protected static function getTitleLink( $type, $lang, $title, &$params ) {
297  if ( !$lang ) {
298  return $title->getPrefixedText();
299  }
300 
301  $services = MediaWikiServices::getInstance();
302  $linkRenderer = $services->getLinkRenderer();
303  if ( $title->isSpecialPage() ) {
304  list( $name, $par ) = $services->getSpecialPageFactory()->
305  resolveAlias( $title->getDBkey() );
306 
307  # Use the language name for log titles, rather than Log/X
308  if ( $name == 'Log' ) {
309  $logPage = new LogPage( $par );
310  $titleLink = $linkRenderer->makeLink( $title, $logPage->getName()->text() );
311  $titleLink = wfMessage( 'parentheses' )
312  ->inLanguage( $lang )
313  ->rawParams( $titleLink )
314  ->escaped();
315  } else {
316  $titleLink = $linkRenderer->makeLink( $title );
317  }
318  } else {
319  $titleLink = $linkRenderer->makeLink( $title );
320  }
321 
322  return $titleLink;
323  }
324 
337  public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
338  if ( !is_array( $params ) ) {
339  $params = [ $params ];
340  }
341 
342  if ( $comment === null ) {
343  $comment = '';
344  }
345 
346  # Trim spaces on user supplied text
347  $comment = trim( $comment );
348 
349  $this->action = $action;
350  $this->target = $target;
351  $this->comment = $comment;
352  $this->params = self::makeParamBlob( $params );
353 
354  if ( $doer === null ) {
355  global $wgUser;
356  $doer = $wgUser;
357  } elseif ( !is_object( $doer ) ) {
359  }
360 
361  $this->doer = $doer;
362 
363  $logEntry = new ManualLogEntry( $this->type, $action );
364  $logEntry->setTarget( $target );
365  $logEntry->setPerformer( $doer );
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_MASTER );
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  // '' always returns true with $user->isAllowed()
470  return $wgLogRestrictions[$this->type] ?? '';
471  }
472 
478  public function isRestricted() {
479  $restriction = $this->getRestriction();
480 
481  return $restriction !== '' && $restriction !== '*';
482  }
483 }
getRestriction()
Returns the right needed to read this log type.
Definition: LogPage.php:467
$context
Definition: load.php:45
bool $updateRecentChanges
Definition: LogPage.php:44
__construct( $type, $rc=true, $udp='skipUDP')
Definition: LogPage.php:83
string $ircActionText
Plaintext version of the message for IRC.
Definition: LogPage.php:50
if(!isset( $args[0])) $lang
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
string $type
One of &#39;&#39;, &#39;block&#39;, &#39;protect&#39;, &#39;rights&#39;, &#39;delete&#39;, &#39;upload&#39;, &#39;move&#39;.
Definition: LogPage.php:58
$wgLogActions
Lists the message key string for formatting individual events of each type and action when listed in ...
$wgLogRestrictions
This restricts log access to those who have a certain right Users without this will not see it in the...
const SUPPRESSED_ACTION
Definition: LogPage.php:41
saveContent()
Definition: LogPage.php:92
$wgLogNames
Lists the message key string for each log type.
const DB_MASTER
Definition: defines.php:26
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition: LogPage.php:390
User $doer
The user doing the action.
Definition: LogPage.php:72
if( $line===false) $args
Definition: cdb.php:64
Class to simplify the use of log pages.
Definition: LogPage.php:33
isRestricted()
Tells if this log is not viewable by all.
Definition: LogPage.php:478
const DELETED_COMMENT
Definition: LogPage.php:35
$wgLang
Definition: Setup.php:857
static newMigration()
Static constructor.
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition: LogPage.php:227
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition: LogPage.php:170
getDescription()
Description of this log type.
Definition: LogPage.php:454
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
string $actionText
Plaintext version of the message.
Definition: LogPage.php:53
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
const DELETED_RESTRICTED
Definition: LogPage.php:37
$wgLogActionsHandlers
The same as above, but here values are names of classes, not messages.
const DELETED_USER
Definition: LogPage.php:36
const SUPPRESSED_USER
Definition: LogPage.php:40
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:210
static getTitleLink( $type, $lang, $title, &$params)
Definition: LogPage.php:296
Title $target
Definition: LogPage.php:75
addEntry( $action, $target, $comment, $params=[], $doer=null)
Add a log entry.
Definition: LogPage.php:337
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
bool $sendToUDP
Definition: LogPage.php:47
static notifyLog( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='')
getRcComment()
Get the RC comment from the last addEntry() call.
Definition: LogPage.php:150
$wgLogHeaders
Lists the message key string for descriptive text to be shown at the top of each log type...
string $params
Blob made of a parameters array.
Definition: LogPage.php:69
getName()
Name of the log.
Definition: LogPage.php:440
string $action
One of &#39;&#39;, &#39;block&#39;, &#39;protect&#39;, &#39;rights&#39;, &#39;delete&#39;, &#39;upload&#39;, &#39;move&#39;, &#39;move_redir&#39;.
Definition: LogPage.php:63
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const DELETED_ACTION
Definition: LogPage.php:34
$wgLogTypes
The logging system has two levels: an event type, which describes the general category and can be vie...
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:198
string $comment
Comment associated with action.
Definition: LogPage.php:66
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition: LogPage.php:417
getComment()
Get the comment from the last addEntry() call.
Definition: LogPage.php:189
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:427