New global config setting $wgMaxTocLevel: Maximum indent level of toc.
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 # Utility class for creating new RC entries
3
4 define( "RC_EDIT", 0);
5 define( "RC_NEW", 1);
6 define( "RC_MOVE", 2);
7 define( "RC_LOG", 3);
8 define( "RC_MOVE_OVER_REDIRECT", 4);
9
10
11 /*
12 mAttributes:
13 rc_timestamp time the entry was made
14 rc_cur_time timestamp on the cur row
15 rc_namespace namespace #
16 rc_title non-prefixed db key
17 rc_type is new entry, used to determine whether updating is necessary
18 rc_minor is minor
19 rc_cur_id id of associated cur entry
20 rc_user user id who made the entry
21 rc_user_text user name who made the entry
22 rc_comment edit summary
23 rc_this_oldid old_id associated with this entry (or zero)
24 rc_last_oldid old_id associated with the entry before this one (or zero)
25 rc_bot is bot, hidden
26 rc_ip IP address of the user in dotted quad notation
27 rc_new obsolete, use rc_type==RC_NEW
28
29 mExtra:
30 prefixedDBkey prefixed db key, used by external app via msg queue
31 lastTimestamp timestamp of previous entry, used in WHERE clause during update
32 lang the interwiki prefix, automatically set in save()
33 */
34
35 class RecentChange
36 {
37 var $mAttribs = array(), $mExtra = array();
38 var $mTitle = false, $mMovedToTitle = false;
39
40 # Factory methods
41
42 /* static */ function newFromRow( $row )
43 {
44 $rc = new RecentChange;
45 $rc->loadFromRow( $row );
46 return $rc;
47 }
48
49 /* static */ function newFromCurRow( $row )
50 {
51 $rc = new RecentChange;
52 $rc->loadFromCurRow( $row );
53 return $rc;
54 }
55
56 # Accessors
57
58 function setAttribs( $attribs )
59 {
60 $this->mAttribs = $attribs;
61 }
62
63 function setExtra( $extra )
64 {
65 $this->mExtra = $extra;
66 }
67
68 function getTitle()
69 {
70 if ( $this->mTitle === false ) {
71 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
72 }
73 return $this->mTitle;
74 }
75
76 function getMovedToTitle()
77 {
78 if ( $this->mMovedToTitle === false ) {
79 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
80 $this->mAttribs['rc_moved_to_title'] );
81 }
82 return $this->mMovedToTitle;
83 }
84
85 # Writes the data in this object to the database
86 function save()
87 {
88 global $wgUseRCQueue, $wgRCQueueID, $wgLocalInterwiki, $wgPutIPinRC;
89 $fname = "RecentChange::save";
90
91 $dbw =& wfGetDB( DB_MASTER );
92 if ( !is_array($this->mExtra) ) {
93 $this->mExtra = array();
94 }
95 $this->mExtra['lang'] = $wgLocalInterwiki;
96
97 if ( !$wgPutIPinRC ) {
98 $this->mAttribs['rc_ip'] = '';
99 }
100
101 # Insert new row
102 $dbw->insertArray( "recentchanges", $this->mAttribs, $fname );
103
104 # Update old rows, if necessary
105 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
106 $oldid = $this->mAttribs['rc_last_oldid'];
107 $ns = $this->mAttribs['rc_namespace'];
108 $title = $this->mAttribs['rc_title'];
109 $lastTime = $this->mExtra['lastTimestamp'];
110 $now = $this->mAttribs['rc_timestamp'];
111 $curId = $this->mAttribs['rc_cur_id'];
112
113 # Update rc_this_oldid for the entries which were current
114 $dbw->updateArray( 'recentchanges',
115 array( /* SET */
116 'rc_this_oldid' => $oldid
117 ), array( /* WHERE */
118 'rc_namespace' => $ns,
119 'rc_title' => $title,
120 'rc_timestamp' => $lastTime
121 ), $fname
122 );
123
124 # Update rc_cur_time
125 $dbw->updateArray( 'recentchanges', array( 'rc_cur_time' => $now ),
126 array( 'rc_cur_id' => $curId ), $fname );
127 }
128
129 # Notify external application
130 if ( $wgUseRCQueue ) {
131 $queue = msg_get_queue( $wgRCQueueID );
132 if (!msg_send( $queue, array_merge( $this->mAttribs, 1, $this->mExtra ),
133 true, false, $error ))
134 {
135 wfDebug( "Error sending message to RC queue, code $error\n" );
136 }
137 }
138 }
139
140 # Makes an entry in the database corresponding to an edit
141 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
142 $oldId, $lastTimestamp, $bot = "default", $ip = '' )
143 {
144 if ( $bot == "default " ) {
145 $bot = $user->isBot();
146 }
147
148 if ( !$ip ) {
149 global $wgIP;
150 $ip = empty( $wgIP ) ? '' : $wgIP;
151 }
152
153 $rc = new RecentChange;
154 $rc->mAttribs = array(
155 'rc_timestamp' => $timestamp,
156 'rc_cur_time' => $timestamp,
157 'rc_namespace' => $title->getNamespace(),
158 'rc_title' => $title->getDBkey(),
159 'rc_type' => RC_EDIT,
160 'rc_minor' => $minor ? 1 : 0,
161 'rc_cur_id' => $title->getArticleID(),
162 'rc_user' => $user->getID(),
163 'rc_user_text' => $user->getName(),
164 'rc_comment' => $comment,
165 'rc_this_oldid' => 0,
166 'rc_last_oldid' => $oldId,
167 'rc_bot' => $bot ? 1 : 0,
168 'rc_moved_to_ns' => 0,
169 'rc_moved_to_title' => '',
170 'rc_ip' => $ip,
171 'rc_new' => 0 # obsolete
172 );
173
174 $rc->mExtra = array(
175 'prefixedDBkey' => $title->getPrefixedDBkey(),
176 'lastTimestamp' => $lastTimestamp
177 );
178 $rc->save();
179 }
180
181 # Makes an entry in the database corresponding to page creation
182 # Note: the title object must be loaded with the new id using resetArticleID()
183 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default", $ip='' )
184 {
185 if ( !$ip ) {
186 global $wgIP;
187 $ip = empty( $wgIP ) ? '' : $wgIP;
188 }
189 if ( $bot == "default" ) {
190 $bot = $user->isBot();
191 }
192
193 $rc = new RecentChange;
194 $rc->mAttribs = array(
195 'rc_timestamp' => $timestamp,
196 'rc_cur_time' => $timestamp,
197 'rc_namespace' => $title->getNamespace(),
198 'rc_title' => $title->getDBkey(),
199 'rc_type' => RC_NEW,
200 'rc_minor' => $minor ? 1 : 0,
201 'rc_cur_id' => $title->getArticleID(),
202 'rc_user' => $user->getID(),
203 'rc_user_text' => $user->getName(),
204 'rc_comment' => $comment,
205 'rc_this_oldid' => 0,
206 'rc_last_oldid' => 0,
207 'rc_bot' => $bot ? 1 : 0,
208 'rc_moved_to_ns' => 0,
209 'rc_moved_to_title' => '',
210 'rc_ip' => $ip,
211 'rc_new' => 1 # obsolete
212 );
213
214 $rc->mExtra = array(
215 'prefixedDBkey' => $title->getPrefixedDBkey(),
216 'lastTimestamp' => 0
217 );
218 $rc->save();
219 }
220
221 # Makes an entry in the database corresponding to a rename
222 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
223 {
224 if ( !$ip ) {
225 global $wgIP;
226 $ip = empty( $wgIP ) ? '' : $wgIP;
227 }
228 $rc = new RecentChange;
229 $rc->mAttribs = array(
230 'rc_timestamp' => $timestamp,
231 'rc_cur_time' => $timestamp,
232 'rc_namespace' => $oldTitle->getNamespace(),
233 'rc_title' => $oldTitle->getDBkey(),
234 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
235 'rc_minor' => 0,
236 'rc_cur_id' => $oldTitle->getArticleID(),
237 'rc_user' => $user->getID(),
238 'rc_user_text' => $user->getName(),
239 'rc_comment' => $comment,
240 'rc_this_oldid' => 0,
241 'rc_last_oldid' => 0,
242 'rc_bot' => $user->isBot() ? 1 : 0,
243 'rc_moved_to_ns' => $newTitle->getNamespace(),
244 'rc_moved_to_title' => $newTitle->getDBkey(),
245 'rc_ip' => $ip,
246 'rc_new' => 0 # obsolete
247 );
248
249 $rc->mExtra = array(
250 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
251 'lastTimestamp' => 0,
252 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
253 );
254 $rc->save();
255 }
256
257 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
258 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
259 }
260
261 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
262 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip='', true );
263 }
264
265 # A log entry is different to an edit in that previous revisions are
266 # not kept
267 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
268 {
269 if ( !$ip ) {
270 global $wgIP;
271 $ip = empty( $wgIP ) ? '' : $wgIP;
272 }
273 $rc = new RecentChange;
274 $rc->mAttribs = array(
275 'rc_timestamp' => $timestamp,
276 'rc_cur_time' => $timestamp,
277 'rc_namespace' => $title->getNamespace(),
278 'rc_title' => $title->getDBkey(),
279 'rc_type' => RC_LOG,
280 'rc_minor' => 0,
281 'rc_cur_id' => $title->getArticleID(),
282 'rc_user' => $user->getID(),
283 'rc_user_text' => $user->getName(),
284 'rc_comment' => $comment,
285 'rc_this_oldid' => 0,
286 'rc_last_oldid' => 0,
287 'rc_bot' => 0,
288 'rc_moved_to_ns' => 0,
289 'rc_moved_to_title' => '',
290 'rc_ip' => $ip,
291 'rc_new' => 0 # obsolete
292 );
293 $rc->mExtra = array(
294 'prefixedDBkey' => $title->getPrefixedDBkey(),
295 'lastTimestamp' => 0
296 );
297 $rc->save();
298 }
299
300 # Initialises the members of this object from a mysql row object
301 function loadFromRow( $row )
302 {
303 $this->mAttribs = get_object_vars( $row );
304 $this->mExtra = array();
305 }
306
307 # Makes a pseudo-RC entry from a cur row, for watchlists and things
308 function loadFromCurRow( $row )
309 {
310 $this->mAttribs = array(
311 "rc_timestamp" => $row->cur_timestamp,
312 "rc_cur_time" => $row->cur_timestamp,
313 "rc_user" => $row->cur_user,
314 "rc_user_text" => $row->cur_user_text,
315 "rc_namespace" => $row->cur_namespace,
316 "rc_title" => $row->cur_title,
317 "rc_comment" => $row->cur_comment,
318 "rc_minor" => !!$row->cur_minor_edit,
319 "rc_type" => $row->cur_is_new ? RC_NEW : RC_EDIT,
320 "rc_cur_id" => $row->cur_id,
321 'rc_this_oldid' => 0,
322 'rc_last_oldid' => 0,
323 'rc_bot' => 0,
324 'rc_moved_to_ns' => 0,
325 'rc_moved_to_title' => '',
326 'rc_ip' => '',
327 'rc_new' => $row->cur_is_new # obsolete
328 );
329
330 $this->mExtra = array();
331 }
332
333
334 # Gets the end part of the diff URL assoicated with this object
335 # Blank if no diff link should be displayed
336 function diffLinkTrail( $forceCur )
337 {
338 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
339 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
340 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
341 if ( $forceCur ) {
342 $trail .= "&diff=0" ;
343 } else {
344 $trail .= "&diff=" . (int)($this->mAttribs['rc_this_oldid']);
345 }
346 } else {
347 $trail = "";
348 }
349 return $trail;
350 }
351 }
352 ?>