]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/LogPage.php
MediaWiki 1.14.0
[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         /* @access private */
38         var $type, $action, $comment, $params, $target, $doer;
39         /* @acess public */
40         var $updateRecentChanges;
41
42         /**
43           * Constructor
44           *
45           * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
46           *               'upload', 'move'
47           * @param bool $rc Whether to update recent changes as well as the logging table
48           */
49         function __construct( $type, $rc = true ) {
50                 $this->type = $type;
51                 $this->updateRecentChanges = $rc;
52         }
53
54         protected function saveContent() {
55                 global $wgUser, $wgLogRestrictions;
56                 $fname = 'LogPage::saveContent';
57
58                 $dbw = wfGetDB( DB_MASTER );
59                 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
60
61                 $this->timestamp = $now = wfTimestampNow();
62                 $data = array(
63                         'log_id' => $log_id,
64                         'log_type' => $this->type,
65                         'log_action' => $this->action,
66                         'log_timestamp' => $dbw->timestamp( $now ),
67                         'log_user' => $this->doer->getId(),
68                         'log_namespace' => $this->target->getNamespace(),
69                         'log_title' => $this->target->getDBkey(),
70                         'log_comment' => $this->comment,
71                         'log_params' => $this->params
72                 );
73                 $dbw->insert( 'logging', $data, $fname );
74                 $newId = !is_null($log_id) ? $log_id : $dbw->insertId();
75
76                 if( !($dbw->affectedRows() > 0) ) {
77                         wfDebugLog( "logging", "LogPage::saveContent failed to insert row - Error {$dbw->lastErrno()}: {$dbw->lastError()}" );
78                 }
79                 # And update recentchanges
80                 if( $this->updateRecentChanges ) {
81                         # Don't add private logs to RC!
82                         if( !isset($wgLogRestrictions[$this->type]) || $wgLogRestrictions[$this->type]=='*' ) {
83                                 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
84                                 $rcComment = $this->getRcComment();
85                                 RecentChange::notifyLog( $now, $titleObj, $this->doer, $rcComment, '',
86                                         $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
87                         }
88                 }
89                 return true;
90         }
91
92         /**
93          * Get the RC comment from the last addEntry() call
94          */
95         public function getRcComment() {
96                 $rcComment = $this->actionText;
97                 if( '' != $this->comment ) {
98                         if ($rcComment == '')
99                                 $rcComment = $this->comment;
100                         else
101                                 $rcComment .= ': ' . $this->comment;
102                 }
103                 return $rcComment;
104         }
105
106         /**
107          * Get the comment from the last addEntry() call
108          */
109         public function getComment() {
110                 return $this->comment;
111         }
112
113         /**
114          * @static
115          */
116         public static function validTypes() {
117                 global $wgLogTypes;
118                 return $wgLogTypes;
119         }
120
121         /**
122          * @static
123          */
124         public static function isLogType( $type ) {
125                 return in_array( $type, LogPage::validTypes() );
126         }
127
128         /**
129          * @static
130          */
131         public static function logName( $type ) {
132                 global $wgLogNames, $wgMessageCache;
133
134                 if( isset( $wgLogNames[$type] ) ) {
135                         $wgMessageCache->loadAllMessages();
136                         return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
137                 } else {
138                         // Bogus log types? Perhaps an extension was removed.
139                         return $type;
140                 }
141         }
142
143         /**
144          * @todo handle missing log types
145          * @param string $type logtype
146          * @return string Headertext of this logtype
147          */
148         static function logHeader( $type ) {
149                 global $wgLogHeaders, $wgMessageCache;
150                 $wgMessageCache->loadAllMessages();
151                 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
152         }
153
154         /**
155          * @static
156          * @return HTML string
157          */
158         static function actionText( $type, $action, $title = NULL, $skin = NULL, 
159                 $params = array(), $filterWikilinks = false ) 
160         {
161                 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
162
163                 $wgMessageCache->loadAllMessages();
164                 $key = "$type/$action";
165                 # Defer patrol log to PatrolLog class
166                 if( $key == 'patrol/patrol' ) {
167                         return PatrolLog::makeActionText( $title, $params, $skin );
168                 }
169                 if( isset( $wgLogActions[$key] ) ) {
170                         if( is_null( $title ) ) {
171                                 $rv = wfMsg( $wgLogActions[$key] );
172                         } else {
173                                 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
174                                 if( $key == 'rights/rights' ) {
175                                         if( $skin ) {
176                                                 $rightsnone = wfMsg( 'rightsnone' );
177                                                 foreach ( $params as &$param ) {
178                                                         $groupArray = array_map( 'trim', explode( ',', $param ) );
179                                                         $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
180                                                         $param = $wgLang->listToText( $groupArray );
181                                                 }
182                                         } else {
183                                                 $rightsnone = wfMsgForContent( 'rightsnone' );
184                                         }
185                                         if( !isset( $params[0] ) || trim( $params[0] ) == '' )
186                                                 $params[0] = $rightsnone;
187                                         if( !isset( $params[1] ) || trim( $params[1] ) == '' )
188                                                 $params[1] = $rightsnone;
189                                 }
190                                 if( count( $params ) == 0 ) {
191                                         if ( $skin ) {
192                                                 $rv = wfMsg( $wgLogActions[$key], $titleLink );
193                                         } else {
194                                                 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
195                                         }
196                                 } else {
197                                         $details = '';
198                                         array_unshift( $params, $titleLink );
199                                         if ( $key == 'block/block' || $key == 'suppress/block' || $key == 'block/reblock' ) {
200                                                 if ( $skin ) {
201                                                         $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . 
202                                                                 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
203                                                 } else {
204                                                         $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
205                                                 }
206                                                 $params[2] = isset( $params[2] ) ? 
207                                                         self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
208                                         } else if ( $type == 'protect' && count($params) == 3 ) {
209                                                 $details .= " {$params[1]}"; // restrictions and expiries
210                                                 if( $params[2] ) {
211                                                         $details .= ' ['.wfMsg('protect-summary-cascade').']';
212                                                 }
213                                         } else if ( $type == 'move' && count( $params ) == 3 ) {
214                                                 if( $params[2] ) {
215                                                         $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
216                                                 }
217                                         }
218                                         $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
219                                 }
220                         }
221                 } else {
222                         global $wgLogActionsHandlers;
223                         if( isset( $wgLogActionsHandlers[$key] ) ) {
224                                 $args = func_get_args();
225                                 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
226                         } else {
227                                 wfDebug( "LogPage::actionText - unknown action $key\n" );
228                                 $rv = "$action";
229                         }
230                 }
231                 if( $filterWikilinks ) {
232                         $rv = str_replace( "[[", "", $rv );
233                         $rv = str_replace( "]]", "", $rv );
234                 }
235                 return $rv;
236         }
237         
238         protected static function getTitleLink( $type, $skin, $title, &$params ) {
239                 global $wgLang, $wgContLang;
240                 if( !$skin ) {
241                         return $title->getPrefixedText();
242                 }
243                 switch( $type ) {
244                         case 'move':
245                                 $titleLink = $skin->makeLinkObj( $title, 
246                                         htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
247                                 $targetTitle = Title::newFromText( $params[0] );
248                                 if ( !$targetTitle ) {
249                                         # Workaround for broken database
250                                         $params[0] = htmlspecialchars( $params[0] );
251                                 } else {
252                                         $params[0] = $skin->makeLinkObj( $targetTitle, htmlspecialchars( $params[0] ) );
253                                 }
254                                 break;
255                         case 'block':
256                                 if( substr( $title->getText(), 0, 1 ) == '#' ) {
257                                         $titleLink = $title->getText();
258                                 } else {
259                                         // TODO: Store the user identifier in the parameters
260                                         // to make this faster for future log entries
261                                         $id = User::idFromName( $title->getText() );
262                                         $titleLink = $skin->userLink( $id, $title->getText() )
263                                                 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
264                                 }
265                                 break;
266                         case 'rights':
267                                 $text = $wgContLang->ucfirst( $title->getText() );
268                                 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
269                                 break;
270                         case 'merge':
271                                 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
272                                 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
273                                 $params[1] = $wgLang->timeanddate( $params[1] );
274                                 break;
275                         default:
276                                 if( $title->getNamespace() == NS_SPECIAL ) {
277                                         list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBKey() );
278                                         # Use the language name for log titles, rather than Log/X
279                                         if( $name == 'Log' ) {
280                                                 $titleLink = '('.$skin->makeLinkObj( $title, LogPage::logName( $par ) ).')';
281                                         } else {
282                                                 $titleLink = $skin->makeLinkObj( $title );
283                                         }
284                                 } else {
285                                         $titleLink = $skin->makeLinkObj( $title );
286                                 }
287                 }
288                 return $titleLink;
289         }
290
291         /**
292          * Add a log entry
293          * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
294          * @param object &$target A title object.
295          * @param string $comment Description associated
296          * @param array $params Parameters passed later to wfMsg.* functions
297          * @param User $doer The user doing the action
298          */
299         function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
300                 if ( !is_array( $params ) ) {
301                         $params = array( $params );
302                 }
303
304                 $this->action = $action;
305                 $this->target = $target;
306                 $this->comment = $comment;
307                 $this->params = LogPage::makeParamBlob( $params );
308                 
309                 if ($doer === null) {
310                         global $wgUser;
311                         $doer = $wgUser;
312                 } elseif (!is_object( $doer ) ) {
313                         $doer = User::newFromId( $doer );
314                 }
315                 
316                 $this->doer = $doer;
317
318                 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
319
320                 return $this->saveContent();
321         }
322
323         /**
324          * Create a blob from a parameter array
325          * @static
326          */
327         static function makeParamBlob( $params ) {
328                 return implode( "\n", $params );
329         }
330
331         /**
332          * Extract a parameter array from a blob
333          * @static
334          */
335         static function extractParams( $blob ) {
336                 if ( $blob === '' ) {
337                         return array();
338                 } else {
339                         return explode( "\n", $blob );
340                 }
341         }
342
343         /**
344          * Convert a comma-delimited list of block log flags
345          * into a more readable (and translated) form
346          *
347          * @param $flags Flags to format
348          * @param $forContent Whether to localize the message depending of the user
349          *                    language
350          * @return string
351          */
352         public static function formatBlockFlags( $flags, $forContent = false ) {
353                 $flags = explode( ',', trim( $flags ) );
354                 if( count( $flags ) > 0 ) {
355                         for( $i = 0; $i < count( $flags ); $i++ )
356                                 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
357                         return '(' . implode( ', ', $flags ) . ')';
358                 } else {
359                         return '';
360                 }
361         }
362
363         /**
364          * Translate a block log flag if possible
365          *
366          * @param $flag Flag to translate
367          * @param $forContent Whether to localize the message depending of the user
368          *                    language
369          * @return string
370          */
371         public static function formatBlockFlag( $flag, $forContent = false ) {
372                 static $messages = array();
373                 if( !isset( $messages[$flag] ) ) {
374                         $k = 'block-log-flags-' . $flag;
375                         if( $forContent )
376                                 $msg = wfMsgForContent( $k );
377                         else
378                                 $msg = wfMsg( $k );
379                         $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
380                 }
381                 return $messages[$flag];
382         }
383 }
384
385 /**
386  * Aliases for backwards compatibility with 1.6
387  */
388 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
389 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
390 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
391 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );