Split off page history code to PageHistory.php out of Article.php and Skin.php.
[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, $wgUseBetterLinksUpdate;
535 global $wgMwRedir;
536
537 $wgLinkCache = new LinkCache();
538
539 # Get old version of link table to allow incremental link updates
540 if ( $wgUseBetterLinksUpdate ) {
541 $wgLinkCache->preFill( $this->mTitle );
542 $wgLinkCache->clear();
543 }
544
545 # Now update the link cache by parsing the text
546 $wgOut = new OutputPage();
547 $wgOut->addWikiText( $text );
548
549 $this->editUpdates( $text );
550 if( $wgMwRedir->matchStart( $text ) )
551 $r = "redirect=no";
552 else
553 $r = "";
554 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL(), $r ) );
555 }
556
557 # Add this page to my watchlist
558
559 function watch( $add = true )
560 {
561 global $wgUser, $wgOut, $wgLang;
562 global $wgDeferredUpdateList;
563
564 if ( 0 == $wgUser->getID() ) {
565 $wgOut->errorpage( "watchnologin", "watchnologintext" );
566 return;
567 }
568 if ( wfReadOnly() ) {
569 $wgOut->readOnlyPage();
570 return;
571 }
572 if( $add )
573 $wgUser->addWatch( $this->mTitle );
574 else
575 $wgUser->removeWatch( $this->mTitle );
576
577 $wgOut->setPagetitle( wfMsg( $add ? "addedwatch" : "removedwatch" ) );
578 $wgOut->setRobotpolicy( "noindex,follow" );
579
580 $sk = $wgUser->getSkin() ;
581 $link = $sk->makeKnownLink ( $this->mTitle->getPrefixedText() ) ;
582
583 if($add)
584 $text = wfMsg( "addedwatchtext", $link );
585 else
586 $text = wfMsg( "removedwatchtext", $link );
587 $wgOut->addHTML( $text );
588
589 $up = new UserUpdate();
590 array_push( $wgDeferredUpdateList, $up );
591
592 $wgOut->returnToMain( false );
593 }
594
595 function unwatch()
596 {
597 $this->watch( false );
598 }
599
600 function protect( $limit = "sysop" )
601 {
602 global $wgUser, $wgOut;
603
604 if ( ! $wgUser->isSysop() ) {
605 $wgOut->sysopRequired();
606 return;
607 }
608 if ( wfReadOnly() ) {
609 $wgOut->readOnlyPage();
610 return;
611 }
612 $id = $this->mTitle->getArticleID();
613 if ( 0 == $id ) {
614 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
615 return;
616 }
617 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
618 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
619 wfQuery( $sql, DB_WRITE, "Article::protect" );
620
621 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
622 if ( $limit === "" ) {
623 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle->getPrefixedText() ), "" );
624 } else {
625 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle->getPrefixedText() ), "" );
626 }
627 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
628 }
629
630 function unprotect()
631 {
632 return $this->protect( "" );
633 }
634
635 function delete()
636 {
637 global $wgUser, $wgOut, $wgMessageCache;
638 global $wpConfirm, $wpReason, $image, $oldimage;
639
640 # This code desperately needs to be totally rewritten
641
642 if ( ( ! $wgUser->isSysop() ) ) {
643 $wgOut->sysopRequired();
644 return;
645 }
646 if ( wfReadOnly() ) {
647 $wgOut->readOnlyPage();
648 return;
649 }
650
651 # Can't delete cached MediaWiki namespace (i.e. vital messages)
652 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && $wgMessageCache->isCacheable( $this->mTitle->getDBkey() ) ) {
653 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
654 return;
655 }
656
657 # Better double-check that it hasn't been deleted yet!
658 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
659 if ( ( "" == trim( $this->mTitle->getText() ) )
660 or ( $this->mTitle->getArticleId() == 0 ) ) {
661 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
662 return;
663 }
664
665 if ( $_POST["wpConfirm"] ) {
666 $this->doDelete();
667 return;
668 }
669
670 # determine whether this page has earlier revisions
671 # and insert a warning if it does
672 # we select the text because it might be useful below
673 $ns = $this->mTitle->getNamespace();
674 $title = $this->mTitle->getDBkey();
675 $etitle = wfStrencode( $title );
676 $sql = "SELECT old_text FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
677 $res = wfQuery( $sql, DB_READ, $fname );
678 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
679 $skin=$wgUser->getSkin();
680 $wgOut->addHTML("<B>".wfMsg("historywarning"));
681 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
682 }
683
684 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
685 $res=wfQuery($sql, DB_READ, $fname);
686 if( ($s=wfFetchObject($res))) {
687
688 # if this is a mini-text, we can paste part of it into the deletion reason
689
690 #if this is empty, an earlier revision may contain "useful" text
691 if($s->cur_text!="") {
692 $text=$s->cur_text;
693 } else {
694 if($old) {
695 $text=$old->old_text;
696 $blanked=1;
697 }
698
699 }
700
701 $length=strlen($text);
702
703 # this should not happen, since it is not possible to store an empty, new
704 # page. Let's insert a standard text in case it does, though
705 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
706
707
708 if($length < 500 && !$wpReason) {
709
710 # comment field=255, let's grep the first 150 to have some user
711 # space left
712 $text=substr($text,0,150);
713 # let's strip out newlines and HTML tags
714 $text=preg_replace("/\"/","'",$text);
715 $text=preg_replace("/\</","&lt;",$text);
716 $text=preg_replace("/\>/","&gt;",$text);
717 $text=preg_replace("/[\n\r]/","",$text);
718 if(!$blanked) {
719 $wpReason=wfMsg("excontent"). " '".$text;
720 } else {
721 $wpReason=wfMsg("exbeforeblank") . " '".$text;
722 }
723 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
724 $wpReason.="'";
725 }
726 }
727
728 return $this->confirmDelete();
729 }
730
731 function confirmDelete( $par = "" )
732 {
733 global $wgOut;
734 global $wpReason;
735
736 wfDebug( "Article::confirmDelete\n" );
737
738 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
739 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
740 $wgOut->setRobotpolicy( "noindex,nofollow" );
741 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
742
743 $t = $this->mTitle->getPrefixedURL();
744
745 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
746 $confirm = wfMsg( "confirm" );
747 $check = wfMsg( "confirmcheck" );
748 $delcom = wfMsg( "deletecomment" );
749
750 $wgOut->addHTML( "
751 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
752 <table border=0><tr><td align=right>
753 {$delcom}:</td><td align=left>
754 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
755 </td></tr><tr><td>&nbsp;</td></tr>
756 <tr><td align=right>
757 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
758 </td><td><label for=\"wpConfirm\">{$check}</label></td>
759 </tr><tr><td>&nbsp;</td><td>
760 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
761 </td></tr></table></form>\n" );
762
763 $wgOut->returnToMain( false );
764 }
765
766 function doDelete()
767 {
768 global $wgOut, $wgUser, $wgLang;
769 global $wpReason;
770 $fname = "Article::doDelete";
771 wfDebug( "$fname\n" );
772
773 $this->doDeleteArticle( $this->mTitle );
774 $deleted = $this->mTitle->getPrefixedText();
775
776 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
777 $wgOut->setRobotpolicy( "noindex,nofollow" );
778
779 $sk = $wgUser->getSkin();
780 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
781 Namespace::getWikipedia() ) .
782 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
783
784 $text = wfMsg( "deletedtext", $deleted, $loglink );
785
786 $wgOut->addHTML( "<p>" . $text );
787 $wgOut->returnToMain( false );
788 }
789
790 function doDeleteArticle( $title )
791 {
792 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList,
793 $wgEnablePersistentLC;
794
795 $fname = "Article::doDeleteArticle";
796 wfDebug( "$fname\n" );
797
798 $ns = $title->getNamespace();
799 $t = wfStrencode( $title->getDBkey() );
800 $id = $title->getArticleID();
801
802 if ( "" == $t ) {
803 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
804 return;
805 }
806
807 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
808 array_push( $wgDeferredUpdateList, $u );
809
810 # Move article and history to the "archive" table
811 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
812 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
813 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
814 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
815 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
816 wfQuery( $sql, DB_WRITE, $fname );
817
818 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
819 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
820 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
821 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
822 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
823 wfQuery( $sql, DB_WRITE, $fname );
824
825 # Now that it's safely backed up, delete it
826
827 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
828 "cur_title='{$t}'";
829 wfQuery( $sql, DB_WRITE, $fname );
830
831 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
832 "old_title='{$t}'";
833 wfQuery( $sql, DB_WRITE, $fname );
834
835 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
836 "rc_title='{$t}'";
837 wfQuery( $sql, DB_WRITE, $fname );
838
839 # Finally, clean up the link tables
840
841 if ( 0 != $id ) {
842
843 $t = wfStrencode( $title->getPrefixedDBkey() );
844
845 if ( $wgEnablePersistentLC ) {
846 // Purge related entries in links cache on delete,
847 wfQuery("DELETE linkscc FROM linkscc,links ".
848 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE);
849 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE);
850 }
851
852 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
853 $res = wfQuery( $sql, DB_READ, $fname );
854
855 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
856 $now = wfTimestampNow();
857 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
858 $first = true;
859
860 while ( $s = wfFetchObject( $res ) ) {
861 $nt = Title::newFromDBkey( $s->l_from );
862 $lid = $nt->getArticleID();
863
864 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
865 $first = false;
866 $sql .= "({$lid},'{$t}')";
867 $sql2 .= "{$lid}";
868 }
869 $sql2 .= ")";
870 if ( ! $first ) {
871 wfQuery( $sql, DB_WRITE, $fname );
872 wfQuery( $sql2, DB_WRITE, $fname );
873 }
874 wfFreeResult( $res );
875
876 $sql = "DELETE FROM links WHERE l_to={$id}";
877 wfQuery( $sql, DB_WRITE, $fname );
878
879 $sql = "DELETE FROM links WHERE l_from='{$t}'";
880 wfQuery( $sql, DB_WRITE, $fname );
881
882 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
883 wfQuery( $sql, DB_WRITE, $fname );
884
885 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
886 wfQuery( $sql, DB_WRITE, $fname );
887 }
888
889 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
890 $art = $title->getPrefixedText();
891 $wpReason = wfCleanQueryVar( $wpReason );
892 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
893
894 # Clear the cached article id so the interface doesn't act like we exist
895 $this->mTitle->resetArticleID( 0 );
896 $this->mTitle->mArticleID = 0;
897 }
898
899 function rollback()
900 {
901 global $wgUser, $wgLang, $wgOut, $from;
902
903 if ( ! $wgUser->isSysop() ) {
904 $wgOut->sysopRequired();
905 return;
906 }
907 if ( wfReadOnly() ) {
908 $wgOut->readOnlyPage( $this->getContent() );
909 return;
910 }
911
912 # Secret enhanced rollback, marks edits rc_bot=1
913 $bot = !!$_REQUEST['bot'];
914
915 # Replace all this user's current edits with the next one down
916 $tt = wfStrencode( $this->mTitle->getDBKey() );
917 $n = $this->mTitle->getNamespace();
918
919 # Get the last editor
920 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
921 $res = wfQuery( $sql, DB_READ );
922 if( ($x = wfNumRows( $res )) != 1 ) {
923 # Something wrong
924 $wgOut->addHTML( wfMsg( "notanarticle" ) );
925 return;
926 }
927 $s = wfFetchObject( $res );
928 $ut = wfStrencode( $s->cur_user_text );
929 $uid = $s->cur_user;
930 $pid = $s->cur_id;
931
932 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
933 if( $from != $s->cur_user_text ) {
934 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
935 $wgOut->addWikiText( wfMsg( "alreadyrolled",
936 htmlspecialchars( $this->mTitle->getPrefixedText()),
937 htmlspecialchars( $from ),
938 htmlspecialchars( $s->cur_user_text ) ) );
939 if($s->cur_comment != "") {
940 $wgOut->addHTML(
941 wfMsg("editcomment",
942 htmlspecialchars( $s->cur_comment ) ) );
943 }
944 return;
945 }
946
947 # Get the last edit not by this guy
948 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp
949 FROM old USE INDEX (name_title_timestamp)
950 WHERE old_namespace={$n} AND old_title='{$tt}'
951 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
952 ORDER BY inverse_timestamp LIMIT 1";
953 $res = wfQuery( $sql, DB_READ );
954 if( wfNumRows( $res ) != 1 ) {
955 # Something wrong
956 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
957 $wgOut->addHTML( wfMsg( "cantrollback" ) );
958 return;
959 }
960 $s = wfFetchObject( $res );
961
962 if ( $bot ) {
963 # Mark all reverted edits as bot
964 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
965 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
966 wfQuery( $sql, DB_WRITE, $fname );
967 }
968
969 # Save it!
970 $newcomment = wfMsg( "revertpage", $s->old_user_text );
971 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
972 $wgOut->setRobotpolicy( "noindex,nofollow" );
973 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
974 $this->updateArticle( $s->old_text, $newcomment, 1, $this->mTitle->userIsWatching(), "", $bot );
975
976 global $wgEnablePersistentLC;
977 if ( $wgEnablePersistentLC ) {
978 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE);
979 }
980
981
982 $wgOut->returnToMain( false );
983 }
984
985
986 # Do standard deferred updates after page view
987
988 /* private */ function viewUpdates()
989 {
990 global $wgDeferredUpdateList;
991 if ( 0 != $this->getID() ) {
992 global $wgDisableCounters;
993 if( !$wgDisableCounters ) {
994 Article::incViewCount( $this->getID() );
995 $u = new SiteStatsUpdate( 1, 0, 0 );
996 array_push( $wgDeferredUpdateList, $u );
997 }
998 $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
999 $this->mTitle->getDBkey() );
1000 array_push( $wgDeferredUpdateList, $u );
1001 }
1002 }
1003
1004 # Do standard deferred updates after page edit.
1005 # Every 1000th edit, prune the recent changes table.
1006
1007 /* private */ function editUpdates( $text )
1008 {
1009 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1010 global $wgMessageCache;
1011
1012 wfSeedRandom();
1013 if ( 0 == mt_rand( 0, 999 ) ) {
1014 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1015 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1016 wfQuery( $sql, DB_WRITE );
1017 }
1018 $id = $this->getID();
1019 $title = $this->mTitle->getPrefixedDBkey();
1020 $shortTitle = $this->mTitle->getDBkey();
1021
1022 $adj = $this->mCountAdjustment;
1023
1024 if ( 0 != $id ) {
1025 $u = new LinksUpdate( $id, $title );
1026 array_push( $wgDeferredUpdateList, $u );
1027 $u = new SiteStatsUpdate( 0, 1, $adj );
1028 array_push( $wgDeferredUpdateList, $u );
1029 $u = new SearchUpdate( $id, $title, $text );
1030 array_push( $wgDeferredUpdateList, $u );
1031
1032 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(), $shortTitle );
1033 array_push( $wgDeferredUpdateList, $u );
1034
1035 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
1036 $wgMessageCache->replace( $shortTitle, $text );
1037 }
1038 }
1039 }
1040
1041 /* private */ function setOldSubtitle()
1042 {
1043 global $wgLang, $wgOut;
1044
1045 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1046 $r = wfMsg( "revisionasof", $td );
1047 $wgOut->setSubtitle( "({$r})" );
1048 }
1049
1050 # This function is called right before saving the wikitext,
1051 # so we can do things like signatures and links-in-context.
1052
1053 function preSaveTransform( $text )
1054 {
1055 $s = "";
1056 while ( "" != $text ) {
1057 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1058 $s .= $this->pstPass2( $p[0] );
1059
1060 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
1061 else {
1062 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1063 $s .= "<nowiki>{$q[0]}</nowiki>";
1064 $text = $q[1];
1065 }
1066 }
1067 return rtrim( $s );
1068 }
1069
1070 /* private */ function pstPass2( $text )
1071 {
1072 global $wgUser, $wgLang, $wgLocaltimezone;
1073
1074 # Signatures
1075 #
1076 $n = $wgUser->getName();
1077 $k = $wgUser->getOption( "nickname" );
1078 if ( "" == $k ) { $k = $n; }
1079 if(isset($wgLocaltimezone)) {
1080 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1081 }
1082 /* Note: this is an ugly timezone hack for the European wikis */
1083 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1084 " (" . date( "T" ) . ")";
1085 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1086
1087 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1088 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1089 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1090 Namespace::getUser() ) . ":$n|$k]]", $text );
1091
1092 # Context links: [[|name]] and [[name (context)|]]
1093 #
1094 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1095 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1096 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1097 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1098
1099 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1100 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1101 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1102 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1103 # [[ns:page (cont)|]]
1104 $context = "";
1105 $t = $this->mTitle->getText();
1106 if ( preg_match( $conpat, $t, $m ) ) {
1107 $context = $m[2];
1108 }
1109 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1110 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1111 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1112
1113 if ( "" == $context ) {
1114 $text = preg_replace( $p2, "[[\\1]]", $text );
1115 } else {
1116 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1117 }
1118
1119 # {{SUBST:xxx}} variables
1120 #
1121 $mw =& MagicWord::get( MAG_SUBST );
1122 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1123
1124 /* Experimental:
1125 # Trim trailing whitespace
1126 # MAG_END (__END__) tag allows for trailing
1127 # whitespace to be deliberately included
1128 $text = rtrim( $text );
1129 $mw =& MagicWord::get( MAG_END );
1130 $mw->matchAndRemove( $text );
1131 */
1132 return $text;
1133 }
1134
1135 /* Caching functions */
1136
1137 # checkLastModified returns true iff it has taken care of all
1138 # output to the client that is necessary for this request.
1139 # (that is, it has sent a cached version of the page)
1140 function tryFileCache() {
1141 static $called = false;
1142 if( $called ) {
1143 wfDebug( " tryFileCache() -- called twice!?\n" );
1144 return;
1145 }
1146 $called = true;
1147 if($this->isFileCacheable()) {
1148 $touched = $this->mTouched;
1149 if( $this->mTitle->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1150 # Expire the main page quicker
1151 $expire = wfUnix2Timestamp( time() - 3600 );
1152 $touched = max( $expire, $touched );
1153 }
1154 $cache = new CacheManager( $this->mTitle );
1155 if($cache->isFileCacheGood( $touched )) {
1156 global $wgOut;
1157 wfDebug( " tryFileCache() - about to load\n" );
1158 $cache->loadFromFileCache();
1159 return true;
1160 } else {
1161 wfDebug( " tryFileCache() - starting buffer\n" );
1162 if($cache->useGzip() && wfClientAcceptsGzip()) {
1163 /* For some reason, adding this header line over in
1164 CacheManager::saveToFileCache() fails on my test
1165 setup at home, though it works on the live install.
1166 Make double-sure... --brion */
1167 header( "Content-Encoding: gzip" );
1168 }
1169 ob_start( array(&$cache, 'saveToFileCache' ) );
1170 }
1171 } else {
1172 wfDebug( " tryFileCache() - not cacheable\n" );
1173 }
1174 }
1175
1176 function isFileCacheable() {
1177 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1178 global $action, $oldid, $diff, $redirect, $printable;
1179 return $wgUseFileCache
1180 and (!$wgShowIPinHeader)
1181 and ($this->getID() != 0)
1182 and ($wgUser->getId() == 0)
1183 and (!$wgUser->getNewtalk())
1184 and ($this->mTitle->getNamespace != Namespace::getSpecial())
1185 and ($action == "view")
1186 and (!isset($oldid))
1187 and (!isset($diff))
1188 and (!isset($redirect))
1189 and (!isset($printable))
1190 and (!$this->mRedirectedFrom);
1191 }
1192
1193 function checkTouched() {
1194 $id = $this->getID();
1195 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1196 $res = wfQuery( $sql, DB_READ, "Article::checkTouched" );
1197 if( $s = wfFetchObject( $res ) ) {
1198 $this->mTouched = $s->cur_touched;
1199 return !$s->cur_is_redirect;
1200 } else {
1201 return false;
1202 }
1203 }
1204
1205 /* static */ function incViewCount( $id )
1206 {
1207 $id = intval( $id );
1208
1209 # Not important enough to warrant an error page in case of failure
1210 $oldignore = wfIgnoreSQLErrors( true );
1211
1212 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE);
1213
1214 if( (rand() % 23 != 0) or (wfLastErrno() != 0) ){
1215 # Most of the time (or on SQL errors), skip row count check
1216 wfIgnoreSQLErrors( $oldignore );
1217 return;
1218 }
1219
1220 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE);
1221 $row = wfFetchObject( $res );
1222 $rown = intval( $row->n );
1223 if( $rown > 1000 ){ // update counters after ~1000 hits
1224 wfProfileIn( "Article::incViewCount-collect" );
1225 $old_user_abort = ignore_user_abort( true );
1226
1227 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE);
1228 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1229 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1230 "GROUP BY hc_id", DB_WRITE);
1231 wfQuery("DELETE FROM hitcounter", DB_WRITE);
1232 wfQuery("UNLOCK TABLES", DB_WRITE);
1233 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1234 "WHERE cur_id = hc_id", DB_WRITE);
1235 wfQuery("DROP TABLE acchits", DB_WRITE);
1236
1237 ignore_user_abort( $old_user_abort );
1238 wfProfileOut( "Article::incViewCount-collect" );
1239 }
1240 wfIgnoreSQLErrors( $oldignore );
1241 }
1242 }
1243
1244 function wfReplaceSubstVar( $matches ) {
1245 return wfMsg( $matches[1] );
1246 }
1247
1248 ?>