873326cb72b606eb7133fc8755d5931156212bce
[lhc/web/wiklou.git] / includes / Article.php
1 <?
2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
4
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
7
8 include_once( "CacheManager.php" );
9
10 class Article {
11 /* private */ var $mContent, $mContentLoaded;
12 /* private */ var $mUser, $mTimestamp, $mUserText;
13 /* private */ var $mCounter, $mComment, $mCountAdjustment;
14 /* private */ var $mMinorEdit, $mRedirectedFrom;
15 /* private */ var $mTouched, $mFileCache;
16
17 function Article( &$title ) {
18 $this->mTitle =& $title;
19 $this->clear();
20 }
21
22 /* private */ function clear()
23 {
24 $this->mContentLoaded = false;
25 $this->mUser = $this->mCounter = -1; # Not loaded
26 $this->mRedirectedFrom = $this->mUserText =
27 $this->mTimestamp = $this->mComment = $this->mFileCache = "";
28 $this->mCountAdjustment = 0;
29 $this->mTouched = "19700101000000";
30 }
31
32 # Note that getContent/loadContent may follow redirects if
33 # not told otherwise, and so may cause a change to wgTitle.
34
35 function getContent( $noredir = false )
36 {
37 global $action,$section,$count; # From query string
38 wfProfileIn( "Article::getContent" );
39
40 if ( 0 == $this->getID() ) {
41 if ( "edit" == $action ) {
42 return ""; # was "newarticletext", now moved above the box)
43 }
44 wfProfileOut();
45 return wfMsg( "noarticletext" );
46 } else {
47 $this->loadContent( $noredir );
48 wfProfileOut();
49
50 if(
51 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
52 ( $this->mTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
53 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle->getText()) &&
54 $action=="view"
55 )
56 {
57 return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
58 else {
59 if($action=="edit") {
60 if($section!="") {
61 if($section=="new") { return ""; }
62
63 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
64 $this->mContent, -1,
65 PREG_SPLIT_DELIM_CAPTURE);
66 if($section==0) {
67 return trim($secs[0]);
68 } else {
69 return trim($secs[$section*2-1] . $secs[$section*2]);
70 }
71 }
72 }
73 return $this->mContent;
74 }
75 }
76 }
77
78 function loadContent( $noredir = false )
79 {
80 global $wgOut, $wgMwRedir;
81 global $oldid, $redirect; # From query
82
83 if ( $this->mContentLoaded ) return;
84 $fname = "Article::loadContent";
85
86 # Pre-fill content with error message so that if something
87 # fails we'll have something telling us what we intended.
88
89 $t = $this->mTitle->getPrefixedText();
90 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
91 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
92 $this->mContent = str_replace( "$1", $t, wfMsg( "missingarticle" ) );
93
94 if ( ! $oldid ) { # Retrieve current version
95 $id = $this->getID();
96 if ( 0 == $id ) return;
97
98 $sql = "SELECT " .
99 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
100 "FROM cur WHERE cur_id={$id}";
101 $res = wfQuery( $sql, $fname );
102 if ( 0 == wfNumRows( $res ) ) { return; }
103
104 $s = wfFetchObject( $res );
105
106 # If we got a redirect, follow it (unless we've been told
107 # not to by either the function parameter or the query
108 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
109 ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
110 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
111 $s->cur_text, $m ) ) {
112 $rt = Title::newFromText( $m[1] );
113
114 # Gotta hand redirects to special pages differently:
115 # Fill the HTTP response "Location" header and ignore
116 # the rest of the page we're on.
117
118 if ( $rt->getInterwiki() != "" ) {
119 $wgOut->redirect( $rt->getFullURL() ) ;
120 return;
121 }
122 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
123 $wgOut->redirect( wfLocalUrl(
124 $rt->getPrefixedURL() ) );
125 return;
126 }
127 $rid = $rt->getArticleID();
128 if ( 0 != $rid ) {
129 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
130 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
131 $res = wfQuery( $sql, $fname );
132
133 if ( 0 != wfNumRows( $res ) ) {
134 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
135 $this->mTitle = $rt;
136 $s = wfFetchObject( $res );
137 }
138 }
139 }
140 }
141 $this->mContent = $s->cur_text;
142 $this->mUser = $s->cur_user;
143 $this->mCounter = $s->cur_counter;
144 $this->mTimestamp = $s->cur_timestamp;
145 $this->mTouched = $s->cur_touched;
146 $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
147 $this->mTitle->mRestrictionsLoaded = true;
148 wfFreeResult( $res );
149 } else { # oldid set, retrieve historical version
150 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
151 "WHERE old_id={$oldid}";
152 $res = wfQuery( $sql, $fname );
153 if ( 0 == wfNumRows( $res ) ) { return; }
154
155 $s = wfFetchObject( $res );
156 $this->mContent = $s->old_text;
157 $this->mUser = $s->old_user;
158 $this->mCounter = 0;
159 $this->mTimestamp = $s->old_timestamp;
160 wfFreeResult( $res );
161 }
162 $this->mContentLoaded = true;
163 }
164
165 function getID() { return $this->mTitle->getArticleID(); }
166
167 function getCount()
168 {
169 if ( -1 == $this->mCounter ) {
170 $id = $this->getID();
171 $this->mCounter = wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
172 }
173 return $this->mCounter;
174 }
175
176 # Would the given text make this article a "good" article (i.e.,
177 # suitable for including in the article count)?
178
179 function isCountable( $text )
180 {
181 global $wgUseCommaCount, $wgMwRedir;
182
183 if ( 0 != $this->mTitle->getNamespace() ) { return 0; }
184 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
185 $token = ($wgUseCommaCount ? "," : "[[" );
186 if ( false === strstr( $text, $token ) ) { return 0; }
187 return 1;
188 }
189
190 # Load the field related to the last edit time of the article.
191 # This isn't necessary for all uses, so it's only done if needed.
192
193 /* private */ function loadLastEdit()
194 {
195 global $wgOut;
196 if ( -1 != $this->mUser ) return;
197
198 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
199 "cur_comment,cur_minor_edit FROM cur WHERE " .
200 "cur_id=" . $this->getID();
201 $res = wfQuery( $sql, "Article::loadLastEdit" );
202
203 if ( wfNumRows( $res ) > 0 ) {
204 $s = wfFetchObject( $res );
205 $this->mUser = $s->cur_user;
206 $this->mUserText = $s->cur_user_text;
207 $this->mTimestamp = $s->cur_timestamp;
208 $this->mComment = $s->cur_comment;
209 $this->mMinorEdit = $s->cur_minor_edit;
210 }
211 }
212
213 function getTimestamp()
214 {
215 $this->loadLastEdit();
216 return $this->mTimestamp;
217 }
218
219 function getUser()
220 {
221 $this->loadLastEdit();
222 return $this->mUser;
223 }
224
225 function getUserText()
226 {
227 $this->loadLastEdit();
228 return $this->mUserText;
229 }
230
231 function getComment()
232 {
233 $this->loadLastEdit();
234 return $this->mComment;
235 }
236
237 function getMinorEdit()
238 {
239 $this->loadLastEdit();
240 return $this->mMinorEdit;
241 }
242
243 # This is the default action of the script: just view the page of
244 # the given title.
245
246 function view()
247 {
248 global $wgUser, $wgOut, $wgLang;
249 global $oldid, $diff; # From query
250 global $wgLinkCache;
251 wfProfileIn( "Article::view" );
252
253 $wgOut->setArticleFlag( true );
254 $wgOut->setRobotpolicy( "index,follow" );
255
256 # If we got diff and oldid in the query, we want to see a
257 # diff page instead of the article.
258
259 if ( isset( $diff ) ) {
260 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
261 $de = new DifferenceEngine( $oldid, $diff );
262 $de->showDiffPage();
263 wfProfileOut();
264 return;
265 }
266 $text = $this->getContent(); # May change wgTitle!
267 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
268 $wgOut->setHTMLTitle( $this->mTitle->getPrefixedText() .
269 " - " . wfMsg( "wikititlesuffix" ) );
270
271 # We're looking at an old revision
272
273 if ( $oldid ) {
274 $this->setOldSubtitle();
275 $wgOut->setRobotpolicy( "noindex,follow" );
276 }
277 if ( "" != $this->mRedirectedFrom ) {
278 $sk = $wgUser->getSkin();
279 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
280 "redirect=no" );
281 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
282 $wgOut->setSubtitle( $s );
283 }
284 $wgOut->checkLastModified( $this->mTouched );
285 $this->tryFileCache();
286 $wgLinkCache->preFill( $this->mTitle );
287 $wgOut->addWikiText( $text );
288
289 # If the article we've just shown is in the "Image" namespace,
290 # follow it with the history list and link list for the image
291 # it describes.
292
293 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
294 $this->imageHistory();
295 $this->imageLinks();
296 }
297 $this->viewUpdates();
298 wfProfileOut();
299 }
300
301 # Theoretically we could defer these whole insert and update
302 # functions for after display, but that's taking a big leap
303 # of faith, and we want to be able to report database
304 # errors at some point.
305
306 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
307 {
308 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
309 $fname = "Article::insertNewArticle";
310
311 $ns = $this->mTitle->getNamespace();
312 $ttl = $this->mTitle->getDBkey();
313 $text = $this->preSaveTransform( $text );
314 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
315 else { $redir = 0; }
316
317 $now = wfTimestampNow();
318 $won = wfInvertTimestamp( $now );
319 wfSeedRandom();
320 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
321 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
322 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
323 "cur_restrictions,cur_user_text,cur_is_redirect," .
324 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
325 wfStrencode( $text ) . "', '" .
326 wfStrencode( $summary ) . "', '" .
327 $wgUser->getID() . "', '{$now}', " .
328 ( $isminor ? 1 : 0 ) . ", 0, '', '" .
329 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
330 $res = wfQuery( $sql, $fname );
331
332 $newid = wfInsertId();
333 $this->mTitle->resetArticleID( $newid );
334
335 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
336 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
337 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
338 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
339 ( $isminor ? 1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
340 wfStrencode( $wgUser->getName() ) . "','" .
341 wfStrencode( $summary ) . "',0,0," .
342 ( $wgUser->isBot() ? 1 : 0 ) . ")";
343 wfQuery( $sql, $fname );
344 if ($watchthis) {
345 if(!$this->mTitle->userIsWatching()) $this->watch();
346 } else {
347 if ( $this->mTitle->userIsWatching() ) {
348 $this->unwatch();
349 }
350 }
351
352 $this->showArticle( $text, wfMsg( "newarticle" ) );
353 }
354
355 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
356 {
357 global $wgOut, $wgUser, $wgLinkCache;
358 global $wgDBtransactions, $wgMwRedir;
359 $fname = "Article::updateArticle";
360
361 $this->loadLastEdit();
362
363 // insert updated section into old text if we have only edited part
364 // of the article
365 if ($section != "") {
366 $oldtext=$this->getContent();
367 if($section=="new") {
368 if($summary) $subject="== {$summary} ==\n\n";
369 $text=$oldtext."\n\n".$subject.$text;
370 } else {
371 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
372 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
373 $secs[$section*2]=$text."\n\n"; // replace with edited
374 if($section) { $secs[$section*2-1]=""; } // erase old headline
375 $text=join("",$secs);
376 }
377 }
378 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
379 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
380 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
381 $redir = 1;
382 $text = $m[1] . "\n"; # Remove all content but redirect
383 }
384 else { $redir = 0; }
385
386 $text = $this->preSaveTransform( $text );
387
388 # Update article, but only if changed.
389
390 if( $wgDBtransactions ) {
391 $sql = "BEGIN";
392 wfQuery( $sql );
393 }
394 $oldtext = $this->getContent( true );
395
396 if ( 0 != strcmp( $text, $oldtext ) ) {
397 $this->mCountAdjustment = $this->isCountable( $text )
398 - $this->isCountable( $oldtext );
399
400 $now = wfTimestampNow();
401 $won = wfInvertTimestamp( $now );
402 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
403 "',cur_comment='" . wfStrencode( $summary ) .
404 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
405 ",cur_timestamp='{$now}',cur_user_text='" .
406 wfStrencode( $wgUser->getName() ) .
407 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
408 "WHERE cur_id=" . $this->getID() .
409 " AND cur_timestamp='" . $this->getTimestamp() . "'";
410 $res = wfQuery( $sql, $fname );
411
412 if( wfAffectedRows() == 0 ) {
413 /* Belated edit conflict! Run away!! */
414 return false;
415 }
416
417 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
418 "old_comment,old_user,old_user_text,old_timestamp," .
419 "old_minor_edit,inverse_timestamp) VALUES (" .
420 $this->mTitle->getNamespace() . ", '" .
421 wfStrencode( $this->mTitle->getDBkey() ) . "', '" .
422 wfStrencode( $oldtext ) . "', '" .
423 wfStrencode( $this->getComment() ) . "', " .
424 $this->getUser() . ", '" .
425 wfStrencode( $this->getUserText() ) . "', '" .
426 $this->getTimestamp() . "', " . $me1 . ", '" .
427 wfInvertTimestamp( $this->getTimestamp() ) . "')";
428 $res = wfQuery( $sql, $fname );
429 $oldid = wfInsertID( $res );
430
431 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
432 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
433 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
434 "'{$now}','{$now}'," . $this->mTitle->getNamespace() . ",'" .
435 wfStrencode( $this->mTitle->getDBkey() ) . "',0,{$me2}," .
436 ( $wgUser->isBot() ? 1 : 0 ) . "," .
437 $this->getID() . "," . $wgUser->getID() . ",'" .
438 wfStrencode( $wgUser->getName() ) . "','" .
439 wfStrencode( $summary ) . "',0,{$oldid})";
440 wfQuery( $sql, $fname );
441
442 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
443 "WHERE rc_namespace=" . $this->mTitle->getNamespace() . " AND " .
444 "rc_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' AND " .
445 "rc_timestamp='" . $this->getTimestamp() . "'";
446 wfQuery( $sql, $fname );
447
448 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
449 "WHERE rc_cur_id=" . $this->getID();
450 wfQuery( $sql, $fname );
451 }
452 if( $wgDBtransactions ) {
453 $sql = "COMMIT";
454 wfQuery( $sql );
455 }
456
457 if ($watchthis) {
458 if (!$this->mTitle->userIsWatching()) $this->watch();
459 } else {
460 if ( $this->mTitle->userIsWatching() ) {
461 $this->unwatch();
462 }
463 }
464
465 $this->showArticle( $text, wfMsg( "updated" ) );
466 return true;
467 }
468
469 # After we've either updated or inserted the article, update
470 # the link tables and redirect to the new page.
471
472 function showArticle( $text, $subtitle )
473 {
474 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
475 global $wgMwRedir;
476
477 $wgLinkCache = new LinkCache();
478
479 # Get old version of link table to allow incremental link updates
480 if ( $wgUseBetterLinksUpdate ) {
481 $wgLinkCache->preFill( $this->mTitle );
482 $wgLinkCache->clear();
483 }
484
485 # Now update the link cache by parsing the text
486 $wgOut = new OutputPage();
487 $wgOut->addWikiText( $text );
488
489 $this->editUpdates( $text );
490 if( $wgMwRedir->matchStart( $text ) )
491 $r = "redirect=no";
492 else
493 $r = "";
494 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL(), $r ) );
495 }
496
497 # If the page we've just displayed is in the "Image" namespace,
498 # we follow it with an upload history of the image and its usage.
499
500 function imageHistory()
501 {
502 global $wgUser, $wgOut, $wgLang;
503 $fname = "Article::imageHistory";
504
505 $sql = "SELECT img_size,img_description,img_user," .
506 "img_user_text,img_timestamp FROM image WHERE " .
507 "img_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "'";
508 $res = wfQuery( $sql, $fname );
509
510 if ( 0 == wfNumRows( $res ) ) { return; }
511
512 $sk = $wgUser->getSkin();
513 $s = $sk->beginImageHistoryList();
514
515 $line = wfFetchObject( $res );
516 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
517 $this->mTitle->getText(), $line->img_user,
518 $line->img_user_text, $line->img_size, $line->img_description );
519
520 $sql = "SELECT oi_size,oi_description,oi_user," .
521 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
522 "oi_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
523 "ORDER BY oi_timestamp DESC";
524 $res = wfQuery( $sql, $fname );
525
526 while ( $line = wfFetchObject( $res ) ) {
527 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
528 $line->oi_archive_name, $line->oi_user,
529 $line->oi_user_text, $line->oi_size, $line->oi_description );
530 }
531 $s .= $sk->endImageHistoryList();
532 $wgOut->addHTML( $s );
533 }
534
535 function imageLinks()
536 {
537 global $wgUser, $wgOut;
538
539 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
540
541 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
542 wfStrencode( $this->mTitle->getDBkey() ) . "'";
543 $res = wfQuery( $sql, "Article::imageLinks" );
544
545 if ( 0 == wfNumRows( $res ) ) {
546 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
547 return;
548 }
549 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
550
551 $sk = $wgUser->getSkin();
552 while ( $s = wfFetchObject( $res ) ) {
553 $name = $s->il_from;
554 $link = $sk->makeKnownLink( $name, "" );
555 $wgOut->addHTML( "<li>{$link}</li>\n" );
556 }
557 $wgOut->addHTML( "</ul>\n" );
558 }
559
560 # Add this page to my watchlist
561
562 function watch()
563 {
564 global $wgUser, $wgOut, $wgLang;
565 global $wgDeferredUpdateList;
566
567 if ( 0 == $wgUser->getID() ) {
568 $wgOut->errorpage( "watchnologin", "watchnologintext" );
569 return;
570 }
571 if ( wfReadOnly() ) {
572 $wgOut->readOnlyPage();
573 return;
574 }
575 $wgUser->addWatch( $this->mTitle );
576
577 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
578 $wgOut->setRobotpolicy( "noindex,follow" );
579
580 $sk = $wgUser->getSkin() ;
581 $link = $sk->makeKnownLink ( $this->mTitle->getPrefixedText() ) ;
582
583 $text = str_replace( "$1", $link ,
584 wfMsg( "addedwatchtext" ) );
585 $wgOut->addHTML( $text );
586
587 $up = new UserUpdate();
588 array_push( $wgDeferredUpdateList, $up );
589
590 $wgOut->returnToMain( false );
591 }
592
593 function unwatch()
594 {
595 global $wgUser, $wgOut, $wgLang;
596 global $wgDeferredUpdateList;
597
598 if ( 0 == $wgUser->getID() ) {
599 $wgOut->errorpage( "watchnologin", "watchnologintext" );
600 return;
601 }
602 if ( wfReadOnly() ) {
603 $wgOut->readOnlyPage();
604 return;
605 }
606 $wgUser->removeWatch( $this->mTitle );
607
608 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
609 $wgOut->setRobotpolicy( "noindex,follow" );
610
611 $sk = $wgUser->getSkin() ;
612 $link = $sk->makeKnownLink ( $this->mTitle->getPrefixedText() ) ;
613
614 $text = str_replace( "$1", $link ,
615 wfMsg( "removedwatchtext" ) );
616 $wgOut->addHTML( $text );
617
618 $up = new UserUpdate();
619 array_push( $wgDeferredUpdateList, $up );
620
621 $wgOut->returnToMain( false );
622 }
623
624 # This shares a lot of issues (and code) with Recent Changes
625
626 function history()
627 {
628 global $wgUser, $wgOut, $wgLang, $offset, $limit;
629
630 # If page hasn't changed, client can cache this
631
632 $wgOut->checkLastModified( $this->getTimestamp() );
633 wfProfileIn( "Article::history" );
634
635 $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
636 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
637 $wgOut->setArticleFlag( false );
638 $wgOut->setRobotpolicy( "noindex,nofollow" );
639
640 if( $this->mTitle->getArticleID() == 0 ) {
641 $wgOut->addHTML( wfMsg( "nohistory" ) );
642 wfProfileOut();
643 return;
644 }
645
646 $offset = (int)$offset;
647 $limit = (int)$limit;
648 if( $limit == 0 ) $limit = 50;
649 $namespace = $this->mTitle->getNamespace();
650 $title = $this->mTitle->getText();
651 $sql = "SELECT old_id,old_user," .
652 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
653 "FROM old USE INDEX (name_title_timestamp) " .
654 "WHERE old_namespace={$namespace} AND " .
655 "old_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
656 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
657 $res = wfQuery( $sql, "Article::history" );
658
659 $revs = wfNumRows( $res );
660 if( $this->mTitle->getArticleID() == 0 ) {
661 $wgOut->addHTML( wfMsg( "nohistory" ) );
662 wfProfileOut();
663 return;
664 }
665
666 $sk = $wgUser->getSkin();
667 $numbar = wfViewPrevNext(
668 $offset, $limit,
669 $this->mTitle->getPrefixedText(),
670 "action=history" );
671 $s = $numbar;
672 $s .= $sk->beginHistoryList();
673
674 if($offset == 0 )
675 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
676 $this->getUserText(), $namespace,
677 $title, 0, $this->getComment(),
678 ( $this->getMinorEdit() > 0 ) );
679
680 $revs = wfNumRows( $res );
681 while ( $line = wfFetchObject( $res ) ) {
682 $s .= $sk->historyLine( $line->old_timestamp, $line->old_user,
683 $line->old_user_text, $namespace,
684 $title, $line->old_id,
685 $line->old_comment, ( $line->old_minor_edit > 0 ) );
686 }
687 $s .= $sk->endHistoryList();
688 $s .= $numbar;
689 $wgOut->addHTML( $s );
690 wfProfileOut();
691 }
692
693 function protect()
694 {
695 global $wgUser, $wgOut;
696
697 if ( ! $wgUser->isSysop() ) {
698 $wgOut->sysopRequired();
699 return;
700 }
701 if ( wfReadOnly() ) {
702 $wgOut->readOnlyPage();
703 return;
704 }
705 $id = $this->mTitle->getArticleID();
706 if ( 0 == $id ) {
707 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
708 return;
709 }
710 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
711 "cur_restrictions='sysop' WHERE cur_id={$id}";
712 wfQuery( $sql, "Article::protect" );
713
714 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
715 }
716
717 function unprotect()
718 {
719 global $wgUser, $wgOut;
720
721 if ( ! $wgUser->isSysop() ) {
722 $wgOut->sysopRequired();
723 return;
724 }
725 if ( wfReadOnly() ) {
726 $wgOut->readOnlyPage();
727 return;
728 }
729 $id = $this->mTitle->getArticleID();
730 if ( 0 == $id ) {
731 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
732 return;
733 }
734 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
735 "cur_restrictions='' WHERE cur_id={$id}";
736 wfQuery( $sql, "Article::unprotect" );
737
738 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
739 }
740
741 function delete()
742 {
743 global $wgUser, $wgOut;
744 global $wpConfirm, $wpReason, $image, $oldimage;
745
746 # Anybody can delete old revisions of images; only sysops
747 # can delete articles and current images
748
749 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
750 $wgOut->sysopRequired();
751 return;
752 }
753 if ( wfReadOnly() ) {
754 $wgOut->readOnlyPage();
755 return;
756 }
757
758 # Better double-check that it hasn't been deleted yet!
759 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
760 if ( $image ) {
761 if ( "" == trim( $image ) ) {
762 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
763 return;
764 }
765 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
766 } else {
767
768 if ( ( "" == trim( $this->mTitle->getText() ) )
769 or ( $this->mTitle->getArticleId() == 0 ) ) {
770 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
771 return;
772 }
773 $sub = str_replace( "$1", $this->mTitle->getPrefixedText(),
774 wfMsg( "deletesub" ) );
775
776 # determine whether this page has earlier revisions
777 # and insert a warning if it does
778 # we select the text because it might be useful below
779 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
780 $res=wfQuery($sql,$fname);
781 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
782 $skin=$wgUser->getSkin();
783 $wgOut->addHTML("<B>".wfMsg("historywarning"));
784 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
785 }
786
787 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."'";
788 $res=wfQuery($sql,$fname);
789 if( ($s=wfFetchObject($res))) {
790
791 # if this is a mini-text, we can paste part of it into the deletion reason
792
793 #if this is empty, an earlier revision may contain "useful" text
794 if($s->cur_text!="") {
795 $text=$s->cur_text;
796 } else {
797 if($old) {
798 $text=$old->old_text;
799 $blanked=1;
800 }
801
802 }
803
804 $length=strlen($text);
805
806 # this should not happen, since it is not possible to store an empty, new
807 # page. Let's insert a standard text in case it does, though
808 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
809
810
811 if($length < 500 && !$wpReason) {
812
813 # comment field=255, let's grep the first 150 to have some user
814 # space left
815 $text=substr($text,0,150);
816 # let's strip out newlines and HTML tags
817 $text=preg_replace("/\"/","'",$text);
818 $text=preg_replace("/\</","&lt;",$text);
819 $text=preg_replace("/\>/","&gt;",$text);
820 $text=preg_replace("/[\n\r]/","",$text);
821 if(!$blanked) {
822 $wpReason=wfMsg("excontent"). " '".$text;
823 } else {
824 $wpReason=wfMsg("exbeforeblank") . " '".$text;
825 }
826 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
827 $wpReason.="'";
828 }
829 }
830
831 }
832
833 # Likewise, deleting old images doesn't require confirmation
834 if ( $oldimage || 1 == $wpConfirm ) {
835 $this->doDelete();
836 return;
837 }
838
839 $wgOut->setSubtitle( $sub );
840 $wgOut->setRobotpolicy( "noindex,nofollow" );
841 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
842
843 $t = $this->mTitle->getPrefixedURL();
844 $q = "action=delete";
845
846 if ( $image ) {
847 $q .= "&image={$image}";
848 } else if ( $oldimage ) {
849 $q .= "&oldimage={$oldimage}";
850 } else {
851 $q .= "&title={$t}";
852 }
853 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
854 $confirm = wfMsg( "confirm" );
855 $check = wfMsg( "confirmcheck" );
856 $delcom = wfMsg( "deletecomment" );
857
858 $wgOut->addHTML( "
859 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
860 <table border=0><tr><td align=right>
861 {$delcom}:</td><td align=left>
862 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
863 </td></tr><tr><td>&nbsp;</td></tr>
864 <tr><td align=right>
865 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
866 </td><td><label for=\"wpConfirm\">{$check}</label></td>
867 </tr><tr><td>&nbsp;</td><td>
868 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
869 </td></tr></table></form>\n" );
870
871 $wgOut->returnToMain( false );
872 }
873
874 function doDelete()
875 {
876 global $wgOut, $wgUser, $wgLang;
877 global $image, $oldimage, $wpReason;
878 $fname = "Article::doDelete";
879
880 if ( $image ) {
881 $dest = wfImageDir( $image );
882 $archive = wfImageDir( $image );
883 if ( ! unlink( "{$dest}/{$image}" ) ) {
884 $wgOut->fileDeleteError( "{$dest}/{$image}" );
885 return;
886 }
887 $sql = "DELETE FROM image WHERE img_name='" .
888 wfStrencode( $image ) . "'";
889 wfQuery( $sql, $fname );
890
891 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
892 wfStrencode( $image ) . "'";
893 $res = wfQuery( $sql, $fname );
894
895 while ( $s = wfFetchObject( $res ) ) {
896 $this->doDeleteOldImage( $s->oi_archive_name );
897 }
898 $sql = "DELETE FROM oldimage WHERE oi_name='" .
899 wfStrencode( $image ) . "'";
900 wfQuery( $sql, $fname );
901
902 # Image itself is now gone, and database is cleaned.
903 # Now we remove the image description page.
904
905 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
906 $this->doDeleteArticle( $nt );
907
908 $deleted = $image;
909 } else if ( $oldimage ) {
910 $this->doDeleteOldImage( $oldimage );
911 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
912 wfStrencode( $oldimage ) . "'";
913 wfQuery( $sql, $fname );
914
915 $deleted = $oldimage;
916 } else {
917 $this->doDeleteArticle( $this->mTitle );
918 $deleted = $this->mTitle->getPrefixedText();
919 }
920 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
921 $wgOut->setRobotpolicy( "noindex,nofollow" );
922
923 $sk = $wgUser->getSkin();
924 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
925 Namespace::getWikipedia() ) .
926 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
927
928 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
929 $text = str_replace( "$2", $loglink, $text );
930
931 $wgOut->addHTML( "<p>" . $text );
932 $wgOut->returnToMain( false );
933 }
934
935 function doDeleteOldImage( $oldimage )
936 {
937 global $wgOut;
938
939 $name = substr( $oldimage, 15 );
940 $archive = wfImageArchiveDir( $name );
941 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
942 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
943 }
944 }
945
946 function doDeleteArticle( $title )
947 {
948 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
949
950 $fname = "Article::doDeleteArticle";
951 $ns = $title->getNamespace();
952 $t = wfStrencode( $title->getDBkey() );
953 $id = $title->getArticleID();
954
955 if ( "" == $t ) {
956 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
957 return;
958 }
959
960 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
961 array_push( $wgDeferredUpdateList, $u );
962
963 # Move article and history to the "archive" table
964 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
965 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
966 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
967 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
968 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
969 wfQuery( $sql, $fname );
970
971 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
972 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
973 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
974 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
975 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
976 wfQuery( $sql, $fname );
977
978 # Now that it's safely backed up, delete it
979
980 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
981 "cur_title='{$t}'";
982 wfQuery( $sql, $fname );
983
984 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
985 "old_title='{$t}'";
986 wfQuery( $sql, $fname );
987
988 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
989 "rc_title='{$t}'";
990 wfQuery( $sql, $fname );
991
992 # Finally, clean up the link tables
993
994 if ( 0 != $id ) {
995 $t = wfStrencode( $title->getPrefixedDBkey() );
996 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
997 $res = wfQuery( $sql, $fname );
998
999 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1000 $now = wfTimestampNow();
1001 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1002 $first = true;
1003
1004 while ( $s = wfFetchObject( $res ) ) {
1005 $nt = Title::newFromDBkey( $s->l_from );
1006 $lid = $nt->getArticleID();
1007
1008 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1009 $first = false;
1010 $sql .= "({$lid},'{$t}')";
1011 $sql2 .= "{$lid}";
1012 }
1013 $sql2 .= ")";
1014 if ( ! $first ) {
1015 wfQuery( $sql, $fname );
1016 wfQuery( $sql2, $fname );
1017 }
1018 wfFreeResult( $res );
1019
1020 $sql = "DELETE FROM links WHERE l_to={$id}";
1021 wfQuery( $sql, $fname );
1022
1023 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1024 wfQuery( $sql, $fname );
1025
1026 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1027 wfQuery( $sql, $fname );
1028
1029 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1030 wfQuery( $sql, $fname );
1031 }
1032
1033 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1034 $art = $title->getPrefixedText();
1035 $wpReason = wfCleanQueryVar( $wpReason );
1036 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1037
1038 # Clear the cached article id so the interface doesn't act like we exist
1039 $this->mTitle->resetArticleID( 0 );
1040 $this->mTitle->mArticleID = 0;
1041 }
1042
1043 function revert()
1044 {
1045 global $wgOut;
1046 global $oldimage;
1047
1048 if ( strlen( $oldimage ) < 16 ) {
1049 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1050 return;
1051 }
1052 if ( wfReadOnly() ) {
1053 $wgOut->readOnlyPage();
1054 return;
1055 }
1056 $name = substr( $oldimage, 15 );
1057
1058 $dest = wfImageDir( $name );
1059 $archive = wfImageArchiveDir( $name );
1060 $curfile = "{$dest}/{$name}";
1061
1062 if ( ! is_file( $curfile ) ) {
1063 $wgOut->fileNotFoundError( $curfile );
1064 return;
1065 }
1066 $oldver = wfTimestampNow() . "!{$name}";
1067 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1068 wfStrencode( $oldimage ) . "'" );
1069
1070 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1071 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1072 return;
1073 }
1074 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1075 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1076 }
1077 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1078
1079 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1080 $wgOut->setRobotpolicy( "noindex,nofollow" );
1081 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1082 $wgOut->returnToMain( false );
1083 }
1084
1085 function rollback()
1086 {
1087 global $wgUser, $wgLang, $wgOut, $from;
1088
1089 if ( ! $wgUser->isSysop() ) {
1090 $wgOut->sysopRequired();
1091 return;
1092 }
1093
1094 # Replace all this user's current edits with the next one down
1095 $tt = wfStrencode( $this->mTitle->getDBKey() );
1096 $n = $this->mTitle->getNamespace();
1097
1098 # Get the last editor
1099 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1100 $res = wfQuery( $sql );
1101 if( ($x = wfNumRows( $res )) != 1 ) {
1102 # Something wrong
1103 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1104 return;
1105 }
1106 $s = wfFetchObject( $res );
1107 $ut = wfStrencode( $s->cur_user_text );
1108 $uid = $s->cur_user;
1109 $pid = $s->cur_id;
1110
1111 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1112 if( $from != $s->cur_user_text ) {
1113 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1114 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1115 htmlspecialchars( $this->mTitle->getPrefixedText()),
1116 htmlspecialchars( $from ),
1117 htmlspecialchars( $s->cur_user_text ) ) );
1118 if($s->cur_comment != "") {
1119 $wgOut->addHTML(
1120 wfMsg("editcomment",
1121 htmlspecialchars( $s->cur_comment ) ) );
1122 }
1123 return;
1124 }
1125
1126 # Get the last edit not by this guy
1127 $sql = "SELECT old_text,old_user,old_user_text
1128 FROM old USE INDEX (name_title_timestamp)
1129 WHERE old_namespace={$n} AND old_title='{$tt}'
1130 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1131 ORDER BY inverse_timestamp LIMIT 1";
1132 $res = wfQuery( $sql );
1133 if( wfNumRows( $res ) != 1 ) {
1134 # Something wrong
1135 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1136 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1137 return;
1138 }
1139 $s = wfFetchObject( $res );
1140
1141 # Save it!
1142 $newcomment = str_replace( "$1", $s->old_user_text, wfMsg( "revertpage" ) );
1143 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1144 $wgOut->setRobotpolicy( "noindex,nofollow" );
1145 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1146 $this->updateArticle( $s->old_text, $newcomment, 1, $this->mTitle->userIsWatching() );
1147
1148 $wgOut->returnToMain( false );
1149 }
1150
1151
1152 # Do standard deferred updates after page view
1153
1154 /* private */ function viewUpdates()
1155 {
1156 global $wgDeferredUpdateList;
1157
1158 if ( 0 != $this->getID() ) {
1159 $u = new ViewCountUpdate( $this->getID() );
1160 array_push( $wgDeferredUpdateList, $u );
1161 $u = new SiteStatsUpdate( 1, 0, 0 );
1162 array_push( $wgDeferredUpdateList, $u );
1163
1164 $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
1165 $this->mTitle->getDBkey() );
1166 array_push( $wgDeferredUpdateList, $u );
1167 }
1168 }
1169
1170 # Do standard deferred updates after page edit.
1171 # Every 1000th edit, prune the recent changes table.
1172
1173 /* private */ function editUpdates( $text )
1174 {
1175 global $wgDeferredUpdateList;
1176
1177 wfSeedRandom();
1178 if ( 0 == mt_rand( 0, 999 ) ) {
1179 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1180 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1181 wfQuery( $sql );
1182 }
1183 $id = $this->getID();
1184 $title = $this->mTitle->getPrefixedDBkey();
1185 $adj = $this->mCountAdjustment;
1186
1187 if ( 0 != $id ) {
1188 $u = new LinksUpdate( $id, $title );
1189 array_push( $wgDeferredUpdateList, $u );
1190 $u = new SiteStatsUpdate( 0, 1, $adj );
1191 array_push( $wgDeferredUpdateList, $u );
1192 $u = new SearchUpdate( $id, $title, $text );
1193 array_push( $wgDeferredUpdateList, $u );
1194
1195 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(),
1196 $this->mTitle->getDBkey() );
1197 array_push( $wgDeferredUpdateList, $u );
1198 }
1199 }
1200
1201 /* private */ function setOldSubtitle()
1202 {
1203 global $wgLang, $wgOut;
1204
1205 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1206 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1207 $wgOut->setSubtitle( "({$r})" );
1208 }
1209
1210 # This function is called right before saving the wikitext,
1211 # so we can do things like signatures and links-in-context.
1212
1213 function preSaveTransform( $text )
1214 {
1215 $s = "";
1216 while ( "" != $text ) {
1217 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1218 $s .= $this->pstPass2( $p[0] );
1219
1220 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
1221 else {
1222 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1223 $s .= "<nowiki>{$q[0]}</nowiki>";
1224 $text = $q[1];
1225 }
1226 }
1227 return rtrim( $s );
1228 }
1229
1230 /* private */ function pstPass2( $text )
1231 {
1232 global $wgUser, $wgLang, $wgLocaltimezone;
1233
1234 # Signatures
1235 #
1236 $n = $wgUser->getName();
1237 $k = $wgUser->getOption( "nickname" );
1238 if ( "" == $k ) { $k = $n; }
1239 if(isset($wgLocaltimezone)) {
1240 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1241 }
1242 /* Note: this is an ugly timezone hack for the European wikis */
1243 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1244 " (" . date( "T" ) . ")";
1245 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1246
1247 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1248 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1249 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1250 Namespace::getUser() ) . ":$n|$k]]", $text );
1251
1252 # Context links: [[|name]] and [[name (context)|]]
1253 #
1254 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1255 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1256 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1257
1258 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1259 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1260 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1261 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1262 # [[ns:page (cont)|]]
1263 $context = "";
1264 $t = $this->mTitle->getText();
1265 if ( preg_match( $conpat, $t, $m ) ) {
1266 $context = $m[2];
1267 }
1268 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1269 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1270 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1271
1272 if ( "" == $context ) {
1273 $text = preg_replace( $p2, "[[\\1]]", $text );
1274 } else {
1275 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1276 }
1277 # Replace local image links with new [[image:]] style
1278
1279 $text = preg_replace(
1280 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1281 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1282 "\\1[[image:\\3.\\4]]", $text );
1283 $text = preg_replace(
1284 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1285 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1286 "\\1[[image:\\3.\\4]]", $text );
1287
1288 return $text;
1289 }
1290
1291 /* Caching functions */
1292
1293 function tryFileCache() {
1294 if($this->isFileCacheable()) {
1295 $cache = new CacheManager( $this->mTitle );
1296 if($cache->isFileCacheGood( $this->mTouched )) {
1297 wfDebug( " tryFileCache() - about to load\n" );
1298 $cache->loadFromFileCache();
1299 exit;
1300 } else {
1301 wfDebug( " tryFileCache() - starting buffer\n" );
1302 if($cache->useGzip() && wfClientAcceptsGzip()) {
1303 /* For some reason, adding this header line over in
1304 CacheManager::saveToFileCache() fails on my test
1305 setup at home, though it works on the live install.
1306 Make double-sure... --brion */
1307 header( "Content-Encoding: gzip" );
1308 }
1309 ob_start( array(&$cache, 'saveToFileCache' ) );
1310 }
1311 } else {
1312 wfDebug( " tryFileCache() - not cacheable\n" );
1313 }
1314 }
1315
1316 function isFileCacheable() {
1317 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1318 global $action, $oldid, $diff, $redirect, $printable;
1319 return $wgUseFileCache
1320 and (!$wgShowIPinHeader)
1321 and ($wgUser->getId() == 0)
1322 and (!$wgUser->getNewtalk())
1323 and ($this->mTitle->getNamespace != Namespace::getSpecial())
1324 and ($action == "view")
1325 and (!isset($oldid))
1326 and (!isset($diff))
1327 and (!isset($redirect))
1328 and (!isset($printable))
1329 and (!$this->mRedirectedFrom);
1330
1331 }
1332
1333
1334 }
1335
1336 ?>