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