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