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