Merge "Show protection log on creation-protected pages"
[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_comment' => $this->comment,
108 'log_params' => $this->params
109 ];
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 global $wgContLang;
333
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 # Truncate for whole multibyte characters.
346 $comment = $wgContLang->truncate( $comment, 255 );
347
348 $this->action = $action;
349 $this->target = $target;
350 $this->comment = $comment;
351 $this->params = self::makeParamBlob( $params );
352
353 if ( $doer === null ) {
354 global $wgUser;
355 $doer = $wgUser;
356 } elseif ( !is_object( $doer ) ) {
357 $doer = User::newFromId( $doer );
358 }
359
360 $this->doer = $doer;
361
362 $logEntry = new ManualLogEntry( $this->type, $action );
363 $logEntry->setTarget( $target );
364 $logEntry->setPerformer( $doer );
365 $logEntry->setParameters( $params );
366 // All log entries using the LogPage to insert into the logging table
367 // are using the old logging system and therefore the legacy flag is
368 // needed to say the LogFormatter the parameters have numeric keys
369 $logEntry->setLegacy( true );
370
371 $formatter = LogFormatter::newFromEntry( $logEntry );
372 $context = RequestContext::newExtraneousContext( $target );
373 $formatter->setContext( $context );
374
375 $this->actionText = $formatter->getPlainActionText();
376 $this->ircActionText = $formatter->getIRCActionText();
377
378 return $this->saveContent();
379 }
380
381 /**
382 * Add relations to log_search table
383 *
384 * @param string $field
385 * @param array $values
386 * @param int $logid
387 * @return bool
388 */
389 public function addRelations( $field, $values, $logid ) {
390 if ( !strlen( $field ) || empty( $values ) ) {
391 return false; // nothing
392 }
393
394 $data = [];
395
396 foreach ( $values as $value ) {
397 $data[] = [
398 'ls_field' => $field,
399 'ls_value' => $value,
400 'ls_log_id' => $logid
401 ];
402 }
403
404 $dbw = wfGetDB( DB_MASTER );
405 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
406
407 return true;
408 }
409
410 /**
411 * Create a blob from a parameter array
412 *
413 * @param array $params
414 * @return string
415 */
416 public static function makeParamBlob( $params ) {
417 return implode( "\n", $params );
418 }
419
420 /**
421 * Extract a parameter array from a blob
422 *
423 * @param string $blob
424 * @return array
425 */
426 public static function extractParams( $blob ) {
427 if ( $blob === '' ) {
428 return [];
429 } else {
430 return explode( "\n", $blob );
431 }
432 }
433
434 /**
435 * Name of the log.
436 * @return Message
437 * @since 1.19
438 */
439 public function getName() {
440 global $wgLogNames;
441
442 // BC
443 if ( isset( $wgLogNames[$this->type] ) ) {
444 $key = $wgLogNames[$this->type];
445 } else {
446 $key = 'log-name-' . $this->type;
447 }
448
449 return wfMessage( $key );
450 }
451
452 /**
453 * Description of this log type.
454 * @return Message
455 * @since 1.19
456 */
457 public function getDescription() {
458 global $wgLogHeaders;
459 // BC
460 if ( isset( $wgLogHeaders[$this->type] ) ) {
461 $key = $wgLogHeaders[$this->type];
462 } else {
463 $key = 'log-description-' . $this->type;
464 }
465
466 return wfMessage( $key );
467 }
468
469 /**
470 * Returns the right needed to read this log type.
471 * @return string
472 * @since 1.19
473 */
474 public function getRestriction() {
475 global $wgLogRestrictions;
476 if ( isset( $wgLogRestrictions[$this->type] ) ) {
477 $restriction = $wgLogRestrictions[$this->type];
478 } else {
479 // '' always returns true with $user->isAllowed()
480 $restriction = '';
481 }
482
483 return $restriction;
484 }
485
486 /**
487 * Tells if this log is not viewable by all.
488 * @return bool
489 * @since 1.19
490 */
491 public function isRestricted() {
492 $restriction = $this->getRestriction();
493
494 return $restriction !== '' && $restriction !== '*';
495 }
496 }