703b3ed95d00626e74905ac4c1d4362953b070dc
[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 /* CHECK MERGE @@@
9 TEST THIS @@@
10
11 * s/\$wgTitle/\$this->mTitle/ performed, many replacements
12 * mTitle variable added to class
13 */
14
15 include_once( "CacheManager.php" );
16
17 class Article {
18 /* private */ var $mContent, $mContentLoaded;
19 /* private */ var $mUser, $mTimestamp, $mUserText;
20 /* private */ var $mCounter, $mComment, $mCountAdjustment;
21 /* private */ var $mMinorEdit, $mRedirectedFrom;
22 /* private */ var $mTouched, $mFileCache, $mTitle;
23
24 function Article( &$title ) {
25 $this->mTitle =& $title;
26 $this->clear();
27 }
28
29 /* private */ function clear()
30 {
31 $this->mContentLoaded = false;
32 $this->mUser = $this->mCounter = -1; # Not loaded
33 $this->mRedirectedFrom = $this->mUserText =
34 $this->mTimestamp = $this->mComment = $this->mFileCache = "";
35 $this->mCountAdjustment = 0;
36 $this->mTouched = "19700101000000";
37 }
38
39 # Note that getContent/loadContent may follow redirects if
40 # not told otherwise, and so may cause a change to mTitle.
41
42 function getContent( $noredir = false )
43 {
44 global $action,$section,$count; # From query string
45 $fname = "Article::getContent";
46 wfProfileIn( $fname );
47
48 if ( 0 == $this->getID() ) {
49 if ( "edit" == $action ) {
50 wfProfileOut( $fname );
51 return ""; # was "newarticletext", now moved above the box)
52 }
53 wfProfileOut( $fname );
54 return wfMsg( "noarticletext" );
55 } else {
56 $this->loadContent( $noredir );
57
58 if(
59 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
60 ( $this->mTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
61 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle->getText()) &&
62 $action=="view"
63 )
64 {
65 wfProfileOut( $fname );
66 return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
67 else {
68 if($action=="edit") {
69 if($section!="") {
70 if($section=="new") {
71 wfProfileOut( $fname );
72 return "";
73 }
74
75 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
76 $this->mContent, -1,
77 PREG_SPLIT_DELIM_CAPTURE);
78 if($section==0) {
79 wfProfileOut( $fname );
80 return trim($secs[0]);
81 } else {
82 wfProfileOut( $fname );
83 return trim($secs[$section*2-1] . $secs[$section*2]);
84 }
85 }
86 }
87 wfProfileOut( $fname );
88 return $this->mContent;
89 }
90 }
91 }
92
93 function loadContent( $noredir = false )
94 {
95 global $wgOut, $wgMwRedir;
96 global $oldid, $redirect; # From query
97
98 if ( $this->mContentLoaded ) return;
99 $fname = "Article::loadContent";
100
101 # Pre-fill content with error message so that if something
102 # fails we'll have something telling us what we intended.
103
104 $t = $this->mTitle->getPrefixedText();
105 if ( isset( $oldid ) ) {
106 $oldid = IntVal( $oldid );
107 $t .= ",oldid={$oldid}";
108 }
109 if ( isset( $redirect ) ) {
110 $redirect = ($redirect == "no") ? "no" : "yes";
111 $t .= ",redirect={$redirect}";
112 }
113 $this->mContent = wfMsg( "missingarticle", $t );
114
115 if ( ! $oldid ) { # Retrieve current version
116 $id = $this->getID();
117 if ( 0 == $id ) return;
118
119 $sql = "SELECT " .
120 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
121 "FROM cur WHERE cur_id={$id}";
122 wfDebug( "$sql\n" );
123 $res = wfQuery( $sql, DB_READ, $fname );
124 if ( 0 == wfNumRows( $res ) ) {
125 return;
126 }
127
128 $s = wfFetchObject( $res );
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 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
132 ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
133 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
134 $s->cur_text, $m ) ) {
135 $rt = Title::newFromText( $m[1] );
136
137 # Gotta hand redirects to special pages differently:
138 # Fill the HTTP response "Location" header and ignore
139 # the rest of the page we're on.
140
141 if ( $rt->getInterwiki() != "" ) {
142 $wgOut->redirect( $rt->getFullURL() ) ;
143 return;
144 }
145 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
146 $wgOut->redirect( wfLocalUrl(
147 $rt->getPrefixedURL() ) );
148 return;
149 }
150 $rid = $rt->getArticleID();
151 if ( 0 != $rid ) {
152 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
153 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
154 $res = wfQuery( $sql, DB_READ, $fname );
155
156 if ( 0 != wfNumRows( $res ) ) {
157 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
158 $this->mTitle = $rt;
159 $s = wfFetchObject( $res );
160 }
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 $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
171 $this->mTitle->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, DB_READ, $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() { return $this->mTitle->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 $wgUseCommaCount, $wgMwRedir;
206
207 if ( 0 != $this->mTitle->getNamespace() ) { return 0; }
208 if ( $wgMwRedir->matchStart( $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, DB_READ, "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, $wgLang;
273 global $oldid, $diff; # From query
274 global $wgLinkCache, $IP;
275 $fname = "Article::view";
276 wfProfileIn( $fname );
277
278 $wgOut->setArticleFlag( true );
279 $wgOut->setRobotpolicy( "index,follow" );
280
281 # If we got diff and oldid in the query, we want to see a
282 # diff page instead of the article.
283
284 if ( isset( $diff ) ) {
285 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
286 $de = new DifferenceEngine( $oldid, $diff );
287 $de->showDiffPage();
288 wfProfileOut( $fname );
289 return;
290 }
291
292 if ( !isset( $oldid ) ) {
293 if( $this->checkTouched() ) {
294 $wgOut->checkLastModified( $this->mTouched );
295 $this->tryFileCache();
296 }
297 }
298
299 $text = $this->getContent(); # May change mTitle
300 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
301 $wgOut->setHTMLTitle( $this->mTitle->getPrefixedText() .
302 " - " . wfMsg( "wikititlesuffix" ) );
303
304 # We're looking at an old revision
305
306 if ( $oldid ) {
307 $this->setOldSubtitle();
308 $wgOut->setRobotpolicy( "noindex,follow" );
309 }
310 if ( "" != $this->mRedirectedFrom ) {
311 $sk = $wgUser->getSkin();
312 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
313 "redirect=no" );
314 $s = wfMsg( "redirectedfrom", $redir );
315 $wgOut->setSubtitle( $s );
316 }
317 $wgOut->checkLastModified( $this->mTouched );
318 $this->tryFileCache();
319 $wgLinkCache->preFill( $this->mTitle );
320 $wgOut->addWikiText( $text );
321
322 $this->viewUpdates();
323 wfProfileOut( $fname );
324 }
325
326 # This is the function that gets called for "action=edit".
327
328 function edit()
329 {
330 global $wgOut, $wgUser;
331 global $wpTextbox1, $wpSummary, $wpSave, $wpPreview;
332 global $wpMinoredit, $wpEdittime, $wpTextbox2;
333
334 $fields = array( "wpTextbox1", "wpSummary", "wpTextbox2" );
335 wfCleanFormFields( $fields );
336
337 if ( ! $this->mTitle->userCanEdit() ) {
338 $wgOut->readOnlyPage( $this->getContent(), true );
339 return;
340 }
341 if ( $wgUser->isBlocked() ) {
342 $this->blockedIPpage();
343 return;
344 }
345 if ( wfReadOnly() ) {
346 if( isset( $wpSave ) or isset( $wpPreview ) ) {
347 $this->editForm( "preview" );
348 } else {
349 $wgOut->readOnlyPage( $this->getContent() );
350 }
351 return;
352 }
353 if ( $_SERVER['REQUEST_METHOD'] != "POST" ) unset( $wpSave );
354 if ( isset( $wpSave ) ) {
355 $this->editForm( "save" );
356 } else if ( isset( $wpPreview ) ) {
357 $this->editForm( "preview" );
358 } else { # First time through
359 $this->editForm( "initial" );
360 }
361 }
362
363 # Since there is only one text field on the edit form,
364 # pressing <enter> will cause the form to be submitted, but
365 # the submit button value won't appear in the query, so we
366 # Fake it here before going back to edit(). This is kind of
367 # ugly, but it helps some old URLs to still work.
368
369 function submit()
370 {
371 global $wpSave, $wpPreview;
372 if ( ! isset( $wpPreview ) ) { $wpSave = 1; }
373
374 $this->edit();
375 }
376
377 # The edit form is self-submitting, so that when things like
378 # preview and edit conflicts occur, we get the same form back
379 # with the extra stuff added. Only when the final submission
380 # is made and all is well do we actually save and redirect to
381 # the newly-edited page.
382
383 function editForm( $formtype )
384 {
385 global $wgOut, $wgUser;
386 global $wpTextbox1, $wpSummary, $wpWatchthis;
387 global $wpSave, $wpPreview;
388 global $wpMinoredit, $wpEdittime, $wpTextbox2, $wpSection;
389 global $oldid, $redirect, $section;
390 global $wgLang;
391
392 if(isset($wpSection)) { $section=$wpSection; } else { $wpSection=$section; }
393
394 $sk = $wgUser->getSkin();
395 $isConflict = false;
396 $wpTextbox1 = rtrim ( $wpTextbox1 ) ; # To avoid text getting longer on each preview
397
398 if(!$this->mTitle->getArticleID()) { # new article
399
400 $wgOut->addWikiText(wfmsg("newarticletext"));
401
402 }
403
404 # Attempt submission here. This will check for edit conflicts,
405 # and redundantly check for locked database, blocked IPs, etc.
406 # that edit() already checked just in case someone tries to sneak
407 # in the back door with a hand-edited submission URL.
408
409 if ( "save" == $formtype ) {
410 if ( $wgUser->isBlocked() ) {
411 $this->blockedIPpage();
412 return;
413 }
414 if ( wfReadOnly() ) {
415 $wgOut->readOnlyPage();
416 return;
417 }
418 # If article is new, insert it.
419
420 $aid = $this->mTitle->getArticleID();
421 if ( 0 == $aid ) {
422 # we need to strip Windoze linebreaks because some browsers
423 # append them and the string comparison fails
424 if ( ( "" == $wpTextbox1 ) ||
425 ( wfMsg( "newarticletext" ) == rtrim( preg_replace("/\r/","",$wpTextbox1) ) ) ) {
426 $wgOut->redirect( wfLocalUrl(
427 $this->mTitle->getPrefixedURL() ) );
428 return;
429 }
430 $this->mCountAdjustment = $this->isCountable( $wpTextbox1 );
431 $this->insertNewArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis );
432 return;
433 }
434 # Article exists. Check for edit conflict.
435 # Don't check for conflict when appending a comment - this should always work
436
437 $this->clear(); # Force reload of dates, etc.
438 if ( $section!="new" && ( $this->getTimestamp() != $wpEdittime ) ) {
439 $isConflict = true;
440 }
441 $u = $wgUser->getID();
442
443 # Supress edit conflict with self
444
445 if ( ( 0 != $u ) && ( $this->getUser() == $u ) ) {
446 $isConflict = false;
447 } else {
448 # switch from section editing to normal editing in edit conflict
449 if($isConflict) {
450 $section="";$wpSection="";
451 }
452
453 }
454 if ( ! $isConflict ) {
455 # All's well: update the article here
456 if($this->updateArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis, $wpSection ))
457 return;
458 else
459 $isConflict = true;
460 }
461 }
462 # First time through: get contents, set time for conflict
463 # checking, etc.
464
465 if ( "initial" == $formtype ) {
466 $wpEdittime = $this->getTimestamp();
467 $wpTextbox1 = $this->getContent(true);
468 $wpSummary = "";
469 }
470 $wgOut->setRobotpolicy( "noindex,nofollow" );
471 $wgOut->setArticleFlag( false );
472
473 if ( $isConflict ) {
474 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
475 $wgOut->setPageTitle( $s );
476 $wgOut->addHTML( wfMsg( "explainconflict" ) );
477
478 $wpTextbox2 = $wpTextbox1;
479 $wpTextbox1 = $this->getContent(true);
480 $wpEdittime = $this->getTimestamp();
481 } else {
482 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
483
484 if($section!="") {
485 if($section=="new") {
486 $s.=wfMsg("commentedit");
487 } else {
488 $s.=wfMsg("sectionedit");
489 }
490 }
491 $wgOut->setPageTitle( $s );
492 if ( $oldid ) {
493 $this->setOldSubtitle();
494 $wgOut->addHTML( wfMsg( "editingold" ) );
495 }
496 }
497
498 if( wfReadOnly() ) {
499 $wgOut->addHTML( "<strong>" .
500 wfMsg( "readonlywarning" ) .
501 "</strong>" );
502 }
503 if( $this->mTitle->isProtected() ) {
504 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
505 "</strong><br />\n" );
506 }
507
508 $kblength = (int)(strlen( $wpTextbox1 ) / 1024);
509 if( $kblength > 29 ) {
510 $wgOut->addHTML( "<strong>" .
511 wfMsg( "longpagewarning", $kblength )
512 . "</strong>" );
513 }
514
515 $rows = $wgUser->getOption( "rows" );
516 $cols = $wgUser->getOption( "cols" );
517
518 $ew = $wgUser->getOption( "editwidth" );
519 if ( $ew ) $ew = " style=\"width:100%\"";
520 else $ew = "" ;
521
522 $q = "action=submit";
523 if ( "no" == $redirect ) { $q .= "&redirect=no"; }
524 $action = wfEscapeHTML( wfLocalUrl( $this->mTitle->getPrefixedURL(), $q ) );
525
526 $summary = wfMsg( "summary" );
527 $subject = wfMsg("subject");
528 $minor = wfMsg( "minoredit" );
529 $watchthis = wfMsg ("watchthis");
530 $save = wfMsg( "savearticle" );
531 $prev = wfMsg( "showpreview" );
532
533 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
534 wfMsg( "cancel" ) );
535 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
536 wfMsg( "edithelp" ) );
537 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
538 wfMsg( "copyrightpage" ) ) );
539
540 $wpTextbox1 = wfEscapeHTML( $wpTextbox1 );
541 $wpTextbox2 = wfEscapeHTML( $wpTextbox2 );
542 $wpSummary = wfEscapeHTML( $wpSummary );
543
544 // activate checkboxes if user wants them to be always active
545 if (!$wpPreview && $wgUser->getOption("watchdefault")) $wpWatchthis=1;
546 if (!$wpPreview && $wgUser->getOption("minordefault")) $wpMinoredit=1;
547
548 // activate checkbox also if user is already watching the page,
549 // require wpWatchthis to be unset so that second condition is not
550 // checked unnecessarily
551 if (!$wpWatchthis && !$wpPreview && $this->mTitle->userIsWatching()) $wpWatchthis=1;
552
553 if ( 0 != $wgUser->getID() ) {
554 $checkboxhtml=
555 "<input tabindex=3 type=checkbox value=1 name='wpMinoredit'".($wpMinoredit?" checked":"").">{$minor}".
556 "<input tabindex=4 type=checkbox name='wpWatchthis'".($wpWatchthis?" checked":"").">{$watchthis}<br>";
557
558 } else {
559 $checkboxhtml="";
560 }
561
562
563 if ( "preview" == $formtype) {
564
565 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
566 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><P>\n";
567 if ( $isConflict ) {
568 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
569 "</h2>\n";
570 }
571 $previewtext = wfUnescapeHTML( $wpTextbox1 );
572
573 if($wgUser->getOption("previewontop")) {
574 $wgOut->addHTML($previewhead);
575 $wgOut->addWikiText( $this->preSaveTransform( $previewtext ) ."\n\n");
576 }
577 $wgOut->addHTML( "<br clear=\"all\" />\n" );
578 }
579
580 # if this is a comment, show a subject line at the top, which is also the edit summary.
581 # Otherwise, show a summary field at the bottom
582 if($section=="new") {
583
584 $commentsubject="{$subject}: <input tabindex=1 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
585 } else {
586
587 $editsummary="{$summary}: <input tabindex=3 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
588 }
589
590 $wgOut->addHTML( "
591 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
592 enctype=\"application/x-www-form-urlencoded\">
593 {$commentsubject}
594 <textarea tabindex=2 name=\"wpTextbox1\" rows={$rows}
595 cols={$cols}{$ew} wrap=\"virtual\">" .
596 $wgLang->recodeForEdit( $wpTextbox1 ) .
597 "
598 </textarea>
599 <br>{$editsummary}
600 {$checkboxhtml}
601 <input tabindex=5 type=submit value=\"{$save}\" name=\"wpSave\">
602 <input tabindex=6 type=submit value=\"{$prev}\" name=\"wpPreview\">
603 <em>{$cancel}</em> | <em>{$edithelp}</em>
604 <br><br>{$copywarn}
605 <input type=hidden value=\"{$section}\" name=\"wpSection\">
606 <input type=hidden value=\"{$wpEdittime}\" name=\"wpEdittime\">\n" );
607
608 if ( $isConflict ) {
609 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
610 DifferenceEngine::showDiff( $wpTextbox2, $wpTextbox1,
611 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
612
613 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
614 <textarea tabindex=6 name=\"wpTextbox2\" rows={$rows} cols={$cols} wrap=virtual>"
615 . $wgLang->recodeForEdit( $wpTextbox2 ) .
616 "
617 </textarea>" );
618 }
619 $wgOut->addHTML( "</form>\n" );
620 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
621 $wgOut->addHTML($previewhead);
622 $wgOut->addWikiText( $this->preSaveTransform( $previewtext ) );
623 }
624 }
625
626 # Theoretically we could defer these whole insert and update
627 # functions for after display, but that's taking a big leap
628 # of faith, and we want to be able to report database
629 # errors at some point.
630
631 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
632 {
633 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
634 global $wgEnablePersistentLC;
635
636 $fname = "Article::insertNewArticle";
637
638 $ns = $this->mTitle->getNamespace();
639 $ttl = $this->mTitle->getDBkey();
640 $text = $this->preSaveTransform( $text );
641 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
642 else { $redir = 0; }
643
644 $now = wfTimestampNow();
645 $won = wfInvertTimestamp( $now );
646 wfSeedRandom();
647 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
648 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
649 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
650 "cur_restrictions,cur_user_text,cur_is_redirect," .
651 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
652 wfStrencode( $text ) . "', '" .
653 wfStrencode( $summary ) . "', '" .
654 $wgUser->getID() . "', '{$now}', " .
655 ( $isminor ? 1 : 0 ) . ", 0, '', '" .
656 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
657 $res = wfQuery( $sql, DB_WRITE, $fname );
658
659 $newid = wfInsertId();
660 $this->mTitle->resetArticleID( $newid );
661
662 if ( $wgEnablePersistentLC ) {
663 // Purge related entries in links cache on new page, to heal broken links
664 $ptitle = wfStrencode( $ttl );
665 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
666 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE);
667 }
668
669 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
670 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
671 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
672 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
673 ( $isminor ? 1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
674 wfStrencode( $wgUser->getName() ) . "','" .
675 wfStrencode( $summary ) . "',0,0," .
676 ( $wgUser->isBot() ? 1 : 0 ) . ")";
677 wfQuery( $sql, DB_WRITE, $fname );
678 if ($watchthis) {
679 if(!$this->mTitle->userIsWatching()) $this->watch();
680 } else {
681 if ( $this->mTitle->userIsWatching() ) {
682 $this->unwatch();
683 }
684 }
685
686 # The talk page isn't in the regular link tables, so we need to update manually:
687 $talkns = $ns ^ 1; # talk -> normal; normal -> talk
688 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
689 wfQuery( $sql, DB_WRITE );
690
691 $this->showArticle( $text, wfMsg( "newarticle" ) );
692 }
693
694 function updateArticle( $text, $summary, $minor, $watchthis, $section = "")
695 {
696 global $wgOut, $wgUser, $wgLinkCache;
697 global $wgDBtransactions, $wgMwRedir;
698 $fname = "Article::updateArticle";
699
700 $this->loadLastEdit();
701
702 // insert updated section into old text if we have only edited part
703 // of the article
704 if ($section != "") {
705 $oldtext=$this->getContent();
706 if($section=="new") {
707 if($summary) $subject="== {$summary} ==\n\n";
708 $text=$oldtext."\n\n".$subject.$text;
709 } else {
710 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
711 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
712 $secs[$section*2]=$text."\n\n"; // replace with edited
713 if($section) { $secs[$section*2-1]=""; } // erase old headline
714 $text=join("",$secs);
715 }
716 }
717 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
718 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
719 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
720 $redir = 1;
721 $text = $m[1] . "\n"; # Remove all content but redirect
722 }
723 else { $redir = 0; }
724
725 $text = $this->preSaveTransform( $text );
726
727 # Update article, but only if changed.
728
729 if( $wgDBtransactions ) {
730 $sql = "BEGIN";
731 wfQuery( $sql, DB_WRITE );
732 }
733 $oldtext = $this->getContent( true );
734
735 if ( 0 != strcmp( $text, $oldtext ) ) {
736 $this->mCountAdjustment = $this->isCountable( $text )
737 - $this->isCountable( $oldtext );
738
739 $now = wfTimestampNow();
740 $won = wfInvertTimestamp( $now );
741 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
742 "',cur_comment='" . wfStrencode( $summary ) .
743 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
744 ",cur_timestamp='{$now}',cur_user_text='" .
745 wfStrencode( $wgUser->getName() ) .
746 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
747 "WHERE cur_id=" . $this->getID() .
748 " AND cur_timestamp='" . $this->getTimestamp() . "'";
749 $res = wfQuery( $sql, DB_WRITE, $fname );
750
751 if( wfAffectedRows() == 0 ) {
752 /* Belated edit conflict! Run away!! */
753 return false;
754 }
755
756 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
757 "old_comment,old_user,old_user_text,old_timestamp," .
758 "old_minor_edit,inverse_timestamp) VALUES (" .
759 $this->mTitle->getNamespace() . ", '" .
760 wfStrencode( $this->mTitle->getDBkey() ) . "', '" .
761 wfStrencode( $oldtext ) . "', '" .
762 wfStrencode( $this->getComment() ) . "', " .
763 $this->getUser() . ", '" .
764 wfStrencode( $this->getUserText() ) . "', '" .
765 $this->getTimestamp() . "', " . $me1 . ", '" .
766 wfInvertTimestamp( $this->getTimestamp() ) . "')";
767 $res = wfQuery( $sql, DB_WRITE, $fname );
768 $oldid = wfInsertID( $res );
769
770 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
771 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
772 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
773 "'{$now}','{$now}'," . $this->mTitle->getNamespace() . ",'" .
774 wfStrencode( $this->mTitle->getDBkey() ) . "',0,{$me2}," .
775 ( $wgUser->isBot() ? 1 : 0 ) . "," .
776 $this->getID() . "," . $wgUser->getID() . ",'" .
777 wfStrencode( $wgUser->getName() ) . "','" .
778 wfStrencode( $summary ) . "',0,{$oldid})";
779 wfQuery( $sql, DB_WRITE, $fname );
780
781 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
782 "WHERE rc_namespace=" . $this->mTitle->getNamespace() . " AND " .
783 "rc_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' AND " .
784 "rc_timestamp='" . $this->getTimestamp() . "'";
785 wfQuery( $sql, DB_WRITE, $fname );
786
787 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
788 "WHERE rc_cur_id=" . $this->getID();
789 wfQuery( $sql, DB_WRITE, $fname );
790
791 if ( $wgEnablePersistentLC ) {
792
793 // Purge link cache for this page
794 $pageid=$this->getID();
795 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE);
796
797 // This next query just makes sure stub colored links to this page
798 // are updated correctly (I think). If performance is more important
799 // than real-time updating of stub links, we really should skip
800 // this query.
801 wfQuery("DELETE linkscc FROM linkscc,links ".
802 "WHERE lcc_title=links.l_from AND l_to={$pageid}", DB_WRITE);
803 }
804
805 }
806 if( $wgDBtransactions ) {
807 $sql = "COMMIT";
808 wfQuery( $sql, DB_WRITE );
809 }
810
811 if ($watchthis) {
812 if (!$this->mTitle->userIsWatching()) $this->watch();
813 } else {
814 if ( $this->mTitle->userIsWatching() ) {
815 $this->unwatch();
816 }
817 }
818
819 $this->showArticle( $text, wfMsg( "updated" ) );
820 return true;
821 }
822
823 # After we've either updated or inserted the article, update
824 # the link tables and redirect to the new page.
825
826 function showArticle( $text, $subtitle )
827 {
828 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
829 global $wgMwRedir;
830
831 $wgLinkCache = new LinkCache();
832
833 # Get old version of link table to allow incremental link updates
834 if ( $wgUseBetterLinksUpdate ) {
835 $wgLinkCache->preFill( $this->mTitle );
836 $wgLinkCache->clear();
837 }
838
839 # Now update the link cache by parsing the text
840 $wgOut = new OutputPage();
841 $wgOut->addWikiText( $text );
842
843 $this->editUpdates( $text );
844 if( $wgMwRedir->matchStart( $text ) )
845 $r = "redirect=no";
846 else
847 $r = "";
848 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL(), $r ) );
849 }
850
851 # Add this page to my watchlist
852
853 function watch( $add = true )
854 {
855 global $wgUser, $wgOut, $wgLang;
856 global $wgDeferredUpdateList;
857
858 if ( 0 == $wgUser->getID() ) {
859 $wgOut->errorpage( "watchnologin", "watchnologintext" );
860 return;
861 }
862 if ( wfReadOnly() ) {
863 $wgOut->readOnlyPage();
864 return;
865 }
866 if( $add )
867 $wgUser->addWatch( $this->mTitle );
868 else
869 $wgUser->removeWatch( $this->mTitle );
870
871 $wgOut->setPagetitle( wfMsg( $add ? "addedwatch" : "removedwatch" ) );
872 $wgOut->setRobotpolicy( "noindex,follow" );
873
874 $sk = $wgUser->getSkin() ;
875 $link = $sk->makeKnownLink ( $this->mTitle->getPrefixedText() ) ;
876
877 if($add)
878 $text = wfMsg( "addedwatchtext", $link );
879 else
880 $text = wfMsg( "removedwatchtext", $link );
881 $wgOut->addHTML( $text );
882
883 $up = new UserUpdate();
884 array_push( $wgDeferredUpdateList, $up );
885
886 $wgOut->returnToMain( false );
887 }
888
889 function unwatch()
890 {
891 $this->watch( false );
892 }
893
894 # This shares a lot of issues (and code) with Recent Changes
895
896 function history()
897 {
898 global $wgUser, $wgOut, $wgLang, $offset, $limit;
899
900 # If page hasn't changed, client can cache this
901
902 $wgOut->checkLastModified( $this->getTimestamp() );
903 $fname = "Article::history";
904 wfProfileIn( $fname );
905
906 $wgOut->setPageTitle( $this->mTitle->getPRefixedText() );
907 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
908 $wgOut->setArticleFlag( false );
909 $wgOut->setRobotpolicy( "noindex,nofollow" );
910
911 if( $this->mTitle->getArticleID() == 0 ) {
912 $wgOut->addHTML( wfMsg( "nohistory" ) );
913 wfProfileOut( $fname );
914 return;
915 }
916
917 $offset = (int)$offset;
918 $limit = (int)$limit;
919 if( $limit == 0 ) $limit = 50;
920 $namespace = $this->mTitle->getNamespace();
921 $title = $this->mTitle->getText();
922 $sql = "SELECT old_id,old_user," .
923 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
924 "FROM old USE INDEX (name_title_timestamp) " .
925 "WHERE old_namespace={$namespace} AND " .
926 "old_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
927 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
928 $res = wfQuery( $sql, DB_READ, "Article::history" );
929
930 $revs = wfNumRows( $res );
931 if( $this->mTitle->getArticleID() == 0 ) {
932 $wgOut->addHTML( wfMsg( "nohistory" ) );
933 wfProfileOut( $fname );
934 return;
935 }
936
937 $sk = $wgUser->getSkin();
938 $numbar = wfViewPrevNext(
939 $offset, $limit,
940 $this->mTitle->getPrefixedText(),
941 "action=history" );
942 $s = $numbar;
943 $s .= $sk->beginHistoryList();
944
945 if($offset == 0 )
946 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
947 $this->getUserText(), $namespace,
948 $title, 0, $this->getComment(),
949 ( $this->getMinorEdit() > 0 ) );
950
951 $revs = wfNumRows( $res );
952 while ( $line = wfFetchObject( $res ) ) {
953 $s .= $sk->historyLine( $line->old_timestamp, $line->old_user,
954 $line->old_user_text, $namespace,
955 $title, $line->old_id,
956 $line->old_comment, ( $line->old_minor_edit > 0 ) );
957 }
958 $s .= $sk->endHistoryList();
959 $s .= $numbar;
960 $wgOut->addHTML( $s );
961 wfProfileOut( $fname );
962 }
963
964 function protect( $limit = "sysop" )
965 {
966 global $wgUser, $wgOut;
967
968 if ( ! $wgUser->isSysop() ) {
969 $wgOut->sysopRequired();
970 return;
971 }
972 if ( wfReadOnly() ) {
973 $wgOut->readOnlyPage();
974 return;
975 }
976 $id = $this->mTitle->getArticleID();
977 if ( 0 == $id ) {
978 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
979 return;
980 }
981 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
982 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
983 wfQuery( $sql, DB_WRITE, "Article::protect" );
984
985 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
986 if ( $limit === "" ) {
987 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle->getPrefixedText() ), "" );
988 } else {
989 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle->getPrefixedText() ), "" );
990 }
991 $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) );
992 }
993
994 function unprotect()
995 {
996 return $this->protect( "" );
997 }
998
999 function delete()
1000 {
1001 global $wgUser, $wgOut;
1002 global $wpConfirm, $wpReason, $image, $oldimage;
1003
1004 # This code desperately needs to be totally rewritten
1005
1006 if ( ( ! $wgUser->isSysop() ) ) {
1007 $wgOut->sysopRequired();
1008 return;
1009 }
1010 if ( wfReadOnly() ) {
1011 $wgOut->readOnlyPage();
1012 return;
1013 }
1014
1015 # Better double-check that it hasn't been deleted yet!
1016 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
1017 if ( ( "" == trim( $this->mTitle->getText() ) )
1018 or ( $this->mTitle->getArticleId() == 0 ) ) {
1019 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
1020 return;
1021 }
1022
1023 if ( $wpConfirm ) {
1024 $this->doDelete();
1025 return;
1026 }
1027
1028 # determine whether this page has earlier revisions
1029 # and insert a warning if it does
1030 # we select the text because it might be useful below
1031 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
1032 $res=wfQuery($sql, DB_READ, $fname);
1033 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
1034 $skin=$wgUser->getSkin();
1035 $wgOut->addHTML("<B>".wfMsg("historywarning"));
1036 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
1037 }
1038
1039 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."'";
1040 $res=wfQuery($sql, DB_READ, $fname);
1041 if( ($s=wfFetchObject($res))) {
1042
1043 # if this is a mini-text, we can paste part of it into the deletion reason
1044
1045 #if this is empty, an earlier revision may contain "useful" text
1046 if($s->cur_text!="") {
1047 $text=$s->cur_text;
1048 } else {
1049 if($old) {
1050 $text=$old->old_text;
1051 $blanked=1;
1052 }
1053
1054 }
1055
1056 $length=strlen($text);
1057
1058 # this should not happen, since it is not possible to store an empty, new
1059 # page. Let's insert a standard text in case it does, though
1060 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
1061
1062
1063 if($length < 500 && !$wpReason) {
1064
1065 # comment field=255, let's grep the first 150 to have some user
1066 # space left
1067 $text=substr($text,0,150);
1068 # let's strip out newlines and HTML tags
1069 $text=preg_replace("/\"/","'",$text);
1070 $text=preg_replace("/\</","&lt;",$text);
1071 $text=preg_replace("/\>/","&gt;",$text);
1072 $text=preg_replace("/[\n\r]/","",$text);
1073 if(!$blanked) {
1074 $wpReason=wfMsg("excontent"). " '".$text;
1075 } else {
1076 $wpReason=wfMsg("exbeforeblank") . " '".$text;
1077 }
1078 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
1079 $wpReason.="'";
1080 }
1081 }
1082
1083 return $this->confirmDelete();
1084 }
1085
1086 function confirmDelete( $par = "" )
1087 {
1088 global $wgOut;
1089
1090 wfDebug( "Article::confirmDelete\n" );
1091
1092 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
1093 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
1094 $wgOut->setRobotpolicy( "noindex,nofollow" );
1095 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
1096
1097 $t = $this->mTitle->getPrefixedURL();
1098
1099 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
1100 $confirm = wfMsg( "confirm" );
1101 $check = wfMsg( "confirmcheck" );
1102 $delcom = wfMsg( "deletecomment" );
1103
1104 $wgOut->addHTML( "
1105 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
1106 <table border=0><tr><td align=right>
1107 {$delcom}:</td><td align=left>
1108 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
1109 </td></tr><tr><td>&nbsp;</td></tr>
1110 <tr><td align=right>
1111 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
1112 </td><td><label for=\"wpConfirm\">{$check}</label></td>
1113 </tr><tr><td>&nbsp;</td><td>
1114 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
1115 </td></tr></table></form>\n" );
1116
1117 $wgOut->returnToMain( false );
1118 }
1119
1120 function doDelete()
1121 {
1122 global $wgOut, $wgUser, $wgLang;
1123 global $wpReason;
1124 $fname = "Article::doDelete";
1125 wfDebug( "$fname\n" );
1126
1127 $this->doDeleteArticle( $this->mTitle );
1128 $deleted = $this->mTitle->getPrefixedText();
1129
1130 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1131 $wgOut->setRobotpolicy( "noindex,nofollow" );
1132
1133 $sk = $wgUser->getSkin();
1134 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1135 Namespace::getWikipedia() ) .
1136 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
1137
1138 $text = wfMsg( "deletedtext", $deleted, $loglink );
1139
1140 $wgOut->addHTML( "<p>" . $text );
1141 $wgOut->returnToMain( false );
1142 }
1143
1144 function doDeleteArticle( $title )
1145 {
1146 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
1147
1148 $fname = "Article::doDeleteArticle";
1149 wfDebug( "$fname\n" );
1150
1151 $ns = $title->getNamespace();
1152 $t = wfStrencode( $title->getDBkey() );
1153 $id = $title->getArticleID();
1154
1155 if ( "" == $t ) {
1156 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
1157 return;
1158 }
1159
1160 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1161 array_push( $wgDeferredUpdateList, $u );
1162
1163 # Move article and history to the "archive" table
1164 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
1165 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
1166 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
1167 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
1168 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
1169 wfQuery( $sql, DB_WRITE, $fname );
1170
1171 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
1172 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
1173 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
1174 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
1175 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1176 wfQuery( $sql, DB_WRITE, $fname );
1177
1178 # Now that it's safely backed up, delete it
1179
1180 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1181 "cur_title='{$t}'";
1182 wfQuery( $sql, DB_WRITE, $fname );
1183
1184 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1185 "old_title='{$t}'";
1186 wfQuery( $sql, DB_WRITE, $fname );
1187
1188 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1189 "rc_title='{$t}'";
1190 wfQuery( $sql, DB_WRITE, $fname );
1191
1192 # Finally, clean up the link tables
1193
1194 if ( 0 != $id ) {
1195
1196 $t = wfStrencode( $title->getPrefixedDBkey() );
1197
1198 if ( $wgEnablePersistentLC ) {
1199 // Purge related entries in links cache on delete,
1200 wfQuery("DELETE linkscc FROM linkscc,links ".
1201 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE);
1202 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE);
1203 }
1204
1205 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
1206 $res = wfQuery( $sql, DB_READ, $fname );
1207
1208 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1209 $now = wfTimestampNow();
1210 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1211 $first = true;
1212
1213 while ( $s = wfFetchObject( $res ) ) {
1214 $nt = Title::newFromDBkey( $s->l_from );
1215 $lid = $nt->getArticleID();
1216
1217 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1218 $first = false;
1219 $sql .= "({$lid},'{$t}')";
1220 $sql2 .= "{$lid}";
1221 }
1222 $sql2 .= ")";
1223 if ( ! $first ) {
1224 wfQuery( $sql, DB_WRITE, $fname );
1225 wfQuery( $sql2, DB_WRITE, $fname );
1226 }
1227 wfFreeResult( $res );
1228
1229 $sql = "DELETE FROM links WHERE l_to={$id}";
1230 wfQuery( $sql, DB_WRITE, $fname );
1231
1232 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1233 wfQuery( $sql, DB_WRITE, $fname );
1234
1235 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1236 wfQuery( $sql, DB_WRITE, $fname );
1237
1238 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1239 wfQuery( $sql, DB_WRITE, $fname );
1240 }
1241
1242 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1243 $art = $title->getPrefixedText();
1244 $wpReason = wfCleanQueryVar( $wpReason );
1245 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
1246
1247 # Clear the cached article id so the interface doesn't act like we exist
1248 $this->mTitle->resetArticleID( 0 );
1249 $this->mTitle->mArticleID = 0;
1250 }
1251
1252 function rollback()
1253 {
1254 global $wgUser, $wgLang, $wgOut, $from;
1255
1256 if ( ! $wgUser->isSysop() ) {
1257 $wgOut->sysopRequired();
1258 return;
1259 }
1260
1261 # Replace all this user's current edits with the next one down
1262 $tt = wfStrencode( $this->mTitle->getDBKey() );
1263 $n = $this->mTitle->getNamespace();
1264
1265 # Get the last editor
1266 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1267 $res = wfQuery( $sql, DB_READ );
1268 if( ($x = wfNumRows( $res )) != 1 ) {
1269 # Something wrong
1270 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1271 return;
1272 }
1273 $s = wfFetchObject( $res );
1274 $ut = wfStrencode( $s->cur_user_text );
1275 $uid = $s->cur_user;
1276 $pid = $s->cur_id;
1277
1278 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1279 if( $from != $s->cur_user_text ) {
1280 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1281 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1282 htmlspecialchars( $this->mTitle->getPrefixedText()),
1283 htmlspecialchars( $from ),
1284 htmlspecialchars( $s->cur_user_text ) ) );
1285 if($s->cur_comment != "") {
1286 $wgOut->addHTML(
1287 wfMsg("editcomment",
1288 htmlspecialchars( $s->cur_comment ) ) );
1289 }
1290 return;
1291 }
1292
1293 # Get the last edit not by this guy
1294 $sql = "SELECT old_text,old_user,old_user_text
1295 FROM old USE INDEX (name_title_timestamp)
1296 WHERE old_namespace={$n} AND old_title='{$tt}'
1297 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1298 ORDER BY inverse_timestamp LIMIT 1";
1299 $res = wfQuery( $sql, DB_READ );
1300 if( wfNumRows( $res ) != 1 ) {
1301 # Something wrong
1302 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1303 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1304 return;
1305 }
1306 $s = wfFetchObject( $res );
1307
1308 # Save it!
1309 $newcomment = wfMsg( "revertpage", $s->old_user_text );
1310 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1311 $wgOut->setRobotpolicy( "noindex,nofollow" );
1312 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1313 $this->updateArticle( $s->old_text, $newcomment, 1, $this->mTitle->userIsWatching() );
1314
1315 $wgOut->returnToMain( false );
1316 }
1317
1318
1319 # Do standard deferred updates after page view
1320
1321 /* private */ function viewUpdates()
1322 {
1323 global $wgDeferredUpdateList;
1324
1325 if ( 0 != $this->getID() ) {
1326 global $wgDisableCounters;
1327 if( !$wgDisableCounters ) {
1328 $u = new ViewCountUpdate( $this->getID() );
1329 array_push( $wgDeferredUpdateList, $u );
1330 $u = new SiteStatsUpdate( 1, 0, 0 );
1331 array_push( $wgDeferredUpdateList, $u );
1332 }
1333 $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
1334 $this->mTitle->getDBkey() );
1335 array_push( $wgDeferredUpdateList, $u );
1336 }
1337 }
1338
1339 # Do standard deferred updates after page edit.
1340 # Every 1000th edit, prune the recent changes table.
1341
1342 /* private */ function editUpdates( $text )
1343 {
1344 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1345
1346 wfSeedRandom();
1347 if ( 0 == mt_rand( 0, 999 ) ) {
1348 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1349 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1350 wfQuery( $sql, DB_WRITE );
1351 }
1352 $id = $this->getID();
1353 $title = $this->mTitle->getPrefixedDBkey();
1354 $adj = $this->mCountAdjustment;
1355
1356 if ( 0 != $id ) {
1357 $u = new LinksUpdate( $id, $title );
1358 array_push( $wgDeferredUpdateList, $u );
1359 $u = new SiteStatsUpdate( 0, 1, $adj );
1360 array_push( $wgDeferredUpdateList, $u );
1361 $u = new SearchUpdate( $id, $title, $text );
1362 array_push( $wgDeferredUpdateList, $u );
1363
1364 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(),
1365 $this->mTitle->getDBkey() );
1366 array_push( $wgDeferredUpdateList, $u );
1367
1368 if ( $this->getNamespace == NS_MEDIAWIKI ) {
1369 $messageCache = $wgMemc->get( "$wgDBname:messages" );
1370 if (!$messageCache) {
1371 $messageCache = wfLoadAllMessages();
1372 }
1373 $messageCache[$title] = $text;
1374 $wgMemc->set( "$wgDBname:messages" );
1375 }
1376 }
1377 }
1378
1379 /* private */ function setOldSubtitle()
1380 {
1381 global $wgLang, $wgOut;
1382
1383 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1384 $r = wfMsg( "revisionasof", $td );
1385 $wgOut->setSubtitle( "({$r})" );
1386 }
1387
1388 function blockedIPpage()
1389 {
1390 global $wgOut, $wgUser, $wgLang;
1391
1392 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
1393 $wgOut->setRobotpolicy( "noindex,nofollow" );
1394 $wgOut->setArticleFlag( false );
1395
1396 $id = $wgUser->blockedBy();
1397 $reason = $wgUser->blockedFor();
1398
1399 $name = User::whoIs( $id );
1400 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
1401 ":{$name}|{$name}]]";
1402
1403 $text = wfMsg( "blockedtext", $link, $reason, getenv( "REMOTE_ADDR" ) );
1404 $wgOut->addWikiText( $text );
1405 $wgOut->returnToMain( false );
1406 }
1407
1408 # This function is called right before saving the wikitext,
1409 # so we can do things like signatures and links-in-context.
1410
1411 function preSaveTransform( $text )
1412 {
1413 $s = "";
1414 while ( "" != $text ) {
1415 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1416 $s .= $this->pstPass2( $p[0] );
1417
1418 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
1419 else {
1420 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1421 $s .= "<nowiki>{$q[0]}</nowiki>";
1422 $text = $q[1];
1423 }
1424 }
1425 return rtrim( $s );
1426 }
1427
1428 /* private */ function pstPass2( $text )
1429 {
1430 global $wgUser, $wgLang, $wgLocaltimezone;
1431
1432 # Signatures
1433 #
1434 $n = $wgUser->getName();
1435 $k = $wgUser->getOption( "nickname" );
1436 if ( "" == $k ) { $k = $n; }
1437 if(isset($wgLocaltimezone)) {
1438 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1439 }
1440 /* Note: this is an ugly timezone hack for the European wikis */
1441 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1442 " (" . date( "T" ) . ")";
1443 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1444
1445 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1446 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1447 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1448 Namespace::getUser() ) . ":$n|$k]]", $text );
1449
1450 # Context links: [[|name]] and [[name (context)|]]
1451 #
1452 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1453 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1454 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1455 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1456
1457 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1458 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1459 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1460 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1461 # [[ns:page (cont)|]]
1462 $context = "";
1463 $t = $this->mTitle->getText();
1464 if ( preg_match( $conpat, $t, $m ) ) {
1465 $context = $m[2];
1466 }
1467 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1468 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1469 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1470
1471 if ( "" == $context ) {
1472 $text = preg_replace( $p2, "[[\\1]]", $text );
1473 } else {
1474 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1475 }
1476
1477 # {{SUBST:xxx}} variables
1478 #
1479 $mw =& MagicWord::get( MAG_SUBST );
1480 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1481
1482 return $text;
1483 }
1484
1485 /* Caching functions */
1486
1487 function tryFileCache() {
1488 if($this->isFileCacheable()) {
1489 $touched = $this->mTouched;
1490 if( strpos( $this->mContent, "{{" ) !== false ) {
1491 # Expire pages with variable replacements in an hour
1492 $expire = wfUnix2Timestamp( time() - 3600 );
1493 $touched = max( $expire, $touched );
1494 }
1495 $cache = new CacheManager( $this->mTitle );
1496 if($cache->isFileCacheGood( $touched )) {
1497 global $wgOut;
1498 wfDebug( " tryFileCache() - about to load\n" );
1499 $cache->loadFromFileCache();
1500 $wgOut->reportTime(); # For profiling
1501 exit;
1502 } else {
1503 wfDebug( " tryFileCache() - starting buffer\n" );
1504 if($cache->useGzip() && wfClientAcceptsGzip()) {
1505 /* For some reason, adding this header line over in
1506 CacheManager::saveToFileCache() fails on my test
1507 setup at home, though it works on the live install.
1508 Make double-sure... --brion */
1509 header( "Content-Encoding: gzip" );
1510 }
1511 ob_start( array(&$cache, 'saveToFileCache' ) );
1512 }
1513 } else {
1514 wfDebug( " tryFileCache() - not cacheable\n" );
1515 }
1516 }
1517
1518 function isFileCacheable() {
1519 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1520 global $action, $oldid, $diff, $redirect, $printable;
1521 return $wgUseFileCache
1522 and (!$wgShowIPinHeader)
1523 and ($this->getID() != 0)
1524 and ($wgUser->getId() == 0)
1525 and (!$wgUser->getNewtalk())
1526 and ($this->mTitle->getNamespace != Namespace::getSpecial())
1527 and ($action == "view")
1528 and (!isset($oldid))
1529 and (!isset($diff))
1530 and (!isset($redirect))
1531 and (!isset($printable))
1532 and (!$this->mRedirectedFrom);
1533 }
1534
1535 function checkTouched() {
1536 $id = $this->getID();
1537 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1538 $res = wfQuery( $sql, DB_READ, "Article::checkTouched" );
1539 if( $s = wfFetchObject( $res ) ) {
1540 $this->mTouched = $s->cur_touched;
1541 return !$s->cur_is_redirect;
1542 } else {
1543 return false;
1544 }
1545 }
1546 }
1547
1548 function wfReplaceSubstVar( $matches ) {
1549 return wfMsg( $matches[1] );
1550 }
1551
1552 ?>