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