add Last-Modified header
[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 if ( !is_array($this->mExtra) ) {
92 $this->mExtra = array();
93 }
94 $this->mExtra['lang'] = $wgLocalInterwiki;
95
96 if ( !$wgPutIPinRC ) {
97 $this->mAttribs['rc_ip'] = '';
98 }
99
100 # Insert new row
101 wfInsertArray( "recentchanges", $this->mAttribs, $fname );
102
103 # Update old rows, if necessary
104 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
105 $oldid = $this->mAttribs['rc_last_oldid'];
106 $ns = $this->mAttribs['rc_namespace'];
107 $title = wfStrencode($this->mAttribs['rc_title']);
108 $lastTime = $this->mExtra['lastTimestamp'];
109 $now = $this->mAttribs['rc_timestamp'];
110 $curId = $this->mAttribs['rc_cur_id'];
111
112 # Update rc_this_oldid for the entries which were current
113 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
114 "WHERE rc_namespace=$ns AND rc_title='$title' AND rc_timestamp='$lastTime'";
115 wfQuery( $sql, DB_WRITE, $fname );
116
117 # Update rc_cur_time
118 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
119 "WHERE rc_cur_id=" . $curId;
120 wfQuery( $sql, DB_WRITE, $fname );
121 }
122
123 # Notify external application
124 if ( $wgUseRCQueue ) {
125 $queue = msg_get_queue( $wgRCQueueID );
126 if (!msg_send( $queue, array_merge( $this->mAttribs, 1, $this->mExtra ),
127 true, false, $error ))
128 {
129 wfDebug( "Error sending message to RC queue, code $error\n" );
130 }
131 }
132 }
133
134 # Makes an entry in the database corresponding to an edit
135 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
136 $oldId, $lastTimestamp, $bot = "default", $ip = '' )
137 {
138 if ( $bot == "default " ) {
139 $bot = $user->isBot();
140 }
141
142 if ( !$ip ) {
143 global $wgIP;
144 $ip = empty( $wgIP ) ? '' : $wgIP;
145 }
146
147 $rc = new RecentChange;
148 $rc->mAttribs = array(
149 'rc_timestamp' => $timestamp,
150 'rc_cur_time' => $timestamp,
151 'rc_namespace' => $title->getNamespace(),
152 'rc_title' => $title->getDBkey(),
153 'rc_type' => RC_EDIT,
154 'rc_minor' => $minor ? 1 : 0,
155 'rc_cur_id' => $title->getArticleID(),
156 'rc_user' => $user->getID(),
157 'rc_user_text' => $user->getName(),
158 'rc_comment' => $comment,
159 'rc_this_oldid' => 0,
160 'rc_last_oldid' => $oldId,
161 'rc_bot' => $bot ? 1 : 0,
162 'rc_moved_to_ns' => 0,
163 'rc_moved_to_title' => '',
164 'rc_ip' => $ip,
165 'rc_new' => 0 # obsolete
166 );
167
168 $rc->mExtra = array(
169 'prefixedDBkey' => $title->getPrefixedDBkey(),
170 'lastTimestamp' => $lastTimestamp
171 );
172 $rc->save();
173 }
174
175 # Makes an entry in the database corresponding to page creation
176 # Note: the title object must be loaded with the new id using resetArticleID()
177 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default", $ip='' )
178 {
179 if ( !$ip ) {
180 global $wgIP;
181 $ip = empty( $wgIP ) ? '' : $wgIP;
182 }
183 if ( $bot == "default" ) {
184 $bot = $user->isBot();
185 }
186
187 $rc = new RecentChange;
188 $rc->mAttribs = array(
189 'rc_timestamp' => $timestamp,
190 'rc_cur_time' => $timestamp,
191 'rc_namespace' => $title->getNamespace(),
192 'rc_title' => $title->getDBkey(),
193 'rc_type' => RC_NEW,
194 'rc_minor' => $minor ? 1 : 0,
195 'rc_cur_id' => $title->getArticleID(),
196 'rc_user' => $user->getID(),
197 'rc_user_text' => $user->getName(),
198 'rc_comment' => $comment,
199 'rc_this_oldid' => 0,
200 'rc_last_oldid' => 0,
201 'rc_bot' => $bot ? 1 : 0,
202 'rc_moved_to_ns' => 0,
203 'rc_moved_to_title' => '',
204 'rc_ip' => $ip,
205 'rc_new' => 1 # obsolete
206 );
207
208 $rc->mExtra = array(
209 'prefixedDBkey' => $title->getPrefixedDBkey(),
210 'lastTimestamp' => 0
211 );
212 $rc->save();
213 }
214
215 # Makes an entry in the database corresponding to a rename
216 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
217 {
218 if ( !$ip ) {
219 global $wgIP;
220 $ip = empty( $wgIP ) ? '' : $wgIP;
221 }
222 $rc = new RecentChange;
223 $rc->mAttribs = array(
224 'rc_timestamp' => $timestamp,
225 'rc_cur_time' => $timestamp,
226 'rc_namespace' => $oldTitle->getNamespace(),
227 'rc_title' => $oldTitle->getDBkey(),
228 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
229 'rc_minor' => 0,
230 'rc_cur_id' => $oldTitle->getArticleID(),
231 'rc_user' => $user->getID(),
232 'rc_user_text' => $user->getName(),
233 'rc_comment' => $comment,
234 'rc_this_oldid' => 0,
235 'rc_last_oldid' => 0,
236 'rc_bot' => $user->isBot() ? 1 : 0,
237 'rc_moved_to_ns' => $newTitle->getNamespace(),
238 'rc_moved_to_title' => $newTitle->getDBkey(),
239 'rc_ip' => $ip,
240 'rc_new' => 0 # obsolete
241 );
242
243 $rc->mExtra = array(
244 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
245 'lastTimestamp' => 0,
246 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
247 );
248 $rc->save();
249 }
250
251 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
252 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
253 }
254
255 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
256 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip='', true );
257 }
258
259 # A log entry is different to an edit in that previous revisions are
260 # not kept
261 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
262 {
263 if ( !$ip ) {
264 global $wgIP;
265 $ip = empty( $wgIP ) ? '' : $wgIP;
266 }
267 $rc = new RecentChange;
268 $rc->mAttribs = array(
269 'rc_timestamp' => $timestamp,
270 'rc_cur_time' => $timestamp,
271 'rc_namespace' => $title->getNamespace(),
272 'rc_title' => $title->getDBkey(),
273 'rc_type' => RC_LOG,
274 'rc_minor' => 0,
275 'rc_cur_id' => $title->getArticleID(),
276 'rc_user' => $user->getID(),
277 'rc_user_text' => $user->getName(),
278 'rc_comment' => $comment,
279 'rc_this_oldid' => 0,
280 'rc_last_oldid' => 0,
281 'rc_bot' => 0,
282 'rc_moved_to_ns' => 0,
283 'rc_moved_to_title' => '',
284 'rc_ip' => $ip,
285 'rc_new' => 0 # obsolete
286 );
287 $rc->mExtra = array(
288 'prefixedDBkey' => $title->getPrefixedDBkey(),
289 'lastTimestamp' => 0
290 );
291 $rc->save();
292 }
293
294 # Initialises the members of this object from a mysql row object
295 function loadFromRow( $row )
296 {
297 $this->mAttribs = get_object_vars( $row );
298 $this->mExtra = array();
299 }
300
301 # Makes a pseudo-RC entry from a cur row, for watchlists and things
302 function loadFromCurRow( $row )
303 {
304 $this->mAttribs = array(
305 "rc_timestamp" => $row->cur_timestamp,
306 "rc_cur_time" => $row->cur_timestamp,
307 "rc_user" => $row->cur_user,
308 "rc_user_text" => $row->cur_user_text,
309 "rc_namespace" => $row->cur_namespace,
310 "rc_title" => $row->cur_title,
311 "rc_comment" => $row->cur_comment,
312 "rc_minor" => !!$row->cur_minor_edit,
313 "rc_type" => $row->cur_is_new ? RC_NEW : RC_EDIT,
314 "rc_cur_id" => $row->cur_id,
315 'rc_this_oldid' => 0,
316 'rc_last_oldid' => 0,
317 'rc_bot' => 0,
318 'rc_moved_to_ns' => 0,
319 'rc_moved_to_title' => '',
320 'rc_ip' => '',
321 'rc_new' => $row->cur_is_new # obsolete
322 );
323
324 $this->mExtra = array();
325 }
326
327
328 # Gets the end part of the diff URL assoicated with this object
329 # Blank if no diff link should be displayed
330 function diffLinkTrail( $forceCur )
331 {
332 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
333 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
334 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
335 if ( $forceCur ) {
336 $trail .= "&diff=0" ;
337 } else {
338 $trail .= "&diff=" . (int)($this->mAttribs['rc_this_oldid']);
339 }
340 } else {
341 $trail = "";
342 }
343 return $trail;
344 }
345 }
346 ?>