Be sure that the text is escaped
[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 *
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;
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 function saveContent() {
55 if( wfReadOnly() ) return false;
56
57 global $wgUser, $wgLogRestrictions;
58 $fname = 'LogPage::saveContent';
59
60 $dbw = wfGetDB( DB_MASTER );
61 $uid = $wgUser->getID();
62 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
63
64 $this->timestamp = $now = wfTimestampNow();
65 $data = array(
66 'log_type' => $this->type,
67 'log_action' => $this->action,
68 'log_timestamp' => $dbw->timestamp( $now ),
69 'log_user' => $uid,
70 'log_namespace' => $this->target->getNamespace(),
71 'log_title' => $this->target->getDBkey(),
72 'log_comment' => $this->comment,
73 'log_params' => $this->params
74 );
75 $dbw->insert( 'logging', $data, $fname );
76 $newId = $dbw->insertId();
77
78 # And update recentchanges
79 if( $this->updateRecentChanges ) {
80 # Don't add private logs to RC!
81 if( !isset($wgLogRestrictions[$this->type]) || $wgLogRestrictions[$this->type]=='*' ) {
82 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
83 $rcComment = $this->getRcComment();
84 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
85 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
86 }
87 }
88 return true;
89 }
90
91 public function getRcComment() {
92 $rcComment = $this->actionText;
93 if( '' != $this->comment ) {
94 if ($rcComment == '')
95 $rcComment = $this->comment;
96 else
97 $rcComment .= ': ' . $this->comment;
98 }
99 return $rcComment;
100 }
101
102 /**
103 * @static
104 */
105 public static function validTypes() {
106 global $wgLogTypes;
107 return $wgLogTypes;
108 }
109
110 /**
111 * @static
112 */
113 public static function isLogType( $type ) {
114 return in_array( $type, LogPage::validTypes() );
115 }
116
117 /**
118 * @static
119 */
120 public static function logName( $type ) {
121 global $wgLogNames, $wgMessageCache;
122
123 if( isset( $wgLogNames[$type] ) ) {
124 $wgMessageCache->loadAllMessages();
125 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
126 } else {
127 // Bogus log types? Perhaps an extension was removed.
128 return $type;
129 }
130 }
131
132 /**
133 * @todo handle missing log types
134 * @param string $type logtype
135 * @return string Headertext of this logtype
136 */
137 static function logHeader( $type ) {
138 global $wgLogHeaders;
139 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
140 }
141
142 /**
143 * @static
144 */
145 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
146 global $wgLang, $wgContLang, $wgLogActions;
147
148 $key = "$type/$action";
149
150 if( $key == 'patrol/patrol' )
151 return PatrolLog::makeActionText( $title, $params, $skin );
152
153 if( isset( $wgLogActions[$key] ) ) {
154 if( is_null( $title ) ) {
155 $rv=wfMsg( $wgLogActions[$key] );
156 } else {
157 if( $skin ) {
158
159 switch( $type ) {
160 case 'move':
161 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
162 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
163 break;
164 case 'block':
165 if( substr( $title->getText(), 0, 1 ) == '#' ) {
166 $titleLink = $title->getText();
167 } else {
168 // TODO: Store the user identifier in the parameters
169 // to make this faster for future log entries
170 $id = User::idFromName( $title->getText() );
171 $titleLink = $skin->userLink( $id, $title->getText() )
172 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
173 }
174 break;
175 case 'rights':
176 $text = $wgContLang->ucfirst( $title->getText() );
177 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
178 break;
179 case 'merge':
180 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
181 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
182 $params[1] = $wgLang->timeanddate( $params[1] );
183 break;
184 default:
185 $titleLink = $skin->makeLinkObj( $title );
186 }
187
188 } else {
189 $titleLink = $title->getPrefixedText();
190 }
191 if( $key == 'rights/rights' ) {
192 if ($skin) {
193 $rightsnone = wfMsg( 'rightsnone' );
194 } else {
195 $rightsnone = wfMsgForContent( 'rightsnone' );
196 }
197 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
198 $params[0] = $rightsnone;
199 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
200 $params[1] = $rightsnone;
201 }
202 if( count( $params ) == 0 ) {
203 if ( $skin ) {
204 $rv = wfMsg( $wgLogActions[$key], $titleLink );
205 } else {
206 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
207 }
208 } else {
209 array_unshift( $params, $titleLink );
210 if ( $key == 'block/block' || $key == 'suppress/block' ) {
211 if ( $skin ) {
212 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
213 } else {
214 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
215 }
216 $params[2] = isset( $params[2] )
217 ? self::formatBlockFlags( $params[2], is_null( $skin ) )
218 : '';
219 }
220 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
221 }
222 }
223 } else {
224 wfDebug( "LogPage::actionText - unknown action $key\n" );
225 $rv = "$action";
226 }
227 if( $filterWikilinks ) {
228 $rv = str_replace( "[[", "", $rv );
229 $rv = str_replace( "]]", "", $rv );
230 }
231 return $rv;
232 }
233
234 /**
235 * Add a log entry
236 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
237 * @param object &$target A title object.
238 * @param string $comment Description associated
239 * @param array $params Parameters passed later to wfMsg.* functions
240 */
241 function addEntry( $action, $target, $comment, $params = array() ) {
242 if ( !is_array( $params ) ) {
243 $params = array( $params );
244 }
245
246 $this->action = $action;
247 $this->target = $target;
248 $this->comment = $comment;
249 $this->params = LogPage::makeParamBlob( $params );
250
251 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
252
253 return $this->saveContent();
254 }
255
256 /**
257 * Create a blob from a parameter array
258 * @static
259 */
260 static function makeParamBlob( $params ) {
261 return implode( "\n", $params );
262 }
263
264 /**
265 * Extract a parameter array from a blob
266 * @static
267 */
268 static function extractParams( $blob ) {
269 if ( $blob === '' ) {
270 return array();
271 } else {
272 return explode( "\n", $blob );
273 }
274 }
275
276 /**
277 * Convert a comma-delimited list of block log flags
278 * into a more readable (and translated) form
279 *
280 * @param $flags Flags to format
281 * @param $forContent Whether to localize the message depending of the user
282 * language
283 * @return string
284 */
285 public static function formatBlockFlags( $flags, $forContent = false ) {
286 $flags = explode( ',', trim( $flags ) );
287 if( count( $flags ) > 0 ) {
288 for( $i = 0; $i < count( $flags ); $i++ )
289 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
290 return '(' . implode( ', ', $flags ) . ')';
291 } else {
292 return '';
293 }
294 }
295
296 /**
297 * Translate a block log flag if possible
298 *
299 * @param $flag Flag to translate
300 * @param $forContent Whether to localize the message depending of the user
301 * language
302 * @return string
303 */
304 public static function formatBlockFlag( $flag, $forContent = false ) {
305 static $messages = array();
306 if( !isset( $messages[$flag] ) ) {
307 $k = 'block-log-flags-' . $flag;
308 if( $forContent )
309 $msg = wfMsgForContent( $k );
310 else
311 $msg = wfMsg( $k );
312 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
313 }
314 return $messages[$flag];
315 }
316 }
317
318 /**
319 * Aliases for backwards compatibility with 1.6
320 */
321 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
322 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
323 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
324 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );