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( GAID_FOR_UPDATE
);
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.
171 $this->mArticle
->forUpdate( true ); # Lock the article
173 if( ( $this->section
!= "new" ) &&
174 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
177 $userid = $wgUser->getID();
179 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
180 $this->section
, $this->textbox1
, $this->summary
);
181 # Suppress edit conflict with self
183 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
186 # switch from section editing to normal editing in edit conflict
189 if( $this->mergeChangesInto( $text ) ){
190 // Successful merge! Maybe we should tell the user the good news?
194 $this->textbox1
= $text;
198 if ( ! $isConflict ) {
201 if( $this->section
!= '' ) {
202 # Try to get a section anchor from the section source, redirect to edited section if header found
203 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
204 # for duplicate heading checking and maybe parsing
205 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
206 # we can't deal with anchors, includes, html etc in the header for now,
207 # headline would need to be parsed to improve this
208 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
209 if($hasmatch and strlen($matches[2]) > 0) {
210 global $wgInputEncoding;
211 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
213 $headline = preg_replace( "/<.*?" . ">/","",$headline );
214 $headline = trim( $headline );
215 $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
216 $replacearray = array(
220 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
224 # update the article here
225 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
231 # First time through: get contents, set time for conflict
234 if ( "initial" == $formtype ) {
235 $this->edittime
= $this->mArticle
->getTimestamp();
236 $this->textbox1
= $this->mArticle
->getContent( true );
240 $wgOut->setRobotpolicy( "noindex,nofollow" );
242 # Enabled article-related sidebar, toplinks, etc.
243 $wgOut->setArticleRelated( true );
246 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
247 $wgOut->setPageTitle( $s );
248 $wgOut->addHTML( wfMsg( "explainconflict" ) );
250 $this->textbox2
= $this->textbox1
;
251 $this->textbox1
= $this->mArticle
->getContent( true );
252 $this->edittime
= $this->mArticle
->getTimestamp();
254 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
256 if( $this->section
!= "" ) {
257 if( $this->section
== "new" ) {
258 $s.=wfMsg("commentedit");
260 $s.=wfMsg("sectionedit");
262 if(!$this->preview
) {
263 $sectitle=preg_match("/^=+(.*?)=+/mi",
266 if( !empty( $matches[1] ) ) {
267 $this->summary
= "/* ". trim($matches[1])." */ ";
271 $wgOut->setPageTitle( $s );
272 if ( $this->oldid
) {
273 $this->mArticle
->setOldSubtitle();
274 $wgOut->addHTML( wfMsg( "editingold" ) );
279 $wgOut->addHTML( "<strong>" .
280 wfMsg( "readonlywarning" ) .
282 } else if ( $isCssJsSubpage and "preview" != $formtype) {
283 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
285 if( $this->mTitle
->isProtected() ) {
286 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
287 "</strong><br />\n" );
290 $kblength = (int)(strlen( $this->textbox1
) / 1024);
291 if( $kblength > 29 ) {
292 $wgOut->addHTML( "<strong>" .
293 wfMsg( "longpagewarning", $kblength )
297 $rows = $wgUser->getOption( "rows" );
298 $cols = $wgUser->getOption( "cols" );
300 $ew = $wgUser->getOption( "editwidth" );
301 if ( $ew ) $ew = " style=\"width:100%\"";
304 $q = "action=submit";
305 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
306 $action = $this->mTitle
->escapeLocalURL( $q );
308 $summary = wfMsg( "summary" );
309 $subject = wfMsg("subject");
310 $minor = wfMsg( "minoredit" );
311 $watchthis = wfMsg ("watchthis");
312 $save = wfMsg( "savearticle" );
313 $prev = wfMsg( "showpreview" );
315 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
317 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
318 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
319 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
320 htmlspecialchars( wfMsg( 'newwindow' ) );
322 global $wgRightsText;
323 $copywarn = "<div id=\"editpage-copywarn\">\n" .
324 wfMsg( $wgRightsText ?
"copyrightwarning" : "copyrightwarning2",
325 "[[" . wfMsg( "copyrightpage" ) . "]]",
326 $wgRightsText ) . "\n</div>";
328 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
329 # prepare toolbar for edit buttons
330 $toolbar = $sk->getEditToolbar();
335 // activate checkboxes if user wants them to be always active
336 if( !$this->preview
) {
337 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
338 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
340 // activate checkbox also if user is already watching the page,
341 // require wpWatchthis to be unset so that second condition is not
342 // checked unnecessarily
343 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
348 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
350 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
351 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
352 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
357 if ( 0 != $wgUser->getID() ) {
358 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
359 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
360 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
363 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
365 if ( "preview" == $formtype) {
366 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
367 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></p>\n";
369 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
373 $parserOptions = ParserOptions
::newFromUser( $wgUser );
374 $parserOptions->setUseCategoryMagic( false );
375 $parserOptions->setEditSection( false );
376 $parserOptions->setEditSectionOnRightClick( false );
378 # don't parse user css/js, show message about preview
379 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
381 if ( $isCssJsSubpage ) {
382 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
383 $previewtext = wfMsg('usercsspreview');
384 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
385 $previewtext = wfMsg('userjspreview');
387 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
388 $wgOut->addHTML( $parserOutput->mText
);
390 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
391 $wgTitle, $parserOptions );
392 $previewHTML = $parserOutput->mText
;
394 if($wgUser->getOption("previewontop")) {
395 $wgOut->addHTML($previewhead);
396 $wgOut->addHTML($previewHTML);
398 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
399 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
400 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
404 # if this is a comment, show a subject line at the top, which is also the edit summary.
405 # Otherwise, show a summary field at the bottom
406 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
407 if( $this->section
== "new" ) {
408 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
411 $commentsubject = "";
412 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
415 if( !$this->preview
) {
416 # Don't select the edit box on preview; this interferes with seeing what's going on.
417 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
419 # Prepare a list of templates used by this page
420 $db =& wfGetDB( DB_SLAVE
);
421 $cur = $db->tableName( 'cur' );
422 $links = $db->tableName( 'links' );
423 $id = $this->mTitle
->getArticleID();
424 $sql = "SELECT cur_namespace,cur_title,cur_id ".
425 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
426 $res = $db->query( $sql, "EditPage::editform" );
428 if ( $db->numRows( $res ) ) {
429 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
430 while ( $row = $db->fetchObject( $res ) ) {
431 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
432 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
435 $templates .= '</ul>';
441 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
442 enctype=\"application/x-www-form-urlencoded\">
444 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
445 cols='{$cols}'{$ew}>" .
446 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
451 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
452 " title=\"".wfMsg('tooltip-save')."\"/>
453 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
454 " title=\"".wfMsg('tooltip-preview')."\"/>
455 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
456 $wgOut->addWikiText( $copywarn );
458 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
459 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
462 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
463 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
464 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
466 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
467 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
468 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
472 $wgOut->addHTML( "</form>\n" );
473 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
474 $wgOut->addHTML($previewhead);
475 $wgOut->addHTML($previewHTML);
479 function blockedIPpage()
481 global $wgOut, $wgUser, $wgLang, $wgIP;
483 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
484 $wgOut->setRobotpolicy( "noindex,nofollow" );
485 $wgOut->setArticleRelated( false );
487 $id = $wgUser->blockedBy();
488 $reason = $wgUser->blockedFor();
491 if ( is_numeric( $id ) ) {
492 $name = User
::whoIs( $id );
496 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
497 ":{$name}|{$name}]]";
499 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
500 $wgOut->returnToMain( false );
505 function userNotLoggedInPage()
507 global $wgOut, $wgUser, $wgLang;
509 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
510 $wgOut->setRobotpolicy( "noindex,nofollow" );
511 $wgOut->setArticleRelated( false );
513 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
514 $wgOut->returnToMain( false );
517 function spamPage ( $matches = array() )
520 $wgOut->setPageTitle( wfMsg( "spamprotectiontitle" ) );
521 $wgOut->setRobotpolicy( "noindex,nofollow" );
522 $wgOut->setArticleRelated( false );
524 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
525 if ( isset ( $matches[0] ) ) {
526 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
528 $wgOut->returnToMain( false );
531 # Forks processes to scan the originating IP for an open proxy server
532 # MemCached can be used to skip IPs that have already been scanned
533 function proxyCheck()
535 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
536 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
538 if ( !$wgBlockOpenProxies ) {
544 if ( $wgUseMemCached ) {
545 $mcKey = "$wgDBname:proxy:ip:$wgIP";
546 $mcValue = $wgMemc->get( $mcKey );
554 $title = Title
::makeTitle( NS_SPECIAL
, "Blockme" );
555 $iphash = md5( $wgIP . $wgProxyKey );
556 $url = $title->getFullURL( "ip=$iphash" );
558 foreach ( $wgProxyPorts as $port ) {
559 $params = implode( " ", array(
560 escapeshellarg( $wgProxyScriptPath ),
561 escapeshellarg( $wgIP ),
562 escapeshellarg( $port ),
563 escapeshellarg( $url )
565 exec( "php $params &>/dev/null &" );
568 if ( $wgUseMemCached ) {
569 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
574 /* private */ function mergeChangesInto( &$text ){
575 $fname = 'EditPage::mergeChangesInto';
576 $oldDate = $this->edittime
;
577 $dbw =& wfGetDB( DB_MASTER
);
578 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
579 $fname, 'FOR UPDATE' );
581 $yourtext = $obj->cur_text
;
582 $ns = $this->mTitle
->getNamespace();
583 $title = $this->mTitle
->getDBkey();
584 $obj = $dbw->getArray( 'old',
585 array( 'old_text','old_flags'),
586 array( 'old_namespace' => $ns, 'old_title' => $title,
587 'old_timestamp' => $dbw->timestamp($oldDate)),
589 $oldText = Article
::getRevisionText( $obj );
591 if(wfMerge($oldText, $text, $yourtext, $result)){