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