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