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