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