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