999c65da4b01773316ba6dc4ff541a172610f12b
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() );
55 if( !$wgRequest->wasPosted() ) $this->save
= false;
57 $this->editForm( "save" );
58 } else if ( $this->preview
) {
59 $this->editForm( "preview" );
60 } else { # First time through
61 $this->editForm( "initial" );
65 function importFormData( &$request ) {
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' );
74 if( !preg_match( '/^\d{14}$/', $this->edittime
) ) $this->edittime
= "";
76 $this->save
= $request->getCheck( 'wpSave' );
77 $this->preview
= $request->getCheck( 'wpPreview' );
78 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
79 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
81 $this->oldid
= $request->getInt( 'oldid' );
83 # Section edit can come from either the form or a link
84 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
87 # Since there is only one text field on the edit form,
88 # pressing <enter> will cause the form to be submitted, but
89 # the submit button value won't appear in the query, so we
90 # Fake it here before going back to edit(). This is kind of
91 # ugly, but it helps some old URLs to still work.
95 if( !$this->preview
) $this->save
= true;
100 # The edit form is self-submitting, so that when things like
101 # preview and edit conflicts occur, we get the same form back
102 # with the extra stuff added. Only when the final submission
103 # is made and all is well do we actually save and redirect to
104 # the newly-edited page.
106 function editForm( $formtype )
108 global $wgOut, $wgUser;
109 global $wgLang, $wgParser, $wgTitle;
110 global $wgAllowAnonymousMinor;
111 global $wgWhitelistEdit;
113 $sk = $wgUser->getSkin();
116 if(!$this->mTitle
->getArticleID()) { # new article
117 $wgOut->addWikiText(wfmsg("newarticletext"));
120 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
121 $wgOut->addWikiText(wfmsg("talkpagetext"));
124 # Attempt submission here. This will check for edit conflicts,
125 # and redundantly check for locked database, blocked IPs, etc.
126 # that edit() already checked just in case someone tries to sneak
127 # in the back door with a hand-edited submission URL.
129 if ( "save" == $formtype ) {
130 if ( $wgUser->isBlocked() ) {
131 $this->blockedIPpage();
134 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
135 $this->userNotLoggedInPage();
138 if ( wfReadOnly() ) {
139 $wgOut->readOnlyPage();
143 # If article is new, insert it.
144 $aid = $this->mTitle
->getArticleID();
146 # Don't save a new article if it's blank.
147 if ( ( "" == $this->textbox1
) ||
148 ( wfMsg( "newarticletext" ) == $this->textbox1
) ) {
149 $wgOut->redirect( $this->mTitle
->getFullURL() );
152 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
156 # Article exists. Check for edit conflict.
158 $this->mArticle
->clear(); # Force reload of dates, etc.
160 if( ( $this->section
!= "new" ) &&
161 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
164 $userid = $wgUser->getID();
166 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
167 $this->section
, $this->textbox1
, $this->summary
);
168 # Suppress edit conflict with self
170 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
173 # switch from section editing to normal editing in edit conflict
176 if( $this->mergeChangesInto( $text ) ){
177 // Successful merge! Maybe we should tell the user the good news?
181 $this->textbox1
= $text;
185 if ( ! $isConflict ) {
186 # All's well: update the article here
187 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
))
193 # First time through: get contents, set time for conflict
196 if ( "initial" == $formtype ) {
197 $this->edittime
= $this->mArticle
->getTimestamp();
198 $this->textbox1
= $this->mArticle
->getContent(true);
202 $wgOut->setRobotpolicy( "noindex,nofollow" );
204 # Enabled article-related sidebar, toplinks, etc.
205 $wgOut->setArticleRelated( true );
208 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
209 $wgOut->setPageTitle( $s );
210 $wgOut->addHTML( wfMsg( "explainconflict" ) );
212 $this->textbox2
= $this->textbox1
;
213 $this->textbox1
= $this->mArticle
->getContent(true);
214 $this->edittime
= $this->mArticle
->getTimestamp();
216 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
218 if( $this->section
!= "" ) {
219 if( $this->section
== "new" ) {
220 $s.=wfMsg("commentedit");
222 $s.=wfMsg("sectionedit");
224 if(!$this->preview
) {
225 $sectitle=preg_match("/^=+(.*?)=+/mi",
228 if( !empty( $matches[1] ) {
229 $this->summary
= "/* ". trim($matches[1])." */ ";
233 $wgOut->setPageTitle( $s );
234 if ( $this->oldid
) {
235 $this->mArticle
->setOldSubtitle();
236 $wgOut->addHTML( wfMsg( "editingold" ) );
241 $wgOut->addHTML( "<strong>" .
242 wfMsg( "readonlywarning" ) .
245 if( $this->mTitle
->isProtected() ) {
246 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
247 "</strong><br />\n" );
250 $kblength = (int)(strlen( $this->textbox1
) / 1024);
251 if( $kblength > 29 ) {
252 $wgOut->addHTML( "<strong>" .
253 wfMsg( "longpagewarning", $kblength )
257 $rows = $wgUser->getOption( "rows" );
258 $cols = $wgUser->getOption( "cols" );
260 $ew = $wgUser->getOption( "editwidth" );
261 if ( $ew ) $ew = " style=\"width:100%\"";
264 $q = "action=submit";
265 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
266 $action = $this->mTitle
->escapeLocalURL( $q );
268 $summary = wfMsg( "summary" );
269 $subject = wfMsg("subject");
270 $minor = wfMsg( "minoredit" );
271 $watchthis = wfMsg ("watchthis");
272 $save = wfMsg( "savearticle" );
273 $prev = wfMsg( "showpreview" );
275 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
277 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
278 wfMsg( "edithelp" ) );
279 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
280 wfMsg( "copyrightpage" ) ) );
282 if($wgUser->getOption("showtoolbar")) {
283 // prepare toolbar for edit buttons
284 $toolbar=$sk->getEditToolbar();
287 // activate checkboxes if user wants them to be always active
288 if( !$this->preview
) {
289 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
290 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
292 // activate checkbox also if user is already watching the page,
293 // require wpWatchthis to be unset so that second condition is not
294 // checked unnecessarily
295 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
300 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
302 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked":"")." id='wpMinoredit' />".
303 "<label for='wpMinoredit'>{$minor}</label>";
308 if ( 0 != $wgUser->getID() ) {
309 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked":"")." id='wpWatchthis' />".
310 "<label for='wpWatchthis'>{$watchthis}</label>";
313 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
315 if ( "preview" == $formtype) {
316 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
317 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
319 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
322 $previewtext = wfUnescapeHTML( $this->textbox1
);
324 $parserOptions = ParserOptions
::newFromUser( $wgUser );
325 $parserOptions->setUseCategoryMagic( false );
326 $parserOptions->setEditSection( false );
327 $parserOptions->setEditSectionOnRightClick( false );
328 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
329 $wgTitle, $parserOptions );
330 $previewHTML = $parserOutput->mText
;
332 if($wgUser->getOption("previewontop")) {
333 $wgOut->addHTML($previewhead);
334 $wgOut->addHTML($previewHTML);
336 $wgOut->addHTML( "<br clear=\"all\" />\n" );
339 # if this is a comment, show a subject line at the top, which is also the edit summary.
340 # Otherwise, show a summary field at the bottom
341 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
342 if( $this->section
== "new" ) {
343 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
346 $commentsubject = "";
347 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
350 if( !$this->preview
) {
351 # Don't select the edit box on preview; this interferes with seeing what's going on.
352 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
356 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
357 enctype=\"application/x-www-form-urlencoded\">
359 <textarea tabindex='2' name=\"wpTextbox1\" rows='{$rows}'
360 cols='{$cols}'{$ew} wrap=\"virtual\">" .
361 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
366 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"s\" />
367 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"p\" />
368 <em>{$cancel}</em> | <em>{$edithelp}</em>
369 <br /><br />{$copywarn}
370 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
371 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
374 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
375 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
376 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
378 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
379 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
380 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
384 $wgOut->addHTML( "</form>\n" );
385 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
386 $wgOut->addHTML($previewhead);
387 $wgOut->addHTML($previewHTML);
392 function blockedIPpage()
394 global $wgOut, $wgUser, $wgLang, $wgIP;
396 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
397 $wgOut->setRobotpolicy( "noindex,nofollow" );
398 $wgOut->setArticleRelated( false );
400 $id = $wgUser->blockedBy();
401 $reason = $wgUser->blockedFor();
404 $name = User
::whoIs( $id );
405 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
406 ":{$name}|{$name}]]";
408 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
409 $wgOut->returnToMain( false );
414 function userNotLoggedInPage()
416 global $wgOut, $wgUser, $wgLang;
418 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
419 $wgOut->setRobotpolicy( "noindex,nofollow" );
420 $wgOut->setArticleRelated( false );
422 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
423 $wgOut->returnToMain( false );
426 # Forks processes to scan the originating IP for an open proxy server
427 # MemCached can be used to skip IPs that have already been scanned
428 function proxyCheck()
430 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
431 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
433 if ( !$wgBlockOpenProxies ) {
439 if ( $wgUseMemCached ) {
440 $mcKey = "$wgDBname:proxy:ip:$wgIP";
441 $mcValue = $wgMemc->get( $mcKey );
449 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
450 $iphash = md5( $wgIP . $wgProxyKey );
451 $url = $title->getFullURL( "ip=$iphash" );
453 foreach ( $wgProxyPorts as $port ) {
454 $params = implode( " ", array(
455 escapeshellarg( $wgProxyScriptPath ),
456 escapeshellarg( $wgIP ),
457 escapeshellarg( $port ),
458 escapeshellarg( $url )
460 exec( "php $params &>/dev/null &" );
463 if ( $wgUseMemCached ) {
464 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
469 /* private */ function mergeChangesInto( &$text ){
470 $oldDate = $this->edittime
;
471 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
472 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
473 $obj = wfFetchObject($res);
475 $yourtext = $obj->cur_text
;
476 $ns = $this->mTitle
->getNamespace();
477 $title = wfStrencode( $this->mTitle
->getDBkey() );
478 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
479 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
480 $obj = wfFetchObject($res);
481 if(wfMerge($obj->old_text
, $text, $yourtext, $result)){