5 * Copyright © 2002, 2004 Brooke Vibber <bvibber@wikimedia.org>
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
26 use MediaWiki\Context\RequestContext
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\MediaWikiServices
;
29 use MediaWiki\Message\Message
;
30 use MediaWiki\SpecialPage\SpecialPage
;
31 use MediaWiki\StubObject\StubUserLang
;
32 use MediaWiki\Title\Title
;
33 use MediaWiki\User\User
;
34 use MediaWiki\User\UserIdentity
;
37 * Class to simplify the use of log pages.
38 * The logs are now kept in a table which is easier to manage and trim
39 * than ever-growing wiki pages.
42 * @note marked as newable in 1.35 for lack of a better alternative,
43 * but should become a stateless service, use the command pattern.
46 public const DELETED_ACTION
= 1;
47 public const DELETED_COMMENT
= 2;
48 public const DELETED_USER
= 4;
49 public const DELETED_RESTRICTED
= 8;
52 public const SUPPRESSED_USER
= self
::DELETED_USER | self
::DELETED_RESTRICTED
;
53 public const SUPPRESSED_ACTION
= self
::DELETED_ACTION | self
::DELETED_RESTRICTED
;
56 public $updateRecentChanges;
61 /** @var string Plaintext version of the message for IRC */
62 private $ircActionText;
64 /** @var string Plaintext version of the message */
67 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
72 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
73 * 'upload', 'move', 'move_redir'
77 /** @var string Comment associated with action */
80 /** @var string Blob made of a parameters array */
83 /** @var UserIdentity The user doing the action */
91 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
93 * @param bool $rc Whether to update recent changes as well as the logging table
94 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
96 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
98 $this->updateRecentChanges
= $rc;
99 $this->sendToUDP
= ( $udp == 'UDP' );
103 * @return int The log_id of the inserted log entry
105 protected function saveContent() {
106 $logRestrictions = MediaWikiServices
::getInstance()->getMainConfig()->get( MainConfigNames
::LogRestrictions
);
108 $dbw = MediaWikiServices
::getInstance()->getConnectionProvider()->getPrimaryDatabase();
110 $now = wfTimestampNow();
111 $actorId = MediaWikiServices
::getInstance()->getActorNormalization()
112 ->acquireActorId( $this->performer
, $dbw );
114 'log_type' => $this->type
,
115 'log_action' => $this->action
,
116 'log_timestamp' => $dbw->timestamp( $now ),
117 'log_actor' => $actorId,
118 'log_namespace' => $this->target
->getNamespace(),
119 'log_title' => $this->target
->getDBkey(),
120 'log_page' => $this->target
->getArticleID(),
121 'log_params' => $this->params
123 $data +
= MediaWikiServices
::getInstance()->getCommentStore()->insert(
128 $dbw->newInsertQueryBuilder()
129 ->insertInto( 'logging' )
131 ->caller( __METHOD__
)->execute();
132 $newId = $dbw->insertId();
134 # And update recentchanges
135 if ( $this->updateRecentChanges
) {
136 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
138 RecentChange
::notifyLog(
139 $now, $titleObj, $this->performer
, $this->getRcComment(), '',
140 $this->type
, $this->action
, $this->target
, $this->comment
,
141 $this->params
, $newId, $this->getRcCommentIRC()
143 } elseif ( $this->sendToUDP
) {
144 # Don't send private logs to UDP
145 if ( isset( $logRestrictions[$this->type
] ) && $logRestrictions[$this->type
] != '*' ) {
149 // Notify external application via UDP.
150 // We send this to IRC but do not want to add it the RC table.
151 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
152 $rc = RecentChange
::newLogEntry(
153 $now, $titleObj, $this->performer
, $this->getRcComment(), '',
154 $this->type
, $this->action
, $this->target
, $this->comment
,
155 $this->params
, $newId, $this->getRcCommentIRC()
157 $rc->notifyRCFeeds();
164 * Get the RC comment from the last addEntry() call
168 public function getRcComment() {
169 $rcComment = $this->actionText
;
171 if ( $this->comment
!= '' ) {
172 if ( $rcComment == '' ) {
173 $rcComment = $this->comment
;
175 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
184 * Get the RC comment from the last addEntry() call for IRC
188 public function getRcCommentIRC() {
189 $rcComment = $this->ircActionText
;
191 if ( $this->comment
!= '' ) {
192 if ( $rcComment == '' ) {
193 $rcComment = $this->comment
;
195 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
204 * Get the comment from the last addEntry() call
207 public function getComment() {
208 return $this->comment
;
212 * Get the list of valid log types
216 public static function validTypes() {
217 $logTypes = MediaWikiServices
::getInstance()->getMainConfig()->get( MainConfigNames
::LogTypes
);
223 * Is $type a valid log type
225 * @param string $type Log type to check
228 public static function isLogType( $type ) {
229 return in_array( $type, self
::validTypes() );
233 * Generate text for a log entry.
234 * Only LogFormatter should call this function.
236 * @param string $type Log type
237 * @param string $action Log action
238 * @param Title|null $title
239 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
240 * content language, since that will go to the IRC feed.
241 * @param array $params
242 * @param bool $filterWikilinks Whether to filter wiki links
243 * @return string HTML
245 public static function actionText( $type, $action, $title = null, $skin = null,
246 $params = [], $filterWikilinks = false
249 $config = MediaWikiServices
::getInstance()->getMainConfig();
250 $key = "$type/$action";
252 $logActions = $config->get( MainConfigNames
::LogActions
);
254 if ( isset( $logActions[$key] ) ) {
255 $message = $logActions[$key];
257 wfDebug( "LogPage::actionText - unknown action $key" );
258 $message = "log-unknown-action";
262 if ( $skin === null ) {
263 $langObj = MediaWikiServices
::getInstance()->getContentLanguage();
264 $langObjOrNull = null;
266 // TODO Is $skin->getLanguage() safe here?
267 StubUserLang
::unstub( $wgLang );
269 $langObjOrNull = $wgLang;
271 if ( $title === null ) {
272 $rv = wfMessage( $message )->inLanguage( $langObj )->escaped();
274 $titleLink = self
::getTitleLink( $title, $langObjOrNull );
276 if ( count( $params ) == 0 ) {
277 $rv = wfMessage( $message )->rawParams( $titleLink )
278 ->inLanguage( $langObj )->escaped();
280 array_unshift( $params, $titleLink );
282 $rv = wfMessage( $message )->rawParams( $params )
283 ->inLanguage( $langObj )->escaped();
287 // For the perplexed, this feature was added in r7855 by Erik.
288 // The feature was added because we liked adding [[$1]] in our log entries
289 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
290 // on Special:Log. The hack is essentially that [[$1]] represented a link
291 // to the title in question. The first parameter to the HTML version (Special:Log)
292 // is that link in HTML form, and so this just gets rid of the ugly [[]].
293 // However, this is a horrible hack and it doesn't work like you expect if, say,
294 // you want to link to something OTHER than the title of the log entry.
295 // The real problem, which Erik was trying to fix (and it sort-of works now) is
296 // that the same messages are being treated as both wikitext *and* HTML.
297 if ( $filterWikilinks ) {
298 $rv = str_replace( '[[', '', $rv );
299 $rv = str_replace( ']]', '', $rv );
306 * @param Title $title
307 * @param ?Language $lang
308 * @return string HTML
310 private static function getTitleLink( Title
$title, ?Language
$lang ): string {
312 return $title->getPrefixedText();
315 $services = MediaWikiServices
::getInstance();
316 $linkRenderer = $services->getLinkRenderer();
318 if ( $title->isSpecialPage() ) {
319 [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
321 if ( $name === 'Log' ) {
322 $logPage = new LogPage( $par );
323 return wfMessage( 'parentheses' )
324 ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
325 ->inLanguage( $lang )
330 return $linkRenderer->makeLink( $title );
336 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
337 * 'upload', 'move', 'move_redir'
338 * @param Title $target
339 * @param string|null $comment Description associated
340 * @param array $params Parameters passed later to wfMessage function
341 * @param int|UserIdentity $performer The user doing the action, or their user id.
342 * Calling with user ID is deprecated since 1.36.
344 * @return int The log_id of the inserted log entry
346 public function addEntry( $action, $target, $comment, $params, $performer ) {
347 // FIXME $params is only documented to accept an array
348 if ( !is_array( $params ) ) {
349 $params = [ $params ];
352 # Trim spaces on user supplied text
353 $comment = trim( $comment ??
'' );
355 $this->action
= $action;
356 $this->target
= $target;
357 $this->comment
= $comment;
358 $this->params
= self
::makeParamBlob( $params );
360 if ( !is_object( $performer ) ) {
361 $performer = User
::newFromId( $performer );
364 $this->performer
= $performer;
366 $logEntry = new ManualLogEntry( $this->type
, $action );
367 $logEntry->setTarget( $target );
368 $logEntry->setPerformer( $performer );
369 $logEntry->setParameters( $params );
370 // All log entries using the LogPage to insert into the logging table
371 // are using the old logging system and therefore the legacy flag is
372 // needed to say the LogFormatter the parameters have numeric keys
373 $logEntry->setLegacy( true );
375 $formatter = MediaWikiServices
::getInstance()->getLogFormatterFactory()->newFromEntry( $logEntry );
376 $context = RequestContext
::newExtraneousContext( $target );
377 $formatter->setContext( $context );
379 $this->actionText
= $formatter->getPlainActionText();
380 $this->ircActionText
= $formatter->getIRCActionText();
382 return $this->saveContent();
386 * Add relations to log_search table
388 * @param string $field
389 * @param array $values
393 public function addRelations( $field, $values, $logid ) {
394 if ( !strlen( $field ) ||
!$values ) {
397 $insert = MediaWikiServices
::getInstance()->getConnectionProvider()->getPrimaryDatabase()
398 ->newInsertQueryBuilder()
399 ->insertInto( 'log_search' )
401 foreach ( $values as $value ) {
402 $insert->row( [ 'ls_field' => $field, 'ls_value' => $value, 'ls_log_id' => $logid ] );
404 $insert->caller( __METHOD__
)->execute();
410 * Create a blob from a parameter array
412 * @param array $params
415 public static function makeParamBlob( $params ) {
416 return implode( "\n", $params );
420 * Extract a parameter array from a blob
422 * @param string $blob
425 public static function extractParams( $blob ) {
426 if ( $blob === '' ) {
429 return explode( "\n", $blob );
438 public function getName() {
439 $logNames = MediaWikiServices
::getInstance()->getMainConfig()->get( MainConfigNames
::LogNames
);
442 $key = $logNames[$this->type
] ??
'log-name-' . $this->type
;
444 return wfMessage( $key );
448 * Description of this log type.
452 public function getDescription() {
453 $logHeaders = MediaWikiServices
::getInstance()->getMainConfig()->get( MainConfigNames
::LogHeaders
);
455 $key = $logHeaders[$this->type
] ??
'log-description-' . $this->type
;
457 return wfMessage( $key );
461 * Returns the right needed to read this log type.
465 public function getRestriction() {
466 $logRestrictions = MediaWikiServices
::getInstance()->getMainConfig()->get( MainConfigNames
::LogRestrictions
);
467 // The empty string fallback will
468 // always return true in permission check
469 return $logRestrictions[$this->type
] ??
'';
473 * Tells if this log is not viewable by all.
477 public function isRestricted() {
478 $restriction = $this->getRestriction();
480 return $restriction !== '' && $restriction !== '*';