Use LinkRenderer instead of deprecated Linker in LogPage
[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 = 12;
41 const SUPPRESSED_ACTION = 9;
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 += CommentStore::getStore()->insert( $dbw, 'log_comment', $this->comment );
108 $data += ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $this->doer );
109 $dbw->insert( 'logging', $data, __METHOD__ );
110 $newId = $dbw->insertId();
111
112 # And update recentchanges
113 if ( $this->updateRecentChanges ) {
114 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
115
116 RecentChange::notifyLog(
117 $now, $titleObj, $this->doer, $this->getRcComment(), '',
118 $this->type, $this->action, $this->target, $this->comment,
119 $this->params, $newId, $this->getRcCommentIRC()
120 );
121 } elseif ( $this->sendToUDP ) {
122 # Don't send private logs to UDP
123 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
124 return $newId;
125 }
126
127 # Notify external application via UDP.
128 # We send this to IRC but do not want to add it the RC table.
129 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
130 $rc = RecentChange::newLogEntry(
131 $now, $titleObj, $this->doer, $this->getRcComment(), '',
132 $this->type, $this->action, $this->target, $this->comment,
133 $this->params, $newId, $this->getRcCommentIRC()
134 );
135 $rc->notifyRCFeeds();
136 }
137
138 return $newId;
139 }
140
141 /**
142 * Get the RC comment from the last addEntry() call
143 *
144 * @return string
145 */
146 public function getRcComment() {
147 $rcComment = $this->actionText;
148
149 if ( $this->comment != '' ) {
150 if ( $rcComment == '' ) {
151 $rcComment = $this->comment;
152 } else {
153 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
154 $this->comment;
155 }
156 }
157
158 return $rcComment;
159 }
160
161 /**
162 * Get the RC comment from the last addEntry() call for IRC
163 *
164 * @return string
165 */
166 public function getRcCommentIRC() {
167 $rcComment = $this->ircActionText;
168
169 if ( $this->comment != '' ) {
170 if ( $rcComment == '' ) {
171 $rcComment = $this->comment;
172 } else {
173 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
174 $this->comment;
175 }
176 }
177
178 return $rcComment;
179 }
180
181 /**
182 * Get the comment from the last addEntry() call
183 * @return string
184 */
185 public function getComment() {
186 return $this->comment;
187 }
188
189 /**
190 * Get the list of valid log types
191 *
192 * @return array Array of strings
193 */
194 public static function validTypes() {
195 global $wgLogTypes;
196
197 return $wgLogTypes;
198 }
199
200 /**
201 * Is $type a valid log type
202 *
203 * @param string $type Log type to check
204 * @return bool
205 */
206 public static function isLogType( $type ) {
207 return in_array( $type, self::validTypes() );
208 }
209
210 /**
211 * Generate text for a log entry.
212 * Only LogFormatter should call this function.
213 *
214 * @param string $type Log type
215 * @param string $action Log action
216 * @param Title|null $title Title object or null
217 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
218 * content language, since that will go to the IRC feed.
219 * @param array $params Parameters
220 * @param bool $filterWikilinks Whether to filter wiki links
221 * @return string HTML
222 */
223 public static function actionText( $type, $action, $title = null, $skin = null,
224 $params = [], $filterWikilinks = false
225 ) {
226 global $wgLang, $wgContLang, $wgLogActions;
227
228 if ( is_null( $skin ) ) {
229 $langObj = $wgContLang;
230 $langObjOrNull = null;
231 } else {
232 $langObj = $wgLang;
233 $langObjOrNull = $wgLang;
234 }
235
236 $key = "$type/$action";
237
238 if ( isset( $wgLogActions[$key] ) ) {
239 if ( is_null( $title ) ) {
240 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
241 } else {
242 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
243
244 if ( count( $params ) == 0 ) {
245 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
246 ->inLanguage( $langObj )->escaped();
247 } else {
248 array_unshift( $params, $titleLink );
249
250 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
251 ->inLanguage( $langObj )->escaped();
252 }
253 }
254 } else {
255 global $wgLogActionsHandlers;
256
257 if ( isset( $wgLogActionsHandlers[$key] ) ) {
258 $args = func_get_args();
259 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
260 } else {
261 wfDebug( "LogPage::actionText - unknown action $key\n" );
262 $rv = "$action";
263 }
264 }
265
266 // For the perplexed, this feature was added in r7855 by Erik.
267 // The feature was added because we liked adding [[$1]] in our log entries
268 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
269 // on Special:Log. The hack is essentially that [[$1]] represented a link
270 // to the title in question. The first parameter to the HTML version (Special:Log)
271 // is that link in HTML form, and so this just gets rid of the ugly [[]].
272 // However, this is a horrible hack and it doesn't work like you expect if, say,
273 // you want to link to something OTHER than the title of the log entry.
274 // The real problem, which Erik was trying to fix (and it sort-of works now) is
275 // that the same messages are being treated as both wikitext *and* HTML.
276 if ( $filterWikilinks ) {
277 $rv = str_replace( '[[', '', $rv );
278 $rv = str_replace( ']]', '', $rv );
279 }
280
281 return $rv;
282 }
283
284 /**
285 * @todo Document
286 * @param string $type
287 * @param Language|null $lang
288 * @param Title $title
289 * @param array &$params
290 * @return string
291 */
292 protected static function getTitleLink( $type, $lang, $title, &$params ) {
293 if ( !$lang ) {
294 return $title->getPrefixedText();
295 }
296
297 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
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 = $linkRenderer->makeLink( $title, $logPage->getName()->text() );
305 $titleLink = wfMessage( 'parentheses' )
306 ->inLanguage( $lang )
307 ->rawParams( $titleLink )
308 ->escaped();
309 } else {
310 $titleLink = $linkRenderer->makeLink( $title );
311 }
312 } else {
313 $titleLink = $linkRenderer->makeLink( $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
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 }