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