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