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