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