Revert r20769: we don't use PHPDocumentor anymore, we use doxygen.
[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 ( $key == 'block/block' ) {
189 if ( $translate ) {
190 $params[1] = $wgLang->translateBlockExpiry( $params[1] );
191 }
192 $params[2] = isset( $params[2] )
193 ? self::formatBlockFlags( $params[2] )
194 : '';
195 }
196 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
197 }
198 }
199 } else {
200 wfDebug( "LogPage::actionText - unknown action $key\n" );
201 $rv = "$action";
202 }
203 if( $filterWikilinks ) {
204 $rv = str_replace( "[[", "", $rv );
205 $rv = str_replace( "]]", "", $rv );
206 }
207 return $rv;
208 }
209
210 /**
211 * Add a log entry
212 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
213 * @param object &$target A title object.
214 * @param string $comment Description associated
215 * @param array $params Parameters passed later to wfMsg.* functions
216 */
217 function addEntry( $action, $target, $comment, $params = array() ) {
218 if ( !is_array( $params ) ) {
219 $params = array( $params );
220 }
221
222 $this->action = $action;
223 $this->target = $target;
224 $this->comment = $comment;
225 $this->params = LogPage::makeParamBlob( $params );
226
227 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
228
229 return $this->saveContent();
230 }
231
232 /**
233 * Create a blob from a parameter array
234 * @static
235 */
236 function makeParamBlob( $params ) {
237 return implode( "\n", $params );
238 }
239
240 /**
241 * Extract a parameter array from a blob
242 * @static
243 */
244 function extractParams( $blob ) {
245 if ( $blob === '' ) {
246 return array();
247 } else {
248 return explode( "\n", $blob );
249 }
250 }
251
252 /**
253 * Convert a comma-delimited list of block log flags
254 * into a more readable (and translated) form
255 *
256 * @param $flags Flags to format
257 * @return string
258 */
259 public static function formatBlockFlags( $flags ) {
260 $flags = explode( ',', trim( $flags ) );
261 if( count( $flags ) > 0 ) {
262 for( $i = 0; $i < count( $flags ); $i++ )
263 $flags[$i] = self::formatBlockFlag( $flags[$i] );
264 return '(' . implode( ', ', $flags ) . ')';
265 } else {
266 return '';
267 }
268 }
269
270 /**
271 * Translate a block log flag if possible
272 *
273 * @param $flag Flag to translate
274 * @return string
275 */
276 public static function formatBlockFlag( $flag ) {
277 static $messages = array();
278 if( !isset( $messages[$flag] ) ) {
279 $k = 'block-log-flags-' . $flag;
280 $msg = wfMsg( $k );
281 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
282 }
283 return $messages[$flag];
284 }
285
286 }
287
288 ?>