(bug 13454) Article::updateCategoryCounts can attempt to execute empty inserts.
[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;
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
76 # log_id doesn't exist on Wikimedia servers yet, and it's a tricky
77 # schema update to do. Hack it for now to ignore the field on MySQL.
78 if ( !is_null( $log_id ) ) {
79 $data['log_id'] = $log_id;
80 }
81 $dbw->insert( 'logging', $data, $fname );
82
83 # And update recentchanges
84 if ( $this->updateRecentChanges ) {
85 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
86 $rcComment = $this->getRcComment();
87 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
88 $this->type, $this->action, $this->target, $this->comment, $this->params );
89 }
90 return true;
91 }
92
93 public function getRcComment() {
94 $rcComment = $this->actionText;
95 if( '' != $this->comment ) {
96 if ($rcComment == '')
97 $rcComment = $this->comment;
98 else
99 $rcComment .= ': ' . $this->comment;
100 }
101 return $rcComment;
102 }
103
104 /**
105 * @static
106 */
107 public static function validTypes() {
108 global $wgLogTypes;
109 return $wgLogTypes;
110 }
111
112 /**
113 * @static
114 */
115 public static function isLogType( $type ) {
116 return in_array( $type, LogPage::validTypes() );
117 }
118
119 /**
120 * @static
121 */
122 public static function logName( $type ) {
123 global $wgLogNames, $wgMessageCache;
124
125 if( isset( $wgLogNames[$type] ) ) {
126 $wgMessageCache->loadAllMessages();
127 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
128 } else {
129 // Bogus log types? Perhaps an extension was removed.
130 return $type;
131 }
132 }
133
134 /**
135 * @todo handle missing log types
136 * @static
137 */
138 static function logHeader( $type ) {
139 global $wgLogHeaders;
140 return wfMsg( $wgLogHeaders[$type] );
141 }
142
143 /**
144 * @static
145 */
146 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
147 global $wgLang, $wgContLang, $wgLogActions;
148
149 $key = "$type/$action";
150
151 if( $key == 'patrol/patrol' )
152 return PatrolLog::makeActionText( $title, $params, $skin );
153
154 if( isset( $wgLogActions[$key] ) ) {
155 if( is_null( $title ) ) {
156 $rv=wfMsg( $wgLogActions[$key] );
157 } else {
158 if( $skin ) {
159
160 switch( $type ) {
161 case 'move':
162 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
163 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
164 break;
165 case 'block':
166 if( substr( $title->getText(), 0, 1 ) == '#' ) {
167 $titleLink = $title->getText();
168 } else {
169 // TODO: Store the user identifier in the parameters
170 // to make this faster for future log entries
171 $id = User::idFromName( $title->getText() );
172 $titleLink = $skin->userLink( $id, $title->getText() )
173 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
174 }
175 break;
176 case 'rights':
177 $text = $wgContLang->ucfirst( $title->getText() );
178 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
179 break;
180 case 'merge':
181 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
182 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
183 $params[1] = $wgLang->timeanddate( $params[1] );
184 break;
185 default:
186 $titleLink = $skin->makeLinkObj( $title );
187 }
188
189 } else {
190 $titleLink = $title->getPrefixedText();
191 }
192 if( $key == 'rights/rights' ) {
193 if ($skin) {
194 $rightsnone = wfMsg( 'rightsnone' );
195 } else {
196 $rightsnone = wfMsgForContent( 'rightsnone' );
197 }
198 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
199 $params[0] = $rightsnone;
200 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
201 $params[1] = $rightsnone;
202 }
203 if( count( $params ) == 0 ) {
204 if ( $skin ) {
205 $rv = wfMsg( $wgLogActions[$key], $titleLink );
206 } else {
207 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
208 }
209 } else {
210 array_unshift( $params, $titleLink );
211 if ( $key == 'block/block' ) {
212 if ( $skin ) {
213 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
214 } else {
215 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
216 }
217 $params[2] = isset( $params[2] )
218 ? self::formatBlockFlags( $params[2], is_null( $skin ) )
219 : '';
220 }
221 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
222 }
223 }
224 } else {
225 wfDebug( "LogPage::actionText - unknown action $key\n" );
226 $rv = "$action";
227 }
228 if( $filterWikilinks ) {
229 $rv = str_replace( "[[", "", $rv );
230 $rv = str_replace( "]]", "", $rv );
231 }
232 return $rv;
233 }
234
235 /**
236 * Add a log entry
237 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
238 * @param object &$target A title object.
239 * @param string $comment Description associated
240 * @param array $params Parameters passed later to wfMsg.* functions
241 */
242 function addEntry( $action, $target, $comment, $params = array() ) {
243 if ( !is_array( $params ) ) {
244 $params = array( $params );
245 }
246
247 $this->action = $action;
248 $this->target = $target;
249 $this->comment = $comment;
250 $this->params = LogPage::makeParamBlob( $params );
251
252 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
253
254 return $this->saveContent();
255 }
256
257 /**
258 * Create a blob from a parameter array
259 * @static
260 */
261 static function makeParamBlob( $params ) {
262 return implode( "\n", $params );
263 }
264
265 /**
266 * Extract a parameter array from a blob
267 * @static
268 */
269 static function extractParams( $blob ) {
270 if ( $blob === '' ) {
271 return array();
272 } else {
273 return explode( "\n", $blob );
274 }
275 }
276
277 /**
278 * Convert a comma-delimited list of block log flags
279 * into a more readable (and translated) form
280 *
281 * @param $flags Flags to format
282 * @param $forContent Whether to localize the message depending of the user
283 * language
284 * @return string
285 */
286 public static function formatBlockFlags( $flags, $forContent = false ) {
287 $flags = explode( ',', trim( $flags ) );
288 if( count( $flags ) > 0 ) {
289 for( $i = 0; $i < count( $flags ); $i++ )
290 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
291 return '(' . implode( ', ', $flags ) . ')';
292 } else {
293 return '';
294 }
295 }
296
297 /**
298 * Translate a block log flag if possible
299 *
300 * @param $flag Flag to translate
301 * @param $forContent Whether to localize the message depending of the user
302 * language
303 * @return string
304 */
305 public static function formatBlockFlag( $flag, $forContent = false ) {
306 static $messages = array();
307 if( !isset( $messages[$flag] ) ) {
308 $k = 'block-log-flags-' . $flag;
309 if( $forContent )
310 $msg = wfMsgForContent( $k );
311 else
312 $msg = wfMsg( $k );
313 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
314 }
315 return $messages[$flag];
316 }
317
318 /**
319 * Determine if the current user is allowed to view a particular
320 * field of this log row, if it's marked as deleted.
321 * @param Row $row
322 * @param int $field
323 * @return bool
324 */
325 public static function userCan( $row, $field ) {
326 if( ( $row->log_deleted & $field ) == $field ) {
327 global $wgUser;
328 $permission = ( $row->log_deleted & self::DELETED_RESTRICTED ) == self::DELETED_RESTRICTED
329 ? 'hiderevision'
330 : 'deleterevision';
331 wfDebug( "Checking for $permission due to $field match on $event->log_deleted\n" );
332 return $wgUser->isAllowed( $permission );
333 } else {
334 return true;
335 }
336 }
337
338 /**
339 * int $field one of DELETED_* bitfield constants
340 * @param Row $row
341 * @return bool
342 */
343 public static function isDeleted( $row, $field ) {
344 return ($row->log_deleted & $field) == $field;
345 }
346 }
347
348 /**
349 * Aliases for backwards compatibility with 1.6
350 */
351 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
352 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
353 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
354 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );