4 # Splitting edit page/HTML interface from Article...
5 # The actual database and text munging is still in Article,
6 # but it should get easier to call those from alternate
14 var $save = false, $preview = false;
15 var $minoredit = false, $watchthis = false;
16 var $textbox1 = "", $textbox2 = "", $summary = "";
17 var $edittime = "", $section = "";
20 function EditPage( $article ) {
21 $this->mArticle
=& $article;
23 $this->mTitle
=& $wgTitle;
26 # This is the function that gets called for "action=edit".
30 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
31 // this is not an article
32 $wgOut->setArticleFlag(false);
34 $this->importFormData( $wgRequest );
36 if ( ! $this->mTitle
->userCanEdit() ) {
37 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
40 if ( $wgUser->isBlocked() ) {
41 $this->blockedIPpage();
44 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
45 $this->userNotLoggedInPage();
49 if( $this->save ||
$this->preview
) {
50 $this->editForm( "preview" );
52 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
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->preview
= $request->getCheck( 'wpPreview' );
77 $this->save
= $request->wasPosted() && !$this->preview
;
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;
112 global $wgSpamRegex, $wgFilterCallback;
114 $sk = $wgUser->getSkin();
116 // css / js subpages of user pages get a special treatment
117 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
119 if(!$this->mTitle
->getArticleID()) { # new article
120 $wgOut->addWikiText(wfmsg("newarticletext"));
123 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
124 $wgOut->addWikiText(wfmsg("talkpagetext"));
127 # Attempt submission here. This will check for edit conflicts,
128 # and redundantly check for locked database, blocked IPs, etc.
129 # that edit() already checked just in case someone tries to sneak
130 # in the back door with a hand-edited submission URL.
132 if ( "save" == $formtype ) {
134 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
, $matches ) ) {
135 $this->spamPage ( $matches );
138 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle
, $this->textbox1
, $this->section
) ) {
139 # Error messages or other handling should be performed by the filter function
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 = '#'.urlencode( str_replace(' ', '_', $headline ) );
215 $replacearray = array(
219 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
223 # update the article here
224 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
230 # First time through: get contents, set time for conflict
233 if ( "initial" == $formtype ) {
234 $this->edittime
= $this->mArticle
->getTimestamp();
235 $this->textbox1
= $this->mArticle
->getContent( true );
239 $wgOut->setRobotpolicy( "noindex,nofollow" );
241 # Enabled article-related sidebar, toplinks, etc.
242 $wgOut->setArticleRelated( true );
245 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
246 $wgOut->setPageTitle( $s );
247 $wgOut->addHTML( wfMsg( "explainconflict" ) );
249 $this->textbox2
= $this->textbox1
;
250 $this->textbox1
= $this->mArticle
->getContent( true );
251 $this->edittime
= $this->mArticle
->getTimestamp();
253 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
255 if( $this->section
!= "" ) {
256 if( $this->section
== "new" ) {
257 $s.=wfMsg("commentedit");
259 $s.=wfMsg("sectionedit");
261 if(!$this->preview
) {
262 $sectitle=preg_match("/^=+(.*?)=+/mi",
265 if( !empty( $matches[1] ) ) {
266 $this->summary
= "/* ". trim($matches[1])." */ ";
270 $wgOut->setPageTitle( $s );
271 if ( $this->oldid
) {
272 $this->mArticle
->setOldSubtitle();
273 $wgOut->addHTML( wfMsg( "editingold" ) );
278 $wgOut->addHTML( "<strong>" .
279 wfMsg( "readonlywarning" ) .
281 } else if ( $isCssJsSubpage and "preview" != $formtype) {
282 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
284 if( $this->mTitle
->isProtected() ) {
285 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
286 "</strong><br />\n" );
289 $kblength = (int)(strlen( $this->textbox1
) / 1024);
290 if( $kblength > 29 ) {
291 $wgOut->addHTML( "<strong>" .
292 wfMsg( "longpagewarning", $kblength )
296 $rows = $wgUser->getOption( "rows" );
297 $cols = $wgUser->getOption( "cols" );
299 $ew = $wgUser->getOption( "editwidth" );
300 if ( $ew ) $ew = " style=\"width:100%\"";
303 $q = "action=submit";
304 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
305 $action = $this->mTitle
->escapeLocalURL( $q );
307 $summary = wfMsg( "summary" );
308 $subject = wfMsg("subject");
309 $minor = wfMsg( "minoredit" );
310 $watchthis = wfMsg ("watchthis");
311 $save = wfMsg( "savearticle" );
312 $prev = wfMsg( "showpreview" );
314 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
316 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
317 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
318 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
319 htmlspecialchars( wfMsg( 'newwindow' ) );
321 global $wgRightsText;
322 $copywarn = "<div id=\"editpage-copywarn\">\n" .
323 wfMsg( $wgRightsText ?
"copyrightwarning" : "copyrightwarning2",
324 "[[" . wfMsg( "copyrightpage" ) . "]]",
325 $wgRightsText ) . "\n</div>";
327 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
328 # prepare toolbar for edit buttons
329 $toolbar = $sk->getEditToolbar();
334 // activate checkboxes if user wants them to be always active
335 if( !$this->preview
) {
336 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
337 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
339 // activate checkbox also if user is already watching the page,
340 // require wpWatchthis to be unset so that second condition is not
341 // checked unnecessarily
342 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
347 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
349 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
350 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
351 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
356 if ( 0 != $wgUser->getID() ) {
357 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
358 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
359 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
362 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
364 if ( "preview" == $formtype) {
365 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
366 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></p>\n";
368 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
372 $parserOptions = ParserOptions
::newFromUser( $wgUser );
373 $parserOptions->setUseCategoryMagic( false );
374 $parserOptions->setEditSection( false );
375 $parserOptions->setEditSectionOnRightClick( false );
377 # don't parse user css/js, show message about preview
378 # 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()" );
418 # Prepare a list of templates used by this page
419 $db =& wfGetDB( DB_SLAVE
);
420 $cur = $db->tableName( 'cur' );
421 $links = $db->tableName( 'links' );
422 $id = $this->mTitle
->getArticleID();
423 $sql = "SELECT cur_namespace,cur_title,cur_id ".
424 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
425 $res = $db->query( $sql, "EditPage::editform" );
427 if ( $db->numRows( $res ) ) {
428 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
429 while ( $row = $db->fetchObject( $res ) ) {
430 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
431 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
434 $templates .= '</ul>';
438 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
439 enctype=\"application/x-www-form-urlencoded\">
441 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
442 cols='{$cols}'{$ew}>" .
443 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
448 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
449 " title=\"".wfMsg('tooltip-save')."\"/>
450 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
451 " title=\"".wfMsg('tooltip-preview')."\"/>
452 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
453 $wgOut->addWikiText( $copywarn );
455 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
456 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
459 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
460 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
461 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
463 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
464 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
465 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
469 $wgOut->addHTML( "</form>\n" );
470 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
471 $wgOut->addHTML($previewhead);
472 $wgOut->addHTML($previewHTML);
477 function blockedIPpage()
479 global $wgOut, $wgUser, $wgLang, $wgIP;
481 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
482 $wgOut->setRobotpolicy( "noindex,nofollow" );
483 $wgOut->setArticleRelated( false );
485 $id = $wgUser->blockedBy();
486 $reason = $wgUser->blockedFor();
489 if ( is_numeric( $id ) ) {
490 $name = User
::whoIs( $id );
494 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
495 ":{$name}|{$name}]]";
497 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
498 $wgOut->returnToMain( false );
503 function userNotLoggedInPage()
505 global $wgOut, $wgUser, $wgLang;
507 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
508 $wgOut->setRobotpolicy( "noindex,nofollow" );
509 $wgOut->setArticleRelated( false );
511 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
512 $wgOut->returnToMain( false );
515 function spamPage ( $matches = array() )
518 $wgOut->setPageTitle( wfMsg( "spamprotectiontitle" ) );
519 $wgOut->setRobotpolicy( "noindex,nofollow" );
520 $wgOut->setArticleRelated( false );
522 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
523 if ( isset ( $matches[0] ) ) {
524 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
526 $wgOut->returnToMain( false );
529 # Forks processes to scan the originating IP for an open proxy server
530 # MemCached can be used to skip IPs that have already been scanned
531 function proxyCheck()
533 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
534 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
536 if ( !$wgBlockOpenProxies ) {
542 if ( $wgUseMemCached ) {
543 $mcKey = "$wgDBname:proxy:ip:$wgIP";
544 $mcValue = $wgMemc->get( $mcKey );
552 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
553 $iphash = md5( $wgIP . $wgProxyKey );
554 $url = $title->getFullURL( "ip=$iphash" );
556 foreach ( $wgProxyPorts as $port ) {
557 $params = implode( " ", array(
558 escapeshellarg( $wgProxyScriptPath ),
559 escapeshellarg( $wgIP ),
560 escapeshellarg( $port ),
561 escapeshellarg( $url )
563 exec( "php $params &>/dev/null &" );
566 if ( $wgUseMemCached ) {
567 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
572 /* private */ function mergeChangesInto( &$text ){
573 $fname = 'EditPage::mergeChangesInto';
574 $oldDate = $this->edittime
;
575 $dbw =& wfGetDB( DB_MASTER
);
576 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
577 $fname, 'FOR UPDATE' );
579 $yourtext = $obj->cur_text
;
580 $ns = $this->mTitle
->getNamespace();
581 $title = $this->mTitle
->getDBkey();
582 $obj = $dbw->getArray( 'old',
583 array( 'old_text','old_flags'),
584 array( 'old_namespace' => $ns, 'old_title' => $title,
585 'old_timestamp' => $dbw->timestamp($oldDate)),
587 $oldText = Article
::getRevisionText( $obj );
589 if(wfMerge($oldText, $text, $yourtext, $result)){