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 ) {
196 if( $this->section
!= '' ) {
197 # Try to get a section anchor from the section source, redirect to edited section if header found
198 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
199 # for duplicate heading checking and maybe parsing
200 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
201 # we can't deal with anchors, includes, html etc in the header for now,
202 # headline would need to be parsed to improve this
203 if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
204 global $wgInputEncoding;
205 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
206 # strip out HTML, will be useful when
207 # $headline = preg_replace( "/<.*?" . ">/","",$headline );
208 $headline = trim( $headline );
209 $sectionanchor = '#'.preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', urlencode( $headline ) );
213 # update the article here
214 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
220 # First time through: get contents, set time for conflict
223 if ( "initial" == $formtype ) {
224 $this->edittime
= $this->mArticle
->getTimestamp();
225 $this->textbox1
= $this->mArticle
->getContent( true );
229 $wgOut->setRobotpolicy( "noindex,nofollow" );
231 # Enabled article-related sidebar, toplinks, etc.
232 $wgOut->setArticleRelated( true );
235 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
236 $wgOut->setPageTitle( $s );
237 $wgOut->addHTML( wfMsg( "explainconflict" ) );
239 $this->textbox2
= $this->textbox1
;
240 $this->textbox1
= $this->mArticle
->getContent( true );
241 $this->edittime
= $this->mArticle
->getTimestamp();
243 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
245 if( $this->section
!= "" ) {
246 if( $this->section
== "new" ) {
247 $s.=wfMsg("commentedit");
249 $s.=wfMsg("sectionedit");
251 if(!$this->preview
) {
252 $sectitle=preg_match("/^=+(.*?)=+/mi",
255 if( !empty( $matches[1] ) ) {
256 $this->summary
= "/* ". trim($matches[1])." */ ";
260 $wgOut->setPageTitle( $s );
261 if ( $this->oldid
) {
262 $this->mArticle
->setOldSubtitle();
263 $wgOut->addHTML( wfMsg( "editingold" ) );
268 $wgOut->addHTML( "<strong>" .
269 wfMsg( "readonlywarning" ) .
271 } else if ( $isCssJsSubpage and "preview" != $formtype) {
272 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
274 if( $this->mTitle
->isProtected() ) {
275 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
276 "</strong><br />\n" );
279 $kblength = (int)(strlen( $this->textbox1
) / 1024);
280 if( $kblength > 29 ) {
281 $wgOut->addHTML( "<strong>" .
282 wfMsg( "longpagewarning", $kblength )
286 $rows = $wgUser->getOption( "rows" );
287 $cols = $wgUser->getOption( "cols" );
289 $ew = $wgUser->getOption( "editwidth" );
290 if ( $ew ) $ew = " style=\"width:100%\"";
293 $q = "action=submit";
294 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
295 $action = $this->mTitle
->escapeLocalURL( $q );
297 $summary = wfMsg( "summary" );
298 $subject = wfMsg("subject");
299 $minor = wfMsg( "minoredit" );
300 $watchthis = wfMsg ("watchthis");
301 $save = wfMsg( "savearticle" );
302 $prev = wfMsg( "showpreview" );
304 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
306 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
307 $edithelp = '<a onclick="window.open('.
308 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10'".'); return false;" href="'.$edithelpurl.'">'.
309 wfMsg( "edithelp" ).'</a>';
310 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
311 wfMsg( "copyrightpage" ) ) );
313 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
314 # prepare toolbar for edit buttons
315 $toolbar = $sk->getEditToolbar();
320 // activate checkboxes if user wants them to be always active
321 if( !$this->preview
) {
322 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
323 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
325 // activate checkbox also if user is already watching the page,
326 // require wpWatchthis to be unset so that second condition is not
327 // checked unnecessarily
328 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
333 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
335 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
336 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
337 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
342 if ( 0 != $wgUser->getID() ) {
343 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
344 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
345 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
348 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
350 if ( "preview" == $formtype) {
351 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
352 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
354 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
357 $previewtext = wfUnescapeHTML( $this->textbox1
);
359 $parserOptions = ParserOptions
::newFromUser( $wgUser );
360 $parserOptions->setUseCategoryMagic( false );
361 $parserOptions->setEditSection( false );
362 $parserOptions->setEditSectionOnRightClick( false );
363 # don't parse user css/js, show message about preview
364 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
365 if ( $isCssJsSubpage ) {
366 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
367 $previewtext = wfMsg('usercsspreview');
368 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
369 $previewtext = wfMsg('userjspreview');
371 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
372 $wgOut->addHTML( $parserOutput->mText
);
374 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
375 $wgTitle, $parserOptions );
376 $previewHTML = $parserOutput->mText
;
378 if($wgUser->getOption("previewontop")) {
379 $wgOut->addHTML($previewhead);
380 $wgOut->addHTML($previewHTML);
382 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
386 # if this is a comment, show a subject line at the top, which is also the edit summary.
387 # Otherwise, show a summary field at the bottom
388 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
389 if( $this->section
== "new" ) {
390 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
393 $commentsubject = "";
394 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
397 if( !$this->preview
) {
398 # Don't select the edit box on preview; this interferes with seeing what's going on.
399 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
403 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
404 enctype=\"application/x-www-form-urlencoded\">
406 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
407 cols='{$cols}'{$ew}>" .
408 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
413 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
414 " title=\"".wfMsg('tooltip-save')."\"/>
415 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
416 " title=\"".wfMsg('tooltip-preview')."\"/>
417 <em>{$cancel}</em> | <em>{$edithelp}</em>
418 <br /><br />{$copywarn}
419 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
420 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
423 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
424 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
425 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
427 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
428 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
429 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
433 $wgOut->addHTML( "</form>\n" );
434 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
435 $wgOut->addHTML($previewhead);
436 $wgOut->addHTML($previewHTML);
441 function blockedIPpage()
443 global $wgOut, $wgUser, $wgLang, $wgIP;
445 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
446 $wgOut->setRobotpolicy( "noindex,nofollow" );
447 $wgOut->setArticleRelated( false );
449 $id = $wgUser->blockedBy();
450 $reason = $wgUser->blockedFor();
453 $name = User
::whoIs( $id );
454 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
455 ":{$name}|{$name}]]";
457 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
458 $wgOut->returnToMain( false );
463 function userNotLoggedInPage()
465 global $wgOut, $wgUser, $wgLang;
467 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
468 $wgOut->setRobotpolicy( "noindex,nofollow" );
469 $wgOut->setArticleRelated( false );
471 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
472 $wgOut->returnToMain( false );
475 # Forks processes to scan the originating IP for an open proxy server
476 # MemCached can be used to skip IPs that have already been scanned
477 function proxyCheck()
479 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
480 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
482 if ( !$wgBlockOpenProxies ) {
488 if ( $wgUseMemCached ) {
489 $mcKey = "$wgDBname:proxy:ip:$wgIP";
490 $mcValue = $wgMemc->get( $mcKey );
498 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
499 $iphash = md5( $wgIP . $wgProxyKey );
500 $url = $title->getFullURL( "ip=$iphash" );
502 foreach ( $wgProxyPorts as $port ) {
503 $params = implode( " ", array(
504 escapeshellarg( $wgProxyScriptPath ),
505 escapeshellarg( $wgIP ),
506 escapeshellarg( $port ),
507 escapeshellarg( $url )
509 exec( "php $params &>/dev/null &" );
512 if ( $wgUseMemCached ) {
513 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
518 /* private */ function mergeChangesInto( &$text ){
519 $oldDate = $this->edittime
;
520 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
521 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
522 $obj = wfFetchObject($res);
524 $yourtext = $obj->cur_text
;
525 $ns = $this->mTitle
->getNamespace();
526 $title = wfStrencode( $this->mTitle
->getDBkey() );
527 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
528 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
529 $obj = wfFetchObject($res);
530 if(wfMerge($obj->old_text
, $text, $yourtext, $result)){