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