66df19f55799664839cbe05c62e03c9fd52d7e49
[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 * @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 /* @access private */
36 var $type, $action, $comment, $params, $target;
37 /* @acess public */
38 var $updateRecentChanges;
39
40 /**
41 * Constructor
42 *
43 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
44 * 'upload', 'move'
45 * @param bool $rc Whether to update recent changes as well as the logging table
46 */
47 function LogPage( $type, $rc = true ) {
48 $this->type = $type;
49 $this->updateRecentChanges = $rc;
50 }
51
52 function saveContent() {
53 if( wfReadOnly() ) return;
54
55 global $wgUser;
56 $fname = 'LogPage::saveContent';
57
58 $dbw =& wfGetDB( DB_MASTER );
59 $uid = $wgUser->getID();
60
61 $this->timestamp = $now = wfTimestampNow();
62 $dbw->insert( 'logging',
63 array(
64 'log_type' => $this->type,
65 'log_action' => $this->action,
66 'log_timestamp' => $dbw->timestamp( $now ),
67 'log_user' => $uid,
68 'log_namespace' => $this->target->getNamespace(),
69 'log_title' => $this->target->getDBkey(),
70 'log_comment' => $this->comment,
71 'log_params' => $this->params
72 ), $fname
73 );
74
75 # And update recentchanges
76 if ( $this->updateRecentChanges ) {
77 $titleObj = Title::makeTitle( NS_SPECIAL, 'Log/' . $this->type );
78 $rcComment = $this->actionText;
79 if( '' != $this->comment ) {
80 if ($rcComment == '')
81 $rcComment = $this->comment;
82 else
83 $rcComment .= ': ' . $this->comment;
84 }
85
86 require_once( 'RecentChange.php' );
87 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
88 $this->type, $this->action, $this->target, $this->comment, $this->params );
89 }
90 return true;
91 }
92
93 /**
94 * @static
95 */
96 function validTypes() {
97 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
98 wfRunHooks( 'LogPageValidTypes', array( &$types ) );
99 return $types;
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' => 'rightslog',
118 'delete' => 'dellogpage',
119 'upload' => 'uploadlogpage',
120 'move' => 'movelogpage'
121 );
122 wfRunHooks( 'LogPageLogName', array( &$typeText ) );
123
124 if( isset( $typeText[$type] ) ) {
125 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
126 } else {
127 // Bogus log types? Perhaps an extension was removed.
128 return $type;
129 }
130 }
131
132 /**
133 * @static
134 */
135 function logHeader( $type ) {
136 static $headerText = array(
137 '' => 'alllogstext',
138 'block' => 'blocklogtext',
139 'protect' => 'protectlogtext',
140 'rights' => 'rightslogtext',
141 'delete' => 'dellogpagetext',
142 'upload' => 'uploadlogpagetext',
143 'move' => 'movelogpagetext'
144 );
145 wfRunHooks( 'LogPageLogHeader', array( &$headerText ) );
146
147 return wfMsg( $headerText[$type] );
148 }
149
150 /**
151 * @static
152 */
153 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
154 global $wgLang, $wgContLang;
155 static $actions = array(
156 'block/block' => 'blocklogentry',
157 'block/unblock' => 'unblocklogentry',
158 'protect/protect' => 'protectedarticle',
159 'protect/unprotect' => 'unprotectedarticle',
160
161 // TODO: This whole section should be moved to extensions/Makesysop/SpecialMakesysop.php
162 'rights/rights' => 'rightslogentry',
163 'rights/addgroup' => 'addgrouplogentry',
164 'rights/rngroup' => 'renamegrouplogentry',
165 'rights/chgroup' => 'changegrouplogentry',
166
167 'delete/delete' => 'deletedarticle',
168 'delete/restore' => 'undeletedarticle',
169 'delete/revision' => 'revdelete-logentry',
170 'upload/upload' => 'uploadedimage',
171 'upload/revert' => 'uploadedimage',
172 'move/move' => '1movedto2',
173 'move/move_redir' => '1movedto2_redir'
174 );
175 wfRunHooks( 'LogPageActionText', array( &$actions ) );
176
177 $key = "$type/$action";
178 if( isset( $actions[$key] ) ) {
179 if( is_null( $title ) ) {
180 $rv=wfMsg( $actions[$key] );
181 } else {
182 if( $skin ) {
183
184 switch( $type ) {
185 case 'move':
186 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
187 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), $params[0] );
188 break;
189 case 'block':
190 if( substr( $title->getText(), 0, 1 ) == '#' ) {
191 $titleLink = $title->getText();
192 } else {
193 $titleLink = $skin->makeLinkObj( $title, $title->getText() );
194 $titleLink .= ' (' . $skin->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions/' . $title->getDBkey() ), wfMsg( 'contribslink' ) ) . ')';
195 }
196 break;
197 case 'rights':
198 if( trim( $params[0] ) == '' )
199 $params[0] = wfMsg( 'rightsnone' );
200 $text = $wgContLang->ucfirst( $title->getText() );
201 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
202 break;
203 default:
204 $titleLink = $skin->makeLinkObj( $title );
205 }
206
207 } else {
208 $titleLink = $title->getPrefixedText();
209 }
210 if( count( $params ) == 0 ) {
211 if ( $skin ) {
212 $rv = wfMsg( $actions[$key], $titleLink );
213 } else {
214 $rv = wfMsgForContent( $actions[$key], $titleLink );
215 }
216 } else {
217 array_unshift( $params, $titleLink );
218 if ( $translate && $key == 'block/block' ) {
219 $params[1] = $wgLang->translateBlockExpiry($params[1]);
220 }
221 $rv = wfMsgReal( $actions[$key], $params, true, !$skin );
222 }
223 }
224 } else {
225 wfDebug( "LogPage::actionText - unknown action $key\n" );
226 $rv = "$action";
227 }
228 if( $filterWikilinks ) {
229 $rv = str_replace( "[[", "", $rv );
230 $rv = str_replace( "]]", "", $rv );
231 }
232 return $rv;
233 }
234
235 /**
236 * Add a log entry
237 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
238 * @param object &$target A title object.
239 * @param string $comment Description associated
240 * @param array $params Parameters passed later to wfMsg.* functions
241 */
242 function addEntry( $action, &$target, $comment, $params = array() ) {
243 if ( !is_array( $params ) ) {
244 $params = array( $params );
245 }
246
247 $this->action = $action;
248 $this->target =& $target;
249 $this->comment = $comment;
250 $this->params = LogPage::makeParamBlob( $params );
251
252 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
253
254 return $this->saveContent();
255 }
256
257 /**
258 * Create a blob from a parameter array
259 * @static
260 */
261 function makeParamBlob( $params ) {
262 return implode( "\n", $params );
263 }
264
265 /**
266 * Extract a parameter array from a blob
267 * @static
268 */
269 function extractParams( $blob ) {
270 if ( $blob === '' ) {
271 return array();
272 } else {
273 return explode( "\n", $blob );
274 }
275 }
276 }
277
278 ?>