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