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