+* Experimental feature to allow translation of block expiry times
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20
21 /**
22 * Contain log classes
23 *
24 * @package MediaWiki
25 */
26
27 /**
28 * Class to simplify the use of log pages.
29 * The logs are now kept in a table which is easier to manage and trim
30 * than ever-growing wiki pages.
31 *
32 * @package MediaWiki
33 */
34 class LogPage {
35 /* private */ var $type, $action, $comment, $params, $target;
36 var $updateRecentChanges = true;
37
38 /**
39 * Constructor
40 *
41 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
42 * 'upload', 'move'
43 */
44 function LogPage( $type ) {
45 $this->type = $type;
46 }
47
48 function saveContent() {
49 if( wfReadOnly() ) return;
50
51 global $wgUser;
52 $fname = 'LogPage::saveContent';
53
54 $dbw =& wfGetDB( DB_MASTER );
55 $uid = $wgUser->getID();
56
57 $this->timestamp = $now = wfTimestampNow();
58 $dbw->insert( 'logging',
59 array(
60 'log_type' => $this->type,
61 'log_action' => $this->action,
62 'log_timestamp' => $dbw->timestamp( $now ),
63 'log_user' => $uid,
64 'log_namespace' => $this->target->getNamespace(),
65 'log_title' => $this->target->getDBkey(),
66 'log_comment' => $this->comment,
67 'log_params' => $this->params
68 ), $fname
69 );
70
71 # And update recentchanges
72 if ( $this->updateRecentChanges ) {
73 $titleObj = Title::makeTitle( NS_SPECIAL, 'Log/' . $this->type );
74 $rcComment = $this->actionText;
75 if( '' != $this->comment ) {
76 if ($rcComment == '')
77 $rcComment = $this->comment;
78 else
79 $rcComment .= ': ' . $this->comment;
80 }
81
82 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment );
83 }
84 return true;
85 }
86
87 /**
88 * @static
89 */
90 function validTypes() {
91 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
92 wfRunHooks( 'LogPageValidTypes', array( &$types) );
93 return $types;
94 }
95
96 /**
97 * @static
98 */
99 function validActions( $type ) {
100 static $actions = array(
101 '' => NULL,
102 'block' => array( 'block', 'unblock' ),
103 'protect' => array( 'protect', 'unprotect' ),
104 'rights' => array( 'rights' ),
105 'delete' => array( 'delete', 'restore' ),
106 'upload' => array( 'upload' ),
107 'move' => array( 'move' )
108 );
109 return $actions[$type];
110 }
111
112 /**
113 * @static
114 */
115 function isLogType( $type ) {
116 return in_array( $type, LogPage::validTypes() );
117 }
118
119 /**
120 * @static
121 */
122 function logName( $type ) {
123 static $typeText = array(
124 '' => 'log',
125 'block' => 'blocklogpage',
126 'protect' => 'protectlogpage',
127 'rights' => 'bureaucratlog',
128 'delete' => 'dellogpage',
129 'upload' => 'uploadlogpage',
130 'move' => 'movelogpage'
131 );
132 wfRunHooks( 'LogPageLogName', array( &$typeText) );
133
134 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
135 }
136
137 /**
138 * @static
139 */
140 function logHeader( $type ) {
141 static $headerText = array(
142 '' => 'alllogstext',
143 'block' => 'blocklogtext',
144 'protect' => 'protectlogtext',
145 'rights' => 'rightslogtext',
146 'delete' => 'dellogpagetext',
147 'upload' => 'uploadlogpagetext',
148 'move' => 'movelogpagetext'
149 );
150 wfRunHooks( 'LogPageLogHeader', array( &$headerText ) );
151
152 return wfMsg( $headerText[$type] );
153 }
154
155 /**
156 * @static
157 */
158 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
159 global $wgLang;
160 static $actions = array(
161 'block/block' => 'blocklogentry',
162 'block/unblock' => 'unblocklogentry',
163 'protect/protect' => 'protectedarticle',
164 'protect/unprotect' => 'unprotectedarticle',
165 'rights/rights' => 'bureaucratlogentry',
166 'rights/addgroup' => 'addgrouplogentry',
167 'rights/rngroup' => 'renamegrouplogentry',
168 'rights/chgroup' => 'changegrouplogentry',
169 'delete/delete' => 'deletedarticle',
170 'delete/restore' => 'undeletedarticle',
171 'upload/upload' => 'uploadedimage',
172 'upload/revert' => 'uploadedimage',
173 'move/move' => '1movedto2',
174 'move/move_redir' => '1movedto2_redir'
175 );
176 $key = "$type/$action";
177 if( isset( $actions[$key] ) ) {
178 if( is_null( $title ) ) {
179 $rv=wfMsg( $actions[$key] );
180 } else {
181 if( $skin ) {
182 if ( $type == 'move' ) {
183 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
184 // Change $param[0] into a link to the title specified in $param[0]
185 $movedTo = Title::newFromText( $params[0] );
186 $params[0] = $skin->makeLinkObj( $movedTo, $params[0] );
187 } else {
188 $titleLink = $skin->makeLinkObj( $title );
189 }
190 } else {
191 $titleLink = $title->getPrefixedText();
192 }
193 if( count( $params ) == 0 ) {
194 $rv = wfMsg( $actions[$key], $titleLink );
195 } else {
196 array_unshift( $params, $titleLink );
197 if ( $key == 'block/block' ) {
198 $params[1] = $wgLang->translateBlockExpiry($params[1]);
199 }
200 $rv = wfMsgReal( $actions[$key], $params, true, false );
201 }
202 }
203 } else {
204 wfDebug( "LogPage::actionText - unknown action $key\n" );
205 $rv = "$action";
206 }
207 if( $filterWikilinks ) {
208 $rv = str_replace( "[[", "", $rv );
209 $rv = str_replace( "]]", "", $rv );
210 }
211 return $rv;
212 }
213
214 /**
215 * Add a log entry
216 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
217 * @param object &$target A title object.
218 * @param string $comment Description associated
219 * @param array $params Parameters passed later to wfMsg.* functions
220 */
221 function addEntry( $action, &$target, $comment, $params = array() ) {
222 if ( !is_array( $params ) ) {
223 $params = array( $params );
224 }
225
226 $this->action = $action;
227 $this->target =& $target;
228 $this->comment = $comment;
229 $this->params = LogPage::makeParamBlob( $params );
230
231 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
232
233 return $this->saveContent();
234 }
235
236 /**
237 * Create a blob from a parameter array
238 * @static
239 */
240 function makeParamBlob( $params ) {
241 return implode( "\n", $params );
242 }
243
244 /**
245 * Extract a parameter array from a blob
246 * @static
247 */
248 function extractParams( $blob ) {
249 if ( $blob === '' ) {
250 return array();
251 } else {
252 return explode( "\n", $blob );
253 }
254 }
255 }
256
257 ?>