Handle comment truncation in CommentStore
[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 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
76 * 'upload', 'move'
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
79 */
80 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
81 $this->type = $type;
82 $this->updateRecentChanges = $rc;
83 $this->sendToUDP = ( $udp == 'UDP' );
84 }
85
86 /**
87 * @return int The log_id of the inserted log entry
88 */
89 protected function saveContent() {
90 global $wgLogRestrictions;
91
92 $dbw = wfGetDB( DB_MASTER );
93 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
94
95 // @todo FIXME private/protected/public property?
96 $this->timestamp = $now = wfTimestampNow();
97 $data = [
98 'log_id' => $log_id,
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_params' => $this->params
108 ];
109 $data += CommentStore::newKey( 'log_comment' )->insert( $dbw, $this->comment );
110 $dbw->insert( 'logging', $data, __METHOD__ );
111 $newId = $dbw->insertId();
112
113 # And update recentchanges
114 if ( $this->updateRecentChanges ) {
115 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
116
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()
121 );
122 } elseif ( $this->sendToUDP ) {
123 # Don't send private logs to UDP
124 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
125 return $newId;
126 }
127
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()
135 );
136 $rc->notifyRCFeeds();
137 }
138
139 return $newId;
140 }
141
142 /**
143 * Get the RC comment from the last addEntry() call
144 *
145 * @return string
146 */
147 public function getRcComment() {
148 $rcComment = $this->actionText;
149
150 if ( $this->comment != '' ) {
151 if ( $rcComment == '' ) {
152 $rcComment = $this->comment;
153 } else {
154 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
155 $this->comment;
156 }
157 }
158
159 return $rcComment;
160 }
161
162 /**
163 * Get the RC comment from the last addEntry() call for IRC
164 *
165 * @return string
166 */
167 public function getRcCommentIRC() {
168 $rcComment = $this->ircActionText;
169
170 if ( $this->comment != '' ) {
171 if ( $rcComment == '' ) {
172 $rcComment = $this->comment;
173 } else {
174 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
175 $this->comment;
176 }
177 }
178
179 return $rcComment;
180 }
181
182 /**
183 * Get the comment from the last addEntry() call
184 * @return string
185 */
186 public function getComment() {
187 return $this->comment;
188 }
189
190 /**
191 * Get the list of valid log types
192 *
193 * @return array Array of strings
194 */
195 public static function validTypes() {
196 global $wgLogTypes;
197
198 return $wgLogTypes;
199 }
200
201 /**
202 * Is $type a valid log type
203 *
204 * @param string $type Log type to check
205 * @return bool
206 */
207 public static function isLogType( $type ) {
208 return in_array( $type, self::validTypes() );
209 }
210
211 /**
212 * Generate text for a log entry.
213 * Only LogFormatter should call this function.
214 *
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
223 */
224 public static function actionText( $type, $action, $title = null, $skin = null,
225 $params = [], $filterWikilinks = false
226 ) {
227 global $wgLang, $wgContLang, $wgLogActions;
228
229 if ( is_null( $skin ) ) {
230 $langObj = $wgContLang;
231 $langObjOrNull = null;
232 } else {
233 $langObj = $wgLang;
234 $langObjOrNull = $wgLang;
235 }
236
237 $key = "$type/$action";
238
239 if ( isset( $wgLogActions[$key] ) ) {
240 if ( is_null( $title ) ) {
241 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
242 } else {
243 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
244
245 if ( count( $params ) == 0 ) {
246 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
247 ->inLanguage( $langObj )->escaped();
248 } else {
249 array_unshift( $params, $titleLink );
250
251 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
252 ->inLanguage( $langObj )->escaped();
253 }
254 }
255 } else {
256 global $wgLogActionsHandlers;
257
258 if ( isset( $wgLogActionsHandlers[$key] ) ) {
259 $args = func_get_args();
260 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
261 } else {
262 wfDebug( "LogPage::actionText - unknown action $key\n" );
263 $rv = "$action";
264 }
265 }
266
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 );
280 }
281
282 return $rv;
283 }
284
285 /**
286 * @todo Document
287 * @param string $type
288 * @param Language|null $lang
289 * @param Title $title
290 * @param array &$params
291 * @return string
292 */
293 protected static function getTitleLink( $type, $lang, $title, &$params ) {
294 if ( !$lang ) {
295 return $title->getPrefixedText();
296 }
297
298 if ( $title->isSpecialPage() ) {
299 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
300
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 )
308 ->escaped();
309 } else {
310 $titleLink = Linker::link( $title );
311 }
312 } else {
313 $titleLink = Linker::link( $title );
314 }
315
316 return $titleLink;
317 }
318
319 /**
320 * Add a log entry
321 *
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
328 *
329 * @return int The log_id of the inserted log entry
330 */
331 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
332 if ( !is_array( $params ) ) {
333 $params = [ $params ];
334 }
335
336 if ( $comment === null ) {
337 $comment = '';
338 }
339
340 # Trim spaces on user supplied text
341 $comment = trim( $comment );
342
343 $this->action = $action;
344 $this->target = $target;
345 $this->comment = $comment;
346 $this->params = self::makeParamBlob( $params );
347
348 if ( $doer === null ) {
349 global $wgUser;
350 $doer = $wgUser;
351 } elseif ( !is_object( $doer ) ) {
352 $doer = User::newFromId( $doer );
353 }
354
355 $this->doer = $doer;
356
357 $logEntry = new ManualLogEntry( $this->type, $action );
358 $logEntry->setTarget( $target );
359 $logEntry->setPerformer( $doer );
360 $logEntry->setParameters( $params );
361 // All log entries using the LogPage to insert into the logging table
362 // are using the old logging system and therefore the legacy flag is
363 // needed to say the LogFormatter the parameters have numeric keys
364 $logEntry->setLegacy( true );
365
366 $formatter = LogFormatter::newFromEntry( $logEntry );
367 $context = RequestContext::newExtraneousContext( $target );
368 $formatter->setContext( $context );
369
370 $this->actionText = $formatter->getPlainActionText();
371 $this->ircActionText = $formatter->getIRCActionText();
372
373 return $this->saveContent();
374 }
375
376 /**
377 * Add relations to log_search table
378 *
379 * @param string $field
380 * @param array $values
381 * @param int $logid
382 * @return bool
383 */
384 public function addRelations( $field, $values, $logid ) {
385 if ( !strlen( $field ) || empty( $values ) ) {
386 return false; // nothing
387 }
388
389 $data = [];
390
391 foreach ( $values as $value ) {
392 $data[] = [
393 'ls_field' => $field,
394 'ls_value' => $value,
395 'ls_log_id' => $logid
396 ];
397 }
398
399 $dbw = wfGetDB( DB_MASTER );
400 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
401
402 return true;
403 }
404
405 /**
406 * Create a blob from a parameter array
407 *
408 * @param array $params
409 * @return string
410 */
411 public static function makeParamBlob( $params ) {
412 return implode( "\n", $params );
413 }
414
415 /**
416 * Extract a parameter array from a blob
417 *
418 * @param string $blob
419 * @return array
420 */
421 public static function extractParams( $blob ) {
422 if ( $blob === '' ) {
423 return [];
424 } else {
425 return explode( "\n", $blob );
426 }
427 }
428
429 /**
430 * Name of the log.
431 * @return Message
432 * @since 1.19
433 */
434 public function getName() {
435 global $wgLogNames;
436
437 // BC
438 if ( isset( $wgLogNames[$this->type] ) ) {
439 $key = $wgLogNames[$this->type];
440 } else {
441 $key = 'log-name-' . $this->type;
442 }
443
444 return wfMessage( $key );
445 }
446
447 /**
448 * Description of this log type.
449 * @return Message
450 * @since 1.19
451 */
452 public function getDescription() {
453 global $wgLogHeaders;
454 // BC
455 if ( isset( $wgLogHeaders[$this->type] ) ) {
456 $key = $wgLogHeaders[$this->type];
457 } else {
458 $key = 'log-description-' . $this->type;
459 }
460
461 return wfMessage( $key );
462 }
463
464 /**
465 * Returns the right needed to read this log type.
466 * @return string
467 * @since 1.19
468 */
469 public function getRestriction() {
470 global $wgLogRestrictions;
471 if ( isset( $wgLogRestrictions[$this->type] ) ) {
472 $restriction = $wgLogRestrictions[$this->type];
473 } else {
474 // '' always returns true with $user->isAllowed()
475 $restriction = '';
476 }
477
478 return $restriction;
479 }
480
481 /**
482 * Tells if this log is not viewable by all.
483 * @return bool
484 * @since 1.19
485 */
486 public function isRestricted() {
487 $restriction = $this->getRestriction();
488
489 return $restriction !== '' && $restriction !== '*';
490 }
491 }