Adding jquery plugins that UploadWizard and jQueryMsg need for Jasmine tests
[lhc/web/wiklou.git] / includes / LogPage.php
1 <?php
2 /**
3 * Contain log classes
4 *
5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * http://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 */
32 class LogPage {
33 const DELETED_ACTION = 1;
34 const DELETED_COMMENT = 2;
35 const DELETED_USER = 4;
36 const DELETED_RESTRICTED = 8;
37 // Convenience fields
38 const SUPPRESSED_USER = 12;
39 const SUPPRESSED_ACTION = 9;
40 /* @access private */
41 var $type, $action, $comment, $params;
42
43 /**
44 * @var User
45 */
46 var $doer;
47
48 /**
49 * @var Title
50 */
51 var $target;
52
53 /* @acess public */
54 var $updateRecentChanges, $sendToUDP;
55
56 /**
57 * Constructor
58 *
59 * @param $type String: one of '', 'block', 'protect', 'rights', 'delete',
60 * 'upload', 'move'
61 * @param $rc Boolean: whether to update recent changes as well as the logging table
62 * @param $udp String: pass 'UDP' to send to the UDP feed if NOT sent to RC
63 */
64 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
65 $this->type = $type;
66 $this->updateRecentChanges = $rc;
67 $this->sendToUDP = ( $udp == 'UDP' );
68 }
69
70 /**
71 * @return bool|int|null
72 */
73 protected function saveContent() {
74 global $wgLogRestrictions;
75
76 $dbw = wfGetDB( DB_MASTER );
77 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
78
79 $this->timestamp = $now = wfTimestampNow();
80 $data = array(
81 'log_id' => $log_id,
82 'log_type' => $this->type,
83 'log_action' => $this->action,
84 'log_timestamp' => $dbw->timestamp( $now ),
85 'log_user' => $this->doer->getId(),
86 'log_user_text' => $this->doer->getName(),
87 'log_namespace' => $this->target->getNamespace(),
88 'log_title' => $this->target->getDBkey(),
89 'log_page' => $this->target->getArticleId(),
90 'log_comment' => $this->comment,
91 'log_params' => $this->params
92 );
93 $dbw->insert( 'logging', $data, __METHOD__ );
94 $newId = !is_null( $log_id ) ? $log_id : $dbw->insertId();
95
96 # And update recentchanges
97 if( $this->updateRecentChanges ) {
98 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
99 RecentChange::notifyLog(
100 $now, $titleObj, $this->doer, $this->getRcComment(), '',
101 $this->type, $this->action, $this->target, $this->comment,
102 $this->params, $newId
103 );
104 } elseif( $this->sendToUDP ) {
105 # Don't send private logs to UDP
106 if( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
107 return true;
108 }
109 # Notify external application via UDP.
110 # We send this to IRC but do not want to add it the RC table.
111 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
112 $rc = RecentChange::newLogEntry(
113 $now, $titleObj, $this->doer, $this->getRcComment(), '',
114 $this->type, $this->action, $this->target, $this->comment,
115 $this->params, $newId
116 );
117 $rc->notifyRC2UDP();
118 }
119 return $newId;
120 }
121
122 /**
123 * Get the RC comment from the last addEntry() call
124 */
125 public function getRcComment() {
126 $rcComment = $this->actionText;
127 if( $this->comment != '' ) {
128 if ( $rcComment == '' ) {
129 $rcComment = $this->comment;
130 } else {
131 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
132 }
133 }
134 return $rcComment;
135 }
136
137 /**
138 * Get the comment from the last addEntry() call
139 */
140 public function getComment() {
141 return $this->comment;
142 }
143
144 /**
145 * Get the list of valid log types
146 *
147 * @return Array of strings
148 */
149 public static function validTypes() {
150 global $wgLogTypes;
151 return $wgLogTypes;
152 }
153
154 /**
155 * Is $type a valid log type
156 *
157 * @param $type String: log type to check
158 * @return Boolean
159 */
160 public static function isLogType( $type ) {
161 return in_array( $type, LogPage::validTypes() );
162 }
163
164 /**
165 * Get the name for the given log type
166 *
167 * @param $type String: logtype
168 * @return String: log name
169 */
170 public static function logName( $type ) {
171 global $wgLogNames;
172
173 if( isset( $wgLogNames[$type] ) ) {
174 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
175 } else {
176 // Bogus log types? Perhaps an extension was removed.
177 return $type;
178 }
179 }
180
181 /**
182 * Get the log header for the given log type
183 *
184 * @todo handle missing log types
185 * @param $type String: logtype
186 * @return String: headertext of this logtype
187 */
188 public static function logHeader( $type ) {
189 global $wgLogHeaders;
190 return wfMsgExt( $wgLogHeaders[$type], array( 'parseinline' ) );
191 }
192
193 /**
194 * Generate text for a log entry
195 *
196 * @param $type String: log type
197 * @param $action String: log action
198 * @param $title Mixed: Title object or null
199 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
200 * content language, since that will go to the IRC feed.
201 * @param $params Array: parameters
202 * @param $filterWikilinks Boolean: whether to filter wiki links
203 * @return HTML string
204 */
205 public static function actionText( $type, $action, $title = null, $skin = null,
206 $params = array(), $filterWikilinks = false )
207 {
208 global $wgLang, $wgContLang, $wgLogActions;
209
210 if ( is_null( $skin ) ) {
211 $langObj = $wgContLang;
212 $langObjOrNull = null;
213 } else {
214 $langObj = $wgLang;
215 $langObjOrNull = $wgLang;
216 }
217
218 $key = "$type/$action";
219 # Defer patrol log to PatrolLog class
220 if( $key == 'patrol/patrol' ) {
221 return PatrolLog::makeActionText( $title, $params, $langObjOrNull );
222 }
223 if( isset( $wgLogActions[$key] ) ) {
224 if( is_null( $title ) ) {
225 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'language' => $langObj ) );
226 } else {
227 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
228 if( preg_match( '/^rights\/(rights|autopromote)/', $key ) ) {
229 $rightsnone = wfMsgExt( 'rightsnone', array( 'parsemag', 'language' => $langObj ) );
230 if( $skin ) {
231 foreach ( $params as &$param ) {
232 $groupArray = array_map( 'trim', explode( ',', $param ) );
233 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
234 $param = $wgLang->listToText( $groupArray );
235 }
236 }
237 if( !isset( $params[0] ) || trim( $params[0] ) == '' ) {
238 $params[0] = $rightsnone;
239 }
240 if( !isset( $params[1] ) || trim( $params[1] ) == '' ) {
241 $params[1] = $rightsnone;
242 }
243 }
244 if( count( $params ) == 0 ) {
245 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'language' => $langObj ), $titleLink );
246 } else {
247 $details = '';
248 array_unshift( $params, $titleLink );
249 // User suppression
250 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
251 if ( $skin ) {
252 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
253 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
254 } else {
255 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
256 }
257 $params[2] = isset( $params[2] ) ?
258 self::formatBlockFlags( $params[2], $langObj ) : '';
259
260 // Page protections
261 } elseif ( $type == 'protect' && count($params) == 3 ) {
262 // Restrictions and expiries
263 if( $skin ) {
264 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
265 } else {
266 $details .= " {$params[1]}";
267 }
268 // Cascading flag...
269 if( $params[2] ) {
270 $details .= ' [' . wfMsgExt( 'protect-summary-cascade', array( 'parsemag', 'language' => $langObj ) ) . ']';
271 }
272
273 // Page moves
274 } elseif ( $type == 'move' && count( $params ) == 3 ) {
275 if( $params[2] ) {
276 $details .= ' [' . wfMsgExt( 'move-redirect-suppressed', array( 'parsemag', 'language' => $langObj ) ) . ']';
277 }
278
279 // Revision deletion
280 } elseif ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
281 $count = substr_count( $params[2], ',' ) + 1; // revisions
282 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
283 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
284 $details .= ': ' . RevisionDeleter::getLogMessage( $count, $nfield, $ofield, $langObj, false );
285
286 // Log deletion
287 } elseif ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
288 $count = substr_count( $params[1], ',' ) + 1; // log items
289 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
290 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
291 $details .= ': ' . RevisionDeleter::getLogMessage( $count, $nfield, $ofield, $langObj, true );
292 }
293
294 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'language' => $langObj ), $params ) . $details;
295 }
296 }
297 } else {
298 global $wgLogActionsHandlers;
299 if( isset( $wgLogActionsHandlers[$key] ) ) {
300 $args = func_get_args();
301 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
302 } else {
303 wfDebug( "LogPage::actionText - unknown action $key\n" );
304 $rv = "$action";
305 }
306 }
307
308 // For the perplexed, this feature was added in r7855 by Erik.
309 // The feature was added because we liked adding [[$1]] in our log entries
310 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
311 // on Special:Log. The hack is essentially that [[$1]] represented a link
312 // to the title in question. The first parameter to the HTML version (Special:Log)
313 // is that link in HTML form, and so this just gets rid of the ugly [[]].
314 // However, this is a horrible hack and it doesn't work like you expect if, say,
315 // you want to link to something OTHER than the title of the log entry.
316 // The real problem, which Erik was trying to fix (and it sort-of works now) is
317 // that the same messages are being treated as both wikitext *and* HTML.
318 if( $filterWikilinks ) {
319 $rv = str_replace( '[[', '', $rv );
320 $rv = str_replace( ']]', '', $rv );
321 }
322 return $rv;
323 }
324
325 /**
326 * TODO document
327 * @param $type String
328 * @param $lang Language or null
329 * @param $title Title
330 * @param $params Array
331 * @return String
332 */
333 protected static function getTitleLink( $type, $lang, $title, &$params ) {
334 global $wgContLang, $wgUserrightsInterwikiDelimiter;
335 if( !$lang ) {
336 return $title->getPrefixedText();
337 }
338 switch( $type ) {
339 case 'move':
340 $titleLink = Linker::link(
341 $title,
342 htmlspecialchars( $title->getPrefixedText() ),
343 array(),
344 array( 'redirect' => 'no' )
345 );
346 $targetTitle = Title::newFromText( $params[0] );
347 if ( !$targetTitle ) {
348 # Workaround for broken database
349 $params[0] = htmlspecialchars( $params[0] );
350 } else {
351 $params[0] = Linker::link(
352 $targetTitle,
353 htmlspecialchars( $params[0] )
354 );
355 }
356 break;
357 case 'block':
358 if( substr( $title->getText(), 0, 1 ) == '#' ) {
359 $titleLink = $title->getText();
360 } else {
361 // TODO: Store the user identifier in the parameters
362 // to make this faster for future log entries
363 $id = User::idFromName( $title->getText() );
364 $titleLink = Linker::userLink( $id, $title->getText() )
365 . Linker::userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
366 }
367 break;
368 case 'rights':
369 $text = $wgContLang->ucfirst( $title->getText() );
370 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
371 if ( count( $parts ) == 2 ) {
372 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
373 htmlspecialchars( $title->getPrefixedText() ) );
374 if ( $titleLink !== false ) {
375 break;
376 }
377 }
378 $titleLink = Linker::link( Title::makeTitle( NS_USER, $text ) );
379 break;
380 case 'merge':
381 $titleLink = Linker::link(
382 $title,
383 $title->getPrefixedText(),
384 array(),
385 array( 'redirect' => 'no' )
386 );
387 $params[0] = Linker::link(
388 Title::newFromText( $params[0] ),
389 htmlspecialchars( $params[0] )
390 );
391 $params[1] = $lang->timeanddate( $params[1] );
392 break;
393 default:
394 if( $title->getNamespace() == NS_SPECIAL ) {
395 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
396 # Use the language name for log titles, rather than Log/X
397 if( $name == 'Log' ) {
398 $titleLink = '(' . Linker::link( $title, LogPage::logName( $par ) ) . ')';
399 } else {
400 $titleLink = Linker::link( $title );
401 }
402 } else {
403 $titleLink = Linker::link( $title );
404 }
405 }
406 return $titleLink;
407 }
408
409 /**
410 * Add a log entry
411 *
412 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
413 * @param $target Title object
414 * @param $comment String: description associated
415 * @param $params Array: parameters passed later to wfMsg.* functions
416 * @param $doer User object: the user doing the action
417 */
418 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
419 if ( !is_array( $params ) ) {
420 $params = array( $params );
421 }
422
423 if ( $comment === null ) {
424 $comment = '';
425 }
426
427 $this->action = $action;
428 $this->target = $target;
429 $this->comment = $comment;
430 $this->params = LogPage::makeParamBlob( $params );
431
432 if ( $doer === null ) {
433 global $wgUser;
434 $doer = $wgUser;
435 } elseif ( !is_object( $doer ) ) {
436 $doer = User::newFromId( $doer );
437 }
438
439 $this->doer = $doer;
440
441 $this->actionText = LogPage::actionText( $this->type, $action, $target, null, $params );
442
443 return $this->saveContent();
444 }
445
446 /**
447 * Add relations to log_search table
448 *
449 * @param $field String
450 * @param $values Array
451 * @param $logid Integer
452 * @return Boolean
453 */
454 public function addRelations( $field, $values, $logid ) {
455 if( !strlen( $field ) || empty( $values ) ) {
456 return false; // nothing
457 }
458 $data = array();
459 foreach( $values as $value ) {
460 $data[] = array(
461 'ls_field' => $field,
462 'ls_value' => $value,
463 'ls_log_id' => $logid
464 );
465 }
466 $dbw = wfGetDB( DB_MASTER );
467 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
468 return true;
469 }
470
471 /**
472 * Create a blob from a parameter array
473 *
474 * @param $params Array
475 * @return String
476 */
477 public static function makeParamBlob( $params ) {
478 return implode( "\n", $params );
479 }
480
481 /**
482 * Extract a parameter array from a blob
483 *
484 * @param $blob String
485 * @return Array
486 */
487 public static function extractParams( $blob ) {
488 if ( $blob === '' ) {
489 return array();
490 } else {
491 return explode( "\n", $blob );
492 }
493 }
494
495 /**
496 * Convert a comma-delimited list of block log flags
497 * into a more readable (and translated) form
498 *
499 * @param $flags Flags to format
500 * @param $lang Language object to use
501 * @return String
502 */
503 public static function formatBlockFlags( $flags, $lang ) {
504 $flags = explode( ',', trim( $flags ) );
505 if( count( $flags ) > 0 ) {
506 for( $i = 0; $i < count( $flags ); $i++ ) {
507 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
508 }
509 return '(' . $lang->commaList( $flags ) . ')';
510 } else {
511 return '';
512 }
513 }
514
515 /**
516 * Translate a block log flag if possible
517 *
518 * @param $flag int Flag to translate
519 * @param $lang Language object to use
520 * @return String
521 */
522 public static function formatBlockFlag( $flag, $lang ) {
523 static $messages = array();
524 if( !isset( $messages[$flag] ) ) {
525 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
526
527 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
528 if ( $msg->exists() ) {
529 $messages[$flag] = $msg->escaped();
530 }
531 }
532 return $messages[$flag];
533 }
534 }
535
536 /**
537 * Aliases for backwards compatibility with 1.6
538 */
539 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
540 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
541 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
542 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );