Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / logging / LogPage.php
bloba085e3e114b2a0a6ed0d1bb1c6957ae933016596
1 <?php
2 /**
3 * Contain log classes
5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
26 /**
27 * Class to simplify the use of log pages.
28 * The logs are now kept in a table which is easier to manage and trim
29 * than ever-growing wiki pages.
31 class LogPage {
32 const DELETED_ACTION = 1;
33 const DELETED_COMMENT = 2;
34 const DELETED_USER = 4;
35 const DELETED_RESTRICTED = 8;
37 // Convenience fields
38 const SUPPRESSED_USER = 12;
39 const SUPPRESSED_ACTION = 9;
41 /** @var bool */
42 public $updateRecentChanges;
44 /** @var bool */
45 public $sendToUDP;
47 /** @var string Plaintext version of the message for IRC */
48 private $ircActionText;
50 /** @var string Plaintext version of the message */
51 private $actionText;
53 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
54 * 'upload', 'move'
56 private $type;
58 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
59 * 'upload', 'move', 'move_redir' */
60 private $action;
62 /** @var string Comment associated with action */
63 private $comment;
65 /** @var string Blob made of a parameters array */
66 private $params;
68 /** @var User The user doing the action */
69 private $doer;
71 /** @var Title */
72 private $target;
74 /**
75 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
76 * 'upload', 'move'
77 * @param bool $rc Whether to update recent changes as well as the logging table
78 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
80 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
81 $this->type = $type;
82 $this->updateRecentChanges = $rc;
83 $this->sendToUDP = ( $udp == 'UDP' );
86 /**
87 * @return int The log_id of the inserted log entry
89 protected function saveContent() {
90 global $wgLogRestrictions;
92 $dbw = wfGetDB( DB_MASTER );
93 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
95 // @todo FIXME private/protected/public property?
96 $this->timestamp = $now = wfTimestampNow();
97 $data = [
98 'log_id' => $log_id,
99 'log_type' => $this->type,
100 'log_action' => $this->action,
101 'log_timestamp' => $dbw->timestamp( $now ),
102 'log_user' => $this->doer->getId(),
103 'log_user_text' => $this->doer->getName(),
104 'log_namespace' => $this->target->getNamespace(),
105 'log_title' => $this->target->getDBkey(),
106 'log_page' => $this->target->getArticleID(),
107 'log_comment' => $this->comment,
108 'log_params' => $this->params
110 $dbw->insert( 'logging', $data, __METHOD__ );
111 $newId = $dbw->insertId();
113 # And update recentchanges
114 if ( $this->updateRecentChanges ) {
115 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
117 RecentChange::notifyLog(
118 $now, $titleObj, $this->doer, $this->getRcComment(), '',
119 $this->type, $this->action, $this->target, $this->comment,
120 $this->params, $newId, $this->getRcCommentIRC()
122 } elseif ( $this->sendToUDP ) {
123 # Don't send private logs to UDP
124 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
125 return $newId;
128 # Notify external application via UDP.
129 # We send this to IRC but do not want to add it the RC table.
130 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
131 $rc = RecentChange::newLogEntry(
132 $now, $titleObj, $this->doer, $this->getRcComment(), '',
133 $this->type, $this->action, $this->target, $this->comment,
134 $this->params, $newId, $this->getRcCommentIRC()
136 $rc->notifyRCFeeds();
139 return $newId;
143 * Get the RC comment from the last addEntry() call
145 * @return string
147 public function getRcComment() {
148 $rcComment = $this->actionText;
150 if ( $this->comment != '' ) {
151 if ( $rcComment == '' ) {
152 $rcComment = $this->comment;
153 } else {
154 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
155 $this->comment;
159 return $rcComment;
163 * Get the RC comment from the last addEntry() call for IRC
165 * @return string
167 public function getRcCommentIRC() {
168 $rcComment = $this->ircActionText;
170 if ( $this->comment != '' ) {
171 if ( $rcComment == '' ) {
172 $rcComment = $this->comment;
173 } else {
174 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
175 $this->comment;
179 return $rcComment;
183 * Get the comment from the last addEntry() call
184 * @return string
186 public function getComment() {
187 return $this->comment;
191 * Get the list of valid log types
193 * @return array Array of strings
195 public static function validTypes() {
196 global $wgLogTypes;
198 return $wgLogTypes;
202 * Is $type a valid log type
204 * @param string $type Log type to check
205 * @return bool
207 public static function isLogType( $type ) {
208 return in_array( $type, self::validTypes() );
212 * Generate text for a log entry.
213 * Only LogFormatter should call this function.
215 * @param string $type Log type
216 * @param string $action Log action
217 * @param Title|null $title Title object or null
218 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
219 * content language, since that will go to the IRC feed.
220 * @param array $params Parameters
221 * @param bool $filterWikilinks Whether to filter wiki links
222 * @return string HTML
224 public static function actionText( $type, $action, $title = null, $skin = null,
225 $params = [], $filterWikilinks = false
227 global $wgLang, $wgContLang, $wgLogActions;
229 if ( is_null( $skin ) ) {
230 $langObj = $wgContLang;
231 $langObjOrNull = null;
232 } else {
233 $langObj = $wgLang;
234 $langObjOrNull = $wgLang;
237 $key = "$type/$action";
239 if ( isset( $wgLogActions[$key] ) ) {
240 if ( is_null( $title ) ) {
241 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
242 } else {
243 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
245 if ( count( $params ) == 0 ) {
246 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
247 ->inLanguage( $langObj )->escaped();
248 } else {
249 array_unshift( $params, $titleLink );
251 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
252 ->inLanguage( $langObj )->escaped();
255 } else {
256 global $wgLogActionsHandlers;
258 if ( isset( $wgLogActionsHandlers[$key] ) ) {
259 $args = func_get_args();
260 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
261 } else {
262 wfDebug( "LogPage::actionText - unknown action $key\n" );
263 $rv = "$action";
267 // For the perplexed, this feature was added in r7855 by Erik.
268 // The feature was added because we liked adding [[$1]] in our log entries
269 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
270 // on Special:Log. The hack is essentially that [[$1]] represented a link
271 // to the title in question. The first parameter to the HTML version (Special:Log)
272 // is that link in HTML form, and so this just gets rid of the ugly [[]].
273 // However, this is a horrible hack and it doesn't work like you expect if, say,
274 // you want to link to something OTHER than the title of the log entry.
275 // The real problem, which Erik was trying to fix (and it sort-of works now) is
276 // that the same messages are being treated as both wikitext *and* HTML.
277 if ( $filterWikilinks ) {
278 $rv = str_replace( '[[', '', $rv );
279 $rv = str_replace( ']]', '', $rv );
282 return $rv;
286 * @todo Document
287 * @param string $type
288 * @param Language|null $lang
289 * @param Title $title
290 * @param array &$params
291 * @return string
293 protected static function getTitleLink( $type, $lang, $title, &$params ) {
294 if ( !$lang ) {
295 return $title->getPrefixedText();
298 if ( $title->isSpecialPage() ) {
299 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
301 # Use the language name for log titles, rather than Log/X
302 if ( $name == 'Log' ) {
303 $logPage = new LogPage( $par );
304 $titleLink = Linker::link( $title, $logPage->getName()->escaped() );
305 $titleLink = wfMessage( 'parentheses' )
306 ->inLanguage( $lang )
307 ->rawParams( $titleLink )
308 ->escaped();
309 } else {
310 $titleLink = Linker::link( $title );
312 } else {
313 $titleLink = Linker::link( $title );
316 return $titleLink;
320 * Add a log entry
322 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
323 * 'upload', 'move', 'move_redir'
324 * @param Title $target Title object
325 * @param string $comment Description associated
326 * @param array $params Parameters passed later to wfMessage function
327 * @param null|int|User $doer The user doing the action. null for $wgUser
329 * @return int The log_id of the inserted log entry
331 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
332 global $wgContLang;
334 if ( !is_array( $params ) ) {
335 $params = [ $params ];
338 if ( $comment === null ) {
339 $comment = '';
342 # Trim spaces on user supplied text
343 $comment = trim( $comment );
345 # Truncate for whole multibyte characters.
346 $comment = $wgContLang->truncate( $comment, 255 );
348 $this->action = $action;
349 $this->target = $target;
350 $this->comment = $comment;
351 $this->params = self::makeParamBlob( $params );
353 if ( $doer === null ) {
354 global $wgUser;
355 $doer = $wgUser;
356 } elseif ( !is_object( $doer ) ) {
357 $doer = User::newFromId( $doer );
360 $this->doer = $doer;
362 $logEntry = new ManualLogEntry( $this->type, $action );
363 $logEntry->setTarget( $target );
364 $logEntry->setPerformer( $doer );
365 $logEntry->setParameters( $params );
366 // All log entries using the LogPage to insert into the logging table
367 // are using the old logging system and therefore the legacy flag is
368 // needed to say the LogFormatter the parameters have numeric keys
369 $logEntry->setLegacy( true );
371 $formatter = LogFormatter::newFromEntry( $logEntry );
372 $context = RequestContext::newExtraneousContext( $target );
373 $formatter->setContext( $context );
375 $this->actionText = $formatter->getPlainActionText();
376 $this->ircActionText = $formatter->getIRCActionText();
378 return $this->saveContent();
382 * Add relations to log_search table
384 * @param string $field
385 * @param array $values
386 * @param int $logid
387 * @return bool
389 public function addRelations( $field, $values, $logid ) {
390 if ( !strlen( $field ) || empty( $values ) ) {
391 return false; // nothing
394 $data = [];
396 foreach ( $values as $value ) {
397 $data[] = [
398 'ls_field' => $field,
399 'ls_value' => $value,
400 'ls_log_id' => $logid
404 $dbw = wfGetDB( DB_MASTER );
405 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
407 return true;
411 * Create a blob from a parameter array
413 * @param array $params
414 * @return string
416 public static function makeParamBlob( $params ) {
417 return implode( "\n", $params );
421 * Extract a parameter array from a blob
423 * @param string $blob
424 * @return array
426 public static function extractParams( $blob ) {
427 if ( $blob === '' ) {
428 return [];
429 } else {
430 return explode( "\n", $blob );
435 * Name of the log.
436 * @return Message
437 * @since 1.19
439 public function getName() {
440 global $wgLogNames;
442 // BC
443 if ( isset( $wgLogNames[$this->type] ) ) {
444 $key = $wgLogNames[$this->type];
445 } else {
446 $key = 'log-name-' . $this->type;
449 return wfMessage( $key );
453 * Description of this log type.
454 * @return Message
455 * @since 1.19
457 public function getDescription() {
458 global $wgLogHeaders;
459 // BC
460 if ( isset( $wgLogHeaders[$this->type] ) ) {
461 $key = $wgLogHeaders[$this->type];
462 } else {
463 $key = 'log-description-' . $this->type;
466 return wfMessage( $key );
470 * Returns the right needed to read this log type.
471 * @return string
472 * @since 1.19
474 public function getRestriction() {
475 global $wgLogRestrictions;
476 if ( isset( $wgLogRestrictions[$this->type] ) ) {
477 $restriction = $wgLogRestrictions[$this->type];
478 } else {
479 // '' always returns true with $user->isAllowed()
480 $restriction = '';
483 return $restriction;
487 * Tells if this log is not viewable by all.
488 * @return bool
489 * @since 1.19
491 public function isRestricted() {
492 $restriction = $this->getRestriction();
494 return $restriction !== '' && $restriction !== '*';