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 global $wgIsMySQL, $wgIsPg;
66 # These fields need to be checked for encoding.
67 # Also remove trailing whitespace, but don't remove _initial_
68 # whitespace from the text boxes. This may be significant formatting.
69 $this->textbox1
= rtrim( $request->getText( "wpTextbox1" ) );
70 $this->textbox2
= rtrim( $request->getText( "wpTextbox2" ) );
71 $this->summary
= trim( $request->getText( "wpSummary" ) );
73 $this->edittime
= $request->getVal( 'wpEdittime' );
75 if( !preg_match( '/^\d{14}$/', $this->edittime
)) $this->edittime
= "";
77 if ( !preg_match( '/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/',
81 $this->preview
= $request->getCheck( 'wpPreview' );
82 $this->save
= $request->wasPosted() && !$this->preview
;
83 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
84 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
86 $this->oldid
= $request->getInt( 'oldid' );
88 # Section edit can come from either the form or a link
89 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
92 # Since there is only one text field on the edit form,
93 # pressing <enter> will cause the form to be submitted, but
94 # the submit button value won't appear in the query, so we
95 # Fake it here before going back to edit(). This is kind of
96 # ugly, but it helps some old URLs to still work.
100 if( !$this->preview
) $this->save
= true;
105 # The edit form is self-submitting, so that when things like
106 # preview and edit conflicts occur, we get the same form back
107 # with the extra stuff added. Only when the final submission
108 # is made and all is well do we actually save and redirect to
109 # the newly-edited page.
111 function editForm( $formtype )
113 global $wgOut, $wgUser;
114 global $wgLang, $wgParser, $wgTitle;
115 global $wgAllowAnonymousMinor;
116 global $wgWhitelistEdit;
119 $sk = $wgUser->getSkin();
121 // css / js subpages of user pages get a special treatment
122 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
124 if(!$this->mTitle
->getArticleID()) { # new article
125 $wgOut->addWikiText(wfmsg("newarticletext"));
128 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
129 $wgOut->addWikiText(wfmsg("talkpagetext"));
132 # Attempt submission here. This will check for edit conflicts,
133 # and redundantly check for locked database, blocked IPs, etc.
134 # that edit() already checked just in case someone tries to sneak
135 # in the back door with a hand-edited submission URL.
137 if ( "save" == $formtype ) {
139 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
) ) {
140 if ( $wgUser->isSysop() ) {
144 $wgOut->redirect( $this->mTitle
->getFullURL() );
148 if ( $wgUser->isBlocked() ) {
149 $this->blockedIPpage();
152 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
153 $this->userNotLoggedInPage();
156 if ( wfReadOnly() ) {
157 $wgOut->readOnlyPage();
161 # If article is new, insert it.
162 $aid = $this->mTitle
->getArticleID();
164 # Don't save a new article if it's blank.
165 if ( ( "" == $this->textbox1
) ||
166 ( wfMsg( "newarticletext" ) == $this->textbox1
) ) {
167 $wgOut->redirect( $this->mTitle
->getFullURL() );
170 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
174 # Article exists. Check for edit conflict.
176 $this->mArticle
->clear(); # Force reload of dates, etc.
178 if( ( $this->section
!= "new" ) &&
179 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
182 $userid = $wgUser->getID();
184 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
185 $this->section
, $this->textbox1
, $this->summary
);
186 # Suppress edit conflict with self
188 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
191 # switch from section editing to normal editing in edit conflict
194 if( $this->mergeChangesInto( $text ) ){
195 // Successful merge! Maybe we should tell the user the good news?
199 $this->textbox1
= $text;
203 if ( ! $isConflict ) {
206 if( $this->section
!= '' ) {
207 # Try to get a section anchor from the section source, redirect to edited section if header found
208 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
209 # for duplicate heading checking and maybe parsing
210 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
211 # we can't deal with anchors, includes, html etc in the header for now,
212 # headline would need to be parsed to improve this
213 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
214 if($hasmatch and strlen($matches[2]) > 0) {
215 global $wgInputEncoding;
216 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
218 $headline = preg_replace( "/<.*?" . ">/","",$headline );
219 $headline = trim( $headline );
220 $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
221 $replacearray = array(
225 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
229 # update the article here
230 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
236 # First time through: get contents, set time for conflict
239 if ( "initial" == $formtype ) {
240 $this->edittime
= $this->mArticle
->getTimestamp();
241 $this->textbox1
= $this->mArticle
->getContent( true );
245 $wgOut->setRobotpolicy( "noindex,nofollow" );
247 # Enabled article-related sidebar, toplinks, etc.
248 $wgOut->setArticleRelated( true );
251 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
252 $wgOut->setPageTitle( $s );
253 $wgOut->addHTML( wfMsg( "explainconflict" ) );
255 $this->textbox2
= $this->textbox1
;
256 $this->textbox1
= $this->mArticle
->getContent( true );
257 $this->edittime
= $this->mArticle
->getTimestamp();
259 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
261 if( $this->section
!= "" ) {
262 if( $this->section
== "new" ) {
263 $s.=wfMsg("commentedit");
265 $s.=wfMsg("sectionedit");
267 if(!$this->preview
) {
268 $sectitle=preg_match("/^=+(.*?)=+/mi",
271 if( !empty( $matches[1] ) ) {
272 $this->summary
= "/* ". trim($matches[1])." */ ";
276 $wgOut->setPageTitle( $s );
277 if ( $this->oldid
) {
278 $this->mArticle
->setOldSubtitle();
279 $wgOut->addHTML( wfMsg( "editingold" ) );
284 $wgOut->addHTML( "<strong>" .
285 wfMsg( "readonlywarning" ) .
287 } else if ( $isCssJsSubpage and "preview" != $formtype) {
288 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
290 if( $this->mTitle
->isProtected() ) {
291 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
292 "</strong><br />\n" );
295 $kblength = (int)(strlen( $this->textbox1
) / 1024);
296 if( $kblength > 29 ) {
297 $wgOut->addHTML( "<strong>" .
298 wfMsg( "longpagewarning", $kblength )
302 $rows = $wgUser->getOption( "rows" );
303 $cols = $wgUser->getOption( "cols" );
305 $ew = $wgUser->getOption( "editwidth" );
306 if ( $ew ) $ew = " style=\"width:100%\"";
309 $q = "action=submit";
310 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
311 $action = $this->mTitle
->escapeLocalURL( $q );
313 $summary = wfMsg( "summary" );
314 $subject = wfMsg("subject");
315 $minor = wfMsg( "minoredit" );
316 $watchthis = wfMsg ("watchthis");
317 $save = wfMsg( "savearticle" );
318 $prev = wfMsg( "showpreview" );
320 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
322 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
323 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
324 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
325 htmlspecialchars( wfMsg( 'newwindow' ) );
326 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
327 wfMsg( "copyrightpage" ) ) );
329 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
330 # prepare toolbar for edit buttons
331 $toolbar = $sk->getEditToolbar();
336 // activate checkboxes if user wants them to be always active
337 if( !$this->preview
) {
338 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
339 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
341 // activate checkbox also if user is already watching the page,
342 // require wpWatchthis to be unset so that second condition is not
343 // checked unnecessarily
344 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
349 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
351 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
352 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
353 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
358 if ( 0 != $wgUser->getID() ) {
359 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
360 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
361 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
364 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
366 if ( "preview" == $formtype) {
367 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
368 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
370 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
374 $parserOptions = ParserOptions
::newFromUser( $wgUser );
375 $parserOptions->setUseCategoryMagic( false );
376 $parserOptions->setEditSection( false );
377 $parserOptions->setEditSectionOnRightClick( false );
378 # don't parse user css/js, show message about preview
379 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
380 if ( $isCssJsSubpage ) {
381 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
382 $previewtext = wfMsg('usercsspreview');
383 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
384 $previewtext = wfMsg('userjspreview');
386 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
387 $wgOut->addHTML( $parserOutput->mText
);
389 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
390 $wgTitle, $parserOptions );
391 $previewHTML = $parserOutput->mText
;
393 if($wgUser->getOption("previewontop")) {
394 $wgOut->addHTML($previewhead);
395 $wgOut->addHTML($previewHTML);
397 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
398 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
399 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
403 # if this is a comment, show a subject line at the top, which is also the edit summary.
404 # Otherwise, show a summary field at the bottom
405 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
406 if( $this->section
== "new" ) {
407 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
410 $commentsubject = "";
411 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
414 if( !$this->preview
) {
415 # Don't select the edit box on preview; this interferes with seeing what's going on.
416 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
420 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
421 enctype=\"application/x-www-form-urlencoded\">
423 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
424 cols='{$cols}'{$ew}>" .
425 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
430 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
431 " title=\"".wfMsg('tooltip-save')."\"/>
432 <input tabindex='6' id='wpSave' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
433 " title=\"".wfMsg('tooltip-preview')."\"/>
434 <em>{$cancel}</em> | <em>{$edithelp}</em>
435 <br /><div id=\"editpage-copywarn\">{$copywarn}</div>
436 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
437 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
440 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
441 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
442 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
444 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
445 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
446 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
450 $wgOut->addHTML( "</form>\n" );
451 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
452 $wgOut->addHTML($previewhead);
453 $wgOut->addHTML($previewHTML);
458 function blockedIPpage()
460 global $wgOut, $wgUser, $wgLang, $wgIP;
462 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
463 $wgOut->setRobotpolicy( "noindex,nofollow" );
464 $wgOut->setArticleRelated( false );
466 $id = $wgUser->blockedBy();
467 $reason = $wgUser->blockedFor();
470 $name = User
::whoIs( $id );
471 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
472 ":{$name}|{$name}]]";
474 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
475 $wgOut->returnToMain( false );
480 function userNotLoggedInPage()
482 global $wgOut, $wgUser, $wgLang;
484 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
485 $wgOut->setRobotpolicy( "noindex,nofollow" );
486 $wgOut->setArticleRelated( false );
488 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
489 $wgOut->returnToMain( false );
494 global $wgOut, $wgSpamRegex;
495 $wgOut->setPageTitle( wfMsg( "spamprotectiontitle" ) );
496 $wgOut->setRobotpolicy( "noindex,nofollow" );
497 $wgOut->setArticleRelated( false );
499 $wgOut->addWikiText( wfMsg( "spamprotectiontext" ) );
500 $wgOut->addWikiText( "<pre>".$wgSpamRegex."</pre>" );
501 $wgOut->returnToMain( false );
504 # Forks processes to scan the originating IP for an open proxy server
505 # MemCached can be used to skip IPs that have already been scanned
506 function proxyCheck()
508 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
509 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
511 if ( !$wgBlockOpenProxies ) {
517 if ( $wgUseMemCached ) {
518 $mcKey = "$wgDBname:proxy:ip:$wgIP";
519 $mcValue = $wgMemc->get( $mcKey );
527 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
528 $iphash = md5( $wgIP . $wgProxyKey );
529 $url = $title->getFullURL( "ip=$iphash" );
531 foreach ( $wgProxyPorts as $port ) {
532 $params = implode( " ", array(
533 escapeshellarg( $wgProxyScriptPath ),
534 escapeshellarg( $wgIP ),
535 escapeshellarg( $port ),
536 escapeshellarg( $url )
538 exec( "php $params &>/dev/null &" );
541 if ( $wgUseMemCached ) {
542 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
547 /* private */ function mergeChangesInto( &$text ){
549 $oldDate = $this->edittime
;
550 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
551 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
552 $obj = wfFetchObject($res);
554 $yourtext = $obj->cur_text
;
555 $ns = $this->mTitle
->getNamespace();
556 $title = wfStrencode( $this->mTitle
->getDBkey() );
557 $oldtable=$wgIsPg?
'"old"':'old';
558 $res = wfQuery("SELECT old_text,old_flags FROM $oldtable WHERE old_namespace = $ns AND ".
559 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
560 $obj = wfFetchObject($res);
561 $oldText = Article
::getRevisionText( $obj );
563 if(wfMerge($oldText, $text, $yourtext, $result)){