* Added a test for a link with multiple pipes
[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 function LogPage( $type ) {
39 # Type is one of 'block', 'protect', 'rights', 'delete', 'upload', 'move'
40 $this->type = $type;
41 }
42
43 function saveContent() {
44 if( wfReadOnly() ) return;
45
46 global $wgUser;
47 $fname = 'LogPage::saveContent';
48
49 $dbw =& wfGetDB( DB_MASTER );
50 $uid = $wgUser->getID();
51
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
64 );
65
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;
72 }
73
74 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment );
75 }
76 return true;
77 }
78
79 /**
80 * @static
81 */
82 function validTypes() {
83 static $types = array( '', 'block', 'protect', 'rights', 'delete', 'upload', 'move' );
84 return $types;
85 }
86
87 /**
88 * @static
89 */
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' )
99 );
100 return $actions[$type];
101 }
102
103 /**
104 * @static
105 */
106 function isLogType( $type ) {
107 return in_array( $type, LogPage::validTypes() );
108 }
109
110 /**
111 * @static
112 */
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'
122 );
123 return str_replace( '_', ' ', wfMsg( $typeText[$type] ) );
124 }
125
126 /**
127 * @static
128 */
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'
138 );
139 return wfMsg( $headerText[$type] );
140 }
141
142 /**
143 * @static
144 */
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'
161 );
162 $key = "$type/$action";
163 if( isset( $actions[$key] ) ) {
164 if( is_null( $title ) ) {
165 $rv=wfMsgForContent( $actions[$key] );
166 }
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 );
176 }
177 } else {
178 $titleLink = $title->getPrefixedText();
179 }
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 );
185 }
186 }
187 } else {
188 wfDebug( "LogPage::actionText - unknown action $key\n" );
189 $rv="$action $titleLink";
190 }
191 if($filterWikilinks) {
192 $rv=str_replace("[[","",$rv);
193 $rv=str_replace("]]","",$rv);
194 }
195 return $rv;
196 }
197
198 /**
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
204 */
205 function addEntry( $action, &$target, $comment, $params = array() ) {
206 if ( !is_array( $params ) ) {
207 $params = array( $params );
208 }
209
210 $this->action = $action;
211 $this->target =& $target;
212 $this->comment = $comment;
213 $this->params = LogPage::makeParamBlob( $params );
214
215 $this->actionText = LogPage::actionText( $this->type, $action,
216 $target, NULL, $params );
217
218 return $this->saveContent();
219 }
220
221 /**
222 * Create a blob from a parameter array
223 * @static
224 */
225 function makeParamBlob( $params )
226 {
227 return implode( "\n", $params );
228 }
229
230 /**
231 * Extract a parameter array from a blob
232 * @static
233 */
234 function extractParams( $blob )
235 {
236 if ( $blob === '' ) {
237 return array();
238 } else {
239 return explode( "\n", $blob );
240 }
241 }
242 }
243
244 ?>