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
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.
32 const DELETED_ACTION
= 1;
33 const DELETED_COMMENT
= 2;
34 const DELETED_USER
= 4;
35 const DELETED_RESTRICTED
= 8;
38 const SUPPRESSED_USER
= 12;
39 const SUPPRESSED_ACTION
= 9;
42 public $updateRecentChanges;
47 /** @var string Plaintext version of the message for IRC */
48 private $ircActionText;
50 /** @var string Plaintext version of the message */
53 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
58 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
59 * 'upload', 'move', 'move_redir' */
62 /** @var string Comment associated with action */
65 /** @var string Blob made of a parameters array */
68 /** @var User The user doing the action */
75 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
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' ) {
82 $this->updateRecentChanges
= $rc;
83 $this->sendToUDP
= ( $udp == 'UDP' );
87 * @return int The log_id of the inserted log entry
89 protected function saveContent() {
90 global $wgLogRestrictions;
92 $dbw = wfGetDB( DB_MASTER
);
94 // @todo FIXME private/protected/public property?
95 $this->timestamp
= $now = wfTimestampNow();
97 'log_type' => $this->type
,
98 'log_action' => $this->action
,
99 'log_timestamp' => $dbw->timestamp( $now ),
100 'log_user' => $this->doer
->getId(),
101 'log_user_text' => $this->doer
->getName(),
102 'log_namespace' => $this->target
->getNamespace(),
103 'log_title' => $this->target
->getDBkey(),
104 'log_page' => $this->target
->getArticleID(),
105 'log_params' => $this->params
107 $data +
= CommentStore
::newKey( 'log_comment' )->insert( $dbw, $this->comment
);
108 $dbw->insert( 'logging', $data, __METHOD__
);
109 $newId = $dbw->insertId();
111 # And update recentchanges
112 if ( $this->updateRecentChanges
) {
113 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
115 RecentChange
::notifyLog(
116 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
117 $this->type
, $this->action
, $this->target
, $this->comment
,
118 $this->params
, $newId, $this->getRcCommentIRC()
120 } elseif ( $this->sendToUDP
) {
121 # Don't send private logs to UDP
122 if ( isset( $wgLogRestrictions[$this->type
] ) && $wgLogRestrictions[$this->type
] != '*' ) {
126 # Notify external application via UDP.
127 # We send this to IRC but do not want to add it the RC table.
128 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
129 $rc = RecentChange
::newLogEntry(
130 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
131 $this->type
, $this->action
, $this->target
, $this->comment
,
132 $this->params
, $newId, $this->getRcCommentIRC()
134 $rc->notifyRCFeeds();
141 * Get the RC comment from the last addEntry() call
145 public function getRcComment() {
146 $rcComment = $this->actionText
;
148 if ( $this->comment
!= '' ) {
149 if ( $rcComment == '' ) {
150 $rcComment = $this->comment
;
152 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
161 * Get the RC comment from the last addEntry() call for IRC
165 public function getRcCommentIRC() {
166 $rcComment = $this->ircActionText
;
168 if ( $this->comment
!= '' ) {
169 if ( $rcComment == '' ) {
170 $rcComment = $this->comment
;
172 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
181 * Get the comment from the last addEntry() call
184 public function getComment() {
185 return $this->comment
;
189 * Get the list of valid log types
191 * @return array Array of strings
193 public static function validTypes() {
200 * Is $type a valid log type
202 * @param string $type Log type to check
205 public static function isLogType( $type ) {
206 return in_array( $type, self
::validTypes() );
210 * Generate text for a log entry.
211 * Only LogFormatter should call this function.
213 * @param string $type Log type
214 * @param string $action Log action
215 * @param Title|null $title Title object or null
216 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
217 * content language, since that will go to the IRC feed.
218 * @param array $params Parameters
219 * @param bool $filterWikilinks Whether to filter wiki links
220 * @return string HTML
222 public static function actionText( $type, $action, $title = null, $skin = null,
223 $params = [], $filterWikilinks = false
225 global $wgLang, $wgContLang, $wgLogActions;
227 if ( is_null( $skin ) ) {
228 $langObj = $wgContLang;
229 $langObjOrNull = null;
232 $langObjOrNull = $wgLang;
235 $key = "$type/$action";
237 if ( isset( $wgLogActions[$key] ) ) {
238 if ( is_null( $title ) ) {
239 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
241 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
243 if ( count( $params ) == 0 ) {
244 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
245 ->inLanguage( $langObj )->escaped();
247 array_unshift( $params, $titleLink );
249 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
250 ->inLanguage( $langObj )->escaped();
254 global $wgLogActionsHandlers;
256 if ( isset( $wgLogActionsHandlers[$key] ) ) {
257 $args = func_get_args();
258 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
260 wfDebug( "LogPage::actionText - unknown action $key\n" );
265 // For the perplexed, this feature was added in r7855 by Erik.
266 // The feature was added because we liked adding [[$1]] in our log entries
267 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
268 // on Special:Log. The hack is essentially that [[$1]] represented a link
269 // to the title in question. The first parameter to the HTML version (Special:Log)
270 // is that link in HTML form, and so this just gets rid of the ugly [[]].
271 // However, this is a horrible hack and it doesn't work like you expect if, say,
272 // you want to link to something OTHER than the title of the log entry.
273 // The real problem, which Erik was trying to fix (and it sort-of works now) is
274 // that the same messages are being treated as both wikitext *and* HTML.
275 if ( $filterWikilinks ) {
276 $rv = str_replace( '[[', '', $rv );
277 $rv = str_replace( ']]', '', $rv );
285 * @param string $type
286 * @param Language|null $lang
287 * @param Title $title
288 * @param array &$params
291 protected static function getTitleLink( $type, $lang, $title, &$params ) {
293 return $title->getPrefixedText();
296 if ( $title->isSpecialPage() ) {
297 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
299 # Use the language name for log titles, rather than Log/X
300 if ( $name == 'Log' ) {
301 $logPage = new LogPage( $par );
302 $titleLink = Linker
::link( $title, $logPage->getName()->escaped() );
303 $titleLink = wfMessage( 'parentheses' )
304 ->inLanguage( $lang )
305 ->rawParams( $titleLink )
308 $titleLink = Linker
::link( $title );
311 $titleLink = Linker
::link( $title );
320 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
321 * 'upload', 'move', 'move_redir'
322 * @param Title $target
323 * @param string $comment Description associated
324 * @param array $params Parameters passed later to wfMessage function
325 * @param null|int|User $doer The user doing the action. null for $wgUser
327 * @return int The log_id of the inserted log entry
329 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
330 if ( !is_array( $params ) ) {
331 $params = [ $params ];
334 if ( $comment === null ) {
338 # Trim spaces on user supplied text
339 $comment = trim( $comment );
341 $this->action
= $action;
342 $this->target
= $target;
343 $this->comment
= $comment;
344 $this->params
= self
::makeParamBlob( $params );
346 if ( $doer === null ) {
349 } elseif ( !is_object( $doer ) ) {
350 $doer = User
::newFromId( $doer );
355 $logEntry = new ManualLogEntry( $this->type
, $action );
356 $logEntry->setTarget( $target );
357 $logEntry->setPerformer( $doer );
358 $logEntry->setParameters( $params );
359 // All log entries using the LogPage to insert into the logging table
360 // are using the old logging system and therefore the legacy flag is
361 // needed to say the LogFormatter the parameters have numeric keys
362 $logEntry->setLegacy( true );
364 $formatter = LogFormatter
::newFromEntry( $logEntry );
365 $context = RequestContext
::newExtraneousContext( $target );
366 $formatter->setContext( $context );
368 $this->actionText
= $formatter->getPlainActionText();
369 $this->ircActionText
= $formatter->getIRCActionText();
371 return $this->saveContent();
375 * Add relations to log_search table
377 * @param string $field
378 * @param array $values
382 public function addRelations( $field, $values, $logid ) {
383 if ( !strlen( $field ) ||
empty( $values ) ) {
384 return false; // nothing
389 foreach ( $values as $value ) {
391 'ls_field' => $field,
392 'ls_value' => $value,
393 'ls_log_id' => $logid
397 $dbw = wfGetDB( DB_MASTER
);
398 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
404 * Create a blob from a parameter array
406 * @param array $params
409 public static function makeParamBlob( $params ) {
410 return implode( "\n", $params );
414 * Extract a parameter array from a blob
416 * @param string $blob
419 public static function extractParams( $blob ) {
420 if ( $blob === '' ) {
423 return explode( "\n", $blob );
432 public function getName() {
436 if ( isset( $wgLogNames[$this->type
] ) ) {
437 $key = $wgLogNames[$this->type
];
439 $key = 'log-name-' . $this->type
;
442 return wfMessage( $key );
446 * Description of this log type.
450 public function getDescription() {
451 global $wgLogHeaders;
453 if ( isset( $wgLogHeaders[$this->type
] ) ) {
454 $key = $wgLogHeaders[$this->type
];
456 $key = 'log-description-' . $this->type
;
459 return wfMessage( $key );
463 * Returns the right needed to read this log type.
467 public function getRestriction() {
468 global $wgLogRestrictions;
469 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
470 $restriction = $wgLogRestrictions[$this->type
];
472 // '' always returns true with $user->isAllowed()
480 * Tells if this log is not viewable by all.
484 public function isRestricted() {
485 $restriction = $this->getRestriction();
487 return $restriction !== '' && $restriction !== '*';