46bace0001b4d5ab4b4506b88e107ade5cfb182d
[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 false;
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 = SpecialPage::getTitleFor( '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 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
87 $this->type, $this->action, $this->target, $this->comment, $this->params );
88 }
89 return true;
90 }
91
92 /**
93 * @static
94 */
95 function validTypes() {
96 global $wgLogTypes;
97 return $wgLogTypes;
98 }
99
100 /**
101 * @static
102 */
103 function isLogType( $type ) {
104 return in_array( $type, LogPage::validTypes() );
105 }
106
107 /**
108 * @static
109 */
110 function logName( $type ) {
111 global $wgLogNames;
112
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;
118 }
119 }
120
121 /**
122 * @fixme: handle missing log types
123 * @static
124 */
125 function logHeader( $type ) {
126 global $wgLogHeaders;
127 return wfMsg( $wgLogHeaders[$type] );
128 }
129
130 /**
131 * @static
132 */
133 static function actionText( $type, $action, $title = NULL, $forContent = true, $params = array(), $filterWikilinks=false, $translate=false ) {
134 global $wgLang, $wgContLang, $wgLogActions;
135
136 $key = "$type/$action";
137 if( isset( $wgLogActions[$key] ) ) {
138 if( is_null( $title ) ) {
139 $rv=wfMsg( $wgLogActions[$key] );
140 } else {
141 if( $forContent ) {
142 $titleLink = $title->getPrefixedText();
143 } else {
144 switch( $type ) {
145 case 'move':
146 $titleLink = Linker::makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
147 $params[0] = Linker::makeLinkObj( Title::newFromText( $params[0] ), $params[0] );
148 break;
149 case 'block':
150 if( substr( $title->getText(), 0, 1 ) == '#' ) {
151 $titleLink = $title->getText();
152 } else {
153 $titleLink = Linker::makeLinkObj( $title, $title->getText() );
154 $titleLink .= ' (' . Linker::makeKnownLinkObj( SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() ), wfMsg( 'contribslink' ) ) . ')';
155 }
156 break;
157 case 'rights':
158 $text = $wgContLang->ucfirst( $title->getText() );
159 $titleLink = Linker::makeLinkObj( Title::makeTitle( NS_USER, $text ) );
160 break;
161 default:
162 $titleLink = Linker::makeLinkObj( $title );
163 }
164 }
165 if( $key == 'rights/rights' ) {
166 if( $forContent ) {
167 $rightsnone = wfMsgForContent( 'rightsnone' );
168 } else {
169 $rightsnone = wfMsg( 'rightsnone' );
170 }
171 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
172 $params[0] = $rightsnone;
173 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
174 $params[1] = $rightsnone;
175 }
176 if( count( $params ) == 0 ) {
177 if ( $forContent ) {
178 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
179 } else {
180 $rv = wfMsg( $wgLogActions[$key], $titleLink );
181 }
182 } else {
183 array_unshift( $params, $titleLink );
184 if ( $translate && $key == 'block/block' ) {
185 $params[1] = $wgLang->translateBlockExpiry($params[1]);
186 }
187 $rv = wfMsgReal( $wgLogActions[$key], $params, true, $forContent );
188 }
189 }
190 } else {
191 wfDebug( "LogPage::actionText - unknown action $key\n" );
192 $rv = "$action";
193 }
194 if( $filterWikilinks ) {
195 $rv = str_replace( '[[', '', $rv );
196 $rv = str_replace( ']]', '', $rv );
197 }
198 return $rv;
199 }
200
201 /**
202 * Add a log entry
203 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
204 * @param object &$target A title object.
205 * @param string $comment Description associated
206 * @param array $params Parameters passed later to wfMsg.* functions
207 */
208 function addEntry( $action, $target, $comment, $params = array() ) {
209 if ( !is_array( $params ) ) {
210 $params = array( $params );
211 }
212
213 $this->action = $action;
214 $this->target = $target;
215 $this->comment = $comment;
216 $this->params = self::makeParamBlob( $params );
217
218 $this->actionText = self::actionText( $this->type, $action, $target, true, $params );
219
220 return $this->saveContent();
221 }
222
223 /**
224 * Create a blob from a parameter array
225 * @static
226 */
227 static function makeParamBlob( $params ) {
228 return implode( "\n", $params );
229 }
230
231 /**
232 * Extract a parameter array from a blob
233 * @static
234 */
235 static function extractParams( $blob ) {
236 if ( $blob === '' ) {
237 return array();
238 } else {
239 return explode( "\n", $blob );
240 }
241 }
242 }
243
244 ?>