$wgUseBetterLinksUpdate no longer needed, removed, doDumbUpdate removed
[lhc/web/wiklou.git] / includes / Article.php
1 <?
2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
4
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
7
8 /* CHECK MERGE @@@
9 TEST THIS @@@
10
11 * s/\$wgTitle/\$this->mTitle/ performed, many replacements
12 * mTitle variable added to class
13 */
14
15 include_once( "CacheManager.php" );
16
17 class Article {
18 /* private */ var $mContent, $mContentLoaded;
19 /* private */ var $mUser, $mTimestamp, $mUserText;
20 /* private */ var $mCounter, $mComment, $mCountAdjustment;
21 /* private */ var $mMinorEdit, $mRedirectedFrom;
22 /* private */ var $mTouched, $mFileCache, $mTitle;
23
24 function Article( &$title ) {
25 $this->mTitle =& $title;
26 $this->clear();
27 }
28
29 /* private */ function clear()
30 {
31 $this->mContentLoaded = false;
32 $this->mUser = $this->mCounter = -1; # Not loaded
33 $this->mRedirectedFrom = $this->mUserText =
34 $this->mTimestamp = $this->mComment = $this->mFileCache = "";
35 $this->mCountAdjustment = 0;
36 $this->mTouched = "19700101000000";
37 }
38
39 # Note that getContent/loadContent may follow redirects if
40 # not told otherwise, and so may cause a change to mTitle.
41
42 function getContent( $noredir = false )
43 {
44 global $action,$section,$count; # From query string
45 $fname = "Article::getContent";
46 wfProfileIn( $fname );
47
48 if ( 0 == $this->getID() ) {
49 if ( "edit" == $action ) {
50 wfProfileOut( $fname );
51 return ""; # was "newarticletext", now moved above the box)
52 }
53 wfProfileOut( $fname );
54 return wfMsg( "noarticletext" );
55 } else {
56 $this->loadContent( $noredir );
57
58 if(
59 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
60 ( $this->mTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
61 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle->getText()) &&
62 $action=="view"
63 )
64 {
65 wfProfileOut( $fname );
66 return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
67 else {
68 if($action=="edit") {
69 if($section!="") {
70 if($section=="new") {
71 wfProfileOut( $fname );
72 return "";
73 }
74
75 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
76 $this->mContent, -1,
77 PREG_SPLIT_DELIM_CAPTURE);
78 if($section==0) {
79 wfProfileOut( $fname );
80 return trim($secs[0]);
81 } else {
82 wfProfileOut( $fname );
83 return trim($secs[$section*2-1] . $secs[$section*2]);
84 }
85 }
86 }
87 wfProfileOut( $fname );
88 return $this->mContent;
89 }
90 }
91 }
92
93 function loadContent( $noredir = false )
94 {
95 global $wgOut, $wgMwRedir;
96 global $oldid, $redirect; # From query
97
98 if ( $this->mContentLoaded ) return;
99 $fname = "Article::loadContent";
100
101 # Pre-fill content with error message so that if something
102 # fails we'll have something telling us what we intended.
103
104 $t = $this->mTitle->getPrefixedText();
105 if ( isset( $oldid ) ) {
106 $oldid = IntVal( $oldid );
107 $t .= ",oldid={$oldid}";
108 }
109 if ( isset( $redirect ) ) {
110 $redirect = ($redirect == "no") ? "no" : "yes";
111 $t .= ",redirect={$redirect}";
112 }
113 $this->mContent = wfMsg( "missingarticle", $t );
114
115 if ( ! $oldid ) { # Retrieve current version
116 $id = $this->getID();
117 if ( 0 == $id ) return;
118
119 $sql = "SELECT " .
120 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
121 "FROM cur WHERE cur_id={$id}";
122 wfDebug( "$sql\n" );
123 $res = wfQuery( $sql, DB_READ, $fname );
124 if ( 0 == wfNumRows( $res ) ) {
125 return;
126 }
127
128 $s = wfFetchObject( $res );
129 # If we got a redirect, follow it (unless we've been told
130 # not to by either the function parameter or the query
131 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
132 ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
133 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
134 $s->cur_text, $m ) ) {
135 $rt = Title::newFromText( $m[1] );
136 if( $rt ) {
137 # Gotta hand redirects to special pages differently:
138 # Fill the HTTP response "Location" header and ignore
139 # the rest of the page we're on.
140
141 if ( $rt->getInterwiki() != "" ) {
142 $wgOut->redirect( $rt->getFullURL() ) ;
143 return;
144 }
145 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
146 $wgOut->redirect( wfLocalUrl(
147 $rt->getPrefixedURL() ) );
148 return;
149 }
150 $rid = $rt->getArticleID();
151 if ( 0 != $rid ) {
152 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
153 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
154 $res = wfQuery( $sql, DB_READ, $fname );
155
156 if ( 0 != wfNumRows( $res ) ) {
157 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
158 $this->mTitle = $rt;
159 $s = wfFetchObject( $res );
160 }
161 }
162 }
163 }
164 }
165
166 $this->mContent = $s->cur_text;
167 $this->mUser = $s->cur_user;
168 $this->mCounter = $s->cur_counter;
169 $this->mTimestamp = $s->cur_timestamp;
170 $this->mTouched = $s->cur_touched;
171 $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
172 $this->mTitle->mRestrictionsLoaded = true;
173 wfFreeResult( $res );
174 } else { # oldid set, retrieve historical version
175 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
176 "WHERE old_id={$oldid}";
177 $res = wfQuery( $sql, DB_READ, $fname );
178 if ( 0 == wfNumRows( $res ) ) { return; }
179
180 $s = wfFetchObject( $res );
181 $this->mContent = $s->old_text;
182 $this->mUser = $s->old_user;
183 $this->mCounter = 0;
184 $this->mTimestamp = $s->old_timestamp;
185 wfFreeResult( $res );
186 }
187 $this->mContentLoaded = true;
188 }
189
190 function getID() {
191 if( $this->mTitle ) {
192 return $this->mTitle->getArticleID();
193 } else {
194 return 0;
195 }
196 }
197
198 function getCount()
199 {
200 if ( -1 == $this->mCounter ) {
201 $id = $this->getID();
202 $this->mCounter = wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
203 }
204 return $this->mCounter;
205 }
206
207 # Would the given text make this article a "good" article (i.e.,
208 # suitable for including in the article count)?
209
210 function isCountable( $text )
211 {
212 global $wgUseCommaCount, $wgMwRedir;
213
214 if ( 0 != $this->mTitle->getNamespace() ) { return 0; }
215 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
216 $token = ($wgUseCommaCount ? "," : "[[" );
217 if ( false === strstr( $text, $token ) ) { return 0; }
218 return 1;
219 }
220
221 # Load the field related to the last edit time of the article.
222 # This isn't necessary for all uses, so it's only done if needed.
223
224 /* private */ function loadLastEdit()
225 {
226 global $wgOut;
227 if ( -1 != $this->mUser ) return;
228
229 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
230 "cur_comment,cur_minor_edit FROM cur WHERE " .
231 "cur_id=" . $this->getID();
232 $res = wfQuery( $sql, DB_READ, "Article::loadLastEdit" );
233
234 if ( wfNumRows( $res ) > 0 ) {
235 $s = wfFetchObject( $res );
236 $this->mUser = $s->cur_user;
237 $this->mUserText = $s->cur_user_text;
238 $this->mTimestamp = $s->cur_timestamp;
239 $this->mComment = $s->cur_comment;
240 $this->mMinorEdit = $s->cur_minor_edit;
241 }
242 }
243
244 function getTimestamp()
245 {
246 $this->loadLastEdit();
247 return $this->mTimestamp;
248 }
249
250 function getUser()
251 {
252 $this->loadLastEdit();
253 return $this->mUser;
254 }
255
256 function getUserText()
257 {
258 $this->loadLastEdit();
259 return $this->mUserText;
260 }
261
262 function getComment()
263 {
264 $this->loadLastEdit();
265 return $this->mComment;
266 }
267
268 function getMinorEdit()
269 {
270 $this->loadLastEdit();
271 return $this->mMinorEdit;
272 }
273
274 # This is the default action of the script: just view the page of
275 # the given title.
276
277 function view()
278 {
279 global $wgUser, $wgOut, $wgLang;
280 global $oldid, $diff; # From query
281 global $wgLinkCache, $IP;
282 $fname = "Article::view";
283 wfProfileIn( $fname );
284
285 $wgOut->setArticleFlag( true );
286 $wgOut->setRobotpolicy( "index,follow" );
287
288 # If we got diff and oldid in the query, we want to see a
289 # diff page instead of the article.
290
291 if ( isset( $diff ) ) {
292 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
293 $de = new DifferenceEngine( $oldid, $diff );
294 $de->showDiffPage();
295 wfProfileOut( $fname );
296 return;
297 }
298
299 if ( !isset( $oldid ) and $this->checkTouched() ) {
300 if( $wgOut->checkLastModified( $this->mTouched ) ){
301 return;
302 } else if ( $this->tryFileCache() ) {
303 # tell wgOut that output is taken care of
304 $wgOut->disable();
305 $this->viewUpdates();
306 return;
307 }
308 }
309
310 $text = $this->getContent(); # May change mTitle
311 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
312 $wgOut->setHTMLTitle( $this->mTitle->getPrefixedText() .
313 " - " . wfMsg( "wikititlesuffix" ) );
314
315 # We're looking at an old revision
316
317 if ( $oldid ) {
318 $this->setOldSubtitle();
319 $wgOut->setRobotpolicy( "noindex,follow" );
320 }
321 if ( "" != $this->mRedirectedFrom ) {
322 $sk = $wgUser->getSkin();
323 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
324 "redirect=no" );
325 $s = wfMsg( "redirectedfrom", $redir );
326 $wgOut->setSubtitle( $s );
327 }
328
329 $wgLinkCache->preFill( $this->mTitle );
330 $wgOut->addWikiText( $text );
331
332 $this->viewUpdates();
333 wfProfileOut( $fname );
334 }
335
336 # Theoretically we could defer these whole insert and update
337 # functions for after display, but that's taking a big leap
338 # of faith, and we want to be able to report database
339 # errors at some point.
340
341 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
342 {
343 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
344 global $wgEnablePersistentLC;
345
346 $fname = "Article::insertNewArticle";
347
348 $this->mCountAdjustment = $this->isCountable( $text );
349
350 $ns = $this->mTitle->getNamespace();
351 $ttl = $this->mTitle->getDBkey();
352 $text = $this->preSaveTransform( $text );
353 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
354 else { $redir = 0; }
355
356 $now = wfTimestampNow();
357 $won = wfInvertTimestamp( $now );
358 wfSeedRandom();
359 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
360 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
361 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
362 "cur_restrictions,cur_user_text,cur_is_redirect," .
363 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
364 wfStrencode( $text ) . "', '" .
365 wfStrencode( $summary ) . "', '" .
366 $wgUser->getID() . "', '{$now}', " .
367 ( $isminor ? 1 : 0 ) . ", 0, '', '" .
368 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
369 $res = wfQuery( $sql, DB_WRITE, $fname );
370
371 $newid = wfInsertId();
372 $this->mTitle->resetArticleID( $newid );
373
374 if ( $wgEnablePersistentLC ) {
375 // Purge related entries in links cache on new page, to heal broken links
376 $ptitle = wfStrencode( $ttl );
377 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
378 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE);
379 }
380
381 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
382 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
383 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
384 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
385 ( $isminor ? 1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
386 wfStrencode( $wgUser->getName() ) . "','" .
387 wfStrencode( $summary ) . "',0,0," .
388 ( $wgUser->isBot() ? 1 : 0 ) . ")";
389 wfQuery( $sql, DB_WRITE, $fname );
390 if ($watchthis) {
391 if(!$this->mTitle->userIsWatching()) $this->watch();
392 } else {
393 if ( $this->mTitle->userIsWatching() ) {
394 $this->unwatch();
395 }
396 }
397
398 # The talk page isn't in the regular link tables, so we need to update manually:
399 $talkns = $ns ^ 1; # talk -> normal; normal -> talk
400 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
401 wfQuery( $sql, DB_WRITE );
402
403 $this->showArticle( $text, wfMsg( "newarticle" ) );
404 }
405
406 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
407 {
408 global $wgOut, $wgUser, $wgLinkCache;
409 global $wgDBtransactions, $wgMwRedir;
410 $fname = "Article::updateArticle";
411
412 $this->loadLastEdit();
413
414 // insert updated section into old text if we have only edited part
415 // of the article
416 if ($section != "") {
417 $oldtext=$this->getContent();
418 if($section=="new") {
419 if($summary) $subject="== {$summary} ==\n\n";
420 $text=$oldtext."\n\n".$subject.$text;
421 } else {
422 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
423 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
424 $secs[$section*2]=$text."\n\n"; // replace with edited
425 if($section) { $secs[$section*2-1]=""; } // erase old headline
426 $text=join("",$secs);
427 }
428 }
429 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
430 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
431 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
432 $redir = 1;
433 $text = $m[1] . "\n"; # Remove all content but redirect
434 }
435 else { $redir = 0; }
436
437 $text = $this->preSaveTransform( $text );
438
439 # Update article, but only if changed.
440
441 if( $wgDBtransactions ) {
442 $sql = "BEGIN";
443 wfQuery( $sql, DB_WRITE );
444 }
445 $oldtext = $this->getContent( true );
446
447 if ( 0 != strcmp( $text, $oldtext ) ) {
448 $this->mCountAdjustment = $this->isCountable( $text )
449 - $this->isCountable( $oldtext );
450
451 $now = wfTimestampNow();
452 $won = wfInvertTimestamp( $now );
453 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
454 "',cur_comment='" . wfStrencode( $summary ) .
455 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
456 ",cur_timestamp='{$now}',cur_user_text='" .
457 wfStrencode( $wgUser->getName() ) .
458 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
459 "WHERE cur_id=" . $this->getID() .
460 " AND cur_timestamp='" . $this->getTimestamp() . "'";
461 $res = wfQuery( $sql, DB_WRITE, $fname );
462
463 if( wfAffectedRows() == 0 ) {
464 /* Belated edit conflict! Run away!! */
465 return false;
466 }
467
468 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
469 "old_comment,old_user,old_user_text,old_timestamp," .
470 "old_minor_edit,inverse_timestamp) VALUES (" .
471 $this->mTitle->getNamespace() . ", '" .
472 wfStrencode( $this->mTitle->getDBkey() ) . "', '" .
473 wfStrencode( $oldtext ) . "', '" .
474 wfStrencode( $this->getComment() ) . "', " .
475 $this->getUser() . ", '" .
476 wfStrencode( $this->getUserText() ) . "', '" .
477 $this->getTimestamp() . "', " . $me1 . ", '" .
478 wfInvertTimestamp( $this->getTimestamp() ) . "')";
479 $res = wfQuery( $sql, DB_WRITE, $fname );
480 $oldid = wfInsertID( $res );
481
482 $bot = (int)($wgUser->isBot() || $forceBot);
483
484 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
485 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
486 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
487 "'{$now}','{$now}'," . $this->mTitle->getNamespace() . ",'" .
488 wfStrencode( $this->mTitle->getDBkey() ) . "',0,{$me2}," .
489 "$bot," . $this->getID() . "," . $wgUser->getID() . ",'" .
490 wfStrencode( $wgUser->getName() ) . "','" .
491 wfStrencode( $summary ) . "',0,{$oldid})";
492 wfQuery( $sql, DB_WRITE, $fname );
493
494 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
495 "WHERE rc_namespace=" . $this->mTitle->getNamespace() . " AND " .
496 "rc_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' AND " .
497 "rc_timestamp='" . $this->getTimestamp() . "'";
498 wfQuery( $sql, DB_WRITE, $fname );
499
500 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
501 "WHERE rc_cur_id=" . $this->getID();
502 wfQuery( $sql, DB_WRITE, $fname );
503
504 global $wgEnablePersistentLC;
505 if ( $wgEnablePersistentLC ) {
506 // Purge link cache for this page
507 $pageid=$this->getID();
508 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE);
509 }
510 }
511
512 if( $wgDBtransactions ) {
513 $sql = "COMMIT";
514 wfQuery( $sql, DB_WRITE );
515 }
516
517 if ($watchthis) {
518 if (!$this->mTitle->userIsWatching()) $this->watch();
519 } else {
520 if ( $this->mTitle->userIsWatching() ) {
521 $this->unwatch();
522 }
523 }
524
525 $this->showArticle( $text, wfMsg( "updated" ) );
526 return true;
527 }
528
529 # After we've either updated or inserted the article, update
530 # the link tables and redirect to the new page.
531
532 function showArticle( $text, $subtitle )
533 {
534 global $wgOut, $wgUser, $wgLinkCache;
535 global $wgMwRedir;
536
537 $wgLinkCache = new LinkCache();
538
539 # Get old version of link table to allow incremental link updates
540 $wgLinkCache->preFill( $this->mTitle );
541 $wgLinkCache->clear();
542
543 # Now update the link cache by parsing the text
544 $wgOut = new OutputPage();
545 $wgOut->addWikiText( $text );
546
547 $this->editUpdates( $text );
548 if( $wgMwRedir->matchStart( $text ) )
549 $r = "redirect=no";
550 else
551 $r = "";
552 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL(), $r ) );
553 }
554
555 # Add this page to my watchlist
556
557 function watch( $add = true )
558 {
559 global $wgUser, $wgOut, $wgLang;
560 global $wgDeferredUpdateList;
561
562 if ( 0 == $wgUser->getID() ) {
563 $wgOut->errorpage( "watchnologin", "watchnologintext" );
564 return;
565 }
566 if ( wfReadOnly() ) {
567 $wgOut->readOnlyPage();
568 return;
569 }
570 if( $add )
571 $wgUser->addWatch( $this->mTitle );
572 else
573 $wgUser->removeWatch( $this->mTitle );
574
575 $wgOut->setPagetitle( wfMsg( $add ? "addedwatch" : "removedwatch" ) );
576 $wgOut->setRobotpolicy( "noindex,follow" );
577
578 $sk = $wgUser->getSkin() ;
579 $link = $sk->makeKnownLink ( $this->mTitle->getPrefixedText() ) ;
580
581 if($add)
582 $text = wfMsg( "addedwatchtext", $link );
583 else
584 $text = wfMsg( "removedwatchtext", $link );
585 $wgOut->addHTML( $text );
586
587 $up = new UserUpdate();
588 array_push( $wgDeferredUpdateList, $up );
589
590 $wgOut->returnToMain( false );
591 }
592
593 function unwatch()
594 {
595 $this->watch( false );
596 }
597
598 function protect( $limit = "sysop" )
599 {
600 global $wgUser, $wgOut;
601
602 if ( ! $wgUser->isSysop() ) {
603 $wgOut->sysopRequired();
604 return;
605 }
606 if ( wfReadOnly() ) {
607 $wgOut->readOnlyPage();
608 return;
609 }
610 $id = $this->mTitle->getArticleID();
611 if ( 0 == $id ) {
612 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
613 return;
614 }
615 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
616 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
617 wfQuery( $sql, DB_WRITE, "Article::protect" );
618
619 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
620 if ( $limit === "" ) {
621 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle->getPrefixedText() ), "" );
622 } else {
623 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle->getPrefixedText() ), "" );
624 }
625 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
626 }
627
628 function unprotect()
629 {
630 return $this->protect( "" );
631 }
632
633 function delete()
634 {
635 global $wgUser, $wgOut, $wgMessageCache;
636 global $wpConfirm, $wpReason, $image, $oldimage;
637
638 # This code desperately needs to be totally rewritten
639
640 if ( ( ! $wgUser->isSysop() ) ) {
641 $wgOut->sysopRequired();
642 return;
643 }
644 if ( wfReadOnly() ) {
645 $wgOut->readOnlyPage();
646 return;
647 }
648
649 # Can't delete cached MediaWiki namespace (i.e. vital messages)
650 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && $wgMessageCache->isCacheable( $this->mTitle->getDBkey() ) ) {
651 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
652 return;
653 }
654
655 # Better double-check that it hasn't been deleted yet!
656 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
657 if ( ( "" == trim( $this->mTitle->getText() ) )
658 or ( $this->mTitle->getArticleId() == 0 ) ) {
659 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
660 return;
661 }
662
663 if ( $_POST["wpConfirm"] ) {
664 $this->doDelete();
665 return;
666 }
667
668 # determine whether this page has earlier revisions
669 # and insert a warning if it does
670 # we select the text because it might be useful below
671 $ns = $this->mTitle->getNamespace();
672 $title = $this->mTitle->getDBkey();
673 $etitle = wfStrencode( $title );
674 $sql = "SELECT old_text FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
675 $res = wfQuery( $sql, DB_READ, $fname );
676 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
677 $skin=$wgUser->getSkin();
678 $wgOut->addHTML("<B>".wfMsg("historywarning"));
679 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
680 }
681
682 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
683 $res=wfQuery($sql, DB_READ, $fname);
684 if( ($s=wfFetchObject($res))) {
685
686 # if this is a mini-text, we can paste part of it into the deletion reason
687
688 #if this is empty, an earlier revision may contain "useful" text
689 if($s->cur_text!="") {
690 $text=$s->cur_text;
691 } else {
692 if($old) {
693 $text=$old->old_text;
694 $blanked=1;
695 }
696
697 }
698
699 $length=strlen($text);
700
701 # this should not happen, since it is not possible to store an empty, new
702 # page. Let's insert a standard text in case it does, though
703 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
704
705
706 if($length < 500 && !$wpReason) {
707
708 # comment field=255, let's grep the first 150 to have some user
709 # space left
710 $text=substr($text,0,150);
711 # let's strip out newlines and HTML tags
712 $text=preg_replace("/\"/","'",$text);
713 $text=preg_replace("/\</","&lt;",$text);
714 $text=preg_replace("/\>/","&gt;",$text);
715 $text=preg_replace("/[\n\r]/","",$text);
716 if(!$blanked) {
717 $wpReason=wfMsg("excontent"). " '".$text;
718 } else {
719 $wpReason=wfMsg("exbeforeblank") . " '".$text;
720 }
721 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
722 $wpReason.="'";
723 }
724 }
725
726 return $this->confirmDelete();
727 }
728
729 function confirmDelete( $par = "" )
730 {
731 global $wgOut;
732 global $wpReason;
733
734 wfDebug( "Article::confirmDelete\n" );
735
736 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
737 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
738 $wgOut->setRobotpolicy( "noindex,nofollow" );
739 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
740
741 $t = $this->mTitle->getPrefixedURL();
742
743 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
744 $confirm = wfMsg( "confirm" );
745 $check = wfMsg( "confirmcheck" );
746 $delcom = wfMsg( "deletecomment" );
747
748 $wgOut->addHTML( "
749 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
750 <table border=0><tr><td align=right>
751 {$delcom}:</td><td align=left>
752 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
753 </td></tr><tr><td>&nbsp;</td></tr>
754 <tr><td align=right>
755 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
756 </td><td><label for=\"wpConfirm\">{$check}</label></td>
757 </tr><tr><td>&nbsp;</td><td>
758 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
759 </td></tr></table></form>\n" );
760
761 $wgOut->returnToMain( false );
762 }
763
764 function doDelete()
765 {
766 global $wgOut, $wgUser, $wgLang;
767 global $wpReason;
768 $fname = "Article::doDelete";
769 wfDebug( "$fname\n" );
770
771 $this->doDeleteArticle( $this->mTitle );
772 $deleted = $this->mTitle->getPrefixedText();
773
774 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
775 $wgOut->setRobotpolicy( "noindex,nofollow" );
776
777 $sk = $wgUser->getSkin();
778 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
779 Namespace::getWikipedia() ) .
780 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
781
782 $text = wfMsg( "deletedtext", $deleted, $loglink );
783
784 $wgOut->addHTML( "<p>" . $text );
785 $wgOut->returnToMain( false );
786 }
787
788 function doDeleteArticle( $title )
789 {
790 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList,
791 $wgEnablePersistentLC;
792
793 $fname = "Article::doDeleteArticle";
794 wfDebug( "$fname\n" );
795
796 $ns = $title->getNamespace();
797 $t = wfStrencode( $title->getDBkey() );
798 $id = $title->getArticleID();
799
800 if ( "" == $t ) {
801 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
802 return;
803 }
804
805 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
806 array_push( $wgDeferredUpdateList, $u );
807
808 # Move article and history to the "archive" table
809 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
810 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
811 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
812 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
813 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
814 wfQuery( $sql, DB_WRITE, $fname );
815
816 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
817 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
818 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
819 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
820 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
821 wfQuery( $sql, DB_WRITE, $fname );
822
823 # Now that it's safely backed up, delete it
824
825 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
826 "cur_title='{$t}'";
827 wfQuery( $sql, DB_WRITE, $fname );
828
829 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
830 "old_title='{$t}'";
831 wfQuery( $sql, DB_WRITE, $fname );
832
833 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
834 "rc_title='{$t}'";
835 wfQuery( $sql, DB_WRITE, $fname );
836
837 # Finally, clean up the link tables
838
839 if ( 0 != $id ) {
840
841 $t = wfStrencode( $title->getPrefixedDBkey() );
842
843 if ( $wgEnablePersistentLC ) {
844 // Purge related entries in links cache on delete,
845 wfQuery("DELETE linkscc FROM linkscc,links ".
846 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE);
847 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE);
848 }
849
850 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
851 $res = wfQuery( $sql, DB_READ, $fname );
852
853 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
854 $now = wfTimestampNow();
855 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
856 $first = true;
857
858 while ( $s = wfFetchObject( $res ) ) {
859 $nt = Title::newFromDBkey( $s->l_from );
860 $lid = $nt->getArticleID();
861
862 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
863 $first = false;
864 $sql .= "({$lid},'{$t}')";
865 $sql2 .= "{$lid}";
866 }
867 $sql2 .= ")";
868 if ( ! $first ) {
869 wfQuery( $sql, DB_WRITE, $fname );
870 wfQuery( $sql2, DB_WRITE, $fname );
871 }
872 wfFreeResult( $res );
873
874 $sql = "DELETE FROM links WHERE l_to={$id}";
875 wfQuery( $sql, DB_WRITE, $fname );
876
877 $sql = "DELETE FROM links WHERE l_from='{$t}'";
878 wfQuery( $sql, DB_WRITE, $fname );
879
880 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
881 wfQuery( $sql, DB_WRITE, $fname );
882
883 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
884 wfQuery( $sql, DB_WRITE, $fname );
885 }
886
887 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
888 $art = $title->getPrefixedText();
889 $wpReason = wfCleanQueryVar( $wpReason );
890 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
891
892 # Clear the cached article id so the interface doesn't act like we exist
893 $this->mTitle->resetArticleID( 0 );
894 $this->mTitle->mArticleID = 0;
895 }
896
897 function rollback()
898 {
899 global $wgUser, $wgLang, $wgOut, $from;
900
901 if ( ! $wgUser->isSysop() ) {
902 $wgOut->sysopRequired();
903 return;
904 }
905 if ( wfReadOnly() ) {
906 $wgOut->readOnlyPage( $this->getContent() );
907 return;
908 }
909
910 # Secret enhanced rollback, marks edits rc_bot=1
911 $bot = !!$_REQUEST['bot'];
912
913 # Replace all this user's current edits with the next one down
914 $tt = wfStrencode( $this->mTitle->getDBKey() );
915 $n = $this->mTitle->getNamespace();
916
917 # Get the last editor
918 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
919 $res = wfQuery( $sql, DB_READ );
920 if( ($x = wfNumRows( $res )) != 1 ) {
921 # Something wrong
922 $wgOut->addHTML( wfMsg( "notanarticle" ) );
923 return;
924 }
925 $s = wfFetchObject( $res );
926 $ut = wfStrencode( $s->cur_user_text );
927 $uid = $s->cur_user;
928 $pid = $s->cur_id;
929
930 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
931 if( $from != $s->cur_user_text ) {
932 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
933 $wgOut->addWikiText( wfMsg( "alreadyrolled",
934 htmlspecialchars( $this->mTitle->getPrefixedText()),
935 htmlspecialchars( $from ),
936 htmlspecialchars( $s->cur_user_text ) ) );
937 if($s->cur_comment != "") {
938 $wgOut->addHTML(
939 wfMsg("editcomment",
940 htmlspecialchars( $s->cur_comment ) ) );
941 }
942 return;
943 }
944
945 # Get the last edit not by this guy
946 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp
947 FROM old USE INDEX (name_title_timestamp)
948 WHERE old_namespace={$n} AND old_title='{$tt}'
949 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
950 ORDER BY inverse_timestamp LIMIT 1";
951 $res = wfQuery( $sql, DB_READ );
952 if( wfNumRows( $res ) != 1 ) {
953 # Something wrong
954 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
955 $wgOut->addHTML( wfMsg( "cantrollback" ) );
956 return;
957 }
958 $s = wfFetchObject( $res );
959
960 if ( $bot ) {
961 # Mark all reverted edits as bot
962 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
963 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
964 wfQuery( $sql, DB_WRITE, $fname );
965 }
966
967 # Save it!
968 $newcomment = wfMsg( "revertpage", $s->old_user_text );
969 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
970 $wgOut->setRobotpolicy( "noindex,nofollow" );
971 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
972 $this->updateArticle( $s->old_text, $newcomment, 1, $this->mTitle->userIsWatching(), "", $bot );
973
974 global $wgEnablePersistentLC;
975 if ( $wgEnablePersistentLC ) {
976 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
977 }
978
979
980 $wgOut->returnToMain( false );
981 }
982
983
984 # Do standard deferred updates after page view
985
986 /* private */ function viewUpdates()
987 {
988 global $wgDeferredUpdateList;
989 if ( 0 != $this->getID() ) {
990 global $wgDisableCounters;
991 if( !$wgDisableCounters ) {
992 Article::incViewCount( $this->getID() );
993 $u = new SiteStatsUpdate( 1, 0, 0 );
994 array_push( $wgDeferredUpdateList, $u );
995 }
996 $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
997 $this->mTitle->getDBkey() );
998 array_push( $wgDeferredUpdateList, $u );
999 }
1000 }
1001
1002 # Do standard deferred updates after page edit.
1003 # Every 1000th edit, prune the recent changes table.
1004
1005 /* private */ function editUpdates( $text )
1006 {
1007 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1008 global $wgMessageCache;
1009
1010 wfSeedRandom();
1011 if ( 0 == mt_rand( 0, 999 ) ) {
1012 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1013 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1014 wfQuery( $sql, DB_WRITE );
1015 }
1016 $id = $this->getID();
1017 $title = $this->mTitle->getPrefixedDBkey();
1018 $shortTitle = $this->mTitle->getDBkey();
1019
1020 $adj = $this->mCountAdjustment;
1021
1022 if ( 0 != $id ) {
1023 $u = new LinksUpdate( $id, $title );
1024 array_push( $wgDeferredUpdateList, $u );
1025 $u = new SiteStatsUpdate( 0, 1, $adj );
1026 array_push( $wgDeferredUpdateList, $u );
1027 $u = new SearchUpdate( $id, $title, $text );
1028 array_push( $wgDeferredUpdateList, $u );
1029
1030 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(), $shortTitle );
1031 array_push( $wgDeferredUpdateList, $u );
1032
1033 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
1034 $wgMessageCache->replace( $shortTitle, $text );
1035 }
1036 }
1037 }
1038
1039 /* private */ function setOldSubtitle()
1040 {
1041 global $wgLang, $wgOut;
1042
1043 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1044 $r = wfMsg( "revisionasof", $td );
1045 $wgOut->setSubtitle( "({$r})" );
1046 }
1047
1048 # This function is called right before saving the wikitext,
1049 # so we can do things like signatures and links-in-context.
1050
1051 function preSaveTransform( $text )
1052 {
1053 $s = "";
1054 while ( "" != $text ) {
1055 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1056 $s .= $this->pstPass2( $p[0] );
1057
1058 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
1059 else {
1060 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1061 $s .= "<nowiki>{$q[0]}</nowiki>";
1062 $text = $q[1];
1063 }
1064 }
1065 return rtrim( $s );
1066 }
1067
1068 /* private */ function pstPass2( $text )
1069 {
1070 global $wgUser, $wgLang, $wgLocaltimezone;
1071
1072 # Signatures
1073 #
1074 $n = $wgUser->getName();
1075 $k = $wgUser->getOption( "nickname" );
1076 if ( "" == $k ) { $k = $n; }
1077 if(isset($wgLocaltimezone)) {
1078 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1079 }
1080 /* Note: this is an ugly timezone hack for the European wikis */
1081 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1082 " (" . date( "T" ) . ")";
1083 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1084
1085 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1086 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1087 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1088 Namespace::getUser() ) . ":$n|$k]]", $text );
1089
1090 # Context links: [[|name]] and [[name (context)|]]
1091 #
1092 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1093 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1094 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1095 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1096
1097 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1098 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1099 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1100 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1101 # [[ns:page (cont)|]]
1102 $context = "";
1103 $t = $this->mTitle->getText();
1104 if ( preg_match( $conpat, $t, $m ) ) {
1105 $context = $m[2];
1106 }
1107 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1108 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1109 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1110
1111 if ( "" == $context ) {
1112 $text = preg_replace( $p2, "[[\\1]]", $text );
1113 } else {
1114 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1115 }
1116
1117 # {{SUBST:xxx}} variables
1118 #
1119 $mw =& MagicWord::get( MAG_SUBST );
1120 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1121
1122 /* Experimental:
1123 # Trim trailing whitespace
1124 # MAG_END (__END__) tag allows for trailing
1125 # whitespace to be deliberately included
1126 $text = rtrim( $text );
1127 $mw =& MagicWord::get( MAG_END );
1128 $mw->matchAndRemove( $text );
1129 */
1130 return $text;
1131 }
1132
1133 /* Caching functions */
1134
1135 # checkLastModified returns true iff it has taken care of all
1136 # output to the client that is necessary for this request.
1137 # (that is, it has sent a cached version of the page)
1138 function tryFileCache() {
1139 static $called = false;
1140 if( $called ) {
1141 wfDebug( " tryFileCache() -- called twice!?\n" );
1142 return;
1143 }
1144 $called = true;
1145 if($this->isFileCacheable()) {
1146 $touched = $this->mTouched;
1147 if( $this->mTitle->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1148 # Expire the main page quicker
1149 $expire = wfUnix2Timestamp( time() - 3600 );
1150 $touched = max( $expire, $touched );
1151 }
1152 $cache = new CacheManager( $this->mTitle );
1153 if($cache->isFileCacheGood( $touched )) {
1154 global $wgOut;
1155 wfDebug( " tryFileCache() - about to load\n" );
1156 $cache->loadFromFileCache();
1157 return true;
1158 } else {
1159 wfDebug( " tryFileCache() - starting buffer\n" );
1160 if($cache->useGzip() && wfClientAcceptsGzip()) {
1161 /* For some reason, adding this header line over in
1162 CacheManager::saveToFileCache() fails on my test
1163 setup at home, though it works on the live install.
1164 Make double-sure... --brion */
1165 header( "Content-Encoding: gzip" );
1166 }
1167 ob_start( array(&$cache, 'saveToFileCache' ) );
1168 }
1169 } else {
1170 wfDebug( " tryFileCache() - not cacheable\n" );
1171 }
1172 }
1173
1174 function isFileCacheable() {
1175 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1176 global $action, $oldid, $diff, $redirect, $printable;
1177 return $wgUseFileCache
1178 and (!$wgShowIPinHeader)
1179 and ($this->getID() != 0)
1180 and ($wgUser->getId() == 0)
1181 and (!$wgUser->getNewtalk())
1182 and ($this->mTitle->getNamespace != Namespace::getSpecial())
1183 and ($action == "view")
1184 and (!isset($oldid))
1185 and (!isset($diff))
1186 and (!isset($redirect))
1187 and (!isset($printable))
1188 and (!$this->mRedirectedFrom);
1189 }
1190
1191 function checkTouched() {
1192 $id = $this->getID();
1193 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1194 $res = wfQuery( $sql, DB_READ, "Article::checkTouched" );
1195 if( $s = wfFetchObject( $res ) ) {
1196 $this->mTouched = $s->cur_touched;
1197 return !$s->cur_is_redirect;
1198 } else {
1199 return false;
1200 }
1201 }
1202
1203 /* static */ function incViewCount( $id )
1204 {
1205 $id = intval( $id );
1206
1207 # Not important enough to warrant an error page in case of failure
1208 $oldignore = wfIgnoreSQLErrors( true );
1209
1210 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE);
1211
1212 if( (rand() % 23 != 0) or (wfLastErrno() != 0) ){
1213 # Most of the time (or on SQL errors), skip row count check
1214 wfIgnoreSQLErrors( $oldignore );
1215 return;
1216 }
1217
1218 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE);
1219 $row = wfFetchObject( $res );
1220 $rown = intval( $row->n );
1221 if( $rown > 1000 ){ // update counters after ~1000 hits
1222 wfProfileIn( "Article::incViewCount-collect" );
1223 $old_user_abort = ignore_user_abort( true );
1224
1225 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE);
1226 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1227 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1228 "GROUP BY hc_id", DB_WRITE);
1229 wfQuery("DELETE FROM hitcounter", DB_WRITE);
1230 wfQuery("UNLOCK TABLES", DB_WRITE);
1231 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1232 "WHERE cur_id = hc_id", DB_WRITE);
1233 wfQuery("DROP TABLE acchits", DB_WRITE);
1234
1235 ignore_user_abort( $old_user_abort );
1236 wfProfileOut( "Article::incViewCount-collect" );
1237 }
1238 wfIgnoreSQLErrors( $oldignore );
1239 }
1240 }
1241
1242 function wfReplaceSubstVar( $matches ) {
1243 return wfMsg( $matches[1] );
1244 }
1245
1246 ?>