* Fixed unclosed <p> tag
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 /* private */ var $type, $action, $comment, $params, $target;
36 var $updateRecentChanges = true;
37
38 /**
39 * Constructor
40 *
41 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
42 * 'upload', 'move'
43 */
44 function LogPage( $type ) {
45 $this->type = $type;
46 }
47
48 function saveContent() {
49 if( wfReadOnly() ) return;
50
51 global $wgUser;
52 $fname = 'LogPage::saveContent';
53
54 $dbw =& wfGetDB( DB_MASTER );
55 $uid = $wgUser->getID();
56
57 $this->timestamp = $now = wfTimestampNow();
58 $dbw->insert( 'logging',
59 array(
60 'log_type' => $this->type,
61 'log_action' => $this->action,
62 'log_timestamp' => $dbw->timestamp( $now ),
63 'log_user' => $uid,
64 'log_namespace' => $this->target->getNamespace(),
65 'log_title' => $this->target->getDBkey(),
66 'log_comment' => $this->comment,
67 'log_params' => $this->params
68 ), $fname
69 );
70
71 # And update recentchanges
72 if ( $this->updateRecentChanges ) {
73 $titleObj = Title::makeTitle( NS_SPECIAL, 'Log/' . $this->type );
74 $rcComment = $this->actionText;
75 if( '' != $this->comment ) {
76 if ($rcComment == '')
77 $rcComment = $this->comment;
78 else
79 $rcComment .= ': ' . $this->comment;
80 }
81
82 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment );
83 }
84 return true;
85 }
86
87 /**
88 * @static
89 */
90 function validTypes() {
91 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
92 return $types;
93 }
94
95 /**
96 * @static
97 */
98 function validActions( $type ) {
99 static $actions = array(
100 '' => NULL,
101 'block' => array( 'block', 'unblock' ),
102 'protect' => array( 'protect', 'unprotect' ),
103 'rights' => array( 'rights' ),
104 'delete' => array( 'delete', 'restore' ),
105 'upload' => array( 'upload' ),
106 'move' => array( 'move' )
107 );
108 return $actions[$type];
109 }
110
111 /**
112 * @static
113 */
114 function isLogType( $type ) {
115 return in_array( $type, LogPage::validTypes() );
116 }
117
118 /**
119 * @static
120 */
121 function logName( $type ) {
122 static $typeText = array(
123 '' => 'log',
124 'block' => 'blocklogpage',
125 'protect' => 'protectlogpage',
126 'rights' => 'bureaucratlog',
127 'delete' => 'dellogpage',
128 'upload' => 'uploadlogpage',
129 'move' => 'movelogpage'
130 );
131 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
132 }
133
134 /**
135 * @static
136 */
137 function logHeader( $type ) {
138 static $headerText = array(
139 '' => 'alllogstext',
140 'block' => 'blocklogtext',
141 'protect' => 'protectlogtext',
142 'rights' => 'rightslogtext',
143 'delete' => 'dellogpagetext',
144 'upload' => 'uploadlogpagetext',
145 'move' => 'movelogpagetext'
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 ) {
154 static $actions = array(
155 'block/block' => 'blocklogentry',
156 'block/unblock' => 'unblocklogentry',
157 'protect/protect' => 'protectedarticle',
158 'protect/unprotect' => 'unprotectedarticle',
159 'rights/rights' => 'bureaucratlogentry',
160 'rights/addgroup' => 'addgrouplogentry',
161 'rights/rngroup' => 'renamegrouplogentry',
162 'rights/chgroup' => 'changegrouplogentry',
163 'delete/delete' => 'deletedarticle',
164 'delete/restore' => 'undeletedarticle',
165 'upload/upload' => 'uploadedimage',
166 'upload/revert' => 'uploadedimage',
167 'move/move' => '1movedto2',
168 'move/move_redir' => '1movedto2_redir'
169 );
170 $key = "$type/$action";
171 if( isset( $actions[$key] ) ) {
172 if( is_null( $title ) ) {
173 $rv=wfMsgForContent( $actions[$key] );
174 } else {
175 if( $skin ) {
176 if ( $type == 'move' ) {
177 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
178 // Change $param[0] into a link to the title specified in $param[0]
179 $movedTo = Title::newFromText( $params[0] );
180 $params[0] = $skin->makeLinkObj( $movedTo, $params[0] );
181 } else {
182 $titleLink = $skin->makeLinkObj( $title );
183 }
184 } else {
185 $titleLink = $title->getPrefixedText();
186 }
187 if( count( $params ) == 0 ) {
188 $rv = wfMsgForContent( $actions[$key], $titleLink );
189 } else {
190 array_unshift( $params, $titleLink );
191 $rv = wfMsgReal( $actions[$key], $params, true, true );
192 }
193 }
194 } else {
195 wfDebug( "LogPage::actionText - unknown action $key\n" );
196 $rv = "$action";
197 }
198 if( $filterWikilinks ) {
199 $rv = str_replace( "[[", "", $rv );
200 $rv = str_replace( "]]", "", $rv );
201 }
202 return $rv;
203 }
204
205 /**
206 * Add a log entry
207 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
208 * @param object &$target A title object.
209 * @param string $comment Description associated
210 * @param array $params Parameters passed later to wfMsg.* functions
211 */
212 function addEntry( $action, &$target, $comment, $params = array() ) {
213 if ( !is_array( $params ) ) {
214 $params = array( $params );
215 }
216
217 $this->action = $action;
218 $this->target =& $target;
219 $this->comment = $comment;
220 $this->params = LogPage::makeParamBlob( $params );
221
222 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
223
224 return $this->saveContent();
225 }
226
227 /**
228 * Create a blob from a parameter array
229 * @static
230 */
231 function makeParamBlob( $params ) {
232 return implode( "\n", $params );
233 }
234
235 /**
236 * Extract a parameter array from a blob
237 * @static
238 */
239 function extractParams( $blob ) {
240 if ( $blob === '' ) {
241 return array();
242 } else {
243 return explode( "\n", $blob );
244 }
245 }
246 }
247
248 ?>