sync from fr.wikipedia.org
[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 static $actions = array(
160 'block/block' => 'blocklogentry',
161 'block/unblock' => 'unblocklogentry',
162 'protect/protect' => 'protectedarticle',
163 'protect/unprotect' => 'unprotectedarticle',
164 'rights/rights' => 'bureaucratlogentry',
165 'rights/addgroup' => 'addgrouplogentry',
166 'rights/rngroup' => 'renamegrouplogentry',
167 'rights/chgroup' => 'changegrouplogentry',
168 'delete/delete' => 'deletedarticle',
169 'delete/restore' => 'undeletedarticle',
170 'upload/upload' => 'uploadedimage',
171 'upload/revert' => 'uploadedimage',
172 'move/move' => '1movedto2',
173 'move/move_redir' => '1movedto2_redir'
174 );
175 $key = "$type/$action";
176 if( isset( $actions[$key] ) ) {
177 if( is_null( $title ) ) {
178 $rv=wfMsg( $actions[$key] );
179 } else {
180 if( $skin ) {
181 if ( $type == 'move' ) {
182 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
183 // Change $param[0] into a link to the title specified in $param[0]
184 $movedTo = Title::newFromText( $params[0] );
185 $params[0] = $skin->makeLinkObj( $movedTo, $params[0] );
186 } else {
187 $titleLink = $skin->makeLinkObj( $title );
188 }
189 } else {
190 $titleLink = $title->getPrefixedText();
191 }
192 if( count( $params ) == 0 ) {
193 $rv = wfMsg( $actions[$key], $titleLink );
194 } else {
195 array_unshift( $params, $titleLink );
196 $rv = wfMsgReal( $actions[$key], $params, true, false );
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 ?>