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