Minor log-related changes for documentation and to support a potential extension
[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 /**
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, $params = array(), $filterWikilinks=false ) {
159 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
160
161 $wgMessageCache->loadAllMessages();
162 $key = "$type/$action";
163
164 if( $key == 'patrol/patrol' )
165 return PatrolLog::makeActionText( $title, $params, $skin );
166
167 if( isset( $wgLogActions[$key] ) ) {
168 if( is_null( $title ) ) {
169 $rv=wfMsg( $wgLogActions[$key] );
170 } else {
171 if( $skin ) {
172
173 switch( $type ) {
174 case 'move':
175 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
176 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
177 break;
178 case 'block':
179 if( substr( $title->getText(), 0, 1 ) == '#' ) {
180 $titleLink = $title->getText();
181 } else {
182 // TODO: Store the user identifier in the parameters
183 // to make this faster for future log entries
184 $id = User::idFromName( $title->getText() );
185 $titleLink = $skin->userLink( $id, $title->getText() )
186 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
187 }
188 break;
189 case 'rights':
190 $text = $wgContLang->ucfirst( $title->getText() );
191 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
192 break;
193 case 'merge':
194 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
195 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
196 $params[1] = $wgLang->timeanddate( $params[1] );
197 break;
198 default:
199 $titleLink = $skin->makeLinkObj( $title );
200 }
201
202 } else {
203 $titleLink = $title->getPrefixedText();
204 }
205 if( $key == 'rights/rights' ) {
206 if ($skin) {
207 $rightsnone = wfMsg( 'rightsnone' );
208 foreach ( $params as &$param ) {
209 $groupArray = array_map( 'trim', explode( ',', $param ) );
210 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
211 $param = $wgLang->listToText( $groupArray );
212 }
213 } else {
214 $rightsnone = wfMsgForContent( 'rightsnone' );
215 }
216 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
217 $params[0] = $rightsnone;
218 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
219 $params[1] = $rightsnone;
220 }
221 if( count( $params ) == 0 ) {
222 if ( $skin ) {
223 $rv = wfMsg( $wgLogActions[$key], $titleLink );
224 } else {
225 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
226 }
227 } else {
228 array_unshift( $params, $titleLink );
229 if ( $key == 'block/block' || $key == 'suppress/block' ) {
230 if ( $skin ) {
231 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
232 } else {
233 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
234 }
235 $params[2] = isset( $params[2] )
236 ? self::formatBlockFlags( $params[2], is_null( $skin ) )
237 : '';
238 }
239 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
240 }
241 }
242 } else {
243 global $wgLogActionsHandlers;
244 if( isset( $wgLogActionsHandlers[$key] ) ) {
245 $args = func_get_args();
246 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
247 } else {
248 wfDebug( "LogPage::actionText - unknown action $key\n" );
249 $rv = "$action";
250 }
251 }
252 if( $filterWikilinks ) {
253 $rv = str_replace( "[[", "", $rv );
254 $rv = str_replace( "]]", "", $rv );
255 }
256 return $rv;
257 }
258
259 /**
260 * Add a log entry
261 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
262 * @param object &$target A title object.
263 * @param string $comment Description associated
264 * @param array $params Parameters passed later to wfMsg.* functions
265 * @param User $doer The user doing the action
266 */
267 function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
268 if ( !is_array( $params ) ) {
269 $params = array( $params );
270 }
271
272 $this->action = $action;
273 $this->target = $target;
274 $this->comment = $comment;
275 $this->params = LogPage::makeParamBlob( $params );
276
277 if ($doer === null) {
278 global $wgUser;
279 $doer = $wgUser;
280 } elseif (!is_object( $doer ) ) {
281 $doer = User::newFromId( $doer );
282 }
283
284 $this->doer = $doer;
285
286 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
287
288 return $this->saveContent();
289 }
290
291 /**
292 * Create a blob from a parameter array
293 * @static
294 */
295 static function makeParamBlob( $params ) {
296 return implode( "\n", $params );
297 }
298
299 /**
300 * Extract a parameter array from a blob
301 * @static
302 */
303 static function extractParams( $blob ) {
304 if ( $blob === '' ) {
305 return array();
306 } else {
307 return explode( "\n", $blob );
308 }
309 }
310
311 /**
312 * Convert a comma-delimited list of block log flags
313 * into a more readable (and translated) form
314 *
315 * @param $flags Flags to format
316 * @param $forContent Whether to localize the message depending of the user
317 * language
318 * @return string
319 */
320 public static function formatBlockFlags( $flags, $forContent = false ) {
321 $flags = explode( ',', trim( $flags ) );
322 if( count( $flags ) > 0 ) {
323 for( $i = 0; $i < count( $flags ); $i++ )
324 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
325 return '(' . implode( ', ', $flags ) . ')';
326 } else {
327 return '';
328 }
329 }
330
331 /**
332 * Translate a block log flag if possible
333 *
334 * @param $flag Flag to translate
335 * @param $forContent Whether to localize the message depending of the user
336 * language
337 * @return string
338 */
339 public static function formatBlockFlag( $flag, $forContent = false ) {
340 static $messages = array();
341 if( !isset( $messages[$flag] ) ) {
342 $k = 'block-log-flags-' . $flag;
343 if( $forContent )
344 $msg = wfMsgForContent( $k );
345 else
346 $msg = wfMsg( $k );
347 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
348 }
349 return $messages[$flag];
350 }
351 }
352
353 /**
354 * Aliases for backwards compatibility with 1.6
355 */
356 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
357 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
358 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
359 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );