(bug 5387) Block log items on RecentChanges don't make use of possible translations:
[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;
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
72 # log_id doesn't exist on Wikimedia servers yet, and it's a tricky
73 # schema update to do. Hack it for now to ignore the field on MySQL.
74 if ( !is_null( $log_id ) ) {
75 $data['log_id'] = $log_id;
76 }
77 $dbw->insert( 'logging', $data, $fname );
78
79 # And update recentchanges
80 if ( $this->updateRecentChanges ) {
81 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
82 $rcComment = $this->getRcComment();
83 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
84 $this->type, $this->action, $this->target, $this->comment, $this->params );
85 }
86 return true;
87 }
88
89 public function getRcComment() {
90 $rcComment = $this->actionText;
91 if( '' != $this->comment ) {
92 if ($rcComment == '')
93 $rcComment = $this->comment;
94 else
95 $rcComment .= ': ' . $this->comment;
96 }
97 return $rcComment;
98 }
99
100 /**
101 * @static
102 */
103 public static function validTypes() {
104 global $wgLogTypes;
105 return $wgLogTypes;
106 }
107
108 /**
109 * @static
110 */
111 public static function isLogType( $type ) {
112 return in_array( $type, LogPage::validTypes() );
113 }
114
115 /**
116 * @static
117 */
118 public static function logName( $type ) {
119 global $wgLogNames;
120
121 if( isset( $wgLogNames[$type] ) ) {
122 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
123 } else {
124 // Bogus log types? Perhaps an extension was removed.
125 return $type;
126 }
127 }
128
129 /**
130 * @todo handle missing log types
131 * @static
132 */
133 static function logHeader( $type ) {
134 global $wgLogHeaders;
135 return wfMsg( $wgLogHeaders[$type] );
136 }
137
138 /**
139 * @static
140 */
141 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
142 global $wgLang, $wgContLang, $wgLogActions;
143
144 $key = "$type/$action";
145
146 if( $key == 'patrol/patrol' )
147 return PatrolLog::makeActionText( $title, $params, $skin );
148
149 if( isset( $wgLogActions[$key] ) ) {
150 if( is_null( $title ) ) {
151 $rv=wfMsg( $wgLogActions[$key] );
152 } else {
153 if( $skin ) {
154
155 switch( $type ) {
156 case 'move':
157 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
158 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
159 break;
160 case 'block':
161 if( substr( $title->getText(), 0, 1 ) == '#' ) {
162 $titleLink = $title->getText();
163 } else {
164 // TODO: Store the user identifier in the parameters
165 // to make this faster for future log entries
166 $id = User::idFromName( $title->getText() );
167 $titleLink = $skin->userLink( $id, $title->getText() )
168 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
169 }
170 break;
171 case 'rights':
172 $text = $wgContLang->ucfirst( $title->getText() );
173 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
174 break;
175 default:
176 $titleLink = $skin->makeLinkObj( $title );
177 }
178
179 } else {
180 $titleLink = $title->getPrefixedText();
181 }
182 if( $key == 'rights/rights' ) {
183 if ($skin) {
184 $rightsnone = wfMsg( 'rightsnone' );
185 } else {
186 $rightsnone = wfMsgForContent( 'rightsnone' );
187 }
188 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
189 $params[0] = $rightsnone;
190 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
191 $params[1] = $rightsnone;
192 }
193 if( count( $params ) == 0 ) {
194 if ( $skin ) {
195 $rv = wfMsg( $wgLogActions[$key], $titleLink );
196 } else {
197 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
198 }
199 } else {
200 array_unshift( $params, $titleLink );
201 if ( $key == 'block/block' ) {
202 if ( $skin ) {
203 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
204 } else {
205 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
206 }
207 $params[2] = isset( $params[2] )
208 ? self::formatBlockFlags( $params[2] )
209 : '';
210 }
211 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
212 }
213 }
214 } else {
215 wfDebug( "LogPage::actionText - unknown action $key\n" );
216 $rv = "$action";
217 }
218 if( $filterWikilinks ) {
219 $rv = str_replace( "[[", "", $rv );
220 $rv = str_replace( "]]", "", $rv );
221 }
222 return $rv;
223 }
224
225 /**
226 * Add a log entry
227 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
228 * @param object &$target A title object.
229 * @param string $comment Description associated
230 * @param array $params Parameters passed later to wfMsg.* functions
231 */
232 function addEntry( $action, $target, $comment, $params = array() ) {
233 if ( !is_array( $params ) ) {
234 $params = array( $params );
235 }
236
237 $this->action = $action;
238 $this->target = $target;
239 $this->comment = $comment;
240 $this->params = LogPage::makeParamBlob( $params );
241
242 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
243
244 return $this->saveContent();
245 }
246
247 /**
248 * Create a blob from a parameter array
249 * @static
250 */
251 static function makeParamBlob( $params ) {
252 return implode( "\n", $params );
253 }
254
255 /**
256 * Extract a parameter array from a blob
257 * @static
258 */
259 static function extractParams( $blob ) {
260 if ( $blob === '' ) {
261 return array();
262 } else {
263 return explode( "\n", $blob );
264 }
265 }
266
267 /**
268 * Convert a comma-delimited list of block log flags
269 * into a more readable (and translated) form
270 *
271 * @param $flags Flags to format
272 * @return string
273 */
274 public static function formatBlockFlags( $flags ) {
275 $flags = explode( ',', trim( $flags ) );
276 if( count( $flags ) > 0 ) {
277 for( $i = 0; $i < count( $flags ); $i++ )
278 $flags[$i] = self::formatBlockFlag( $flags[$i] );
279 return '(' . implode( ', ', $flags ) . ')';
280 } else {
281 return '';
282 }
283 }
284
285 /**
286 * Translate a block log flag if possible
287 *
288 * @param $flag Flag to translate
289 * @return string
290 */
291 public static function formatBlockFlag( $flag ) {
292 static $messages = array();
293 if( !isset( $messages[$flag] ) ) {
294 $k = 'block-log-flags-' . $flag;
295 $msg = wfMsg( $k );
296 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
297 }
298 return $messages[$flag];
299 }
300
301 }
302
303