3 # Copyright (C) 2002, 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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.
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.
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
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.
36 var $type, $action, $comment, $params, $target;
38 var $updateRecentChanges;
43 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
45 * @param bool $rc Whether to update recent changes as well as the logging table
47 function LogPage( $type, $rc = true ) {
49 $this->updateRecentChanges
= $rc;
52 function saveContent() {
53 if( wfReadOnly() ) return;
56 $fname = 'LogPage::saveContent';
58 $dbw =& wfGetDB( DB_MASTER
);
59 $uid = $wgUser->getID();
61 $this->timestamp
= $now = wfTimestampNow();
62 $dbw->insert( 'logging',
64 'log_type' => $this->type
,
65 'log_action' => $this->action
,
66 'log_timestamp' => $dbw->timestamp( $now ),
68 'log_namespace' => $this->target
->getNamespace(),
69 'log_title' => $this->target
->getDBkey(),
70 'log_comment' => $this->comment
,
71 'log_params' => $this->params
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
) {
81 $rcComment = $this->comment
;
83 $rcComment .= ': ' . $this->comment
;
86 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment );
94 function validTypes() {
95 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
96 wfRunHooks( 'LogPageValidTypes', array( &$types ) );
103 function validActions( $type ) {
104 static $actions = array(
106 'block' => array( 'block', 'unblock' ),
107 'protect' => array( 'protect', 'unprotect' ),
108 'rights' => array( 'rights' ),
109 'delete' => array( 'delete', 'restore' ),
110 'upload' => array( 'upload' ),
111 'move' => array( 'move' )
113 return $actions[$type];
119 function isLogType( $type ) {
120 return in_array( $type, LogPage
::validTypes() );
126 function logName( $type ) {
127 static $typeText = array(
129 'block' => 'blocklogpage',
130 'protect' => 'protectlogpage',
131 'rights' => 'bureaucratlog',
132 'delete' => 'dellogpage',
133 'upload' => 'uploadlogpage',
134 'move' => 'movelogpage'
136 wfRunHooks( 'LogPageLogName', array( &$typeText ) );
138 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
144 function logHeader( $type ) {
145 static $headerText = array(
147 'block' => 'blocklogtext',
148 'protect' => 'protectlogtext',
149 'rights' => 'rightslogtext',
150 'delete' => 'dellogpagetext',
151 'upload' => 'uploadlogpagetext',
152 'move' => 'movelogpagetext'
154 wfRunHooks( 'LogPageLogHeader', array( &$headerText ) );
156 return wfMsg( $headerText[$type] );
162 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
164 static $actions = array(
165 'block/block' => 'blocklogentry',
166 'block/unblock' => 'unblocklogentry',
167 'protect/protect' => 'protectedarticle',
168 'protect/unprotect' => 'unprotectedarticle',
169 'rights/rights' => 'bureaucratlogentry',
170 'rights/addgroup' => 'addgrouplogentry',
171 'rights/rngroup' => 'renamegrouplogentry',
172 'rights/chgroup' => 'changegrouplogentry',
173 'delete/delete' => 'deletedarticle',
174 'delete/restore' => 'undeletedarticle',
175 'upload/upload' => 'uploadedimage',
176 'upload/revert' => 'uploadedimage',
177 'move/move' => '1movedto2',
178 'move/move_redir' => '1movedto2_redir'
180 $key = "$type/$action";
181 if( isset( $actions[$key] ) ) {
182 if( is_null( $title ) ) {
183 $rv=wfMsg( $actions[$key] );
186 if ( $type == 'move' ) {
187 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
188 // Change $param[0] into a link to the title specified in $param[0]
189 $movedTo = Title
::newFromText( $params[0] );
190 $params[0] = $skin->makeLinkObj( $movedTo, $params[0] );
192 $titleLink = $skin->makeLinkObj( $title );
195 $titleLink = $title->getPrefixedText();
197 if( count( $params ) == 0 ) {
198 $rv = wfMsg( $actions[$key], $titleLink );
200 array_unshift( $params, $titleLink );
201 if ( $translate && $key == 'block/block' ) {
202 $params[1] = $wgLang->translateBlockExpiry($params[1]);
204 $rv = wfMsgReal( $actions[$key], $params, true, false );
208 wfDebug( "LogPage::actionText - unknown action $key\n" );
211 if( $filterWikilinks ) {
212 $rv = str_replace( "[[", "", $rv );
213 $rv = str_replace( "]]", "", $rv );
220 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
221 * @param object &$target A title object.
222 * @param string $comment Description associated
223 * @param array $params Parameters passed later to wfMsg.* functions
225 function addEntry( $action, &$target, $comment, $params = array() ) {
226 if ( !is_array( $params ) ) {
227 $params = array( $params );
230 $this->action
= $action;
231 $this->target
=& $target;
232 $this->comment
= $comment;
233 $this->params
= LogPage
::makeParamBlob( $params );
235 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
237 return $this->saveContent();
241 * Create a blob from a parameter array
244 function makeParamBlob( $params ) {
245 return implode( "\n", $params );
249 * Extract a parameter array from a blob
252 function extractParams( $blob ) {
253 if ( $blob === '' ) {
256 return explode( "\n", $blob );