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
);
93 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
95 // @todo FIXME private/protected/public property?
96 $this->timestamp
= $now = wfTimestampNow();
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
] != '*' ) {
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();
143 * Get the RC comment from the last addEntry() call
147 public function getRcComment() {
148 $rcComment = $this->actionText
;
150 if ( $this->comment
!= '' ) {
151 if ( $rcComment == '' ) {
152 $rcComment = $this->comment
;
154 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
163 * Get the RC comment from the last addEntry() call for IRC
167 public function getRcCommentIRC() {
168 $rcComment = $this->ircActionText
;
170 if ( $this->comment
!= '' ) {
171 if ( $rcComment == '' ) {
172 $rcComment = $this->comment
;
174 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
183 * Get the comment from the last addEntry() call
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() {
202 * Is $type a valid log type
204 * @param string $type Log type to check
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;
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();
243 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
245 if ( count( $params ) == 0 ) {
246 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
247 ->inLanguage( $langObj )->escaped();
249 array_unshift( $params, $titleLink );
251 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
252 ->inLanguage( $langObj )->escaped();
256 global $wgLogActionsHandlers;
258 if ( isset( $wgLogActionsHandlers[$key] ) ) {
259 $args = func_get_args();
260 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
262 wfDebug( "LogPage::actionText - unknown action $key\n" );
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 );
287 * @param string $type
288 * @param Language|null $lang
289 * @param Title $title
290 * @param array &$params
293 protected static function getTitleLink( $type, $lang, $title, &$params ) {
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 )
310 $titleLink = Linker
::link( $title );
313 $titleLink = Linker
::link( $title );
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 ) {
334 if ( !is_array( $params ) ) {
335 $params = [ $params ];
338 if ( $comment === null ) {
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 ) {
356 } elseif ( !is_object( $doer ) ) {
357 $doer = User
::newFromId( $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
389 public function addRelations( $field, $values, $logid ) {
390 if ( !strlen( $field ) ||
empty( $values ) ) {
391 return false; // nothing
396 foreach ( $values as $value ) {
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' );
411 * Create a blob from a parameter array
413 * @param array $params
416 public static function makeParamBlob( $params ) {
417 return implode( "\n", $params );
421 * Extract a parameter array from a blob
423 * @param string $blob
426 public static function extractParams( $blob ) {
427 if ( $blob === '' ) {
430 return explode( "\n", $blob );
439 public function getName() {
443 if ( isset( $wgLogNames[$this->type
] ) ) {
444 $key = $wgLogNames[$this->type
];
446 $key = 'log-name-' . $this->type
;
449 return wfMessage( $key );
453 * Description of this log type.
457 public function getDescription() {
458 global $wgLogHeaders;
460 if ( isset( $wgLogHeaders[$this->type
] ) ) {
461 $key = $wgLogHeaders[$this->type
];
463 $key = 'log-description-' . $this->type
;
466 return wfMessage( $key );
470 * Returns the right needed to read this log type.
474 public function getRestriction() {
475 global $wgLogRestrictions;
476 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
477 $restriction = $wgLogRestrictions[$this->type
];
479 // '' always returns true with $user->isAllowed()
487 * Tells if this log is not viewable by all.
491 public function isRestricted() {
492 $restriction = $this->getRestriction();
494 return $restriction !== '' && $restriction !== '*';