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