Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 += 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, $wgLogActions;
227
228 if ( is_null( $skin ) ) {
229 $langObj = MediaWikiServices::getInstance()->getContentLanguage();
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 $services = MediaWikiServices::getInstance();
298 $linkRenderer = $services->getLinkRenderer();
299 if ( $title->isSpecialPage() ) {
300 list( $name, $par ) = $services->getSpecialPageFactory()->
301 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 = $linkRenderer->makeLink( $title, $logPage->getName()->text() );
307 $titleLink = wfMessage( 'parentheses' )
308 ->inLanguage( $lang )
309 ->rawParams( $titleLink )
310 ->escaped();
311 } else {
312 $titleLink = $linkRenderer->makeLink( $title );
313 }
314 } else {
315 $titleLink = $linkRenderer->makeLink( $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
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 if ( !is_array( $params ) ) {
335 $params = [ $params ];
336 }
337
338 if ( $comment === null ) {
339 $comment = '';
340 }
341
342 # Trim spaces on user supplied text
343 $comment = trim( $comment );
344
345 $this->action = $action;
346 $this->target = $target;
347 $this->comment = $comment;
348 $this->params = self::makeParamBlob( $params );
349
350 if ( $doer === null ) {
351 global $wgUser;
352 $doer = $wgUser;
353 } elseif ( !is_object( $doer ) ) {
354 $doer = User::newFromId( $doer );
355 }
356
357 $this->doer = $doer;
358
359 $logEntry = new ManualLogEntry( $this->type, $action );
360 $logEntry->setTarget( $target );
361 $logEntry->setPerformer( $doer );
362 $logEntry->setParameters( $params );
363 // All log entries using the LogPage to insert into the logging table
364 // are using the old logging system and therefore the legacy flag is
365 // needed to say the LogFormatter the parameters have numeric keys
366 $logEntry->setLegacy( true );
367
368 $formatter = LogFormatter::newFromEntry( $logEntry );
369 $context = RequestContext::newExtraneousContext( $target );
370 $formatter->setContext( $context );
371
372 $this->actionText = $formatter->getPlainActionText();
373 $this->ircActionText = $formatter->getIRCActionText();
374
375 return $this->saveContent();
376 }
377
378 /**
379 * Add relations to log_search table
380 *
381 * @param string $field
382 * @param array $values
383 * @param int $logid
384 * @return bool
385 */
386 public function addRelations( $field, $values, $logid ) {
387 if ( !strlen( $field ) || empty( $values ) ) {
388 return false;
389 }
390
391 $data = [];
392
393 foreach ( $values as $value ) {
394 $data[] = [
395 'ls_field' => $field,
396 'ls_value' => $value,
397 'ls_log_id' => $logid
398 ];
399 }
400
401 $dbw = wfGetDB( DB_MASTER );
402 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
403
404 return true;
405 }
406
407 /**
408 * Create a blob from a parameter array
409 *
410 * @param array $params
411 * @return string
412 */
413 public static function makeParamBlob( $params ) {
414 return implode( "\n", $params );
415 }
416
417 /**
418 * Extract a parameter array from a blob
419 *
420 * @param string $blob
421 * @return array
422 */
423 public static function extractParams( $blob ) {
424 if ( $blob === '' ) {
425 return [];
426 } else {
427 return explode( "\n", $blob );
428 }
429 }
430
431 /**
432 * Name of the log.
433 * @return Message
434 * @since 1.19
435 */
436 public function getName() {
437 global $wgLogNames;
438
439 // BC
440 $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
441
442 return wfMessage( $key );
443 }
444
445 /**
446 * Description of this log type.
447 * @return Message
448 * @since 1.19
449 */
450 public function getDescription() {
451 global $wgLogHeaders;
452 // BC
453 $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
454
455 return wfMessage( $key );
456 }
457
458 /**
459 * Returns the right needed to read this log type.
460 * @return string
461 * @since 1.19
462 */
463 public function getRestriction() {
464 global $wgLogRestrictions;
465 // '' always returns true with $user->isAllowed()
466 return $wgLogRestrictions[$this->type] ?? '';
467 }
468
469 /**
470 * Tells if this log is not viewable by all.
471 * @return bool
472 * @since 1.19
473 */
474 public function isRestricted() {
475 $restriction = $this->getRestriction();
476
477 return $restriction !== '' && $restriction !== '*';
478 }
479 }