introduced "import" right for Special:Import
[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;
36 var $updateRecentChanges = true;
37
38 function LogPage( $type ) {
39 # Type is one of 'block', 'protect', 'rights', 'delete', 'upload', 'move'
40 $this->type = $type;
41 }
42
43 function saveContent() {
44 if( wfReadOnly() ) return;
45
46 global $wgUser;
47 $fname = 'LogPage::saveContent';
48
49 $dbw =& wfGetDB( DB_MASTER );
50 $uid = $wgUser->getID();
51
52 $this->timestamp = $now = wfTimestampNow();
53 $dbw->insert( 'logging',
54 array(
55 'log_type' => $this->type,
56 'log_action' => $this->action,
57 'log_timestamp' => $dbw->timestamp( $now ),
58 'log_user' => $uid,
59 'log_namespace' => $this->target->getNamespace(),
60 'log_title' => $this->target->getDBkey(),
61 'log_comment' => $this->comment,
62 'log_params' => $this->params
63 ), $fname
64 );
65
66 # And update recentchanges
67 if ( $this->updateRecentChanges ) {
68 $rcComment = $this->actionText;
69 if( '' != $this->comment ) {
70 $rcComment .= ': ' . $this->comment;
71 }
72 $titleObj = Title::makeTitle( NS_SPECIAL, 'Log/' . $this->type );
73 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment );
74 }
75 return true;
76 }
77
78 /**
79 * @static
80 */
81 function validTypes() {
82 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
83 return $types;
84 }
85
86 /**
87 * @static
88 */
89 function validActions( $type ) {
90 static $actions = array(
91 '' => NULL,
92 'block' => array( 'block', 'unblock' ),
93 'protect' => array( 'protect', 'unprotect' ),
94 'rights' => array( 'rights' ),
95 'delete' => array( 'delete', 'restore' ),
96 'upload' => array( 'upload' ),
97 'move' => array( 'move' )
98 );
99 return $actions[$type];
100 }
101
102 /**
103 * @static
104 */
105 function isLogType( $type ) {
106 return in_array( $type, LogPage::validTypes() );
107 }
108
109 /**
110 * @static
111 */
112 function logName( $type ) {
113 static $typeText = array(
114 '' => 'log',
115 'block' => 'blocklogpage',
116 'protect' => 'protectlogpage',
117 'rights' => 'bureaucratlog',
118 'delete' => 'dellogpage',
119 'upload' => 'uploadlogpage',
120 'move' => 'movelogpage'
121 );
122 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
123 }
124
125 /**
126 * @static
127 */
128 function logHeader( $type ) {
129 static $headerText = array(
130 '' => 'alllogstext',
131 'block' => 'blocklogtext',
132 'protect' => 'protectlogtext',
133 'rights' => 'rightslogtext',
134 'delete' => 'dellogpagetext',
135 'upload' => 'uploadlogpagetext',
136 'move' => 'movelogpagetext'
137 );
138 return wfMsg( $headerText[$type] );
139 }
140
141 /**
142 * @static
143 */
144 function actionText( $type, $action, $titleLink = NULL, $params = array() ) {
145 static $actions = array(
146 'block/block' => 'blocklogentry',
147 'block/unblock' => 'unblocklogentry',
148 'protect/protect' => 'protectedarticle',
149 'protect/unprotect' => 'unprotectedarticle',
150 'rights/rights' => 'bureaucratlogentry',
151 'delete/delete' => 'deletedarticle',
152 'delete/restore' => 'undeletedarticle',
153 'upload/upload' => 'uploadedimage',
154 'upload/revert' => 'uploadedimage',
155 'move/move' => '1movedto2',
156 'move/move_redir' => '1movedto2_redir'
157 );
158 $key = "$type/$action";
159 if( isset( $actions[$key] ) ) {
160 if( is_null( $titleLink ) ) {
161 return wfMsgForContent( $actions[$key] );
162 } elseif ( count( $params ) == 0 ) {
163 return wfMsgForContent( $actions[$key], $titleLink );
164 } else {
165 array_unshift( $params, $titleLink );
166 return wfMsgReal( $actions[$key], $params, true, true );
167 }
168 } else {
169 wfDebug( "LogPage::actionText - unknown action $key\n" );
170 return "$action $titleLink";
171 }
172 }
173
174 /**
175 * Add a log entry
176 * @param string $action one of 'block', 'protect', 'rights', 'delete', 'upload'
177 * @param object &$target A title object.
178 * @param string $comment Description associated
179 * @param array $params Parameters passed later to wfMsg.* functions
180 */
181 function addEntry( $action, &$target, $comment, $params = array() ) {
182 global $wgLang, $wgUser;
183
184 if ( !is_array( $params ) ) {
185 $params = array( $params );
186 }
187
188 $this->action = $action;
189 $this->target =& $target;
190 $this->comment = $comment;
191 $this->params = LogPage::makeParamBlob( $params );
192
193 $this->actionText = LogPage::actionText( $this->type, $action,
194 $target->getPrefixedText(), $params );
195 return $this->saveContent();
196 }
197
198 /**
199 * Create a blob from a parameter array
200 * @static
201 */
202 function makeParamBlob( $params )
203 {
204 return implode( "\n", $params );
205 }
206
207 /**
208 * Extract a parameter array from a blob
209 * @static
210 */
211 function extractParams( $blob )
212 {
213 if ( $blob === '' ) {
214 return array();
215 } else {
216 return explode( "\n", $blob );
217 }
218 }
219 }
220
221 ?>