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($matches[1]) { $this->summary
= "/* ". trim($matches[1])." */ "; }
231 $wgOut->setPageTitle( $s );
232 if ( $this->oldid
) {
233 $this->mArticle
->setOldSubtitle();
234 $wgOut->addHTML( wfMsg( "editingold" ) );
239 $wgOut->addHTML( "<strong>" .
240 wfMsg( "readonlywarning" ) .
243 if( $this->mTitle
->isProtected() ) {
244 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
245 "</strong><br />\n" );
248 $kblength = (int)(strlen( $this->textbox1
) / 1024);
249 if( $kblength > 29 ) {
250 $wgOut->addHTML( "<strong>" .
251 wfMsg( "longpagewarning", $kblength )
255 $rows = $wgUser->getOption( "rows" );
256 $cols = $wgUser->getOption( "cols" );
258 $ew = $wgUser->getOption( "editwidth" );
259 if ( $ew ) $ew = " style=\"width:100%\"";
262 $q = "action=submit";
263 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
264 $action = $this->mTitle
->escapeLocalURL( $q );
266 $summary = wfMsg( "summary" );
267 $subject = wfMsg("subject");
268 $minor = wfMsg( "minoredit" );
269 $watchthis = wfMsg ("watchthis");
270 $save = wfMsg( "savearticle" );
271 $prev = wfMsg( "showpreview" );
273 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
275 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
276 wfMsg( "edithelp" ) );
277 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
278 wfMsg( "copyrightpage" ) ) );
280 if($wgUser->getOption("showtoolbar")) {
281 // prepare toolbar for edit buttons
282 $toolbar=$sk->getEditToolbar();
285 // activate checkboxes if user wants them to be always active
286 if( !$this->preview
) {
287 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
288 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
290 // activate checkbox also if user is already watching the page,
291 // require wpWatchthis to be unset so that second condition is not
292 // checked unnecessarily
293 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
298 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
300 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked":"")." id='wpMinoredit' />".
301 "<label for='wpMinoredit'>{$minor}</label>";
306 if ( 0 != $wgUser->getID() ) {
307 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked":"")." id='wpWatchthis' />".
308 "<label for='wpWatchthis'>{$watchthis}</label>";
311 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
313 if ( "preview" == $formtype) {
314 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
315 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><p>\n";
317 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
320 $previewtext = wfUnescapeHTML( $this->textbox1
);
322 $parserOptions = ParserOptions
::newFromUser( $wgUser );
323 $parserOptions->setUseCategoryMagic( false );
324 $parserOptions->setEditSection( false );
325 $parserOptions->setEditSectionOnRightClick( false );
326 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
327 $wgTitle, $parserOptions );
328 $previewHTML = $parserOutput->mText
;
330 if($wgUser->getOption("previewontop")) {
331 $wgOut->addHTML($previewhead);
332 $wgOut->addHTML($previewHTML);
334 $wgOut->addHTML( "<br clear=\"all\" />\n" );
337 # if this is a comment, show a subject line at the top, which is also the edit summary.
338 # Otherwise, show a summary field at the bottom
339 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
340 if( $this->section
== "new" ) {
341 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
344 $commentsubject = "";
345 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
348 if( !$this->preview
) {
349 # Don't select the edit box on preview; this interferes with seeing what's going on.
350 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
354 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
355 enctype=\"application/x-www-form-urlencoded\">
357 <textarea tabindex='2' name=\"wpTextbox1\" rows='{$rows}'
358 cols='{$cols}'{$ew} wrap=\"virtual\">" .
359 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
364 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"s\" />
365 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"p\" />
366 <em>{$cancel}</em> | <em>{$edithelp}</em>
367 <br /><br />{$copywarn}
368 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
369 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
372 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
373 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
374 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
376 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
377 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
378 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
382 $wgOut->addHTML( "</form>\n" );
383 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
384 $wgOut->addHTML($previewhead);
385 $wgOut->addHTML($previewHTML);
390 function blockedIPpage()
392 global $wgOut, $wgUser, $wgLang, $wgIP;
394 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
395 $wgOut->setRobotpolicy( "noindex,nofollow" );
396 $wgOut->setArticleRelated( false );
398 $id = $wgUser->blockedBy();
399 $reason = $wgUser->blockedFor();
402 $name = User
::whoIs( $id );
403 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
404 ":{$name}|{$name}]]";
406 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
407 $wgOut->returnToMain( false );
412 function userNotLoggedInPage()
414 global $wgOut, $wgUser, $wgLang;
416 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
417 $wgOut->setRobotpolicy( "noindex,nofollow" );
418 $wgOut->setArticleRelated( false );
420 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
421 $wgOut->returnToMain( false );
424 # Forks processes to scan the originating IP for an open proxy server
425 # MemCached can be used to skip IPs that have already been scanned
426 function proxyCheck()
428 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
429 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
431 if ( !$wgBlockOpenProxies ) {
437 if ( $wgUseMemCached ) {
438 $mcKey = "$wgDBname:proxy:ip:$wgIP";
439 $mcValue = $wgMemc->get( $mcKey );
447 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
448 $iphash = md5( $wgIP . $wgProxyKey );
449 $url = $title->getFullURL( "ip=$iphash" );
451 foreach ( $wgProxyPorts as $port ) {
452 $params = implode( " ", array(
453 escapeshellarg( $wgProxyScriptPath ),
454 escapeshellarg( $wgIP ),
455 escapeshellarg( $port ),
456 escapeshellarg( $url )
458 exec( "php $params &>/dev/null &" );
461 if ( $wgUseMemCached ) {
462 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
467 /* private */ function mergeChangesInto( &$text ){
468 $oldDate = $this->edittime
;
469 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
470 $this->mTitle
->getArticleID() . " FOR UPDATE", DB_WRITE
);
471 $obj = wfFetchObject($res);
473 $yourtext = $obj->cur_text
;
474 $ns = $this->mTitle
->getNamespace();
475 $title = wfStrencode( $this->mTitle
->getDBkey() );
476 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
477 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE
);
478 $obj = wfFetchObject($res);
479 if(wfMerge($obj->old_text
, $text, $yourtext, $result)){