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