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