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