(bug 5167) Add {{SUBPAGENAME}} variable
[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 /* @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;
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 = 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;
84 }
85
86 require_once( 'RecentChange.php' );
87 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment );
88 }
89 return true;
90 }
91
92 /**
93 * @static
94 */
95 function validTypes() {
96 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
97 wfRunHooks( 'LogPageValidTypes', array( &$types ) );
98 return $types;
99 }
100
101 /**
102 * @static
103 */
104 function isLogType( $type ) {
105 return in_array( $type, LogPage::validTypes() );
106 }
107
108 /**
109 * @static
110 */
111 function logName( $type ) {
112 static $typeText = array(
113 '' => 'log',
114 'block' => 'blocklogpage',
115 'protect' => 'protectlogpage',
116 'rights' => 'bureaucratlog',
117 'delete' => 'dellogpage',
118 'upload' => 'uploadlogpage',
119 'move' => 'movelogpage'
120 );
121 wfRunHooks( 'LogPageLogName', array( &$typeText ) );
122
123 if( isset( $typeText[$type] ) ) {
124 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
125 } else {
126 // Bogus log types? Perhaps an extension was removed.
127 return $type;
128 }
129 }
130
131 /**
132 * @static
133 */
134 function logHeader( $type ) {
135 static $headerText = array(
136 '' => 'alllogstext',
137 'block' => 'blocklogtext',
138 'protect' => 'protectlogtext',
139 'rights' => 'rightslogtext',
140 'delete' => 'dellogpagetext',
141 'upload' => 'uploadlogpagetext',
142 'move' => 'movelogpagetext'
143 );
144 wfRunHooks( 'LogPageLogHeader', array( &$headerText ) );
145
146 return wfMsg( $headerText[$type] );
147 }
148
149 /**
150 * @static
151 */
152 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
153 global $wgLang;
154 static $actions = array(
155 'block/block' => 'blocklogentry',
156 'block/unblock' => 'unblocklogentry',
157 'protect/protect' => 'protectedarticle',
158 'protect/unprotect' => 'unprotectedarticle',
159
160 // TODO: This whole section should be moved to extensions/Makesysop/SpecialMakesysop.php
161 'rights/rights' => 'bureaucratlogentry',
162 'rights/addgroup' => 'addgrouplogentry',
163 'rights/rngroup' => 'renamegrouplogentry',
164 'rights/chgroup' => 'changegrouplogentry',
165
166 'delete/delete' => 'deletedarticle',
167 'delete/restore' => 'undeletedarticle',
168 'upload/upload' => 'uploadedimage',
169 'upload/revert' => 'uploadedimage',
170 'move/move' => '1movedto2',
171 'move/move_redir' => '1movedto2_redir'
172 );
173 wfRunHooks( 'LogPageActionText', array( &$actions ) );
174
175 $key = "$type/$action";
176 if( isset( $actions[$key] ) ) {
177 if( is_null( $title ) ) {
178 $rv=wfMsg( $actions[$key] );
179 } else {
180 if( $skin ) {
181
182 switch( $type ) {
183 case 'move':
184 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
185 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), $params[0] );
186 break;
187 case 'block':
188 if( substr( $title->getText(), 0, 1 ) == '#' ) {
189 $titleLink = $title->getText();
190 } else {
191 $titleLink = $skin->makeLinkObj( $title, $title->getText() );
192 $titleLink .= ' (' . $skin->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions/' . urlencode( $title->getDBkey() ) ), wfMsg( 'contribslink' ) ) . ')';
193 }
194 break;
195 default:
196 $titleLink = $skin->makeLinkObj( $title );
197 }
198
199 } else {
200 $titleLink = $title->getPrefixedText();
201 }
202 if( count( $params ) == 0 ) {
203 $rv = wfMsg( $actions[$key], $titleLink );
204 } else {
205 array_unshift( $params, $titleLink );
206 if ( $translate && $key == 'block/block' ) {
207 $params[1] = $wgLang->translateBlockExpiry($params[1]);
208 }
209 $rv = wfMsgReal( $actions[$key], $params, true, false ); // FIXME: use wfMsgForContent() ?
210 }
211 }
212 } else {
213 wfDebug( "LogPage::actionText - unknown action $key\n" );
214 $rv = "$action";
215 }
216 if( $filterWikilinks ) {
217 $rv = str_replace( "[[", "", $rv );
218 $rv = str_replace( "]]", "", $rv );
219 }
220 return $rv;
221 }
222
223 /**
224 * Add a log entry
225 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
226 * @param object &$target A title object.
227 * @param string $comment Description associated
228 * @param array $params Parameters passed later to wfMsg.* functions
229 */
230 function addEntry( $action, &$target, $comment, $params = array() ) {
231 if ( !is_array( $params ) ) {
232 $params = array( $params );
233 }
234
235 $this->action = $action;
236 $this->target =& $target;
237 $this->comment = $comment;
238 $this->params = LogPage::makeParamBlob( $params );
239
240 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
241
242 return $this->saveContent();
243 }
244
245 /**
246 * Create a blob from a parameter array
247 * @static
248 */
249 function makeParamBlob( $params ) {
250 return implode( "\n", $params );
251 }
252
253 /**
254 * Extract a parameter array from a blob
255 * @static
256 */
257 function extractParams( $blob ) {
258 if ( $blob === '' ) {
259 return array();
260 } else {
261 return explode( "\n", $blob );
262 }
263 }
264 }
265
266 ?>