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