3 # Splitting edit page/HTML interface from Article...
4 # The actual database and text munging is still in Article,
5 # but it should get easier to call those from alternate
13 var $save = false, $preview = false;
14 var $minoredit = false, $watchthis = false;
15 var $textbox1 = "", $textbox2 = "", $summary = "";
16 var $edittime = "", $section = "";
19 function EditPage( $article ) {
20 $this->mArticle
=& $article;
22 $this->mTitle
=& $wgTitle;
25 # This is the function that gets called for "action=edit".
29 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
30 // this is not an article
31 $wgOut->setArticleFlag(false);
33 $this->importFormData( $wgRequest );
35 if ( ! $this->mTitle
->userCanEdit() ) {
36 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
39 if ( $wgUser->isBlocked() ) {
40 $this->blockedIPpage();
43 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
44 $this->userNotLoggedInPage();
48 if( $this->save ||
$this->preview
) {
49 $this->editForm( "preview" );
51 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
56 $this->editForm( "save" );
57 } else if ( $this->preview
) {
58 $this->editForm( "preview" );
59 } else { # First time through
60 $this->editForm( "initial" );
64 function importFormData( &$request ) {
65 # These fields need to be checked for encoding.
66 # Also remove trailing whitespace, but don't remove _initial_
67 # whitespace from the text boxes. This may be significant formatting.
68 $this->textbox1
= rtrim( $request->getText( "wpTextbox1" ) );
69 $this->textbox2
= rtrim( $request->getText( "wpTextbox2" ) );
70 $this->summary
= trim( $request->getText( "wpSummary" ) );
72 $this->edittime
= $request->getVal( 'wpEdittime' );
73 if( !preg_match( '/^\d{14}$/', $this->edittime
) ) $this->edittime
= "";
75 $this->preview
= $request->getCheck( 'wpPreview' );
76 $this->save
= $request->wasPosted() && !$this->preview
;
77 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
78 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
80 $this->oldid
= $request->getInt( 'oldid' );
82 # Section edit can come from either the form or a link
83 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
86 # Since there is only one text field on the edit form,
87 # pressing <enter> will cause the form to be submitted, but
88 # the submit button value won't appear in the query, so we
89 # Fake it here before going back to edit(). This is kind of
90 # ugly, but it helps some old URLs to still work.
94 if( !$this->preview
) $this->save
= true;
99 # The edit form is self-submitting, so that when things like
100 # preview and edit conflicts occur, we get the same form back
101 # with the extra stuff added. Only when the final submission
102 # is made and all is well do we actually save and redirect to
103 # the newly-edited page.
105 function editForm( $formtype )
107 global $wgOut, $wgUser;
108 global $wgLang, $wgParser, $wgTitle;
109 global $wgAllowAnonymousMinor;
110 global $wgWhitelistEdit;
113 $sk = $wgUser->getSkin();
116 if(!$this->mTitle
->getArticleID()) { # new article
117 $wgOut->addWikiText(wfmsg("newarticletext"));
120 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
121 $wgOut->addWikiText(wfmsg("talkpagetext"));
124 # Attempt submission here. This will check for edit conflicts,
125 # and redundantly check for locked database, blocked IPs, etc.
126 # that edit() already checked just in case someone tries to sneak
127 # in the back door with a hand-edited submission URL.
129 if ( "save" == $formtype ) {
131 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $wpTextbox1 ) ) {
133 $wgOut->redirect( $this->mTitle
->getFullURL() );
136 if ( $wgUser->isBlocked() ) {
137 $this->blockedIPpage();
140 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
141 $this->userNotLoggedInPage();
144 if ( wfReadOnly() ) {
145 $wgOut->readOnlyPage();
149 # If article is new, insert it.
150 $aid = $this->mTitle
->getArticleID();
152 # Don't save a new article if it's blank.
153 if ( ( "" == $this->textbox1
) ||
154 ( wfMsg( "newarticletext" ) == $this->textbox1
) ) {
155 $wgOut->redirect( $this->mTitle
->getFullURL() );
158 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
162 # Article exists. Check for edit conflict.
164 $this->mArticle
->clear(); # Force reload of dates, etc.
166 if( ( $this->section
!= "new" ) &&
167 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
170 $userid = $wgUser->getID();
172 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
173 $this->section
, $this->textbox1
, $this->summary
);
174 # Suppress edit conflict with self
176 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
179 # switch from section editing to normal editing in edit conflict
182 if( $this->mergeChangesInto( $text ) ){
183 // Successful merge! Maybe we should tell the user the good news?
187 $this->textbox1
= $text;
191 if ( ! $isConflict ) {
192 # All's well: update the article here
193 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
))
199 # First time through: get contents, set time for conflict
202 if ( "initial" == $formtype ) {
203 $this->edittime
= $this->mArticle
->getTimestamp();
204 $this->textbox1
= $this->mArticle
->getContent( true );
208 $wgOut->setRobotpolicy( "noindex,nofollow" );
210 # Enabled article-related sidebar, toplinks, etc.
211 $wgOut->setArticleRelated( true );
214 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
215 $wgOut->setPageTitle( $s );
216 $wgOut->addHTML( wfMsg( "explainconflict" ) );
218 $this->textbox2
= $this->textbox1
;
219 $this->textbox1
= $this->mArticle
->getContent( true );
220 $this->edittime
= $this->mArticle
->getTimestamp();
222 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
224 if( $this->section
!= "" ) {
225 if( $this->section
== "new" ) {
226 $s.=wfMsg("commentedit");
228 $s.=wfMsg("sectionedit");
230 if(!$this->preview
) {
231 $sectitle=preg_match("/^=+(.*?)=+/mi",
234 if( !empty( $matches[1] ) ) {
235 $this->summary
= "/* ". trim($matches[1])." */ ";
239 $wgOut->setPageTitle( $s );
240 if ( $this->oldid
) {
241 $this->mArticle
->setOldSubtitle();
242 $wgOut->addHTML( wfMsg( "editingold" ) );
247 $wgOut->addHTML( "<strong>" .
248 wfMsg( "readonlywarning" ) .
251 if( $this->mTitle
->isProtected() ) {
252 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
253 "</strong><br />\n" );
256 $kblength = (int)(strlen( $this->textbox1
) / 1024);
257 if( $kblength > 29 ) {
258 $wgOut->addHTML( "<strong>" .
259 wfMsg( "longpagewarning", $kblength )
263 $rows = $wgUser->getOption( "rows" );
264 $cols = $wgUser->getOption( "cols" );
266 $ew = $wgUser->getOption( "editwidth" );
267 if ( $ew ) $ew = " style=\"width:100%\"";
270 $q = "action=submit";
271 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
272 $action = $this->mTitle
->escapeLocalURL( $q );
274 $summary = wfMsg( "summary" );
275 $subject = wfMsg("subject");
276 $minor = wfMsg( "minoredit" );
277 $watchthis = wfMsg ("watchthis");
278 $save = wfMsg( "savearticle" );
279 $prev = wfMsg( "showpreview" );
281 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
283 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
284 $edithelp = '<a onclick="javascript:window.open('.
285 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10'".'); return false;" href="'.$edithelpurl.'">'.
286 wfMsg( "edithelp" ).'</a>';
287 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
288 wfMsg( "copyrightpage" ) ) );
290 if( $wgUser->getOption("showtoolbar") ) {
291 # prepare toolbar for edit buttons
292 $toolbar = $sk->getEditToolbar();
297 // activate checkboxes if user wants them to be always active
298 if( !$this->preview
) {
299 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
300 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
302 // activate checkbox also if user is already watching the page,
303 // require wpWatchthis to be unset so that second condition is not
304 // checked unnecessarily
305 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
310 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
312 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
313 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
314 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
319 if ( 0 != $wgUser->getID() ) {
320 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
321 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
322 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
325 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
327 if ( "preview" == $formtype) {
328 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
329 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
331 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
334 $previewtext = wfUnescapeHTML( $this->textbox1
);
336 $parserOptions = ParserOptions
::newFromUser( $wgUser );
337 $parserOptions->setUseCategoryMagic( false );
338 $parserOptions->setEditSection( false );
339 $parserOptions->setEditSectionOnRightClick( false );
340 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
341 $wgTitle, $parserOptions );
342 $previewHTML = $parserOutput->mText
;
344 if($wgUser->getOption("previewontop")) {
345 $wgOut->addHTML($previewhead);
346 $wgOut->addHTML($previewHTML);
348 $wgOut->addHTML( "<br clear=\"all\" />\n" );
351 # if this is a comment, show a subject line at the top, which is also the edit summary.
352 # Otherwise, show a summary field at the bottom
353 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
354 if( $this->section
== "new" ) {
355 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
358 $commentsubject = "";
359 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
362 if( !$this->preview
) {
363 # Don't select the edit box on preview; this interferes with seeing what's going on.
364 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
368 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
369 enctype=\"application/x-www-form-urlencoded\">
371 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
372 cols='{$cols}'{$ew}>" .
373 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
378 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
379 " title=\"".wfMsg('tooltip-save')."\"/>
380 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
381 " title=\"".wfMsg('tooltip-preview')."\"/>
382 <em>{$cancel}</em> | <em>{$edithelp}</em>
383 <br /><br />{$copywarn}
384 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
385 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
388 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
389 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
390 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
392 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
393 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
394 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
398 $wgOut->addHTML( "</form>\n" );
399 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
400 $wgOut->addHTML($previewhead);
401 $wgOut->addHTML($previewHTML);
406 function blockedIPpage()
408 global $wgOut, $wgUser, $wgLang, $wgIP;
410 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
411 $wgOut->setRobotpolicy( "noindex,nofollow" );
412 $wgOut->setArticleRelated( false );
414 $id = $wgUser->blockedBy();
415 $reason = $wgUser->blockedFor();
418 $name = User
::whoIs( $id );
419 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
420 ":{$name}|{$name}]]";
422 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
423 $wgOut->returnToMain( false );
428 function userNotLoggedInPage()
430 global $wgOut, $wgUser, $wgLang;
432 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
433 $wgOut->setRobotpolicy( "noindex,nofollow" );
434 $wgOut->setArticleRelated( false );
436 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
437 $wgOut->returnToMain( false );
440 # Forks processes to scan the originating IP for an open proxy server
441 # MemCached can be used to skip IPs that have already been scanned
442 function proxyCheck()
444 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
445 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
447 if ( !$wgBlockOpenProxies ) {
453 if ( $wgUseMemCached ) {
454 $mcKey = "$wgDBname:proxy:ip:$wgIP";
455 $mcValue = $wgMemc->get( $mcKey );
463 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
464 $iphash = md5( $wgIP . $wgProxyKey );
465 $url = $title->getFullURL( "ip=$iphash" );
467 foreach ( $wgProxyPorts as $port ) {
468 $params = implode( " ", array(
469 escapeshellarg( $wgProxyScriptPath ),
470 escapeshellarg( $wgIP ),
471 escapeshellarg( $port ),
472 escapeshellarg( $url )
474 exec( "php $params &>/dev/null &" );
477 if ( $wgUseMemCached ) {
478 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
483 /* private */ function mergeChangesInto( &$text ){
484 $oldDate = $this->edittime
;
485 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
486 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
487 $obj = wfFetchObject($res);
489 $yourtext = $obj->cur_text
;
490 $ns = $this->mTitle
->getNamespace();
491 $title = wfStrencode( $this->mTitle
->getDBkey() );
492 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
493 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
494 $obj = wfFetchObject($res);
495 if(wfMerge($obj->old_text
, $text, $yourtext, $result)){