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