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