Load balancing enabled in places where it's thought to be reasonably safe
[lhc/web/wiklou.git] / includes / Article.php
1 <?php
2 # $Id$
3 #
4 # Class representing a Wikipedia article and history.
5 # See design.doc for an overview.
6
7 # Note: edit user interface and cache support functions have been
8 # moved to separate EditPage and CacheManager classes.
9
10 require_once( 'CacheManager.php' );
11
12 class Article {
13 /* private */ var $mContent, $mContentLoaded;
14 /* private */ var $mUser, $mTimestamp, $mUserText;
15 /* private */ var $mCounter, $mComment, $mCountAdjustment;
16 /* private */ var $mMinorEdit, $mRedirectedFrom;
17 /* private */ var $mTouched, $mFileCache, $mTitle;
18 /* private */ var $mId, $mTable;
19
20 function Article( &$title ) {
21 $this->mTitle =& $title;
22 $this->clear();
23 }
24
25 /* private */ function clear()
26 {
27 $this->mContentLoaded = false;
28 $this->mCurID = $this->mUser = $this->mCounter = -1; # Not loaded
29 $this->mRedirectedFrom = $this->mUserText =
30 $this->mTimestamp = $this->mComment = $this->mFileCache = '';
31 $this->mCountAdjustment = 0;
32 $this->mTouched = '19700101000000';
33 }
34
35 # Get revision text associated with an old or archive row
36 # $row is usually an object from wfFetchRow(), both the flags and the text field must be included
37 /* static */ function getRevisionText( $row, $prefix = 'old_' ) {
38 # Get data
39 $textField = $prefix . 'text';
40 $flagsField = $prefix . 'flags';
41
42 if ( isset( $row->$flagsField ) ) {
43 $flags = explode( ",", $row->$flagsField );
44 } else {
45 $flags = array();
46 }
47
48 if ( isset( $row->$textField ) ) {
49 $text = $row->$textField;
50 } else {
51 return false;
52 }
53
54 if ( in_array( 'link', $flags ) ) {
55 # Handle link type
56 $text = Article::followLink( $text );
57 } elseif ( in_array( 'gzip', $flags ) ) {
58 # Deal with optional compression of archived pages.
59 # This can be done periodically via maintenance/compressOld.php, and
60 # as pages are saved if $wgCompressRevisions is set.
61 return gzinflate( $text );
62 }
63 return $text;
64 }
65
66 /* static */ function compressRevisionText( &$text ) {
67 global $wgCompressRevisions;
68 if( !$wgCompressRevisions ) {
69 return '';
70 }
71 if( !function_exists( 'gzdeflate' ) ) {
72 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
73 return '';
74 }
75 $text = gzdeflate( $text );
76 return 'gzip';
77 }
78
79 # Returns the text associated with a "link" type old table row
80 /* static */ function followLink( $link ) {
81 # Split the link into fields and values
82 $lines = explode( '\n', $link );
83 $hash = '';
84 $locations = array();
85 foreach ( $lines as $line ) {
86 # Comments
87 if ( $line{0} == '#' ) {
88 continue;
89 }
90 # Field/value pairs
91 if ( preg_match( '/^(.*?)\s*:\s*(.*)$/', $line, $matches ) ) {
92 $field = strtolower($matches[1]);
93 $value = $matches[2];
94 if ( $field == 'hash' ) {
95 $hash = $value;
96 } elseif ( $field == 'location' ) {
97 $locations[] = $value;
98 }
99 }
100 }
101
102 if ( $hash === '' ) {
103 return false;
104 }
105
106 # Look in each specified location for the text
107 $text = false;
108 foreach ( $locations as $location ) {
109 $text = Article::fetchFromLocation( $location, $hash );
110 if ( $text !== false ) {
111 break;
112 }
113 }
114
115 return $text;
116 }
117
118 /* static */ function fetchFromLocation( $location, $hash ) {
119 global $wgLoadBalancer;
120 $fname = 'fetchFromLocation';
121 wfProfileIn( $fname );
122
123 $p = strpos( $location, ':' );
124 if ( $p === false ) {
125 wfProfileOut( $fname );
126 return false;
127 }
128
129 $type = substr( $location, 0, $p );
130 $text = false;
131 switch ( $type ) {
132 case 'mysql':
133 # MySQL locations are specified by mysql://<machineID>/<dbname>/<tblname>/<index>
134 # Machine ID 0 is the current connection
135 if ( preg_match( '/^mysql:\/\/(\d+)\/([A-Za-z_]+)\/([A-Za-z_]+)\/([A-Za-z_]+)$/',
136 $location, $matches ) ) {
137 $machineID = $matches[1];
138 $dbName = $matches[2];
139 $tblName = $matches[3];
140 $index = $matches[4];
141 if ( $machineID == 0 ) {
142 # Current connection
143 $db =& wfGetDB();
144 } else {
145 # Alternate connection
146 $db =& $wgLoadBalancer->getConnection( $machineID );
147
148 if ( array_key_exists( $machineId, $wgKnownMysqlServers ) ) {
149 # Try to open, return false on failure
150 $params = $wgKnownDBServers[$machineId];
151 $db = Database::newFromParams( $params['server'], $params['user'], $params['password'],
152 $dbName, 1, false, true, true );
153 }
154 }
155 if ( $db->isOpen() ) {
156 $index = wfStrencode( $index );
157 $res = $db->query( "SELECT blob_data FROM $dbName.$tblName WHERE blob_index='$index'", $fname );
158 $row = $db->fetchObject( $res );
159 $text = $row->text_data;
160 }
161 }
162 break;
163 case 'file':
164 # File locations are of the form file://<filename>, relative to the current directory
165 if ( preg_match( '/^file:\/\/(.*)$', $location, $matches ) )
166 $filename = strstr( $location, 'file://' );
167 $text = @file_get_contents( $matches[1] );
168 }
169 if ( $text !== false ) {
170 # Got text, now we need to interpret it
171 # The first line contains information about how to do this
172 $p = strpos( $text, '\n' );
173 $type = substr( $text, 0, $p );
174 $text = substr( $text, $p + 1 );
175 switch ( $type ) {
176 case 'plain':
177 break;
178 case 'gzip':
179 $text = gzinflate( $text );
180 break;
181 case 'object':
182 $object = unserialize( $text );
183 $text = $object->getItem( $hash );
184 break;
185 default:
186 $text = false;
187 }
188 }
189 wfProfileOut( $fname );
190 return $text;
191 }
192
193 # Note that getContent/loadContent may follow redirects if
194 # not told otherwise, and so may cause a change to mTitle.
195
196 # Return the text of this revision
197 function getContent( $noredir )
198 {
199 global $wgRequest;
200
201 # Get variables from query string :P
202 $action = $wgRequest->getText( 'action', 'view' );
203 $section = $wgRequest->getText( 'section' );
204
205 $fname = 'Article::getContent';
206 wfProfileIn( $fname );
207
208 if ( 0 == $this->getID() ) {
209 if ( 'edit' == $action ) {
210 wfProfileOut( $fname );
211 return ''; # was "newarticletext", now moved above the box)
212 }
213 wfProfileOut( $fname );
214 return wfMsg( 'noarticletext' );
215 } else {
216 $this->loadContent( $noredir );
217
218 if(
219 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
220 ( $this->mTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
221 preg_match('/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/',$this->mTitle->getText()) &&
222 $action=='view'
223 )
224 {
225 wfProfileOut( $fname );
226 return $this->mContent . "\n" .wfMsg('anontalkpagetext'); }
227 else {
228 if($action=='edit') {
229 if($section!='') {
230 if($section=='new') {
231 wfProfileOut( $fname );
232 return '';
233 }
234
235 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
236 # comments to be stripped as well)
237 $rv=$this->getSection($this->mContent,$section);
238 wfProfileOut( $fname );
239 return $rv;
240 }
241 }
242 wfProfileOut( $fname );
243 return $this->mContent;
244 }
245 }
246 }
247
248 # This function returns the text of a section, specified by a number ($section).
249 # A section is text under a heading like == Heading == or <h1>Heading</h1>, or
250 # the first section before any such heading (section 0).
251 #
252 # If a section contains subsections, these are also returned.
253 #
254 function getSection($text,$section) {
255
256 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
257 # comments to be stripped as well)
258 $striparray=array();
259 $parser=new Parser();
260 $parser->mOutputType=OT_WIKI;
261 $striptext=$parser->strip($text, $striparray, true);
262
263 # now that we can be sure that no pseudo-sections are in the source,
264 # split it up by section
265 $secs =
266 preg_split(
267 '/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
268 $striptext, -1,
269 PREG_SPLIT_DELIM_CAPTURE);
270 if($section==0) {
271 $rv=$secs[0];
272 } else {
273 $headline=$secs[$section*2-1];
274 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
275 $hlevel=$matches[1];
276
277 # translate wiki heading into level
278 if(strpos($hlevel,'=')!==false) {
279 $hlevel=strlen($hlevel);
280 }
281
282 $rv=$headline. $secs[$section*2];
283 $count=$section+1;
284
285 $break=false;
286 while(!empty($secs[$count*2-1]) && !$break) {
287
288 $subheadline=$secs[$count*2-1];
289 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
290 $subhlevel=$matches[1];
291 if(strpos($subhlevel,'=')!==false) {
292 $subhlevel=strlen($subhlevel);
293 }
294 if($subhlevel > $hlevel) {
295 $rv.=$subheadline.$secs[$count*2];
296 }
297 if($subhlevel <= $hlevel) {
298 $break=true;
299 }
300 $count++;
301
302 }
303 }
304 # reinsert stripped tags
305 $rv=$parser->unstrip($rv,$striparray);
306 $rv=$parser->unstripNoWiki($rv,$striparray);
307 $rv=trim($rv);
308 return $rv;
309
310 }
311
312
313 # Load the revision (including cur_text) into this object
314 function loadContent( $noredir = false )
315 {
316 global $wgOut, $wgMwRedir, $wgRequest, $wgIsPg, $wgLoadBalancer;
317
318 # Query variables :P
319 $oldid = $wgRequest->getVal( 'oldid' );
320 $redirect = $wgRequest->getVal( 'redirect' );
321
322 if ( $this->mContentLoaded ) return;
323 $fname = 'Article::loadContent';
324
325 # Pre-fill content with error message so that if something
326 # fails we'll have something telling us what we intended.
327
328 $t = $this->mTitle->getPrefixedText();
329 if ( isset( $oldid ) ) {
330 $oldid = IntVal( $oldid );
331 $t .= ",oldid={$oldid}";
332 }
333 if ( isset( $redirect ) ) {
334 $redirect = ($redirect == 'no') ? 'no' : 'yes';
335 $t .= ",redirect={$redirect}";
336 }
337 $this->mContent = wfMsg( 'missingarticle', $t );
338
339 if ( ! $oldid ) { # Retrieve current version
340 $id = $this->getID();
341 if ( 0 == $id ) return;
342
343 $sql = 'SELECT ' .
344 'cur_text,cur_timestamp,cur_user,cur_user_text,cur_comment,cur_counter,cur_restrictions,cur_touched ' .
345 "FROM cur WHERE cur_id={$id}";
346 wfDebug( "$sql\n" );
347 $res = wfQuery( $sql, DB_READ, $fname );
348 if ( 0 == wfNumRows( $res ) ) {
349 return;
350 }
351
352 $s = wfFetchObject( $res );
353 # If we got a redirect, follow it (unless we've been told
354 # not to by either the function parameter or the query
355 if ( ( 'no' != $redirect ) && ( false == $noredir ) &&
356 ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
357 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
358 $s->cur_text, $m ) ) {
359 $rt = Title::newFromText( $m[1] );
360 if( $rt ) {
361 # Gotta hand redirects to special pages differently:
362 # Fill the HTTP response "Location" header and ignore
363 # the rest of the page we're on.
364
365 if ( $rt->getInterwiki() != '' ) {
366 $wgOut->redirect( $rt->getFullURL() ) ;
367 return;
368 }
369 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
370 $wgOut->redirect( $rt->getFullURL() );
371 return;
372 }
373 $rid = $rt->getArticleID();
374 if ( 0 != $rid ) {
375 $sql = 'SELECT cur_text,cur_timestamp,cur_user,cur_user_text,cur_comment,' .
376 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
377 $res = wfQuery( $sql, DB_READ, $fname );
378
379 if ( 0 != wfNumRows( $res ) ) {
380 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
381 $this->mTitle = $rt;
382 $s = wfFetchObject( $res );
383 }
384 }
385 }
386 }
387 }
388
389 $this->mContent = $s->cur_text;
390 $this->mUser = $s->cur_user;
391 $this->mUserText = $s->cur_user_text;
392 $this->mComment = $s->cur_comment;
393 $this->mCounter = $s->cur_counter;
394 $this->mTimestamp = $s->cur_timestamp;
395 $this->mTouched = $s->cur_touched;
396 $this->mTitle->mRestrictions = explode( ',', trim( $s->cur_restrictions ) );
397 $this->mTitle->mRestrictionsLoaded = true;
398 wfFreeResult( $res );
399 } else { # oldid set, retrieve historical version
400 $wgLoadBalancer->force(-1);
401 $oldtable=$wgIsPg?'"old"':'old';
402 $sql = "SELECT old_namespace,old_title,old_text,old_timestamp,".
403 "old_user,old_user_text,old_comment,old_flags FROM old ".
404 "WHERE old_id={$oldid}";
405 $res = wfQuery( $sql, DB_READ, $fname );
406 $wgLoadBalancer->force(0);
407 if ( 0 == wfNumRows( $res ) ) {
408 return;
409 }
410
411 $s = wfFetchObject( $res );
412 if( $this->mTitle->getNamespace() != $s->old_namespace ||
413 $this->mTitle->getDBkey() != $s->old_title ) {
414 $oldTitle = Title::makeTitle( $s->old_namesapce, $s->old_title );
415 $this->mTitle = $oldTitle;
416 $wgTitle = $oldTitle;
417 }
418 $this->mContent = Article::getRevisionText( $s );
419 $this->mUser = $s->old_user;
420 $this->mUserText = $s->old_user_text;
421 $this->mComment = $s->old_comment;
422 $this->mCounter = 0;
423 $this->mTimestamp = $s->old_timestamp;
424 wfFreeResult( $res );
425 }
426 $this->mContentLoaded = true;
427 return $this->mContent;
428 }
429
430 # Gets the article text without using so many damn globals
431 # Returns false on error
432 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
433 global $wgMwRedir, $wgIsPg;
434
435 if ( $this->mContentLoaded ) {
436 return $this->mContent;
437 }
438 $this->mContent = false;
439
440 $fname = 'Article::loadContent';
441
442 if ( ! $oldid ) { # Retrieve current version
443 $id = $this->getID();
444 if ( 0 == $id ) {
445 return false;
446 }
447
448 $sql = 'SELECT ' .
449 'cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched ' .
450 "FROM cur WHERE cur_id={$id}";
451 $res = wfQuery( $sql, DB_READ, $fname );
452 if ( 0 == wfNumRows( $res ) ) {
453 return false;
454 }
455
456 $s = wfFetchObject( $res );
457 # If we got a redirect, follow it (unless we've been told
458 # not to by either the function parameter or the query
459 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text ) ) {
460 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
461 $s->cur_text, $m ) ) {
462 $rt = Title::newFromText( $m[1] );
463 if( $rt && $rt->getInterwiki() == '' && $rt->getNamespace() != Namespace::getSpecial() ) {
464 $rid = $rt->getArticleID();
465 if ( 0 != $rid ) {
466 $sql = 'SELECT cur_text,cur_timestamp,cur_user,' .
467 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
468 $res = wfQuery( $sql, DB_READ, $fname );
469
470 if ( 0 != wfNumRows( $res ) ) {
471 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
472 $this->mTitle = $rt;
473 $s = wfFetchObject( $res );
474 }
475 }
476 }
477 }
478 }
479
480 $this->mContent = $s->cur_text;
481 $this->mUser = $s->cur_user;
482 $this->mCounter = $s->cur_counter;
483 $this->mTimestamp = $s->cur_timestamp;
484 $this->mTouched = $s->cur_touched;
485 $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
486 $this->mTitle->mRestrictionsLoaded = true;
487 wfFreeResult( $res );
488 } else { # oldid set, retrieve historical version
489 $oldtable=$wgIsPg?'"old"':'old';
490 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM $oldtable " .
491 "WHERE old_id={$oldid}";
492 $res = wfQuery( $sql, DB_READ, $fname );
493 if ( 0 == wfNumRows( $res ) ) {
494 return false;
495 }
496
497 $s = wfFetchObject( $res );
498 $this->mContent = Article::getRevisionText( $s );
499 $this->mUser = $s->old_user;
500 $this->mCounter = 0;
501 $this->mTimestamp = $s->old_timestamp;
502 wfFreeResult( $res );
503 }
504 $this->mContentLoaded = true;
505 return $this->mContent;
506 }
507
508 function getID() {
509 if( $this->mTitle ) {
510 return $this->mTitle->getArticleID();
511 } else {
512 return 0;
513 }
514 }
515
516 function getCount()
517 {
518 if ( -1 == $this->mCounter ) {
519 $id = $this->getID();
520 $this->mCounter = wfGetSQL( 'cur', 'cur_counter', "cur_id={$id}" );
521 }
522 return $this->mCounter;
523 }
524
525 # Would the given text make this article a "good" article (i.e.,
526 # suitable for including in the article count)?
527
528 function isCountable( $text )
529 {
530 global $wgUseCommaCount, $wgMwRedir;
531
532 if ( 0 != $this->mTitle->getNamespace() ) { return 0; }
533 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
534 $token = ($wgUseCommaCount ? ',' : '[[' );
535 if ( false === strstr( $text, $token ) ) { return 0; }
536 return 1;
537 }
538
539 # Loads everything from cur except cur_text
540 # This isn't necessary for all uses, so it's only done if needed.
541
542 /* private */ function loadLastEdit()
543 {
544 global $wgOut;
545 if ( -1 != $this->mUser ) return;
546
547 $sql = 'SELECT cur_user,cur_user_text,cur_timestamp,' .
548 'cur_comment,cur_minor_edit FROM cur WHERE ' .
549 'cur_id=' . $this->getID();
550 $res = wfQuery( $sql, DB_READ, 'Article::loadLastEdit' );
551
552 if ( wfNumRows( $res ) > 0 ) {
553 $s = wfFetchObject( $res );
554 $this->mUser = $s->cur_user;
555 $this->mUserText = $s->cur_user_text;
556 $this->mTimestamp = $s->cur_timestamp;
557 $this->mComment = $s->cur_comment;
558 $this->mMinorEdit = $s->cur_minor_edit;
559 }
560 }
561
562 function getTimestamp()
563 {
564 $this->loadLastEdit();
565 return $this->mTimestamp;
566 }
567
568 function getUser()
569 {
570 $this->loadLastEdit();
571 return $this->mUser;
572 }
573
574 function getUserText()
575 {
576 $this->loadLastEdit();
577 return $this->mUserText;
578 }
579
580 function getComment()
581 {
582 $this->loadLastEdit();
583 return $this->mComment;
584 }
585
586 function getMinorEdit()
587 {
588 $this->loadLastEdit();
589 return $this->mMinorEdit;
590 }
591
592 function getContributors($limit = 0, $offset = 0)
593 {
594 $fname = 'Article::getContributors';
595
596 # XXX: this is expensive; cache this info somewhere.
597
598 $title = $this->mTitle;
599
600 $contribs = array();
601
602 $sql = 'SELECT old_user, old_user_text, ' .
603 ' user_real_name, MAX(old_timestamp) as timestamp' .
604 ' FROM old LEFT JOIN user ON old.old_user = user.user_id ' .
605 ' WHERE old.old_namespace = ' . $title->getNamespace() .
606 ' AND old.old_title = "' . $title->getDBkey() . '"' .
607 ' AND old.old_user != ' . $this->getUser() .
608 ' GROUP BY old.old_user ' .
609 ' ORDER BY timestamp DESC ';
610
611 if ($limit > 0) {
612 $sql .= ' LIMIT '.$limit;
613 }
614
615 $res = wfQuery($sql, DB_READ, $fname);
616
617 while ( $line = wfFetchObject( $res ) ) {
618 $contribs[] = array($line->old_user, $line->old_user_text, $line->user_real_name);
619 }
620
621 wfFreeResult($res);
622
623 return $contribs;
624 }
625
626 # This is the default action of the script: just view the page of
627 # the given title.
628
629 function view()
630 {
631 global $wgUser, $wgOut, $wgLang, $wgRequest;
632 global $wgLinkCache, $IP, $wgEnableParserCache;
633
634 $fname = 'Article::view';
635 wfProfileIn( $fname );
636
637 # Get variables from query string :P
638 $oldid = $wgRequest->getVal( 'oldid' );
639 $diff = $wgRequest->getVal( 'diff' );
640
641 $wgOut->setArticleFlag( true );
642 $wgOut->setRobotpolicy( 'index,follow' );
643
644 # If we got diff and oldid in the query, we want to see a
645 # diff page instead of the article.
646
647 if ( !is_null( $diff ) ) {
648 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
649 $de = new DifferenceEngine( intval($oldid), intval($diff) );
650 $de->showDiffPage();
651 wfProfileOut( $fname );
652 return;
653 }
654
655 if ( !is_null( $oldid ) and $this->checkTouched() ) {
656 if( $wgOut->checkLastModified( $this->mTouched ) ){
657 return;
658 } else if ( $this->tryFileCache() ) {
659 # tell wgOut that output is taken care of
660 $wgOut->disable();
661 $this->viewUpdates();
662 return;
663 }
664 }
665
666 # Should the parser cache be used?
667 if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) ) {
668 $pcache = true;
669 } else {
670 $pcache = false;
671 }
672
673 $outputDone = false;
674 if ( $pcache ) {
675 if ( $wgOut->tryParserCache( $this, $wgUser ) ) {
676 $outputDone = true;
677 }
678 }
679
680 if ( !$outputDone ) {
681 $text = $this->getContent( false ); # May change mTitle by following a redirect
682
683 # Another whitelist check in case oldid or redirects are altering the title
684 if ( !$this->mTitle->userCanRead() ) {
685 $wgOut->loginToUse();
686 $wgOut->output();
687 exit;
688 }
689
690
691 # We're looking at an old revision
692
693 if ( !empty( $oldid ) ) {
694 $this->setOldSubtitle();
695 $wgOut->setRobotpolicy( 'noindex,follow' );
696 }
697 if ( '' != $this->mRedirectedFrom ) {
698 $sk = $wgUser->getSkin();
699 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, '',
700 'redirect=no' );
701 $s = wfMsg( 'redirectedfrom', $redir );
702 $wgOut->setSubtitle( $s );
703
704 # Can't cache redirects
705 $pcache = false;
706 }
707
708 $wgLinkCache->preFill( $this->mTitle );
709
710 # wrap user css and user js in pre and don't parse
711 # XXX: use $this->mTitle->usCssJsSubpage() when php is fixed/ a workaround is found
712 if (
713 $this->mTitle->getNamespace() == Namespace::getUser() &&
714 preg_match('/\\/[\\w]+\\.(css|js)$/', $this->mTitle->getDBkey())
715 ) {
716 $wgOut->addWikiText( wfMsg('clearyourcache'));
717 $wgOut->addHTML( '<pre>'.htmlspecialchars($this->mContent)."\n</pre>" );
718 } else if ( $pcache ) {
719 $wgOut->addWikiText( $text, true, $this );
720 } else {
721 $wgOut->addWikiText( $text );
722 }
723 }
724 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
725
726 # Add link titles as META keywords
727 $wgOut->addMetaTags() ;
728
729 $this->viewUpdates();
730 wfProfileOut( $fname );
731 }
732
733 # Theoretically we could defer these whole insert and update
734 # functions for after display, but that's taking a big leap
735 # of faith, and we want to be able to report database
736 # errors at some point.
737
738 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
739 {
740 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
741 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer, $wgIsPg, $wgIsMySQL;
742
743 $fname = 'Article::insertNewArticle';
744
745 $this->mCountAdjustment = $this->isCountable( $text );
746
747 $ns = $this->mTitle->getNamespace();
748 $ttl = $this->mTitle->getDBkey();
749 $text = $this->preSaveTransform( $text );
750 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
751 else { $redir = 0; }
752
753 $now = wfTimestampNow();
754 $won = wfInvertTimestamp( $now );
755 wfSeedRandom();
756 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
757
758 if ($wgIsPg) {
759 $cur_id_column="cur_id,";
760 $cur_id=wfGetSQL(""," nextval('cur_cur_id_seq')");
761 $cur_id_value="{$cur_id},";
762 } else {
763 $cur_id_column="";
764 $cur_id="";
765 $cur_id_value="";
766 }
767
768 $isminor = ( $isminor && $wgUser->getID() ) ? 1 : 0;
769 $sql = "INSERT INTO cur ({$cur_id_column}cur_namespace,cur_title,cur_text," .
770 'cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter,' .
771 'cur_restrictions,cur_user_text,cur_is_redirect,' .
772 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$cur_id_value}{$ns},'" . wfStrencode( $ttl ) . "', '" .
773 wfStrencode( $text ) . "', '" .
774 wfStrencode( $summary ) . "', '" .
775 $wgUser->getID() . "', '{$now}', " .
776 $isminor . ", 0, '', '" .
777 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
778 $res = wfQuery( $sql, DB_WRITE, $fname );
779
780 $newid = $wgIsPg?$cur_id:wfInsertId();
781 $this->mTitle->resetArticleID( $newid );
782
783 Article::onArticleCreate( $this->mTitle );
784 RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
785
786 if ($watchthis) {
787 if(!$this->mTitle->userIsWatching()) $this->watch();
788 } else {
789 if ( $this->mTitle->userIsWatching() ) {
790 $this->unwatch();
791 }
792 }
793
794 # The talk page isn't in the regular link tables, so we need to update manually:
795 $talkns = $ns ^ 1; # talk -> normal; normal -> talk
796 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
797 wfQuery( $sql, DB_WRITE );
798
799 # standard deferred updates
800 $this->editUpdates( $text );
801
802 $this->showArticle( $text, wfMsg( 'newarticle' ) );
803 }
804
805
806 /* Side effects: loads last edit */
807 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ''){
808 $this->loadLastEdit();
809 $oldtext = $this->getContent( true );
810 if ($section != '') {
811 if($section=='new') {
812 if($summary) $subject="== {$summary} ==\n\n";
813 $text=$oldtext."\n\n".$subject.$text;
814 } else {
815
816 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
817 # comments to be stripped as well)
818 $striparray=array();
819 $parser=new Parser();
820 $parser->mOutputType=OT_WIKI;
821 $oldtext=$parser->strip($oldtext, $striparray, true);
822
823 # now that we can be sure that no pseudo-sections are in the source,
824 # split it up
825 # Unfortunately we can't simply do a preg_replace because that might
826 # replace the wrong section, so we have to use the section counter instead
827 $secs=preg_split('/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
828 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
829 $secs[$section*2]=$text."\n\n"; // replace with edited
830
831 # section 0 is top (intro) section
832 if($section!=0) {
833
834 # headline of old section - we need to go through this section
835 # to determine if there are any subsections that now need to
836 # be erased, as the mother section has been replaced with
837 # the text of all subsections.
838 $headline=$secs[$section*2-1];
839 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
840 $hlevel=$matches[1];
841
842 # determine headline level for wikimarkup headings
843 if(strpos($hlevel,'=')!==false) {
844 $hlevel=strlen($hlevel);
845 }
846
847 $secs[$section*2-1]=''; // erase old headline
848 $count=$section+1;
849 $break=false;
850 while(!empty($secs[$count*2-1]) && !$break) {
851
852 $subheadline=$secs[$count*2-1];
853 preg_match(
854 '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
855 $subhlevel=$matches[1];
856 if(strpos($subhlevel,'=')!==false) {
857 $subhlevel=strlen($subhlevel);
858 }
859 if($subhlevel > $hlevel) {
860 // erase old subsections
861 $secs[$count*2-1]='';
862 $secs[$count*2]='';
863 }
864 if($subhlevel <= $hlevel) {
865 $break=true;
866 }
867 $count++;
868
869 }
870
871 }
872 $text=join('',$secs);
873 # reinsert the stuff that we stripped out earlier
874 $text=$parser->unstrip($text,$striparray);
875 $text=$parser->unstripNoWiki($text,$striparray);
876 }
877
878 }
879 return $text;
880 }
881
882 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' )
883 {
884 global $wgOut, $wgUser, $wgLinkCache;
885 global $wgDBtransactions, $wgMwRedir;
886 global $wgUseSquid, $wgInternalServer;
887 global $wgIsPg;
888 $fname = 'Article::updateArticle';
889
890 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
891 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
892 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ')[^\\n]+)/i', $text, $m ) ) {
893 $redir = 1;
894 $text = $m[1] . "\n"; # Remove all content but redirect
895 }
896 else { $redir = 0; }
897
898 $text = $this->preSaveTransform( $text );
899
900 # Update article, but only if changed.
901
902 if( $wgDBtransactions ) {
903 $sql = 'BEGIN';
904 wfQuery( $sql, DB_WRITE );
905 }
906 $oldtext = $this->getContent( true );
907
908 if ( 0 != strcmp( $text, $oldtext ) ) {
909 $this->mCountAdjustment = $this->isCountable( $text )
910 - $this->isCountable( $oldtext );
911
912 $now = wfTimestampNow();
913 $won = wfInvertTimestamp( $now );
914 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
915 "',cur_comment='" . wfStrencode( $summary ) .
916 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
917 ",cur_timestamp='{$now}',cur_user_text='" .
918 wfStrencode( $wgUser->getName() ) .
919 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
920 "WHERE cur_id=" . $this->getID() .
921 " AND cur_timestamp='" . $this->getTimestamp() . "'";
922 $res = wfQuery( $sql, DB_WRITE, $fname );
923
924 if( wfAffectedRows() == 0 ) {
925 /* Belated edit conflict! Run away!! */
926 return false;
927 }
928
929 # This overwrites $oldtext if revision compression is on
930 $flags = Article::compressRevisionText( $oldtext );
931
932 $oldtable=$wgIsPg?'"old"':'old';
933 if ($wgIsPg) {
934 $oldtable='"old"';
935 $old_id_column='old_id,';
936 $old_id=wfGetSQL(""," nextval('old_old_id_seq')");
937 $old_id_value=$old_id.',';
938 } else {
939 $oldtable='old';
940 $old_id_column='';
941 $old_id_value='';
942 }
943
944 $sql = "INSERT INTO $oldtable ({$old_id_column}old_namespace,old_title,old_text," .
945 'old_comment,old_user,old_user_text,old_timestamp,' .
946 'old_minor_edit,inverse_timestamp,old_flags) VALUES (' .
947 $old_id_value.
948 $this->mTitle->getNamespace() . ", '" .
949 wfStrencode( $this->mTitle->getDBkey() ) . "', '" .
950 wfStrencode( $oldtext ) . "', '" .
951 wfStrencode( $this->getComment() ) . "', " .
952 $this->getUser() . ", '" .
953 wfStrencode( $this->getUserText() ) . "', '" .
954 $this->getTimestamp() . "', " . $me1 . ", '" .
955 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
956 $res = wfQuery( $sql, DB_WRITE, $fname );
957
958 $oldid = $wgIsPg?$old_id:wfInsertId( $res );
959
960 $bot = (int)($wgUser->isBot() || $forceBot);
961 RecentChange::notifyEdit( $now, $this->mTitle, $me2, $wgUser, $summary,
962 $oldid, $this->getTimestamp(), $bot );
963 Article::onArticleEdit( $this->mTitle );
964 }
965
966 if( $wgDBtransactions ) {
967 $sql = 'COMMIT';
968 wfQuery( $sql, DB_WRITE );
969 }
970
971 if ($watchthis) {
972 if (!$this->mTitle->userIsWatching()) $this->watch();
973 } else {
974 if ( $this->mTitle->userIsWatching() ) {
975 $this->unwatch();
976 }
977 }
978 # standard deferred updates
979 $this->editUpdates( $text );
980
981
982 $urls = array();
983 # Template namespace
984 # Purge all articles linking here
985 if ( $this->mTitle->getNamespace() == NS_TEMPLATE) {
986 $titles = $this->mTitle->getLinksTo();
987 Title::touchArray( $titles );
988 if ( $wgUseSquid ) {
989 foreach ( $titles as $title ) {
990 $urls[] = $title->getInternalURL();
991 }
992 }
993 }
994
995 # Squid updates
996 if ( $wgUseSquid ) {
997 $urls = array_merge( $urls, $this->mTitle->getSquidURLs() );
998 $u = new SquidUpdate( $urls );
999 $u->doUpdate();
1000 }
1001
1002 $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor );
1003 return true;
1004 }
1005
1006 # After we've either updated or inserted the article, update
1007 # the link tables and redirect to the new page.
1008
1009 function showArticle( $text, $subtitle , $sectionanchor = '' )
1010 {
1011 global $wgOut, $wgUser, $wgLinkCache;
1012 global $wgMwRedir;
1013
1014 $wgLinkCache = new LinkCache();
1015
1016 # Get old version of link table to allow incremental link updates
1017 $wgLinkCache->preFill( $this->mTitle );
1018 $wgLinkCache->clear();
1019
1020 # Now update the link cache by parsing the text
1021 $wgOut = new OutputPage();
1022 $wgOut->addWikiText( $text );
1023
1024 if( $wgMwRedir->matchStart( $text ) )
1025 $r = 'redirect=no';
1026 else
1027 $r = '';
1028 $wgOut->redirect( $this->mTitle->getFullURL( $r ).$sectionanchor );
1029 }
1030
1031 # Add this page to my watchlist
1032
1033 function watch( $add = true )
1034 {
1035 global $wgUser, $wgOut, $wgLang;
1036 global $wgDeferredUpdateList;
1037
1038 if ( 0 == $wgUser->getID() ) {
1039 $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
1040 return;
1041 }
1042 if ( wfReadOnly() ) {
1043 $wgOut->readOnlyPage();
1044 return;
1045 }
1046 if( $add )
1047 $wgUser->addWatch( $this->mTitle );
1048 else
1049 $wgUser->removeWatch( $this->mTitle );
1050
1051 $wgOut->setPagetitle( wfMsg( $add ? 'addedwatch' : 'removedwatch' ) );
1052 $wgOut->setRobotpolicy( 'noindex,follow' );
1053
1054 $sk = $wgUser->getSkin() ;
1055 $link = $this->mTitle->getPrefixedText();
1056
1057 if($add)
1058 $text = wfMsg( 'addedwatchtext', $link );
1059 else
1060 $text = wfMsg( 'removedwatchtext', $link );
1061 $wgOut->addWikiText( $text );
1062
1063 $up = new UserUpdate();
1064 array_push( $wgDeferredUpdateList, $up );
1065
1066 $wgOut->returnToMain( false );
1067 }
1068
1069 function unwatch()
1070 {
1071 $this->watch( false );
1072 }
1073
1074 function protect( $limit = 'sysop' )
1075 {
1076 global $wgUser, $wgOut, $wgRequest;
1077
1078 if ( ! $wgUser->isSysop() ) {
1079 $wgOut->sysopRequired();
1080 return;
1081 }
1082 if ( wfReadOnly() ) {
1083 $wgOut->readOnlyPage();
1084 return;
1085 }
1086 $id = $this->mTitle->getArticleID();
1087 if ( 0 == $id ) {
1088 $wgOut->fatalError( wfMsg( 'badarticleerror' ) );
1089 return;
1090 }
1091
1092 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
1093 $reason = $wgRequest->getText( 'wpReasonProtect' );
1094
1095 if ( $confirm ) {
1096
1097 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
1098 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
1099 wfQuery( $sql, DB_WRITE, 'Article::protect' );
1100
1101 $log = new LogPage( wfMsg( 'protectlogpage' ), wfMsg( 'protectlogtext' ) );
1102 if ( $limit === "" ) {
1103 $log->addEntry( wfMsg( 'unprotectedarticle', $this->mTitle->getPrefixedText() ), $reason );
1104 } else {
1105 $log->addEntry( wfMsg( 'protectedarticle', $this->mTitle->getPrefixedText() ), $reason );
1106 }
1107 $wgOut->redirect( $this->mTitle->getFullURL() );
1108 return;
1109 } else {
1110 $reason = htmlspecialchars( wfMsg( 'protectreason' ) );
1111 return $this->confirmProtect( '', $reason, $limit );
1112 }
1113 }
1114
1115 # Output protection confirmation dialog
1116 function confirmProtect( $par, $reason, $limit = 'sysop' )
1117 {
1118 global $wgOut;
1119
1120 wfDebug( "Article::confirmProtect\n" );
1121
1122 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
1123 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1124
1125 $check = '';
1126 $protcom = '';
1127
1128 if ( $limit === '' ) {
1129 $wgOut->setPageTitle( wfMsg( 'confirmunprotect' ) );
1130 $wgOut->setSubtitle( wfMsg( 'unprotectsub', $sub ) );
1131 $wgOut->addWikiText( wfMsg( 'confirmunprotecttext' ) );
1132 $check = htmlspecialchars( wfMsg( 'confirmunprotect' ) );
1133 $protcom = htmlspecialchars( wfMsg( 'unprotectcomment' ) );
1134 $formaction = $this->mTitle->escapeLocalURL( 'action=unprotect' . $par );
1135 } else {
1136 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
1137 $wgOut->setSubtitle( wfMsg( 'protectsub', $sub ) );
1138 $wgOut->addWikiText( wfMsg( 'confirmprotecttext' ) );
1139 $check = htmlspecialchars( wfMsg( 'confirmprotect' ) );
1140 $protcom = htmlspecialchars( wfMsg( 'protectcomment' ) );
1141 $formaction = $this->mTitle->escapeLocalURL( 'action=protect' . $par );
1142 }
1143
1144 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1145
1146 $wgOut->addHTML( "
1147 <form id='protectconfirm' method='post' action=\"{$formaction}\">
1148 <table border='0'>
1149 <tr>
1150 <td align='right'>
1151 <label for='wpReasonProtect'>{$protcom}:</label>
1152 </td>
1153 <td align='left'>
1154 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
1155 </td>
1156 </tr>
1157 <tr>
1158 <td>&nbsp;</td>
1159 </tr>
1160 <tr>
1161 <td align='right'>
1162 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
1163 </td>
1164 <td>
1165 <label for='wpConfirmProtect'>{$check}</label>
1166 </td>
1167 </tr>
1168 <tr>
1169 <td>&nbsp;</td>
1170 <td>
1171 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
1172 </td>
1173 </tr>
1174 </table>
1175 </form>\n" );
1176
1177 $wgOut->returnToMain( false );
1178 }
1179
1180 function unprotect()
1181 {
1182 return $this->protect( '' );
1183 }
1184
1185 # UI entry point for page deletion
1186 function delete()
1187 {
1188 global $wgUser, $wgOut, $wgMessageCache, $wgRequest, $wgIsPg;
1189 $fname = 'Article::delete';
1190 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
1191 $reason = $wgRequest->getText( 'wpReason' );
1192
1193 # This code desperately needs to be totally rewritten
1194
1195 # Check permissions
1196 if ( ( ! $wgUser->isSysop() ) ) {
1197 $wgOut->sysopRequired();
1198 return;
1199 }
1200 if ( wfReadOnly() ) {
1201 $wgOut->readOnlyPage();
1202 return;
1203 }
1204
1205 # Better double-check that it hasn't been deleted yet!
1206 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
1207 if ( ( '' == trim( $this->mTitle->getText() ) )
1208 or ( $this->mTitle->getArticleId() == 0 ) ) {
1209 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1210 return;
1211 }
1212
1213 if ( $confirm ) {
1214 $this->doDelete( $reason );
1215 return;
1216 }
1217
1218 # determine whether this page has earlier revisions
1219 # and insert a warning if it does
1220 # we select the text because it might be useful below
1221 $ns = $this->mTitle->getNamespace();
1222 $title = $this->mTitle->getDBkey();
1223 $etitle = wfStrencode( $title );
1224 $oldtable=$wgIsPg?'"old"':'old';
1225 $sql = "SELECT old_text,old_flags FROM $oldtable WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
1226 $res = wfQuery( $sql, DB_READ, $fname );
1227 if( ($old=wfFetchObject($res)) && !$confirm ) {
1228 $skin=$wgUser->getSkin();
1229 $wgOut->addHTML('<b>'.wfMsg('historywarning'));
1230 $wgOut->addHTML( $skin->historyLink() .'</b>');
1231 }
1232
1233 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
1234 $res=wfQuery($sql, DB_READ, $fname);
1235 if( ($s=wfFetchObject($res))) {
1236
1237 # if this is a mini-text, we can paste part of it into the deletion reason
1238
1239 #if this is empty, an earlier revision may contain "useful" text
1240 $blanked = false;
1241 if($s->cur_text!="") {
1242 $text=$s->cur_text;
1243 } else {
1244 if($old) {
1245 $text = Article::getRevisionText( $old );
1246 $blanked = true;
1247 }
1248
1249 }
1250
1251 $length=strlen($text);
1252
1253 # this should not happen, since it is not possible to store an empty, new
1254 # page. Let's insert a standard text in case it does, though
1255 if($length == 0 && $reason === '') {
1256 $reason = wfMsg('exblank');
1257 }
1258
1259 if($length < 500 && $reason === '') {
1260
1261 # comment field=255, let's grep the first 150 to have some user
1262 # space left
1263 $text=substr($text,0,150);
1264 # let's strip out newlines and HTML tags
1265 $text=preg_replace('/\"/',"'",$text);
1266 $text=preg_replace('/\</','&lt;',$text);
1267 $text=preg_replace('/\>/','&gt;',$text);
1268 $text=preg_replace("/[\n\r]/",'',$text);
1269 if(!$blanked) {
1270 $reason=wfMsg('excontent'). " '".$text;
1271 } else {
1272 $reason=wfMsg('exbeforeblank') . " '".$text;
1273 }
1274 if($length>150) { $reason .= '...'; } # we've only pasted part of the text
1275 $reason.="'";
1276 }
1277 }
1278
1279 return $this->confirmDelete( '', $reason );
1280 }
1281
1282 # Output deletion confirmation dialog
1283 function confirmDelete( $par, $reason )
1284 {
1285 global $wgOut;
1286
1287 wfDebug( "Article::confirmDelete\n" );
1288
1289 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
1290 $wgOut->setSubtitle( wfMsg( 'deletesub', $sub ) );
1291 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1292 $wgOut->addWikiText( wfMsg( 'confirmdeletetext' ) );
1293
1294 $formaction = $this->mTitle->escapeLocalURL( 'action=delete' . $par );
1295
1296 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1297 $check = htmlspecialchars( wfMsg( 'confirmcheck' ) );
1298 $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
1299
1300 $wgOut->addHTML( "
1301 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1302 <table border='0'>
1303 <tr>
1304 <td align='right'>
1305 <label for='wpReason'>{$delcom}:</label>
1306 </td>
1307 <td align='left'>
1308 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1309 </td>
1310 </tr>
1311 <tr>
1312 <td>&nbsp;</td>
1313 </tr>
1314 <tr>
1315 <td align='right'>
1316 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1317 </td>
1318 <td>
1319 <label for='wpConfirm'>{$check}</label>
1320 </td>
1321 </tr>
1322 <tr>
1323 <td>&nbsp;</td>
1324 <td>
1325 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1326 </td>
1327 </tr>
1328 </table>
1329 </form>\n" );
1330
1331 $wgOut->returnToMain( false );
1332 }
1333
1334 # Perform a deletion and output success or failure messages
1335 function doDelete( $reason )
1336 {
1337 global $wgOut, $wgUser, $wgLang;
1338 $fname = 'Article::doDelete';
1339 wfDebug( "$fname\n" );
1340
1341 if ( $this->doDeleteArticle( $reason ) ) {
1342 $deleted = $this->mTitle->getPrefixedText();
1343
1344 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1345 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1346
1347 $sk = $wgUser->getSkin();
1348 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1349 Namespace::getWikipedia() ) .
1350 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
1351
1352 $text = wfMsg( "deletedtext", $deleted, $loglink );
1353
1354 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
1355 $wgOut->returnToMain( false );
1356 } else {
1357 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1358 }
1359 }
1360
1361 # Back-end article deletion
1362 # Deletes the article with database consistency, writes logs, purges caches
1363 # Returns success
1364 function doDeleteArticle( $reason )
1365 {
1366 global $wgUser, $wgLang;
1367 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1368
1369 $fname = 'Article::doDeleteArticle';
1370 wfDebug( $fname."\n" );
1371
1372 $ns = $this->mTitle->getNamespace();
1373 $t = wfStrencode( $this->mTitle->getDBkey() );
1374 $id = $this->mTitle->getArticleID();
1375
1376 if ( '' == $t || $id == 0 ) {
1377 return false;
1378 }
1379
1380 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1381 array_push( $wgDeferredUpdateList, $u );
1382
1383 $linksTo = $this->mTitle->getLinksTo();
1384
1385 # Squid purging
1386 if ( $wgUseSquid ) {
1387 $urls = array(
1388 $this->mTitle->getInternalURL(),
1389 $this->mTitle->getInternalURL( 'history' )
1390 );
1391 foreach ( $linksTo as $linkTo ) {
1392 $urls[] = $linkTo->getInternalURL();
1393 }
1394
1395 $u = new SquidUpdate( $urls );
1396 array_push( $wgDeferredUpdateList, $u );
1397
1398 }
1399
1400 # Client and file cache invalidation
1401 Title::touchArray( $linksTo );
1402
1403 # Move article and history to the "archive" table
1404 $sql = 'INSERT INTO archive (ar_namespace,ar_title,ar_text,' .
1405 'ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,' .
1406 'ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment,' .
1407 'cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur ' .
1408 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
1409 wfQuery( $sql, DB_WRITE, $fname );
1410
1411 $sql = 'INSERT INTO archive (ar_namespace,ar_title,ar_text,' .
1412 'ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,' .
1413 'ar_flags) SELECT old_namespace,old_title,old_text,old_comment,' .
1414 'old_user,old_user_text,old_timestamp,old_minor_edit,old_flags ' .
1415 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1416 wfQuery( $sql, DB_WRITE, $fname );
1417
1418 # Now that it's safely backed up, delete it
1419
1420 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1421 "cur_title='{$t}'";
1422 wfQuery( $sql, DB_WRITE, $fname );
1423
1424 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1425 "old_title='{$t}'";
1426 wfQuery( $sql, DB_WRITE, $fname );
1427
1428 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1429 "rc_title='{$t}'";
1430 wfQuery( $sql, DB_WRITE, $fname );
1431
1432 # Finally, clean up the link tables
1433 $t = wfStrencode( $this->mTitle->getPrefixedDBkey() );
1434
1435 Article::onArticleDelete( $this->mTitle );
1436
1437 $sql = 'INSERT INTO brokenlinks (bl_from,bl_to) VALUES ';
1438 $first = true;
1439
1440 foreach ( $linksTo as $titleObj ) {
1441 if ( ! $first ) { $sql .= ','; }
1442 $first = false;
1443 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1444 $linkID = $titleObj->getArticleID();
1445 $sql .= "({$linkID},'{$t}')";
1446 }
1447 if ( ! $first ) {
1448 wfQuery( $sql, DB_WRITE, $fname );
1449 }
1450
1451 $sql = "DELETE FROM links WHERE l_to={$id}";
1452 wfQuery( $sql, DB_WRITE, $fname );
1453
1454 $sql = "DELETE FROM links WHERE l_from={$id}";
1455 wfQuery( $sql, DB_WRITE, $fname );
1456
1457 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
1458 wfQuery( $sql, DB_WRITE, $fname );
1459
1460 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1461 wfQuery( $sql, DB_WRITE, $fname );
1462
1463 $sql = "DELETE FROM categorylinks WHERE cl_from={$id}";
1464 wfQuery( $sql, DB_WRITE, $fname );
1465
1466 $log = new LogPage( wfMsg( 'dellogpage' ), wfMsg( 'dellogpagetext' ) );
1467 $art = $this->mTitle->getPrefixedText();
1468 $log->addEntry( wfMsg( 'deletedarticle', $art ), $reason );
1469
1470 # Clear the cached article id so the interface doesn't act like we exist
1471 $this->mTitle->resetArticleID( 0 );
1472 $this->mTitle->mArticleID = 0;
1473 return true;
1474 }
1475
1476 function rollback()
1477 {
1478 global $wgUser, $wgLang, $wgOut, $wgRequest, $wgIsMySQL, $wgIsPg;
1479
1480 if ( ! $wgUser->isSysop() ) {
1481 $wgOut->sysopRequired();
1482 return;
1483 }
1484 if ( wfReadOnly() ) {
1485 $wgOut->readOnlyPage( $this->getContent( true ) );
1486 return;
1487 }
1488
1489 # Enhanced rollback, marks edits rc_bot=1
1490 $bot = $wgRequest->getBool( 'bot' );
1491
1492 # Replace all this user's current edits with the next one down
1493 $tt = wfStrencode( $this->mTitle->getDBKey() );
1494 $n = $this->mTitle->getNamespace();
1495
1496 # Get the last editor
1497 $sql = 'SELECT cur_id,cur_user,cur_user_text,cur_comment ' .
1498 "FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1499 $res = wfQuery( $sql, DB_READ );
1500 if( ($x = wfNumRows( $res )) != 1 ) {
1501 # Something wrong
1502 $wgOut->addHTML( wfMsg( 'notanarticle' ) );
1503 return;
1504 }
1505 $s = wfFetchObject( $res );
1506 $ut = wfStrencode( $s->cur_user_text );
1507 $uid = $s->cur_user;
1508 $pid = $s->cur_id;
1509
1510 $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
1511 if( $from != $s->cur_user_text ) {
1512 $wgOut->setPageTitle(wfmsg('rollbackfailed'));
1513 $wgOut->addWikiText( wfMsg( 'alreadyrolled',
1514 htmlspecialchars( $this->mTitle->getPrefixedText()),
1515 htmlspecialchars( $from ),
1516 htmlspecialchars( $s->cur_user_text ) ) );
1517 if($s->cur_comment != '') {
1518 $wgOut->addHTML(
1519 wfMsg('editcomment',
1520 htmlspecialchars( $s->cur_comment ) ) );
1521 }
1522 return;
1523 }
1524
1525 # Get the last edit not by this guy
1526
1527 $use_index=$wgIsMySQL?"USE INDEX (name_title_timestamp)":"";
1528 $oldtable=$wgIsPg?'"old"':'old';
1529 $sql = 'SELECT old_text,old_user,old_user_text,old_timestamp,old_flags ' .
1530 "FROM $oldtable {$use_index} " .
1531 "WHERE old_namespace={$n} AND old_title='{$tt}' " .
1532 "AND (old_user <> {$uid} OR old_user_text <> '{$ut}') " .
1533 'ORDER BY inverse_timestamp LIMIT 1';
1534 $res = wfQuery( $sql, DB_READ );
1535 if( wfNumRows( $res ) != 1 ) {
1536 # Something wrong
1537 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
1538 $wgOut->addHTML( wfMsg( 'cantrollback' ) );
1539 return;
1540 }
1541 $s = wfFetchObject( $res );
1542
1543 if ( $bot ) {
1544 # Mark all reverted edits as bot
1545 $sql = 'UPDATE recentchanges SET rc_bot=1 WHERE ' .
1546 "rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1547 wfQuery( $sql, DB_WRITE, $fname );
1548 }
1549
1550 # Save it!
1551 $newcomment = wfMsg( 'revertpage', $s->old_user_text, $from );
1552 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1553 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1554 $wgOut->addHTML( '<h2>' . $newcomment . "</h2>\n<hr />\n" );
1555 $this->updateArticle( Article::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
1556 Article::onArticleEdit( $this->mTitle );
1557 $wgOut->returnToMain( false );
1558 }
1559
1560
1561 # Do standard deferred updates after page view
1562
1563 /* private */ function viewUpdates()
1564 {
1565 global $wgDeferredUpdateList;
1566 if ( 0 != $this->getID() ) {
1567 global $wgDisableCounters;
1568 if( !$wgDisableCounters ) {
1569 Article::incViewCount( $this->getID() );
1570 $u = new SiteStatsUpdate( 1, 0, 0 );
1571 array_push( $wgDeferredUpdateList, $u );
1572 }
1573 $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
1574 $this->mTitle->getDBkey() );
1575 array_push( $wgDeferredUpdateList, $u );
1576 }
1577 }
1578
1579 # Do standard deferred updates after page edit.
1580 # Every 1000th edit, prune the recent changes table.
1581
1582 /* private */ function editUpdates( $text )
1583 {
1584 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1585 global $wgMessageCache;
1586
1587 wfSeedRandom();
1588 if ( 0 == mt_rand( 0, 999 ) ) {
1589 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1590 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1591 wfQuery( $sql, DB_WRITE );
1592 }
1593 $id = $this->getID();
1594 $title = $this->mTitle->getPrefixedDBkey();
1595 $shortTitle = $this->mTitle->getDBkey();
1596
1597 $adj = $this->mCountAdjustment;
1598
1599 if ( 0 != $id ) {
1600 $u = new LinksUpdate( $id, $title );
1601 array_push( $wgDeferredUpdateList, $u );
1602 $u = new SiteStatsUpdate( 0, 1, $adj );
1603 array_push( $wgDeferredUpdateList, $u );
1604 $u = new SearchUpdate( $id, $title, $text );
1605 array_push( $wgDeferredUpdateList, $u );
1606
1607 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(), $shortTitle );
1608 array_push( $wgDeferredUpdateList, $u );
1609
1610 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
1611 $wgMessageCache->replace( $shortTitle, $text );
1612 }
1613 }
1614 }
1615
1616 /* private */ function setOldSubtitle()
1617 {
1618 global $wgLang, $wgOut;
1619
1620 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1621 $r = wfMsg( 'revisionasof', $td );
1622 $wgOut->setSubtitle( "({$r})" );
1623 }
1624
1625 # This function is called right before saving the wikitext,
1626 # so we can do things like signatures and links-in-context.
1627
1628 function preSaveTransform( $text )
1629 {
1630 global $wgParser, $wgUser;
1631 return $wgParser->preSaveTransform( $text, $this->mTitle, $wgUser, ParserOptions::newFromUser( $wgUser ) );
1632 }
1633
1634 /* Caching functions */
1635
1636 # checkLastModified returns true if it has taken care of all
1637 # output to the client that is necessary for this request.
1638 # (that is, it has sent a cached version of the page)
1639 function tryFileCache() {
1640 static $called = false;
1641 if( $called ) {
1642 wfDebug( " tryFileCache() -- called twice!?\n" );
1643 return;
1644 }
1645 $called = true;
1646 if($this->isFileCacheable()) {
1647 $touched = $this->mTouched;
1648 if( $this->mTitle->getPrefixedDBkey() == wfMsg( 'mainpage' ) ) {
1649 # Expire the main page quicker
1650 $expire = wfUnix2Timestamp( time() - 3600 );
1651 $touched = max( $expire, $touched );
1652 }
1653 $cache = new CacheManager( $this->mTitle );
1654 if($cache->isFileCacheGood( $touched )) {
1655 global $wgOut;
1656 wfDebug( " tryFileCache() - about to load\n" );
1657 $cache->loadFromFileCache();
1658 return true;
1659 } else {
1660 wfDebug( " tryFileCache() - starting buffer\n" );
1661 ob_start( array(&$cache, 'saveToFileCache' ) );
1662 }
1663 } else {
1664 wfDebug( " tryFileCache() - not cacheable\n" );
1665 }
1666 }
1667
1668 function isFileCacheable() {
1669 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1670 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1671
1672 return $wgUseFileCache
1673 and (!$wgShowIPinHeader)
1674 and ($this->getID() != 0)
1675 and ($wgUser->getId() == 0)
1676 and (!$wgUser->getNewtalk())
1677 and ($this->mTitle->getNamespace() != Namespace::getSpecial())
1678 and ($action == 'view')
1679 and (!isset($oldid))
1680 and (!isset($diff))
1681 and (!isset($redirect))
1682 and (!isset($printable))
1683 and (!$this->mRedirectedFrom);
1684 }
1685
1686 # Loads cur_touched and returns a value indicating if it should be used
1687 function checkTouched() {
1688 $id = $this->getID();
1689 $sql = 'SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id='.$id;
1690 $res = wfQuery( $sql, DB_READ, 'Article::checkTouched' );
1691 if( $s = wfFetchObject( $res ) ) {
1692 $this->mTouched = $s->cur_touched;
1693 return !$s->cur_is_redirect;
1694 } else {
1695 return false;
1696 }
1697 }
1698
1699 # Edit an article without doing all that other stuff
1700 function quickEdit( $text, $comment = '', $minor = 0 ) {
1701 global $wgUser, $wgMwRedir, $wgIsPg;
1702 $fname = 'Article::quickEdit';
1703 wfProfileIn( $fname );
1704
1705 $ns = $this->mTitle->getNamespace();
1706 $dbkey = $this->mTitle->getDBkey();
1707 $encDbKey = wfStrencode( $dbkey );
1708 $timestamp = wfTimestampNow();
1709
1710 # Save to history
1711 $oldtable=$wgIsPg?'"old"':'old';
1712 $sql = "INSERT INTO $oldtable (old_namespace,old_title,old_text,old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp)
1713 SELECT cur_namespace,cur_title,cur_text,cur_comment,cur_user,cur_user_text,cur_timestamp,99999999999999-cur_timestamp
1714 FROM cur WHERE cur_namespace=$ns AND cur_title='$encDbKey'";
1715 wfQuery( $sql, DB_WRITE );
1716
1717 # Use the affected row count to determine if the article is new
1718 $numRows = wfAffectedRows();
1719
1720 # Make an array of fields to be inserted
1721 $fields = array(
1722 'cur_text' => $text,
1723 'cur_timestamp' => $timestamp,
1724 'cur_user' => $wgUser->getID(),
1725 'cur_user_text' => $wgUser->getName(),
1726 'inverse_timestamp' => wfInvertTimestamp( $timestamp ),
1727 'cur_comment' => $comment,
1728 'cur_is_redirect' => $wgMwRedir->matchStart( $text ) ? 1 : 0,
1729 'cur_minor_edit' => intval($minor),
1730 'cur_touched' => $timestamp,
1731 );
1732
1733 if ( $numRows ) {
1734 # Update article
1735 $fields['cur_is_new'] = 0;
1736 wfUpdateArray( 'cur', $fields, array( 'cur_namespace' => $ns, 'cur_title' => $dbkey ), $fname );
1737 } else {
1738 # Insert new article
1739 $fields['cur_is_new'] = 1;
1740 $fields['cur_namespace'] = $ns;
1741 $fields['cur_title'] = $dbkey;
1742 $fields['cur_random'] = $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1743 wfInsertArray( 'cur', $fields, $fname );
1744 }
1745 wfProfileOut( $fname );
1746 }
1747
1748 /* static */ function incViewCount( $id )
1749 {
1750 $id = intval( $id );
1751 global $wgHitcounterUpdateFreq;
1752
1753 if( $wgHitcounterUpdateFreq <= 1 ){ //
1754 wfQuery('UPDATE cur SET cur_counter = cur_counter + 1 ' .
1755 'WHERE cur_id = '.$id, DB_WRITE);
1756 return;
1757 }
1758
1759 # Not important enough to warrant an error page in case of failure
1760 $oldignore = wfIgnoreSQLErrors( true );
1761
1762 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE);
1763
1764 $checkfreq = intval( $wgHitcounterUpdateFreq/25 + 1 );
1765 if( (rand() % $checkfreq != 0) or (wfLastErrno() != 0) ){
1766 # Most of the time (or on SQL errors), skip row count check
1767 wfIgnoreSQLErrors( $oldignore );
1768 return;
1769 }
1770
1771 $res = wfQuery('SELECT COUNT(*) as n FROM hitcounter', DB_WRITE);
1772 $row = wfFetchObject( $res );
1773 $rown = intval( $row->n );
1774 if( $rown >= $wgHitcounterUpdateFreq ){
1775 wfProfileIn( 'Article::incViewCount-collect' );
1776 $old_user_abort = ignore_user_abort( true );
1777
1778 wfQuery('LOCK TABLES hitcounter WRITE', DB_WRITE);
1779 wfQuery('CREATE TEMPORARY TABLE acchits TYPE=HEAP '.
1780 'SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter '.
1781 'GROUP BY hc_id', DB_WRITE);
1782 wfQuery('DELETE FROM hitcounter', DB_WRITE);
1783 wfQuery('UNLOCK TABLES', DB_WRITE);
1784 wfQuery('UPDATE cur,acchits SET cur_counter=cur_counter + hc_n '.
1785 'WHERE cur_id = hc_id', DB_WRITE);
1786 wfQuery('DROP TABLE acchits', DB_WRITE);
1787
1788 ignore_user_abort( $old_user_abort );
1789 wfProfileOut( 'Article::incViewCount-collect' );
1790 }
1791 wfIgnoreSQLErrors( $oldignore );
1792 }
1793
1794 # The onArticle*() functions are supposed to be a kind of hooks
1795 # which should be called whenever any of the specified actions
1796 # are done.
1797 #
1798 # This is a good place to put code to clear caches, for instance.
1799
1800 # This is called on page move and undelete, as well as edit
1801 /* static */ function onArticleCreate($title_obj){
1802 global $wgUseSquid, $wgDeferredUpdateList;
1803
1804 $titles = $title_obj->getBrokenLinksTo();
1805
1806 # Purge squid
1807 if ( $wgUseSquid ) {
1808 $urls = $title_obj->getSquidURLs();
1809 foreach ( $titles as $linkTitle ) {
1810 $urls[] = $linkTitle->getInternalURL();
1811 }
1812 $u = new SquidUpdate( $urls );
1813 array_push( $wgDeferredUpdateList, $u );
1814 }
1815
1816 # Clear persistent link cache
1817 LinkCache::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1818 }
1819
1820 /* static */ function onArticleDelete($title_obj){
1821 LinkCache::linksccClearLinksTo( $title_obj->getArticleID() );
1822 }
1823
1824 /* static */ function onArticleEdit($title_obj){
1825 LinkCache::linksccClearPage( $title_obj->getArticleID() );
1826 }
1827
1828 # Info about this page
1829
1830 function info()
1831 {
1832 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgAllowPageInfo;
1833
1834 if ( !$wgAllowPageInfo ) {
1835 $wgOut->errorpage( "nosuchaction", "nosuchactiontext" );
1836 return;
1837 }
1838
1839 $basenamespace = $wgTitle->getNamespace() & (~1);
1840 $cur_clause = "cur_title='".$wgTitle->getDBkey()."' AND cur_namespace=".$basenamespace;
1841 $old_clause = "old_title='".$wgTitle->getDBkey()."' AND old_namespace=".$basenamespace;
1842 $wl_clause = "wl_title='".$wgTitle->getDBkey()."' AND wl_namespace=".$basenamespace;
1843 $fullTitle = $wgTitle->makeName($basenamespace, $wgTitle->getDBKey());
1844 $wgOut->setPagetitle( $fullTitle );
1845 $wgOut->setSubtitle( wfMsg( "infosubtitle" ));
1846
1847 # first, see if the page exists at all.
1848 $sql = "SELECT COUNT(*) FROM cur WHERE ".$cur_clause;
1849 $exists = wfSingleQuery( $sql , DB_READ );
1850 if ($exists < 1) {
1851 $wgOut->addHTML( wfMsg("noarticletext") );
1852 } else {
1853 $sql = "SELECT COUNT(*) FROM watchlist WHERE ".$wl_clause;
1854 $wgOut->addHTML( "<ul><li>" . wfMsg("numwatchers") . wfSingleQuery( $sql, DB_READ ) . "</li>" );
1855 $sql = "SELECT COUNT(*) FROM old WHERE ".$old_clause;
1856 $old = wfSingleQuery( $sql, DB_READ );
1857 $wgOut->addHTML( "<li>" . wfMsg("numedits") . ($old + 1) . "</li>");
1858
1859 # to find number of distinct authors, we need to do some
1860 # funny stuff because of the cur/old table split:
1861 # - first, find the name of the 'cur' author
1862 # - then, find the number of *other* authors in 'old'
1863
1864 # find 'cur' author
1865 $sql = "SELECT cur_user_text FROM cur WHERE ".$cur_clause;
1866 $cur_author = wfSingleQuery( $sql, DB_READ );
1867
1868 # find number of 'old' authors excluding 'cur' author
1869 $sql = "SELECT COUNT(DISTINCT old_user_text) FROM old WHERE ".$old_clause
1870 ." AND old_user_text<>'" . $cur_author . "'";
1871 $authors = wfSingleQuery( $sql, DB_READ ) + 1;
1872
1873 # now for the Talk page ...
1874 $cur_clause = "cur_title='".$wgTitle->getDBkey()."' AND cur_namespace=".($basenamespace+1);
1875 $old_clause = "old_title='".$wgTitle->getDBkey()."' AND old_namespace=".($basenamespace+1);
1876
1877 # does it exist?
1878 $sql = "SELECT COUNT(*) FROM cur WHERE ".$cur_clause;
1879 $exists = wfSingleQuery( $sql , DB_READ );
1880
1881 # number of edits
1882 if ($exists > 0) {
1883 $sql = "SELECT COUNT(*) FROM old WHERE ".$old_clause;
1884 $old = wfSingleQuery( $sql, DB_READ );
1885 $wgOut->addHTML( "<li>" . wfMsg("numtalkedits") . ($old + 1) . "</li>");
1886 }
1887 $wgOut->addHTML( "<li>" . wfMsg("numauthors") . $authors . "</li>" );
1888
1889 # number of authors
1890 if ($exists > 0) {
1891 $sql = "SELECT cur_user_text FROM cur WHERE ".$cur_clause;
1892 $cur_author = wfSingleQuery( $sql, DB_READ );
1893
1894 $sql = "SELECT COUNT(DISTINCT old_user_text) FROM old WHERE "
1895 .$old_clause." AND old_user_text<>'" . $cur_author . "'";
1896 $authors = wfSingleQuery( $sql, DB_READ ) + 1;
1897
1898 $wgOut->addHTML( "<li>" . wfMsg("numtalkauthors") . $authors . "</li></ul>" );
1899 }
1900 }
1901 }
1902 }
1903
1904 ?>