* (bug 6230) Regression fix: <nowiki> in [URL link text]
[mediawiki.git] / includes / LogPage.php
blob753c1dc47b501c8c30ab05ffc22283db84628439
1 <?php
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /* @access private */
36 var $type, $action, $comment, $params, $target;
37 /* @acess public */
38 var $updateRecentChanges;
40 /**
41 * Constructor
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
47 function LogPage( $type, $rc = true ) {
48 $this->type = $type;
49 $this->updateRecentChanges = $rc;
52 function saveContent() {
53 if( wfReadOnly() ) return false;
55 global $wgUser;
56 $fname = 'LogPage::saveContent';
58 $dbw =& wfGetDB( DB_MASTER );
59 $uid = $wgUser->getID();
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
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;
86 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
87 $this->type, $this->action, $this->target, $this->comment, $this->params );
89 return true;
92 /**
93 * @static
95 function validTypes() {
96 global $wgLogTypes;
97 return $wgLogTypes;
101 * @static
103 function isLogType( $type ) {
104 return in_array( $type, LogPage::validTypes() );
108 * @static
110 function logName( $type ) {
111 global $wgLogNames;
113 if( isset( $wgLogNames[$type] ) ) {
114 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
115 } else {
116 // Bogus log types? Perhaps an extension was removed.
117 return $type;
122 * @fixme: handle missing log types
123 * @static
125 function logHeader( $type ) {
126 global $wgLogHeaders;
127 return wfMsg( $wgLogHeaders[$type] );
131 * @static
133 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
134 global $wgLang, $wgContLang, $wgLogActions;
136 $key = "$type/$action";
137 if( isset( $wgLogActions[$key] ) ) {
138 if( is_null( $title ) ) {
139 $rv=wfMsg( $wgLogActions[$key] );
140 } else {
141 if( $skin ) {
143 switch( $type ) {
144 case 'move':
145 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
146 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), $params[0] );
147 break;
148 case 'block':
149 if( substr( $title->getText(), 0, 1 ) == '#' ) {
150 $titleLink = $title->getText();
151 } else {
152 $titleLink = $skin->makeLinkObj( $title, $title->getText() );
153 $titleLink .= ' (' . $skin->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions/' . $title->getDBkey() ), wfMsg( 'contribslink' ) ) . ')';
155 break;
156 case 'rights':
157 if( trim( $params[0] ) == '' )
158 $params[0] = wfMsg( 'rightsnone' );
159 $text = $wgContLang->ucfirst( $title->getText() );
160 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
161 break;
162 default:
163 $titleLink = $skin->makeLinkObj( $title );
166 } else {
167 $titleLink = $title->getPrefixedText();
169 if( count( $params ) == 0 ) {
170 if ( $skin ) {
171 $rv = wfMsg( $wgLogActions[$key], $titleLink );
172 } else {
173 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
175 } else {
176 array_unshift( $params, $titleLink );
177 if ( $translate && $key == 'block/block' ) {
178 $params[1] = $wgLang->translateBlockExpiry($params[1]);
180 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
183 } else {
184 wfDebug( "LogPage::actionText - unknown action $key\n" );
185 $rv = "$action";
187 if( $filterWikilinks ) {
188 $rv = str_replace( "[[", "", $rv );
189 $rv = str_replace( "]]", "", $rv );
191 return $rv;
195 * Add a log entry
196 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
197 * @param object &$target A title object.
198 * @param string $comment Description associated
199 * @param array $params Parameters passed later to wfMsg.* functions
201 function addEntry( $action, &$target, $comment, $params = array() ) {
202 if ( !is_array( $params ) ) {
203 $params = array( $params );
206 $this->action = $action;
207 $this->target =& $target;
208 $this->comment = $comment;
209 $this->params = LogPage::makeParamBlob( $params );
211 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
213 return $this->saveContent();
217 * Create a blob from a parameter array
218 * @static
220 function makeParamBlob( $params ) {
221 return implode( "\n", $params );
225 * Extract a parameter array from a blob
226 * @static
228 function extractParams( $blob ) {
229 if ( $blob === '' ) {
230 return array();
231 } else {
232 return explode( "\n", $blob );