Small tweak to section editing- try to generate a section (toc-)anchor from the edite...
[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_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, $bot = "default" )
130 {
131 if ( $bot == "default " ) {
132 $bot = $user->isBot();
133 }
134
135 $rc = new RecentChange;
136 $rc->mAttribs = array(
137 'rc_timestamp' => $timestamp,
138 'rc_cur_time' => $timestamp,
139 'rc_namespace' => $title->getNamespace(),
140 'rc_title' => $title->getDBkey(),
141 'rc_type' => RC_EDIT,
142 'rc_minor' => $minor ? 1 : 0,
143 'rc_cur_id' => $title->getArticleID(),
144 'rc_user' => $user->getID(),
145 'rc_user_text' => $user->getName(),
146 'rc_comment' => $comment,
147 'rc_this_oldid' => 0,
148 'rc_last_oldid' => $oldId,
149 'rc_bot' => $bot ? 1 : 0,
150 'rc_moved_to_ns' => 0,
151 'rc_moved_to_title' => '',
152 'rc_new' => 0 # obsolete
153 );
154
155 $rc->mExtra = array(
156 'prefixedDBkey' => $title->getPrefixedDBkey(),
157 'lastTimestamp' => $lastTimestamp
158 );
159 $rc->save();
160 }
161
162 # Makes an entry in the database corresponding to page creation
163 # Note: the title object must be loaded with the new id using resetArticleID()
164 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default" )
165 {
166 if ( $bot == "default" ) {
167 $bot = $user->isBot();
168 }
169 $rc = new RecentChange;
170 $rc->mAttribs = array(
171 'rc_timestamp' => $timestamp,
172 'rc_cur_time' => $timestamp,
173 'rc_namespace' => $title->getNamespace(),
174 'rc_title' => $title->getDBkey(),
175 'rc_type' => RC_NEW,
176 'rc_minor' => $minor ? 1 : 0,
177 'rc_cur_id' => $title->getArticleID(),
178 'rc_user' => $user->getID(),
179 'rc_user_text' => $user->getName(),
180 'rc_comment' => $comment,
181 'rc_this_oldid' => 0,
182 'rc_last_oldid' => 0,
183 'rc_bot' => $bot ? 1 : 0,
184 'rc_moved_to_ns' => 0,
185 'rc_moved_to_title' => '',
186 'rc_new' => 1 # obsolete
187 );
188
189 $rc->mExtra = array(
190 'prefixedDBkey' => $title->getPrefixedDBkey(),
191 'lastTimestamp' => 0
192 );
193 $rc->save();
194 }
195
196 # Makes an entry in the database corresponding to a rename
197 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment )
198 {
199 $rc = new RecentChange;
200 $rc->mAttribs = array(
201 'rc_timestamp' => $timestamp,
202 'rc_cur_time' => $timestamp,
203 'rc_namespace' => $oldTitle->getNamespace(),
204 'rc_title' => $oldTitle->getDBkey(),
205 'rc_type' => RC_MOVE,
206 'rc_minor' => 0,
207 'rc_cur_id' => $oldTitle->getArticleID(),
208 'rc_user' => $user->getID(),
209 'rc_user_text' => $user->getName(),
210 'rc_comment' => $comment,
211 'rc_this_oldid' => 0,
212 'rc_last_oldid' => 0,
213 'rc_bot' => $user->isBot() ? 1 : 0,
214 'rc_moved_to_ns' => $newTitle->getNamespace(),
215 'rc_moved_to_title' => $newTitle->getDBkey(),
216 'rc_new' => 0 # obsolete
217 );
218
219 $rc->mExtra = array(
220 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
221 'lastTimestamp' => 0,
222 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
223 );
224 $rc->save();
225 }
226
227 # A log entry is different to an edit in that previous revisions are
228 # not kept
229 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment )
230 {
231 $rc = new RecentChange;
232 $rc->mAttribs = array(
233 'rc_timestamp' => $timestamp,
234 'rc_cur_time' => $timestamp,
235 'rc_namespace' => $title->getNamespace(),
236 'rc_title' => $title->getDBkey(),
237 'rc_type' => RC_LOG,
238 'rc_minor' => 0,
239 'rc_cur_id' => $title->getArticleID(),
240 'rc_user' => $user->getID(),
241 'rc_user_text' => $user->getName(),
242 'rc_comment' => $comment,
243 'rc_this_oldid' => 0,
244 'rc_last_oldid' => 0,
245 'rc_bot' => 0,
246 'rc_moved_to_ns' => 0,
247 'rc_moved_to_title' => '',
248 'rc_new' => 0 # obsolete
249 );
250 $rc->mExtra = array(
251 'prefixedDBkey' => $title->getPrefixedDBkey(),
252 'lastTimestamp' => 0
253 );
254 $rc->save();
255 }
256
257 # Initialises the members of this object from a mysql row object
258 function loadFromRow( $row )
259 {
260 $this->mAttribs = get_object_vars( $row );
261 $this->mExtra = array();
262 }
263
264 # Makes a pseudo-RC entry from a cur row, for watchlists and things
265 function loadFromCurRow( $row )
266 {
267 $this->mAttribs = array(
268 "rc_timestamp" => $row->cur_timestamp,
269 "rc_cur_time" => $row->cur_timestamp,
270 "rc_user" => $row->cur_user,
271 "rc_user_text" => $row->cur_user_text,
272 "rc_namespace" => $row->cur_namespace,
273 "rc_title" => $row->cur_title,
274 "rc_comment" => $row->cur_comment,
275 "rc_minor" => !!$row->cur_minor_edit,
276 "rc_type" => $row->cur_is_new ? RC_NEW : RC_EDIT,
277 "rc_cur_id" => $row->cur_id,
278 'rc_this_oldid' => 0,
279 'rc_last_oldid' => 0,
280 'rc_bot' => 0,
281 'rc_moved_to_ns' => 0,
282 'rc_moved_to_title' => '',
283 'rc_new' => $row->cur_is_new # obsolete
284 );
285
286 $this->mExtra = array();
287 }
288
289
290 # Gets the end part of the diff URL assoicated with this object
291 # Blank if no diff link should be displayed
292 function diffLinkTrail( $forceCur )
293 {
294 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
295 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
296 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
297 if ( $forceCur ) {
298 $trail .= "&diff=0" ;
299 } else {
300 $trail .= "&diff=" . (int)($this->mAttribs['rc_this_oldid']);
301 }
302 } else {
303 $trail = "";
304 }
305 return $trail;
306 }
307 }
308 ?>