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