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