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, $this->textbox1
) ) {
134 if ( $wgUser->isSysop() ) {
138 $wgOut->redirect( $this->mTitle
->getFullURL() );
142 if ( $wgUser->isBlocked() ) {
143 $this->blockedIPpage();
146 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
147 $this->userNotLoggedInPage();
150 if ( wfReadOnly() ) {
151 $wgOut->readOnlyPage();
155 # If article is new, insert it.
156 $aid = $this->mTitle
->getArticleID();
158 # Don't save a new article if it's blank.
159 if ( ( "" == $this->textbox1
) ||
160 ( wfMsg( "newarticletext" ) == $this->textbox1
) ) {
161 $wgOut->redirect( $this->mTitle
->getFullURL() );
164 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
168 # Article exists. Check for edit conflict.
170 $this->mArticle
->clear(); # Force reload of dates, etc.
172 if( ( $this->section
!= "new" ) &&
173 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
176 $userid = $wgUser->getID();
178 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
179 $this->section
, $this->textbox1
, $this->summary
);
180 # Suppress edit conflict with self
182 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
185 # switch from section editing to normal editing in edit conflict
188 if( $this->mergeChangesInto( $text ) ){
189 // Successful merge! Maybe we should tell the user the good news?
193 $this->textbox1
= $text;
197 if ( ! $isConflict ) {
200 if( $this->section
!= '' ) {
201 # Try to get a section anchor from the section source, redirect to edited section if header found
202 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
203 # for duplicate heading checking and maybe parsing
204 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
205 # we can't deal with anchors, includes, html etc in the header for now,
206 # headline would need to be parsed to improve this
207 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
208 if($hasmatch and strlen($matches[2]) > 0) {
209 global $wgInputEncoding;
210 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
212 $headline = preg_replace( "/<.*?" . ">/","",$headline );
213 $headline = trim( $headline );
214 $sectionanchor = '#'.preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', urlencode( $headline ) );
218 # update the article here
219 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
225 # First time through: get contents, set time for conflict
228 if ( "initial" == $formtype ) {
229 $this->edittime
= $this->mArticle
->getTimestamp();
230 $this->textbox1
= $this->mArticle
->getContent( true );
234 $wgOut->setRobotpolicy( "noindex,nofollow" );
236 # Enabled article-related sidebar, toplinks, etc.
237 $wgOut->setArticleRelated( true );
240 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
241 $wgOut->setPageTitle( $s );
242 $wgOut->addHTML( wfMsg( "explainconflict" ) );
244 $this->textbox2
= $this->textbox1
;
245 $this->textbox1
= $this->mArticle
->getContent( true );
246 $this->edittime
= $this->mArticle
->getTimestamp();
248 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
250 if( $this->section
!= "" ) {
251 if( $this->section
== "new" ) {
252 $s.=wfMsg("commentedit");
254 $s.=wfMsg("sectionedit");
256 if(!$this->preview
) {
257 $sectitle=preg_match("/^=+(.*?)=+/mi",
260 if( !empty( $matches[1] ) ) {
261 $this->summary
= "/* ". trim($matches[1])." */ ";
265 $wgOut->setPageTitle( $s );
266 if ( $this->oldid
) {
267 $this->mArticle
->setOldSubtitle();
268 $wgOut->addHTML( wfMsg( "editingold" ) );
273 $wgOut->addHTML( "<strong>" .
274 wfMsg( "readonlywarning" ) .
276 } else if ( $isCssJsSubpage and "preview" != $formtype) {
277 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
279 if( $this->mTitle
->isProtected() ) {
280 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
281 "</strong><br />\n" );
284 $kblength = (int)(strlen( $this->textbox1
) / 1024);
285 if( $kblength > 29 ) {
286 $wgOut->addHTML( "<strong>" .
287 wfMsg( "longpagewarning", $kblength )
291 $rows = $wgUser->getOption( "rows" );
292 $cols = $wgUser->getOption( "cols" );
294 $ew = $wgUser->getOption( "editwidth" );
295 if ( $ew ) $ew = " style=\"width:100%\"";
298 $q = "action=submit";
299 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
300 $action = $this->mTitle
->escapeLocalURL( $q );
302 $summary = wfMsg( "summary" );
303 $subject = wfMsg("subject");
304 $minor = wfMsg( "minoredit" );
305 $watchthis = wfMsg ("watchthis");
306 $save = wfMsg( "savearticle" );
307 $prev = wfMsg( "showpreview" );
309 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
311 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
312 $edithelp = '<a onclick="window.open('.
313 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10,scrollbars=yes'".'); return false;" href="'.$edithelpurl.'">'.
314 wfMsg( "edithelp" ).'</a>';
315 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
316 wfMsg( "copyrightpage" ) ) );
318 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
319 # prepare toolbar for edit buttons
320 $toolbar = $sk->getEditToolbar();
325 // activate checkboxes if user wants them to be always active
326 if( !$this->preview
) {
327 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
328 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
330 // activate checkbox also if user is already watching the page,
331 // require wpWatchthis to be unset so that second condition is not
332 // checked unnecessarily
333 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
338 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
340 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
341 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
342 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
347 if ( 0 != $wgUser->getID() ) {
348 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
349 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
350 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
353 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
355 if ( "preview" == $formtype) {
356 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
357 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
359 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
363 $parserOptions = ParserOptions
::newFromUser( $wgUser );
364 $parserOptions->setUseCategoryMagic( false );
365 $parserOptions->setEditSection( false );
366 $parserOptions->setEditSectionOnRightClick( false );
367 # don't parse user css/js, show message about preview
368 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
369 if ( $isCssJsSubpage ) {
370 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
371 $previewtext = wfMsg('usercsspreview');
372 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
373 $previewtext = wfMsg('userjspreview');
375 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
376 $wgOut->addHTML( $parserOutput->mText
);
378 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
379 $wgTitle, $parserOptions );
380 $previewHTML = $parserOutput->mText
;
382 if($wgUser->getOption("previewontop")) {
383 $wgOut->addHTML($previewhead);
384 $wgOut->addHTML($previewHTML);
386 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
387 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
391 # if this is a comment, show a subject line at the top, which is also the edit summary.
392 # Otherwise, show a summary field at the bottom
393 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
394 if( $this->section
== "new" ) {
395 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
398 $commentsubject = "";
399 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
402 if( !$this->preview
) {
403 # Don't select the edit box on preview; this interferes with seeing what's going on.
404 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
408 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
409 enctype=\"application/x-www-form-urlencoded\">
411 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
412 cols='{$cols}'{$ew}>" .
413 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
418 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
419 " title=\"".wfMsg('tooltip-save')."\"/>
420 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
421 " title=\"".wfMsg('tooltip-preview')."\"/>
422 <em>{$cancel}</em> | <em>{$edithelp}</em>
423 <br /><br />{$copywarn}
424 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
425 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
428 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
429 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
430 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
432 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
433 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
434 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
438 $wgOut->addHTML( "</form>\n" );
439 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
440 $wgOut->addHTML($previewhead);
441 $wgOut->addHTML($previewHTML);
446 function blockedIPpage()
448 global $wgOut, $wgUser, $wgLang, $wgIP;
450 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
451 $wgOut->setRobotpolicy( "noindex,nofollow" );
452 $wgOut->setArticleRelated( false );
454 $id = $wgUser->blockedBy();
455 $reason = $wgUser->blockedFor();
458 $name = User
::whoIs( $id );
459 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
460 ":{$name}|{$name}]]";
462 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
463 $wgOut->returnToMain( false );
468 function userNotLoggedInPage()
470 global $wgOut, $wgUser, $wgLang;
472 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
473 $wgOut->setRobotpolicy( "noindex,nofollow" );
474 $wgOut->setArticleRelated( false );
476 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
477 $wgOut->returnToMain( false );
482 global $wgOut, $wgSpamRegex;
483 $wgOut->setPageTitle( wfMsg( "spamprotectiontitle" ) );
484 $wgOut->setRobotpolicy( "noindex,nofollow" );
485 $wgOut->setArticleRelated( false );
487 $wgOut->addWikiText( wfMsg( "spamprotectiontext" ) );
488 $wgOut->addWikiText( "<pre>".$wgSpamRegex."</pre>" );
489 $wgOut->returnToMain( false );
492 # Forks processes to scan the originating IP for an open proxy server
493 # MemCached can be used to skip IPs that have already been scanned
494 function proxyCheck()
496 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
497 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
499 if ( !$wgBlockOpenProxies ) {
505 if ( $wgUseMemCached ) {
506 $mcKey = "$wgDBname:proxy:ip:$wgIP";
507 $mcValue = $wgMemc->get( $mcKey );
515 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
516 $iphash = md5( $wgIP . $wgProxyKey );
517 $url = $title->getFullURL( "ip=$iphash" );
519 foreach ( $wgProxyPorts as $port ) {
520 $params = implode( " ", array(
521 escapeshellarg( $wgProxyScriptPath ),
522 escapeshellarg( $wgIP ),
523 escapeshellarg( $port ),
524 escapeshellarg( $url )
526 exec( "php $params &>/dev/null &" );
529 if ( $wgUseMemCached ) {
530 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
535 /* private */ function mergeChangesInto( &$text ){
536 $oldDate = $this->edittime
;
537 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
538 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
539 $obj = wfFetchObject($res);
541 $yourtext = $obj->cur_text
;
542 $ns = $this->mTitle
->getNamespace();
543 $title = wfStrencode( $this->mTitle
->getDBkey() );
544 $res = wfQuery("SELECT old_text,old_flags FROM old WHERE old_namespace = $ns AND ".
545 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
546 $obj = wfFetchObject($res);
547 $oldText = Article
::getRevisionText( $obj );
549 if(wfMerge($oldText, $text, $yourtext, $result)){