Load messages in a less shitty and more consistent manner for extensions. Previously...
[lhc/web/wiklou.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 public function getRcComment() {
93 $rcComment = $this->actionText;
94 if( '' != $this->comment ) {
95 if ($rcComment == '')
96 $rcComment = $this->comment;
97 else
98 $rcComment .= ': ' . $this->comment;
99 }
100 return $rcComment;
101 }
102
103 /**
104 * @static
105 */
106 public static function validTypes() {
107 global $wgLogTypes;
108 return $wgLogTypes;
109 }
110
111 /**
112 * @static
113 */
114 public static function isLogType( $type ) {
115 return in_array( $type, LogPage::validTypes() );
116 }
117
118 /**
119 * @static
120 */
121 public static function logName( $type ) {
122 global $wgLogNames, $wgMessageCache;
123
124 if( isset( $wgLogNames[$type] ) ) {
125 $wgMessageCache->loadAllMessages();
126 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
127 } else {
128 // Bogus log types? Perhaps an extension was removed.
129 return $type;
130 }
131 }
132
133 /**
134 * @todo handle missing log types
135 * @param string $type logtype
136 * @return string Headertext of this logtype
137 */
138 static function logHeader( $type ) {
139 global $wgLogHeaders, $wgMessageCache;
140 $wgMessageCache->loadAllMessages();
141 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
142 }
143
144 /**
145 * @static
146 * @return HTML string
147 */
148 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
149 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
150
151 $wgMessageCache->loadAllMessages();
152 $key = "$type/$action";
153
154 if( $key == 'patrol/patrol' )
155 return PatrolLog::makeActionText( $title, $params, $skin );
156
157 if( isset( $wgLogActions[$key] ) ) {
158 if( is_null( $title ) ) {
159 $rv=wfMsg( $wgLogActions[$key] );
160 } else {
161 if( $skin ) {
162
163 switch( $type ) {
164 case 'move':
165 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
166 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
167 break;
168 case 'block':
169 if( substr( $title->getText(), 0, 1 ) == '#' ) {
170 $titleLink = $title->getText();
171 } else {
172 // TODO: Store the user identifier in the parameters
173 // to make this faster for future log entries
174 $id = User::idFromName( $title->getText() );
175 $titleLink = $skin->userLink( $id, $title->getText() )
176 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
177 }
178 break;
179 case 'rights':
180 $text = $wgContLang->ucfirst( $title->getText() );
181 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
182 break;
183 case 'merge':
184 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
185 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
186 $params[1] = $wgLang->timeanddate( $params[1] );
187 break;
188 default:
189 $titleLink = $skin->makeLinkObj( $title );
190 }
191
192 } else {
193 $titleLink = $title->getPrefixedText();
194 }
195 if( $key == 'rights/rights' ) {
196 if ($skin) {
197 $rightsnone = wfMsg( 'rightsnone' );
198 foreach ( $params as &$param ) {
199 $groupArray = array_map( 'trim', explode( ',', $param ) );
200 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
201 $param = $wgLang->listToText( $groupArray );
202 }
203 } else {
204 $rightsnone = wfMsgForContent( 'rightsnone' );
205 }
206 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
207 $params[0] = $rightsnone;
208 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
209 $params[1] = $rightsnone;
210 }
211 if( count( $params ) == 0 ) {
212 if ( $skin ) {
213 $rv = wfMsg( $wgLogActions[$key], $titleLink );
214 } else {
215 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
216 }
217 } else {
218 array_unshift( $params, $titleLink );
219 if ( $key == 'block/block' || $key == 'suppress/block' ) {
220 if ( $skin ) {
221 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
222 } else {
223 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
224 }
225 $params[2] = isset( $params[2] )
226 ? self::formatBlockFlags( $params[2], is_null( $skin ) )
227 : '';
228 }
229 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
230 }
231 }
232 } else {
233 global $wgLogActionsHandlers;
234 if( isset( $wgLogActionsHandlers[$key] ) ) {
235 $args = func_get_args();
236 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
237 } else {
238 wfDebug( "LogPage::actionText - unknown action $key\n" );
239 $rv = "$action";
240 }
241 }
242 if( $filterWikilinks ) {
243 $rv = str_replace( "[[", "", $rv );
244 $rv = str_replace( "]]", "", $rv );
245 }
246 return $rv;
247 }
248
249 /**
250 * Add a log entry
251 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
252 * @param object &$target A title object.
253 * @param string $comment Description associated
254 * @param array $params Parameters passed later to wfMsg.* functions
255 * @param User $doer The user doing the action
256 */
257 function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
258 if ( !is_array( $params ) ) {
259 $params = array( $params );
260 }
261
262 $this->action = $action;
263 $this->target = $target;
264 $this->comment = $comment;
265 $this->params = LogPage::makeParamBlob( $params );
266
267 if ($doer === null) {
268 global $wgUser;
269 $doer = $wgUser;
270 } elseif (!is_object( $doer ) ) {
271 $doer = User::newFromId( $doer );
272 }
273
274 $this->doer = $doer;
275
276 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
277
278 return $this->saveContent();
279 }
280
281 /**
282 * Create a blob from a parameter array
283 * @static
284 */
285 static function makeParamBlob( $params ) {
286 return implode( "\n", $params );
287 }
288
289 /**
290 * Extract a parameter array from a blob
291 * @static
292 */
293 static function extractParams( $blob ) {
294 if ( $blob === '' ) {
295 return array();
296 } else {
297 return explode( "\n", $blob );
298 }
299 }
300
301 /**
302 * Convert a comma-delimited list of block log flags
303 * into a more readable (and translated) form
304 *
305 * @param $flags Flags to format
306 * @param $forContent Whether to localize the message depending of the user
307 * language
308 * @return string
309 */
310 public static function formatBlockFlags( $flags, $forContent = false ) {
311 $flags = explode( ',', trim( $flags ) );
312 if( count( $flags ) > 0 ) {
313 for( $i = 0; $i < count( $flags ); $i++ )
314 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
315 return '(' . implode( ', ', $flags ) . ')';
316 } else {
317 return '';
318 }
319 }
320
321 /**
322 * Translate a block log flag if possible
323 *
324 * @param $flag Flag to translate
325 * @param $forContent Whether to localize the message depending of the user
326 * language
327 * @return string
328 */
329 public static function formatBlockFlag( $flag, $forContent = false ) {
330 static $messages = array();
331 if( !isset( $messages[$flag] ) ) {
332 $k = 'block-log-flags-' . $flag;
333 if( $forContent )
334 $msg = wfMsgForContent( $k );
335 else
336 $msg = wfMsg( $k );
337 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
338 }
339 return $messages[$flag];
340 }
341 }
342
343 /**
344 * Aliases for backwards compatibility with 1.6
345 */
346 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
347 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
348 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
349 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );