Move isDeleted and userCan functions
[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 * @static
135 */
136 static function logHeader( $type ) {
137 global $wgLogHeaders;
138 return wfMsgHtml( $wgLogHeaders[$type] );
139 }
140
141 /**
142 * @static
143 */
144 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
145 global $wgLang, $wgContLang, $wgLogActions;
146
147 $key = "$type/$action";
148
149 if( $key == 'patrol/patrol' )
150 return PatrolLog::makeActionText( $title, $params, $skin );
151
152 if( isset( $wgLogActions[$key] ) ) {
153 if( is_null( $title ) ) {
154 $rv=wfMsg( $wgLogActions[$key] );
155 } else {
156 if( $skin ) {
157
158 switch( $type ) {
159 case 'move':
160 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
161 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
162 break;
163 case 'block':
164 if( substr( $title->getText(), 0, 1 ) == '#' ) {
165 $titleLink = $title->getText();
166 } else {
167 // TODO: Store the user identifier in the parameters
168 // to make this faster for future log entries
169 $id = User::idFromName( $title->getText() );
170 $titleLink = $skin->userLink( $id, $title->getText() )
171 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
172 }
173 break;
174 case 'rights':
175 $text = $wgContLang->ucfirst( $title->getText() );
176 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
177 break;
178 case 'merge':
179 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
180 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
181 $params[1] = $wgLang->timeanddate( $params[1] );
182 break;
183 default:
184 $titleLink = $skin->makeLinkObj( $title );
185 }
186
187 } else {
188 $titleLink = $title->getPrefixedText();
189 }
190 if( $key == 'rights/rights' ) {
191 if ($skin) {
192 $rightsnone = wfMsg( 'rightsnone' );
193 } else {
194 $rightsnone = wfMsgForContent( 'rightsnone' );
195 }
196 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
197 $params[0] = $rightsnone;
198 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
199 $params[1] = $rightsnone;
200 }
201 if( count( $params ) == 0 ) {
202 if ( $skin ) {
203 $rv = wfMsg( $wgLogActions[$key], $titleLink );
204 } else {
205 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
206 }
207 } else {
208 array_unshift( $params, $titleLink );
209 if ( $key == 'block/block' || $key == 'suppress/block' ) {
210 if ( $skin ) {
211 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
212 } else {
213 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
214 }
215 $params[2] = isset( $params[2] )
216 ? self::formatBlockFlags( $params[2], is_null( $skin ) )
217 : '';
218 }
219 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
220 }
221 }
222 } else {
223 wfDebug( "LogPage::actionText - unknown action $key\n" );
224 $rv = "$action";
225 }
226 if( $filterWikilinks ) {
227 $rv = str_replace( "[[", "", $rv );
228 $rv = str_replace( "]]", "", $rv );
229 }
230 return $rv;
231 }
232
233 /**
234 * Add a log entry
235 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
236 * @param object &$target A title object.
237 * @param string $comment Description associated
238 * @param array $params Parameters passed later to wfMsg.* functions
239 */
240 function addEntry( $action, $target, $comment, $params = array() ) {
241 if ( !is_array( $params ) ) {
242 $params = array( $params );
243 }
244
245 $this->action = $action;
246 $this->target = $target;
247 $this->comment = $comment;
248 $this->params = LogPage::makeParamBlob( $params );
249
250 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
251
252 return $this->saveContent();
253 }
254
255 /**
256 * Create a blob from a parameter array
257 * @static
258 */
259 static function makeParamBlob( $params ) {
260 return implode( "\n", $params );
261 }
262
263 /**
264 * Extract a parameter array from a blob
265 * @static
266 */
267 static function extractParams( $blob ) {
268 if ( $blob === '' ) {
269 return array();
270 } else {
271 return explode( "\n", $blob );
272 }
273 }
274
275 /**
276 * Convert a comma-delimited list of block log flags
277 * into a more readable (and translated) form
278 *
279 * @param $flags Flags to format
280 * @param $forContent Whether to localize the message depending of the user
281 * language
282 * @return string
283 */
284 public static function formatBlockFlags( $flags, $forContent = false ) {
285 $flags = explode( ',', trim( $flags ) );
286 if( count( $flags ) > 0 ) {
287 for( $i = 0; $i < count( $flags ); $i++ )
288 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
289 return '(' . implode( ', ', $flags ) . ')';
290 } else {
291 return '';
292 }
293 }
294
295 /**
296 * Translate a block log flag if possible
297 *
298 * @param $flag Flag to translate
299 * @param $forContent Whether to localize the message depending of the user
300 * language
301 * @return string
302 */
303 public static function formatBlockFlag( $flag, $forContent = false ) {
304 static $messages = array();
305 if( !isset( $messages[$flag] ) ) {
306 $k = 'block-log-flags-' . $flag;
307 if( $forContent )
308 $msg = wfMsgForContent( $k );
309 else
310 $msg = wfMsg( $k );
311 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
312 }
313 return $messages[$flag];
314 }
315 }
316
317 /**
318 * Aliases for backwards compatibility with 1.6
319 */
320 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
321 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
322 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
323 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );