* Added a TODO note to isASCII()
[mediawiki.git] / includes / LogPage.php
blob1345503ad4befc7fda312a3fbf43fa88b5a699d6
1 <?php
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.
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
21 /**
22 * Contain log classes
24 * @package MediaWiki
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.
32 * @package MediaWiki
34 class LogPage {
35 /* private */ var $type, $action, $comment, $params, $target;
36 var $updateRecentChanges = true;
38 function LogPage( $type ) {
39 # Type is one of 'block', 'protect', 'rights', 'delete', 'upload', 'move'
40 $this->type = $type;
43 function saveContent() {
44 if( wfReadOnly() ) return;
46 global $wgUser;
47 $fname = 'LogPage::saveContent';
49 $dbw =& wfGetDB( DB_MASTER );
50 $uid = $wgUser->getID();
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
66 # And update recentchanges
67 if ( $this->updateRecentChanges ) {
68 $titleObj = Title::makeTitle( NS_SPECIAL, 'Log/' . $this->type );
69 $rcComment = $this->actionText;
70 if( '' != $this->comment ) {
71 $rcComment .= ': ' . $this->comment;
74 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment );
76 return true;
79 /**
80 * @static
82 function validTypes() {
83 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
84 return $types;
87 /**
88 * @static
90 function validActions( $type ) {
91 static $actions = array(
92 '' => NULL,
93 'block' => array( 'block', 'unblock' ),
94 'protect' => array( 'protect', 'unprotect' ),
95 'rights' => array( 'rights' ),
96 'delete' => array( 'delete', 'restore' ),
97 'upload' => array( 'upload' ),
98 'move' => array( 'move' )
100 return $actions[$type];
104 * @static
106 function isLogType( $type ) {
107 return in_array( $type, LogPage::validTypes() );
111 * @static
113 function logName( $type ) {
114 static $typeText = array(
115 '' => 'log',
116 'block' => 'blocklogpage',
117 'protect' => 'protectlogpage',
118 'rights' => 'bureaucratlog',
119 'delete' => 'dellogpage',
120 'upload' => 'uploadlogpage',
121 'move' => 'movelogpage'
123 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
127 * @static
129 function logHeader( $type ) {
130 static $headerText = array(
131 '' => 'alllogstext',
132 'block' => 'blocklogtext',
133 'protect' => 'protectlogtext',
134 'rights' => 'rightslogtext',
135 'delete' => 'dellogpagetext',
136 'upload' => 'uploadlogpagetext',
137 'move' => 'movelogpagetext'
139 return wfMsg( $headerText[$type] );
143 * @static
145 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
146 static $actions = array(
147 'block/block' => 'blocklogentry',
148 'block/unblock' => 'unblocklogentry',
149 'protect/protect' => 'protectedarticle',
150 'protect/unprotect' => 'unprotectedarticle',
151 'rights/rights' => 'bureaucratlogentry',
152 'rights/addgroup' => 'addgrouplogentry',
153 'rights/rngroup' => 'renamegrouplogentry',
154 'rights/chgroup' => 'changegrouplogentry',
155 'delete/delete' => 'deletedarticle',
156 'delete/restore' => 'undeletedarticle',
157 'upload/upload' => 'uploadedimage',
158 'upload/revert' => 'uploadedimage',
159 'move/move' => '1movedto2',
160 'move/move_redir' => '1movedto2_redir'
162 $key = "$type/$action";
163 if( isset( $actions[$key] ) ) {
164 if( is_null( $title ) ) {
165 $rv=wfMsgForContent( $actions[$key] );
167 else {
168 if ( $skin ) {
169 if ( $type == 'move' ) {
170 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
171 // Change $param[0] into a link to the title specified in $param[0]
172 $movedTo = Title::newFromText( $params[0] );
173 $params[0] = $skin->makeLinkObj( $movedTo, $params[0] );
174 } else {
175 $titleLink = $skin->makeLinkObj( $title );
177 } else {
178 $titleLink = $title->getPrefixedText();
180 if ( count( $params ) == 0 ) {
181 $rv=wfMsgForContent( $actions[$key], $titleLink );
182 } else {
183 array_unshift( $params, $titleLink );
184 $rv=wfMsgReal( $actions[$key], $params, true, true );
187 } else {
188 wfDebug( "LogPage::actionText - unknown action $key\n" );
189 $rv="$action $titleLink";
191 if($filterWikilinks) {
192 $rv=str_replace("[[","",$rv);
193 $rv=str_replace("]]","",$rv);
195 return $rv;
199 * Add a log entry
200 * @param string $action one of 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
201 * @param object &$target A title object.
202 * @param string $comment Description associated
203 * @param array $params Parameters passed later to wfMsg.* functions
205 function addEntry( $action, &$target, $comment, $params = array() ) {
206 if ( !is_array( $params ) ) {
207 $params = array( $params );
210 $this->action = $action;
211 $this->target =& $target;
212 $this->comment = $comment;
213 $this->params = LogPage::makeParamBlob( $params );
215 $this->actionText = LogPage::actionText( $this->type, $action,
216 $target, NULL, $params );
218 return $this->saveContent();
221 /**
222 * Create a blob from a parameter array
223 * @static
225 function makeParamBlob( $params )
227 return implode( "\n", $params );
231 * Extract a parameter array from a blob
232 * @static
234 function extractParams( $blob )
236 if ( $blob === '' ) {
237 return array();
238 } else {
239 return explode( "\n", $blob );