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