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