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();
115 // css / js subpages of user pages get a special treatment
116 $isCssJsSubpage = Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() );
118 if(!$this->mTitle
->getArticleID()) { # new article
119 $wgOut->addWikiText(wfmsg("newarticletext"));
122 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
123 $wgOut->addWikiText(wfmsg("talkpagetext"));
126 # Attempt submission here. This will check for edit conflicts,
127 # and redundantly check for locked database, blocked IPs, etc.
128 # that edit() already checked just in case someone tries to sneak
129 # in the back door with a hand-edited submission URL.
131 if ( "save" == $formtype ) {
133 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $wpTextbox1 ) ) {
135 $wgOut->redirect( $this->mTitle
->getFullURL() );
138 if ( $wgUser->isBlocked() ) {
139 $this->blockedIPpage();
142 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
143 $this->userNotLoggedInPage();
146 if ( wfReadOnly() ) {
147 $wgOut->readOnlyPage();
151 # If article is new, insert it.
152 $aid = $this->mTitle
->getArticleID();
154 # Don't save a new article if it's blank.
155 if ( ( "" == $this->textbox1
) ||
156 ( wfMsg( "newarticletext" ) == $this->textbox1
) ) {
157 $wgOut->redirect( $this->mTitle
->getFullURL() );
160 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
164 # Article exists. Check for edit conflict.
166 $this->mArticle
->clear(); # Force reload of dates, etc.
168 if( ( $this->section
!= "new" ) &&
169 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
172 $userid = $wgUser->getID();
174 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
175 $this->section
, $this->textbox1
, $this->summary
);
176 # Suppress edit conflict with self
178 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
181 # switch from section editing to normal editing in edit conflict
184 if( $this->mergeChangesInto( $text ) ){
185 // Successful merge! Maybe we should tell the user the good news?
189 $this->textbox1
= $text;
193 if ( ! $isConflict ) {
194 # All's well: update the article here
195 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
))
201 # First time through: get contents, set time for conflict
204 if ( "initial" == $formtype ) {
205 $this->edittime
= $this->mArticle
->getTimestamp();
206 $this->textbox1
= $this->mArticle
->getContent( true );
210 $wgOut->setRobotpolicy( "noindex,nofollow" );
212 # Enabled article-related sidebar, toplinks, etc.
213 $wgOut->setArticleRelated( true );
216 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
217 $wgOut->setPageTitle( $s );
218 $wgOut->addHTML( wfMsg( "explainconflict" ) );
220 $this->textbox2
= $this->textbox1
;
221 $this->textbox1
= $this->mArticle
->getContent( true );
222 $this->edittime
= $this->mArticle
->getTimestamp();
224 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
226 if( $this->section
!= "" ) {
227 if( $this->section
== "new" ) {
228 $s.=wfMsg("commentedit");
230 $s.=wfMsg("sectionedit");
232 if(!$this->preview
) {
233 $sectitle=preg_match("/^=+(.*?)=+/mi",
236 if( !empty( $matches[1] ) ) {
237 $this->summary
= "/* ". trim($matches[1])." */ ";
241 $wgOut->setPageTitle( $s );
242 if ( $this->oldid
) {
243 $this->mArticle
->setOldSubtitle();
244 $wgOut->addHTML( wfMsg( "editingold" ) );
249 $wgOut->addHTML( "<strong>" .
250 wfMsg( "readonlywarning" ) .
252 } else if ( $isCssJsSubpage and "preview" != $formtype) {
253 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
255 if( $this->mTitle
->isProtected() ) {
256 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
257 "</strong><br />\n" );
260 $kblength = (int)(strlen( $this->textbox1
) / 1024);
261 if( $kblength > 29 ) {
262 $wgOut->addHTML( "<strong>" .
263 wfMsg( "longpagewarning", $kblength )
267 $rows = $wgUser->getOption( "rows" );
268 $cols = $wgUser->getOption( "cols" );
270 $ew = $wgUser->getOption( "editwidth" );
271 if ( $ew ) $ew = " style=\"width:100%\"";
274 $q = "action=submit";
275 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
276 $action = $this->mTitle
->escapeLocalURL( $q );
278 $summary = wfMsg( "summary" );
279 $subject = wfMsg("subject");
280 $minor = wfMsg( "minoredit" );
281 $watchthis = wfMsg ("watchthis");
282 $save = wfMsg( "savearticle" );
283 $prev = wfMsg( "showpreview" );
285 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
287 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
288 $edithelp = '<a onclick="window.open('.
289 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10'".'); return false;" href="'.$edithelpurl.'">'.
290 wfMsg( "edithelp" ).'</a>';
291 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
292 wfMsg( "copyrightpage" ) ) );
294 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
295 # prepare toolbar for edit buttons
296 $toolbar = $sk->getEditToolbar();
301 // activate checkboxes if user wants them to be always active
302 if( !$this->preview
) {
303 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
304 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
306 // activate checkbox also if user is already watching the page,
307 // require wpWatchthis to be unset so that second condition is not
308 // checked unnecessarily
309 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
314 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
316 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
317 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
318 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
323 if ( 0 != $wgUser->getID() ) {
324 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
325 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
326 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
329 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
331 if ( "preview" == $formtype) {
332 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
333 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
335 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
338 $previewtext = $this->textbox1
;
340 $parserOptions = ParserOptions
::newFromUser( $wgUser );
341 $parserOptions->setUseCategoryMagic( false );
342 $parserOptions->setEditSection( false );
343 $parserOptions->setEditSectionOnRightClick( false );
344 # don't parse user css/js, show message about preview
345 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
346 if ( $isCssJsSubpage ) {
347 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
348 $previewtext = wfMsg('usercsspreview');
349 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
350 $previewtext = wfMsg('userjspreview');
352 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
353 $wgOut->addHTML( $parserOutput->mText
);
355 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
356 $wgTitle, $parserOptions );
357 $previewHTML = $parserOutput->mText
;
359 if($wgUser->getOption("previewontop")) {
360 $wgOut->addHTML($previewhead);
361 $wgOut->addHTML($previewHTML);
363 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
367 # if this is a comment, show a subject line at the top, which is also the edit summary.
368 # Otherwise, show a summary field at the bottom
369 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
370 if( $this->section
== "new" ) {
371 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
374 $commentsubject = "";
375 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
378 if( !$this->preview
) {
379 # Don't select the edit box on preview; this interferes with seeing what's going on.
380 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
384 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
385 enctype=\"application/x-www-form-urlencoded\">
387 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
388 cols='{$cols}'{$ew}>" .
389 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
394 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
395 " title=\"".wfMsg('tooltip-save')."\"/>
396 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
397 " title=\"".wfMsg('tooltip-preview')."\"/>
398 <em>{$cancel}</em> | <em>{$edithelp}</em>
399 <br /><br />{$copywarn}
400 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
401 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
404 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
405 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
406 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
408 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
409 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
410 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
414 $wgOut->addHTML( "</form>\n" );
415 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
416 $wgOut->addHTML($previewhead);
417 $wgOut->addHTML($previewHTML);
422 function blockedIPpage()
424 global $wgOut, $wgUser, $wgLang, $wgIP;
426 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
427 $wgOut->setRobotpolicy( "noindex,nofollow" );
428 $wgOut->setArticleRelated( false );
430 $id = $wgUser->blockedBy();
431 $reason = $wgUser->blockedFor();
434 $name = User
::whoIs( $id );
435 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
436 ":{$name}|{$name}]]";
438 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
439 $wgOut->returnToMain( false );
444 function userNotLoggedInPage()
446 global $wgOut, $wgUser, $wgLang;
448 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
449 $wgOut->setRobotpolicy( "noindex,nofollow" );
450 $wgOut->setArticleRelated( false );
452 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
453 $wgOut->returnToMain( false );
456 # Forks processes to scan the originating IP for an open proxy server
457 # MemCached can be used to skip IPs that have already been scanned
458 function proxyCheck()
460 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
461 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
463 if ( !$wgBlockOpenProxies ) {
469 if ( $wgUseMemCached ) {
470 $mcKey = "$wgDBname:proxy:ip:$wgIP";
471 $mcValue = $wgMemc->get( $mcKey );
479 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
480 $iphash = md5( $wgIP . $wgProxyKey );
481 $url = $title->getFullURL( "ip=$iphash" );
483 foreach ( $wgProxyPorts as $port ) {
484 $params = implode( " ", array(
485 escapeshellarg( $wgProxyScriptPath ),
486 escapeshellarg( $wgIP ),
487 escapeshellarg( $port ),
488 escapeshellarg( $url )
490 exec( "php $params &>/dev/null &" );
493 if ( $wgUseMemCached ) {
494 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
499 /* private */ function mergeChangesInto( &$text ){
500 $oldDate = $this->edittime
;
501 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
502 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
503 $obj = wfFetchObject($res);
505 $yourtext = $obj->cur_text
;
506 $ns = $this->mTitle
->getNamespace();
507 $title = wfStrencode( $this->mTitle
->getDBkey() );
508 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
509 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
510 $obj = wfFetchObject($res);
511 if(wfMerge($obj->old_text
, $text, $yourtext, $result)){