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 );
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() );
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;
112 $sk = $wgUser->getSkin();
115 if(!$this->mTitle
->getArticleID()) { # new article
116 $wgOut->addWikiText(wfmsg("newarticletext"));
119 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
120 $wgOut->addWikiText(wfmsg("talkpagetext"));
123 # Attempt submission here. This will check for edit conflicts,
124 # and redundantly check for locked database, blocked IPs, etc.
125 # that edit() already checked just in case someone tries to sneak
126 # in the back door with a hand-edited submission URL.
128 if ( "save" == $formtype ) {
129 if ( $wgUser->isBlocked() ) {
130 $this->blockedIPpage();
133 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
134 $this->userNotLoggedInPage();
137 if ( wfReadOnly() ) {
138 $wgOut->readOnlyPage();
142 # If article is new, insert it.
143 $aid = $this->mTitle
->getArticleID();
145 # Don't save a new article if it's blank.
146 if ( ( "" == $this->textbox1
) ||
147 ( wfMsg( "newarticletext" ) == $this->textbox1
) ) {
148 $wgOut->redirect( $this->mTitle
->getFullURL() );
151 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
155 # Article exists. Check for edit conflict.
157 $this->mArticle
->clear(); # Force reload of dates, etc.
159 if( ( $this->section
!= "new" ) &&
160 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
163 $userid = $wgUser->getID();
165 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
166 $this->section
, $this->textbox1
, $this->summary
);
167 # Suppress edit conflict with self
169 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
172 # switch from section editing to normal editing in edit conflict
175 if( $this->mergeChangesInto( $text ) ){
176 // Successful merge! Maybe we should tell the user the good news?
180 $this->textbox1
= $text;
184 if ( ! $isConflict ) {
185 # All's well: update the article here
186 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
))
192 # First time through: get contents, set time for conflict
195 if ( "initial" == $formtype ) {
196 $this->edittime
= $this->mArticle
->getTimestamp();
197 $this->textbox1
= $this->mArticle
->getContent(true);
201 $wgOut->setRobotpolicy( "noindex,nofollow" );
203 # Enabled article-related sidebar, toplinks, etc.
204 $wgOut->setArticleRelated( true );
207 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
208 $wgOut->setPageTitle( $s );
209 $wgOut->addHTML( wfMsg( "explainconflict" ) );
211 $this->textbox2
= $this->textbox1
;
212 $this->textbox1
= $this->mArticle
->getContent(true);
213 $this->edittime
= $this->mArticle
->getTimestamp();
215 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
217 if( $this->section
!= "" ) {
218 if( $this->section
== "new" ) {
219 $s.=wfMsg("commentedit");
221 $s.=wfMsg("sectionedit");
223 if(!$this->preview
) {
224 $sectitle=preg_match("/^=+(.*?)=+/mi",
227 if( !empty( $matches[1] ) ) {
228 $this->summary
= "/* ". trim($matches[1])." */ ";
232 $wgOut->setPageTitle( $s );
233 if ( $this->oldid
) {
234 $this->mArticle
->setOldSubtitle();
235 $wgOut->addHTML( wfMsg( "editingold" ) );
240 $wgOut->addHTML( "<strong>" .
241 wfMsg( "readonlywarning" ) .
244 if( $this->mTitle
->isProtected() ) {
245 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
246 "</strong><br />\n" );
249 $kblength = (int)(strlen( $this->textbox1
) / 1024);
250 if( $kblength > 29 ) {
251 $wgOut->addHTML( "<strong>" .
252 wfMsg( "longpagewarning", $kblength )
256 $rows = $wgUser->getOption( "rows" );
257 $cols = $wgUser->getOption( "cols" );
259 $ew = $wgUser->getOption( "editwidth" );
260 if ( $ew ) $ew = " style=\"width:100%\"";
263 $q = "action=submit";
264 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
265 $action = $this->mTitle
->escapeLocalURL( $q );
267 $summary = wfMsg( "summary" );
268 $subject = wfMsg("subject");
269 $minor = wfMsg( "minoredit" );
270 $watchthis = wfMsg ("watchthis");
271 $save = wfMsg( "savearticle" );
272 $prev = wfMsg( "showpreview" );
274 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
276 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
277 wfMsg( "edithelp" ) );
278 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
279 wfMsg( "copyrightpage" ) ) );
281 if($wgUser->getOption("showtoolbar")) {
282 // prepare toolbar for edit buttons
283 $toolbar=$sk->getEditToolbar();
286 // activate checkboxes if user wants them to be always active
287 if( !$this->preview
) {
288 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
289 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
291 // activate checkbox also if user is already watching the page,
292 // require wpWatchthis to be unset so that second condition is not
293 // checked unnecessarily
294 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
299 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
301 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
302 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
303 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
308 if ( 0 != $wgUser->getID() ) {
309 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
310 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
311 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
314 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
316 if ( "preview" == $formtype) {
317 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
318 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
320 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
323 $previewtext = wfUnescapeHTML( $this->textbox1
);
325 $parserOptions = ParserOptions
::newFromUser( $wgUser );
326 $parserOptions->setUseCategoryMagic( false );
327 $parserOptions->setEditSection( false );
328 $parserOptions->setEditSectionOnRightClick( false );
329 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
330 $wgTitle, $parserOptions );
331 $previewHTML = $parserOutput->mText
;
333 if($wgUser->getOption("previewontop")) {
334 $wgOut->addHTML($previewhead);
335 $wgOut->addHTML($previewHTML);
337 $wgOut->addHTML( "<br clear=\"all\" />\n" );
340 # if this is a comment, show a subject line at the top, which is also the edit summary.
341 # Otherwise, show a summary field at the bottom
342 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
343 if( $this->section
== "new" ) {
344 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
347 $commentsubject = "";
348 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
351 if( !$this->preview
) {
352 # Don't select the edit box on preview; this interferes with seeing what's going on.
353 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
357 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
358 enctype=\"application/x-www-form-urlencoded\">
360 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
361 cols='{$cols}'{$ew}>" .
362 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
367 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
368 " title=\"".wfMsg('tooltip-save')."\"/>
369 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
370 " title=\"".wfMsg('tooltip-preview')."\"/>
371 <em>{$cancel}</em> | <em>{$edithelp}</em>
372 <br /><br />{$copywarn}
373 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
374 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
377 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
378 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
379 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
381 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
382 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
383 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
387 $wgOut->addHTML( "</form>\n" );
388 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
389 $wgOut->addHTML($previewhead);
390 $wgOut->addHTML($previewHTML);
395 function blockedIPpage()
397 global $wgOut, $wgUser, $wgLang, $wgIP;
399 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
400 $wgOut->setRobotpolicy( "noindex,nofollow" );
401 $wgOut->setArticleRelated( false );
403 $id = $wgUser->blockedBy();
404 $reason = $wgUser->blockedFor();
407 $name = User
::whoIs( $id );
408 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
409 ":{$name}|{$name}]]";
411 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
412 $wgOut->returnToMain( false );
417 function userNotLoggedInPage()
419 global $wgOut, $wgUser, $wgLang;
421 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
422 $wgOut->setRobotpolicy( "noindex,nofollow" );
423 $wgOut->setArticleRelated( false );
425 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
426 $wgOut->returnToMain( false );
429 # Forks processes to scan the originating IP for an open proxy server
430 # MemCached can be used to skip IPs that have already been scanned
431 function proxyCheck()
433 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
434 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
436 if ( !$wgBlockOpenProxies ) {
442 if ( $wgUseMemCached ) {
443 $mcKey = "$wgDBname:proxy:ip:$wgIP";
444 $mcValue = $wgMemc->get( $mcKey );
452 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
453 $iphash = md5( $wgIP . $wgProxyKey );
454 $url = $title->getFullURL( "ip=$iphash" );
456 foreach ( $wgProxyPorts as $port ) {
457 $params = implode( " ", array(
458 escapeshellarg( $wgProxyScriptPath ),
459 escapeshellarg( $wgIP ),
460 escapeshellarg( $port ),
461 escapeshellarg( $url )
463 exec( "php $params &>/dev/null &" );
466 if ( $wgUseMemCached ) {
467 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
472 /* private */ function mergeChangesInto( &$text ){
473 $oldDate = $this->edittime
;
474 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
475 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
476 $obj = wfFetchObject($res);
478 $yourtext = $obj->cur_text
;
479 $ns = $this->mTitle
->getNamespace();
480 $title = wfStrencode( $this->mTitle
->getDBkey() );
481 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
482 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
483 $obj = wfFetchObject($res);
484 if(wfMerge($obj->old_text
, $text, $yourtext, $result)){