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
26 use MediaWiki\MediaWikiServices
;
29 * Class to simplify the use of log pages.
30 * The logs are now kept in a table which is easier to manage and trim
31 * than ever-growing wiki pages.
34 const DELETED_ACTION
= 1;
35 const DELETED_COMMENT
= 2;
36 const DELETED_USER
= 4;
37 const DELETED_RESTRICTED
= 8;
40 const SUPPRESSED_USER
= self
::DELETED_USER | self
::DELETED_RESTRICTED
;
41 const SUPPRESSED_ACTION
= self
::DELETED_ACTION | self
::DELETED_RESTRICTED
;
44 public $updateRecentChanges;
49 /** @var string Plaintext version of the message for IRC */
50 private $ircActionText;
52 /** @var string Plaintext version of the message */
55 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
60 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
61 * 'upload', 'move', 'move_redir'
65 /** @var string Comment associated with action */
68 /** @var string Blob made of a parameters array */
71 /** @var User The user doing the action */
78 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
80 * @param bool $rc Whether to update recent changes as well as the logging table
81 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
83 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
85 $this->updateRecentChanges
= $rc;
86 $this->sendToUDP
= ( $udp == 'UDP' );
90 * @return int The log_id of the inserted log entry
92 protected function saveContent() {
93 global $wgLogRestrictions;
95 $dbw = wfGetDB( DB_MASTER
);
97 // @todo FIXME private/protected/public property?
98 $this->timestamp
= $now = wfTimestampNow();
100 'log_type' => $this->type
,
101 'log_action' => $this->action
,
102 'log_timestamp' => $dbw->timestamp( $now ),
103 'log_namespace' => $this->target
->getNamespace(),
104 'log_title' => $this->target
->getDBkey(),
105 'log_page' => $this->target
->getArticleID(),
106 'log_params' => $this->params
108 $data +
= MediaWikiServices
::getInstance()->getCommentStore()->insert(
113 $data +
= ActorMigration
::newMigration()->getInsertValues( $dbw, 'log_user', $this->doer
);
114 $dbw->insert( 'logging', $data, __METHOD__
);
115 $newId = $dbw->insertId();
117 # And update recentchanges
118 if ( $this->updateRecentChanges
) {
119 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
121 RecentChange
::notifyLog(
122 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
123 $this->type
, $this->action
, $this->target
, $this->comment
,
124 $this->params
, $newId, $this->getRcCommentIRC()
126 } elseif ( $this->sendToUDP
) {
127 # Don't send private logs to UDP
128 if ( isset( $wgLogRestrictions[$this->type
] ) && $wgLogRestrictions[$this->type
] != '*' ) {
132 # Notify external application via UDP.
133 # We send this to IRC but do not want to add it the RC table.
134 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
135 $rc = RecentChange
::newLogEntry(
136 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
137 $this->type
, $this->action
, $this->target
, $this->comment
,
138 $this->params
, $newId, $this->getRcCommentIRC()
140 $rc->notifyRCFeeds();
147 * Get the RC comment from the last addEntry() call
151 public function getRcComment() {
152 $rcComment = $this->actionText
;
154 if ( $this->comment
!= '' ) {
155 if ( $rcComment == '' ) {
156 $rcComment = $this->comment
;
158 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
167 * Get the RC comment from the last addEntry() call for IRC
171 public function getRcCommentIRC() {
172 $rcComment = $this->ircActionText
;
174 if ( $this->comment
!= '' ) {
175 if ( $rcComment == '' ) {
176 $rcComment = $this->comment
;
178 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
187 * Get the comment from the last addEntry() call
190 public function getComment() {
191 return $this->comment
;
195 * Get the list of valid log types
197 * @return array Array of strings
199 public static function validTypes() {
206 * Is $type a valid log type
208 * @param string $type Log type to check
211 public static function isLogType( $type ) {
212 return in_array( $type, self
::validTypes() );
216 * Generate text for a log entry.
217 * Only LogFormatter should call this function.
219 * @param string $type Log type
220 * @param string $action Log action
221 * @param Title|null $title Title object or null
222 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
223 * content language, since that will go to the IRC feed.
224 * @param array $params Parameters
225 * @param bool $filterWikilinks Whether to filter wiki links
226 * @return string HTML
228 public static function actionText( $type, $action, $title = null, $skin = null,
229 $params = [], $filterWikilinks = false
231 global $wgLang, $wgLogActions;
233 if ( is_null( $skin ) ) {
234 $langObj = MediaWikiServices
::getInstance()->getContentLanguage();
235 $langObjOrNull = null;
238 $langObjOrNull = $wgLang;
241 $key = "$type/$action";
243 if ( isset( $wgLogActions[$key] ) ) {
244 if ( is_null( $title ) ) {
245 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
247 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
249 if ( count( $params ) == 0 ) {
250 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
251 ->inLanguage( $langObj )->escaped();
253 array_unshift( $params, $titleLink );
255 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
256 ->inLanguage( $langObj )->escaped();
260 global $wgLogActionsHandlers;
262 if ( isset( $wgLogActionsHandlers[$key] ) ) {
263 $args = func_get_args();
264 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
266 wfDebug( "LogPage::actionText - unknown action $key\n" );
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 );
291 * @param string $type
292 * @param Language|null $lang
293 * @param Title $title
294 * @param array &$params
297 protected static function getTitleLink( $type, $lang, $title, &$params ) {
299 return $title->getPrefixedText();
302 $services = MediaWikiServices
::getInstance();
303 $linkRenderer = $services->getLinkRenderer();
304 if ( $title->isSpecialPage() ) {
305 list( $name, $par ) = $services->getSpecialPageFactory()->
306 resolveAlias( $title->getDBkey() );
308 # Use the language name for log titles, rather than Log/X
309 if ( $name == 'Log' ) {
310 $logPage = new LogPage( $par );
311 $titleLink = $linkRenderer->makeLink( $title, $logPage->getName()->text() );
312 $titleLink = wfMessage( 'parentheses' )
313 ->inLanguage( $lang )
314 ->rawParams( $titleLink )
317 $titleLink = $linkRenderer->makeLink( $title );
320 $titleLink = $linkRenderer->makeLink( $title );
329 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
330 * 'upload', 'move', 'move_redir'
331 * @param Title $target
332 * @param string $comment Description associated
333 * @param array $params Parameters passed later to wfMessage function
334 * @param null|int|User $doer The user doing the action. null for $wgUser
336 * @return int The log_id of the inserted log entry
338 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
339 if ( !is_array( $params ) ) {
340 $params = [ $params ];
343 if ( $comment === null ) {
347 # Trim spaces on user supplied text
348 $comment = trim( $comment );
350 $this->action
= $action;
351 $this->target
= $target;
352 $this->comment
= $comment;
353 $this->params
= self
::makeParamBlob( $params );
355 if ( $doer === null ) {
358 } elseif ( !is_object( $doer ) ) {
359 $doer = User
::newFromId( $doer );
364 $logEntry = new ManualLogEntry( $this->type
, $action );
365 $logEntry->setTarget( $target );
366 $logEntry->setPerformer( $doer );
367 $logEntry->setParameters( $params );
368 // All log entries using the LogPage to insert into the logging table
369 // are using the old logging system and therefore the legacy flag is
370 // needed to say the LogFormatter the parameters have numeric keys
371 $logEntry->setLegacy( true );
373 $formatter = LogFormatter
::newFromEntry( $logEntry );
374 $context = RequestContext
::newExtraneousContext( $target );
375 $formatter->setContext( $context );
377 $this->actionText
= $formatter->getPlainActionText();
378 $this->ircActionText
= $formatter->getIRCActionText();
380 return $this->saveContent();
384 * Add relations to log_search table
386 * @param string $field
387 * @param array $values
391 public function addRelations( $field, $values, $logid ) {
392 if ( !strlen( $field ) ||
empty( $values ) ) {
398 foreach ( $values as $value ) {
400 'ls_field' => $field,
401 'ls_value' => $value,
402 'ls_log_id' => $logid
406 $dbw = wfGetDB( DB_MASTER
);
407 $dbw->insert( 'log_search', $data, __METHOD__
, [ 'IGNORE' ] );
413 * Create a blob from a parameter array
415 * @param array $params
418 public static function makeParamBlob( $params ) {
419 return implode( "\n", $params );
423 * Extract a parameter array from a blob
425 * @param string $blob
428 public static function extractParams( $blob ) {
429 if ( $blob === '' ) {
432 return explode( "\n", $blob );
441 public function getName() {
445 $key = $wgLogNames[$this->type
] ??
'log-name-' . $this->type
;
447 return wfMessage( $key );
451 * Description of this log type.
455 public function getDescription() {
456 global $wgLogHeaders;
458 $key = $wgLogHeaders[$this->type
] ??
'log-description-' . $this->type
;
460 return wfMessage( $key );
464 * Returns the right needed to read this log type.
468 public function getRestriction() {
469 global $wgLogRestrictions;
470 // '' always returns true with $user->isAllowed()
471 return $wgLogRestrictions[$this->type
] ??
'';
475 * Tells if this log is not viewable by all.
479 public function isRestricted() {
480 $restriction = $this->getRestriction();
482 return $restriction !== '' && $restriction !== '*';