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 ) );
215 $sectionanchor = str_replace('%','.',$sectionanchor);
219 # update the article here
220 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
226 # First time through: get contents, set time for conflict
229 if ( "initial" == $formtype ) {
230 $this->edittime
= $this->mArticle
->getTimestamp();
231 $this->textbox1
= $this->mArticle
->getContent( true );
235 $wgOut->setRobotpolicy( "noindex,nofollow" );
237 # Enabled article-related sidebar, toplinks, etc.
238 $wgOut->setArticleRelated( true );
241 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
242 $wgOut->setPageTitle( $s );
243 $wgOut->addHTML( wfMsg( "explainconflict" ) );
245 $this->textbox2
= $this->textbox1
;
246 $this->textbox1
= $this->mArticle
->getContent( true );
247 $this->edittime
= $this->mArticle
->getTimestamp();
249 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
251 if( $this->section
!= "" ) {
252 if( $this->section
== "new" ) {
253 $s.=wfMsg("commentedit");
255 $s.=wfMsg("sectionedit");
257 if(!$this->preview
) {
258 $sectitle=preg_match("/^=+(.*?)=+/mi",
261 if( !empty( $matches[1] ) ) {
262 $this->summary
= "/* ". trim($matches[1])." */ ";
266 $wgOut->setPageTitle( $s );
267 if ( $this->oldid
) {
268 $this->mArticle
->setOldSubtitle();
269 $wgOut->addHTML( wfMsg( "editingold" ) );
274 $wgOut->addHTML( "<strong>" .
275 wfMsg( "readonlywarning" ) .
277 } else if ( $isCssJsSubpage and "preview" != $formtype) {
278 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
280 if( $this->mTitle
->isProtected() ) {
281 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
282 "</strong><br />\n" );
285 $kblength = (int)(strlen( $this->textbox1
) / 1024);
286 if( $kblength > 29 ) {
287 $wgOut->addHTML( "<strong>" .
288 wfMsg( "longpagewarning", $kblength )
292 $rows = $wgUser->getOption( "rows" );
293 $cols = $wgUser->getOption( "cols" );
295 $ew = $wgUser->getOption( "editwidth" );
296 if ( $ew ) $ew = " style=\"width:100%\"";
299 $q = "action=submit";
300 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
301 $action = $this->mTitle
->escapeLocalURL( $q );
303 $summary = wfMsg( "summary" );
304 $subject = wfMsg("subject");
305 $minor = wfMsg( "minoredit" );
306 $watchthis = wfMsg ("watchthis");
307 $save = wfMsg( "savearticle" );
308 $prev = wfMsg( "showpreview" );
310 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
312 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
313 $edithelp = '<a onclick="window.open('.
314 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10,scrollbars=yes'".'); return false;" href="'.$edithelpurl.'">'.
315 wfMsg( "edithelp" ).'</a>';
316 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
317 wfMsg( "copyrightpage" ) ) );
319 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
320 # prepare toolbar for edit buttons
321 $toolbar = $sk->getEditToolbar();
326 // activate checkboxes if user wants them to be always active
327 if( !$this->preview
) {
328 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
329 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
331 // activate checkbox also if user is already watching the page,
332 // require wpWatchthis to be unset so that second condition is not
333 // checked unnecessarily
334 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
339 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
341 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
342 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
343 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
348 if ( 0 != $wgUser->getID() ) {
349 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
350 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
351 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
354 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
356 if ( "preview" == $formtype) {
357 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
358 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
360 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
364 $parserOptions = ParserOptions
::newFromUser( $wgUser );
365 $parserOptions->setUseCategoryMagic( false );
366 $parserOptions->setEditSection( false );
367 $parserOptions->setEditSectionOnRightClick( false );
368 # don't parse user css/js, show message about preview
369 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
370 if ( $isCssJsSubpage ) {
371 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
372 $previewtext = wfMsg('usercsspreview');
373 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
374 $previewtext = wfMsg('userjspreview');
376 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
377 $wgOut->addHTML( $parserOutput->mText
);
379 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
380 $wgTitle, $parserOptions );
381 $previewHTML = $parserOutput->mText
;
383 if($wgUser->getOption("previewontop")) {
384 $wgOut->addHTML($previewhead);
385 $wgOut->addHTML($previewHTML);
387 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
388 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
392 # if this is a comment, show a subject line at the top, which is also the edit summary.
393 # Otherwise, show a summary field at the bottom
394 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
395 if( $this->section
== "new" ) {
396 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
399 $commentsubject = "";
400 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
403 if( !$this->preview
) {
404 # Don't select the edit box on preview; this interferes with seeing what's going on.
405 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
409 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
410 enctype=\"application/x-www-form-urlencoded\">
412 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
413 cols='{$cols}'{$ew}>" .
414 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
419 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
420 " title=\"".wfMsg('tooltip-save')."\"/>
421 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
422 " title=\"".wfMsg('tooltip-preview')."\"/>
423 <em>{$cancel}</em> | <em>{$edithelp}</em>
424 <br /><br />{$copywarn}
425 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
426 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
429 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
430 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
431 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
433 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
434 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
435 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
439 $wgOut->addHTML( "</form>\n" );
440 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
441 $wgOut->addHTML($previewhead);
442 $wgOut->addHTML($previewHTML);
447 function blockedIPpage()
449 global $wgOut, $wgUser, $wgLang, $wgIP;
451 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
452 $wgOut->setRobotpolicy( "noindex,nofollow" );
453 $wgOut->setArticleRelated( false );
455 $id = $wgUser->blockedBy();
456 $reason = $wgUser->blockedFor();
459 $name = User
::whoIs( $id );
460 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
461 ":{$name}|{$name}]]";
463 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
464 $wgOut->returnToMain( false );
469 function userNotLoggedInPage()
471 global $wgOut, $wgUser, $wgLang;
473 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
474 $wgOut->setRobotpolicy( "noindex,nofollow" );
475 $wgOut->setArticleRelated( false );
477 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
478 $wgOut->returnToMain( false );
483 global $wgOut, $wgSpamRegex;
484 $wgOut->setPageTitle( wfMsg( "spamprotectiontitle" ) );
485 $wgOut->setRobotpolicy( "noindex,nofollow" );
486 $wgOut->setArticleRelated( false );
488 $wgOut->addWikiText( wfMsg( "spamprotectiontext" ) );
489 $wgOut->addWikiText( "<pre>".$wgSpamRegex."</pre>" );
490 $wgOut->returnToMain( false );
493 # Forks processes to scan the originating IP for an open proxy server
494 # MemCached can be used to skip IPs that have already been scanned
495 function proxyCheck()
497 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
498 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
500 if ( !$wgBlockOpenProxies ) {
506 if ( $wgUseMemCached ) {
507 $mcKey = "$wgDBname:proxy:ip:$wgIP";
508 $mcValue = $wgMemc->get( $mcKey );
516 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
517 $iphash = md5( $wgIP . $wgProxyKey );
518 $url = $title->getFullURL( "ip=$iphash" );
520 foreach ( $wgProxyPorts as $port ) {
521 $params = implode( " ", array(
522 escapeshellarg( $wgProxyScriptPath ),
523 escapeshellarg( $wgIP ),
524 escapeshellarg( $port ),
525 escapeshellarg( $url )
527 exec( "php $params &>/dev/null &" );
530 if ( $wgUseMemCached ) {
531 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
536 /* private */ function mergeChangesInto( &$text ){
537 $oldDate = $this->edittime
;
538 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
539 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
540 $obj = wfFetchObject($res);
542 $yourtext = $obj->cur_text
;
543 $ns = $this->mTitle
->getNamespace();
544 $title = wfStrencode( $this->mTitle
->getDBkey() );
545 $res = wfQuery("SELECT old_text,old_flags FROM old WHERE old_namespace = $ns AND ".
546 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
547 $obj = wfFetchObject($res);
548 $oldText = Article
::getRevisionText( $obj );
550 if(wfMerge($oldText, $text, $yourtext, $result)){