Merge "Fix param doc of ChangeTagsList::updateChangeTagsOnAll"
[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 */
63 private $action;
64
65 /** @var string Comment associated with action */
66 private $comment;
67
68 /** @var string Blob made of a parameters array */
69 private $params;
70
71 /** @var User The user doing the action */
72 private $doer;
73
74 /** @var Title */
75 private $target;
76
77 /**
78 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
79 * 'upload', 'move'
80 * @param bool $rc Whether to update recent changes as well as the logging table
81 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
82 */
83 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
84 $this->type = $type;
85 $this->updateRecentChanges = $rc;
86 $this->sendToUDP = ( $udp == 'UDP' );
87 }
88
89 /**
90 * @return int The log_id of the inserted log entry
91 */
92 protected function saveContent() {
93 global $wgLogRestrictions;
94
95 $dbw = wfGetDB( DB_MASTER );
96
97 // @todo FIXME private/protected/public property?
98 $this->timestamp = $now = wfTimestampNow();
99 $data = [
100 'log_type' => $this->type,
101 'log_action' => $this->action,
102 'log_timestamp' => $dbw->timestamp( $now ),
103 'log_namespace' => $this->target->getNamespace(),
104 'log_title' => $this->target->getDBkey(),
105 'log_page' => $this->target->getArticleID(),
106 'log_params' => $this->params
107 ];
108 $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
109 $dbw,
110 'log_comment',
111 $this->comment
112 );
113 $data += ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $this->doer );
114 $dbw->insert( 'logging', $data, __METHOD__ );
115 $newId = $dbw->insertId();
116
117 # And update recentchanges
118 if ( $this->updateRecentChanges ) {
119 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
120
121 RecentChange::notifyLog(
122 $now, $titleObj, $this->doer, $this->getRcComment(), '',
123 $this->type, $this->action, $this->target, $this->comment,
124 $this->params, $newId, $this->getRcCommentIRC()
125 );
126 } elseif ( $this->sendToUDP ) {
127 # Don't send private logs to UDP
128 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
129 return $newId;
130 }
131
132 # Notify external application via UDP.
133 # We send this to IRC but do not want to add it the RC table.
134 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
135 $rc = RecentChange::newLogEntry(
136 $now, $titleObj, $this->doer, $this->getRcComment(), '',
137 $this->type, $this->action, $this->target, $this->comment,
138 $this->params, $newId, $this->getRcCommentIRC()
139 );
140 $rc->notifyRCFeeds();
141 }
142
143 return $newId;
144 }
145
146 /**
147 * Get the RC comment from the last addEntry() call
148 *
149 * @return string
150 */
151 public function getRcComment() {
152 $rcComment = $this->actionText;
153
154 if ( $this->comment != '' ) {
155 if ( $rcComment == '' ) {
156 $rcComment = $this->comment;
157 } else {
158 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
159 $this->comment;
160 }
161 }
162
163 return $rcComment;
164 }
165
166 /**
167 * Get the RC comment from the last addEntry() call for IRC
168 *
169 * @return string
170 */
171 public function getRcCommentIRC() {
172 $rcComment = $this->ircActionText;
173
174 if ( $this->comment != '' ) {
175 if ( $rcComment == '' ) {
176 $rcComment = $this->comment;
177 } else {
178 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
179 $this->comment;
180 }
181 }
182
183 return $rcComment;
184 }
185
186 /**
187 * Get the comment from the last addEntry() call
188 * @return string
189 */
190 public function getComment() {
191 return $this->comment;
192 }
193
194 /**
195 * Get the list of valid log types
196 *
197 * @return array Array of strings
198 */
199 public static function validTypes() {
200 global $wgLogTypes;
201
202 return $wgLogTypes;
203 }
204
205 /**
206 * Is $type a valid log type
207 *
208 * @param string $type Log type to check
209 * @return bool
210 */
211 public static function isLogType( $type ) {
212 return in_array( $type, self::validTypes() );
213 }
214
215 /**
216 * Generate text for a log entry.
217 * Only LogFormatter should call this function.
218 *
219 * @param string $type Log type
220 * @param string $action Log action
221 * @param Title|null $title Title object or null
222 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
223 * content language, since that will go to the IRC feed.
224 * @param array $params Parameters
225 * @param bool $filterWikilinks Whether to filter wiki links
226 * @return string HTML
227 */
228 public static function actionText( $type, $action, $title = null, $skin = null,
229 $params = [], $filterWikilinks = false
230 ) {
231 global $wgLang, $wgLogActions;
232
233 if ( is_null( $skin ) ) {
234 $langObj = MediaWikiServices::getInstance()->getContentLanguage();
235 $langObjOrNull = null;
236 } else {
237 $langObj = $wgLang;
238 $langObjOrNull = $wgLang;
239 }
240
241 $key = "$type/$action";
242
243 if ( isset( $wgLogActions[$key] ) ) {
244 if ( is_null( $title ) ) {
245 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
246 } else {
247 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
248
249 if ( count( $params ) == 0 ) {
250 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
251 ->inLanguage( $langObj )->escaped();
252 } else {
253 array_unshift( $params, $titleLink );
254
255 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
256 ->inLanguage( $langObj )->escaped();
257 }
258 }
259 } else {
260 global $wgLogActionsHandlers;
261
262 if ( isset( $wgLogActionsHandlers[$key] ) ) {
263 $args = func_get_args();
264 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
265 } else {
266 wfDebug( "LogPage::actionText - unknown action $key\n" );
267 $rv = "$action";
268 }
269 }
270
271 // For the perplexed, this feature was added in r7855 by Erik.
272 // The feature was added because we liked adding [[$1]] in our log entries
273 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
274 // on Special:Log. The hack is essentially that [[$1]] represented a link
275 // to the title in question. The first parameter to the HTML version (Special:Log)
276 // is that link in HTML form, and so this just gets rid of the ugly [[]].
277 // However, this is a horrible hack and it doesn't work like you expect if, say,
278 // you want to link to something OTHER than the title of the log entry.
279 // The real problem, which Erik was trying to fix (and it sort-of works now) is
280 // that the same messages are being treated as both wikitext *and* HTML.
281 if ( $filterWikilinks ) {
282 $rv = str_replace( '[[', '', $rv );
283 $rv = str_replace( ']]', '', $rv );
284 }
285
286 return $rv;
287 }
288
289 /**
290 * @todo Document
291 * @param string $type
292 * @param Language|null $lang
293 * @param Title $title
294 * @param array &$params
295 * @return string
296 */
297 protected static function getTitleLink( $type, $lang, $title, &$params ) {
298 if ( !$lang ) {
299 return $title->getPrefixedText();
300 }
301
302 $services = MediaWikiServices::getInstance();
303 $linkRenderer = $services->getLinkRenderer();
304 if ( $title->isSpecialPage() ) {
305 list( $name, $par ) = $services->getSpecialPageFactory()->
306 resolveAlias( $title->getDBkey() );
307
308 # Use the language name for log titles, rather than Log/X
309 if ( $name == 'Log' ) {
310 $logPage = new LogPage( $par );
311 $titleLink = $linkRenderer->makeLink( $title, $logPage->getName()->text() );
312 $titleLink = wfMessage( 'parentheses' )
313 ->inLanguage( $lang )
314 ->rawParams( $titleLink )
315 ->escaped();
316 } else {
317 $titleLink = $linkRenderer->makeLink( $title );
318 }
319 } else {
320 $titleLink = $linkRenderer->makeLink( $title );
321 }
322
323 return $titleLink;
324 }
325
326 /**
327 * Add a log entry
328 *
329 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
330 * 'upload', 'move', 'move_redir'
331 * @param Title $target
332 * @param string $comment Description associated
333 * @param array $params Parameters passed later to wfMessage function
334 * @param null|int|User $doer The user doing the action. null for $wgUser
335 *
336 * @return int The log_id of the inserted log entry
337 */
338 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
339 if ( !is_array( $params ) ) {
340 $params = [ $params ];
341 }
342
343 if ( $comment === null ) {
344 $comment = '';
345 }
346
347 # Trim spaces on user supplied text
348 $comment = trim( $comment );
349
350 $this->action = $action;
351 $this->target = $target;
352 $this->comment = $comment;
353 $this->params = self::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;
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 $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
446
447 return wfMessage( $key );
448 }
449
450 /**
451 * Description of this log type.
452 * @return Message
453 * @since 1.19
454 */
455 public function getDescription() {
456 global $wgLogHeaders;
457 // BC
458 $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
459
460 return wfMessage( $key );
461 }
462
463 /**
464 * Returns the right needed to read this log type.
465 * @return string
466 * @since 1.19
467 */
468 public function getRestriction() {
469 global $wgLogRestrictions;
470 // '' always returns true with $user->isAllowed()
471 return $wgLogRestrictions[$this->type] ?? '';
472 }
473
474 /**
475 * Tells if this log is not viewable by all.
476 * @return bool
477 * @since 1.19
478 */
479 public function isRestricted() {
480 $restriction = $this->getRestriction();
481
482 return $restriction !== '' && $restriction !== '*';
483 }
484 }