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