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