Merge "Add 3D filetype for STL files"
[lhc/web/wiklou.git] / includes / logging / LogPage.php
1 <?php
2 /**
3 * Contain log classes
4 *
5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
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.
30 */
31 class LogPage {
32 const DELETED_ACTION = 1;
33 const DELETED_COMMENT = 2;
34 const DELETED_USER = 4;
35 const DELETED_RESTRICTED = 8;
36
37 // Convenience fields
38 const SUPPRESSED_USER = 12;
39 const SUPPRESSED_ACTION = 9;
40
41 /** @var bool */
42 public $updateRecentChanges;
43
44 /** @var bool */
45 public $sendToUDP;
46
47 /** @var string Plaintext version of the message for IRC */
48 private $ircActionText;
49
50 /** @var string Plaintext version of the message */
51 private $actionText;
52
53 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
54 * 'upload', 'move'
55 */
56 private $type;
57
58 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
59 * 'upload', 'move', 'move_redir' */
60 private $action;
61
62 /** @var string Comment associated with action */
63 private $comment;
64
65 /** @var string Blob made of a parameters array */
66 private $params;
67
68 /** @var User The user doing the action */
69 private $doer;
70
71 /** @var Title */
72 private $target;
73
74 /**
75 * Constructor
76 *
77 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
78 * 'upload', 'move'
79 * @param bool $rc Whether to update recent changes as well as the logging table
80 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
81 */
82 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
83 $this->type = $type;
84 $this->updateRecentChanges = $rc;
85 $this->sendToUDP = ( $udp == 'UDP' );
86 }
87
88 /**
89 * @return int The log_id of the inserted log entry
90 */
91 protected function saveContent() {
92 global $wgLogRestrictions;
93
94 $dbw = wfGetDB( DB_MASTER );
95 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
96
97 // @todo FIXME private/protected/public property?
98 $this->timestamp = $now = wfTimestampNow();
99 $data = [
100 'log_id' => $log_id,
101 'log_type' => $this->type,
102 'log_action' => $this->action,
103 'log_timestamp' => $dbw->timestamp( $now ),
104 'log_user' => $this->doer->getId(),
105 'log_user_text' => $this->doer->getName(),
106 'log_namespace' => $this->target->getNamespace(),
107 'log_title' => $this->target->getDBkey(),
108 'log_page' => $this->target->getArticleID(),
109 'log_comment' => $this->comment,
110 'log_params' => $this->params
111 ];
112 $dbw->insert( 'logging', $data, __METHOD__ );
113 $newId = $dbw->insertId();
114
115 # And update recentchanges
116 if ( $this->updateRecentChanges ) {
117 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
118
119 RecentChange::notifyLog(
120 $now, $titleObj, $this->doer, $this->getRcComment(), '',
121 $this->type, $this->action, $this->target, $this->comment,
122 $this->params, $newId, $this->getRcCommentIRC()
123 );
124 } elseif ( $this->sendToUDP ) {
125 # Don't send private logs to UDP
126 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
127 return $newId;
128 }
129
130 # Notify external application via UDP.
131 # We send this to IRC but do not want to add it the RC table.
132 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
133 $rc = RecentChange::newLogEntry(
134 $now, $titleObj, $this->doer, $this->getRcComment(), '',
135 $this->type, $this->action, $this->target, $this->comment,
136 $this->params, $newId, $this->getRcCommentIRC()
137 );
138 $rc->notifyRCFeeds();
139 }
140
141 return $newId;
142 }
143
144 /**
145 * Get the RC comment from the last addEntry() call
146 *
147 * @return string
148 */
149 public function getRcComment() {
150 $rcComment = $this->actionText;
151
152 if ( $this->comment != '' ) {
153 if ( $rcComment == '' ) {
154 $rcComment = $this->comment;
155 } else {
156 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
157 $this->comment;
158 }
159 }
160
161 return $rcComment;
162 }
163
164 /**
165 * Get the RC comment from the last addEntry() call for IRC
166 *
167 * @return string
168 */
169 public function getRcCommentIRC() {
170 $rcComment = $this->ircActionText;
171
172 if ( $this->comment != '' ) {
173 if ( $rcComment == '' ) {
174 $rcComment = $this->comment;
175 } else {
176 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
177 $this->comment;
178 }
179 }
180
181 return $rcComment;
182 }
183
184 /**
185 * Get the comment from the last addEntry() call
186 * @return string
187 */
188 public function getComment() {
189 return $this->comment;
190 }
191
192 /**
193 * Get the list of valid log types
194 *
195 * @return array Array of strings
196 */
197 public static function validTypes() {
198 global $wgLogTypes;
199
200 return $wgLogTypes;
201 }
202
203 /**
204 * Is $type a valid log type
205 *
206 * @param string $type Log type to check
207 * @return bool
208 */
209 public static function isLogType( $type ) {
210 return in_array( $type, LogPage::validTypes() );
211 }
212
213 /**
214 * Generate text for a log entry.
215 * Only LogFormatter should call this function.
216 *
217 * @param string $type Log type
218 * @param string $action Log action
219 * @param Title|null $title Title object or null
220 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
221 * content language, since that will go to the IRC feed.
222 * @param array $params Parameters
223 * @param bool $filterWikilinks Whether to filter wiki links
224 * @return string HTML
225 */
226 public static function actionText( $type, $action, $title = null, $skin = null,
227 $params = [], $filterWikilinks = false
228 ) {
229 global $wgLang, $wgContLang, $wgLogActions;
230
231 if ( is_null( $skin ) ) {
232 $langObj = $wgContLang;
233 $langObjOrNull = null;
234 } else {
235 $langObj = $wgLang;
236 $langObjOrNull = $wgLang;
237 }
238
239 $key = "$type/$action";
240
241 if ( isset( $wgLogActions[$key] ) ) {
242 if ( is_null( $title ) ) {
243 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
244 } else {
245 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
246
247 if ( count( $params ) == 0 ) {
248 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
249 ->inLanguage( $langObj )->escaped();
250 } else {
251 array_unshift( $params, $titleLink );
252
253 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
254 ->inLanguage( $langObj )->escaped();
255 }
256 }
257 } else {
258 global $wgLogActionsHandlers;
259
260 if ( isset( $wgLogActionsHandlers[$key] ) ) {
261 $args = func_get_args();
262 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
263 } else {
264 wfDebug( "LogPage::actionText - unknown action $key\n" );
265 $rv = "$action";
266 }
267 }
268
269 // For the perplexed, this feature was added in r7855 by Erik.
270 // The feature was added because we liked adding [[$1]] in our log entries
271 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
272 // on Special:Log. The hack is essentially that [[$1]] represented a link
273 // to the title in question. The first parameter to the HTML version (Special:Log)
274 // is that link in HTML form, and so this just gets rid of the ugly [[]].
275 // However, this is a horrible hack and it doesn't work like you expect if, say,
276 // you want to link to something OTHER than the title of the log entry.
277 // The real problem, which Erik was trying to fix (and it sort-of works now) is
278 // that the same messages are being treated as both wikitext *and* HTML.
279 if ( $filterWikilinks ) {
280 $rv = str_replace( '[[', '', $rv );
281 $rv = str_replace( ']]', '', $rv );
282 }
283
284 return $rv;
285 }
286
287 /**
288 * @todo Document
289 * @param string $type
290 * @param Language|null $lang
291 * @param Title $title
292 * @param array $params
293 * @return string
294 */
295 protected static function getTitleLink( $type, $lang, $title, &$params ) {
296 if ( !$lang ) {
297 return $title->getPrefixedText();
298 }
299
300 if ( $title->isSpecialPage() ) {
301 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
302
303 # Use the language name for log titles, rather than Log/X
304 if ( $name == 'Log' ) {
305 $logPage = new LogPage( $par );
306 $titleLink = Linker::link( $title, $logPage->getName()->escaped() );
307 $titleLink = wfMessage( 'parentheses' )
308 ->inLanguage( $lang )
309 ->rawParams( $titleLink )
310 ->escaped();
311 } else {
312 $titleLink = Linker::link( $title );
313 }
314 } else {
315 $titleLink = Linker::link( $title );
316 }
317
318 return $titleLink;
319 }
320
321 /**
322 * Add a log entry
323 *
324 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
325 * 'upload', 'move', 'move_redir'
326 * @param Title $target Title object
327 * @param string $comment Description associated
328 * @param array $params Parameters passed later to wfMessage function
329 * @param null|int|User $doer The user doing the action. null for $wgUser
330 *
331 * @return int The log_id of the inserted log entry
332 */
333 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
334 global $wgContLang;
335
336 if ( !is_array( $params ) ) {
337 $params = [ $params ];
338 }
339
340 if ( $comment === null ) {
341 $comment = '';
342 }
343
344 # Trim spaces on user supplied text
345 $comment = trim( $comment );
346
347 # Truncate for whole multibyte characters.
348 $comment = $wgContLang->truncate( $comment, 255 );
349
350 $this->action = $action;
351 $this->target = $target;
352 $this->comment = $comment;
353 $this->params = LogPage::makeParamBlob( $params );
354
355 if ( $doer === null ) {
356 global $wgUser;
357 $doer = $wgUser;
358 } elseif ( !is_object( $doer ) ) {
359 $doer = User::newFromId( $doer );
360 }
361
362 $this->doer = $doer;
363
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 );
372
373 $formatter = LogFormatter::newFromEntry( $logEntry );
374 $context = RequestContext::newExtraneousContext( $target );
375 $formatter->setContext( $context );
376
377 $this->actionText = $formatter->getPlainActionText();
378 $this->ircActionText = $formatter->getIRCActionText();
379
380 return $this->saveContent();
381 }
382
383 /**
384 * Add relations to log_search table
385 *
386 * @param string $field
387 * @param array $values
388 * @param int $logid
389 * @return bool
390 */
391 public function addRelations( $field, $values, $logid ) {
392 if ( !strlen( $field ) || empty( $values ) ) {
393 return false; // nothing
394 }
395
396 $data = [];
397
398 foreach ( $values as $value ) {
399 $data[] = [
400 'ls_field' => $field,
401 'ls_value' => $value,
402 'ls_log_id' => $logid
403 ];
404 }
405
406 $dbw = wfGetDB( DB_MASTER );
407 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
408
409 return true;
410 }
411
412 /**
413 * Create a blob from a parameter array
414 *
415 * @param array $params
416 * @return string
417 */
418 public static function makeParamBlob( $params ) {
419 return implode( "\n", $params );
420 }
421
422 /**
423 * Extract a parameter array from a blob
424 *
425 * @param string $blob
426 * @return array
427 */
428 public static function extractParams( $blob ) {
429 if ( $blob === '' ) {
430 return [];
431 } else {
432 return explode( "\n", $blob );
433 }
434 }
435
436 /**
437 * Name of the log.
438 * @return Message
439 * @since 1.19
440 */
441 public function getName() {
442 global $wgLogNames;
443
444 // BC
445 if ( isset( $wgLogNames[$this->type] ) ) {
446 $key = $wgLogNames[$this->type];
447 } else {
448 $key = 'log-name-' . $this->type;
449 }
450
451 return wfMessage( $key );
452 }
453
454 /**
455 * Description of this log type.
456 * @return Message
457 * @since 1.19
458 */
459 public function getDescription() {
460 global $wgLogHeaders;
461 // BC
462 if ( isset( $wgLogHeaders[$this->type] ) ) {
463 $key = $wgLogHeaders[$this->type];
464 } else {
465 $key = 'log-description-' . $this->type;
466 }
467
468 return wfMessage( $key );
469 }
470
471 /**
472 * Returns the right needed to read this log type.
473 * @return string
474 * @since 1.19
475 */
476 public function getRestriction() {
477 global $wgLogRestrictions;
478 if ( isset( $wgLogRestrictions[$this->type] ) ) {
479 $restriction = $wgLogRestrictions[$this->type];
480 } else {
481 // '' always returns true with $user->isAllowed()
482 $restriction = '';
483 }
484
485 return $restriction;
486 }
487
488 /**
489 * Tells if this log is not viewable by all.
490 * @return bool
491 * @since 1.19
492 */
493 public function isRestricted() {
494 $restriction = $this->getRestriction();
495
496 return $restriction !== '' && $restriction !== '*';
497 }
498 }