4 * Contain the EditPage class
9 * Splitting edit page/HTML interface from Article...
10 * The actual database and text munging is still in Article,
11 * but it should get easier to call those from alternate
21 var $save = false, $preview = false;
22 var $minoredit = false, $watchthis = false;
23 var $textbox1 = '', $textbox2 = '', $summary = '';
24 var $edittime = '', $section = '';
31 function EditPage( $article ) {
32 $this->mArticle
=& $article;
34 $this->mTitle
=& $wgTitle;
38 * This is the function that gets called for "action=edit".
41 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
42 // this is not an article
43 $wgOut->setArticleFlag(false);
45 $this->importFormData( $wgRequest );
47 if ( ! $this->mTitle
->userCanEdit() ) {
48 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
51 if ( $wgUser->isBlocked() ) {
52 $this->blockedIPpage();
55 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
56 $this->userNotLoggedInPage();
60 if( $this->save ||
$this->preview
) {
61 $this->editForm( 'preview' );
63 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
68 $this->editForm( 'save' );
69 } else if ( $this->preview
) {
70 $this->editForm( 'preview' );
71 } else { # First time through
72 $this->editForm( 'initial' );
79 function importFormData( &$request ) {
80 # These fields need to be checked for encoding.
81 # Also remove trailing whitespace, but don't remove _initial_
82 # whitespace from the text boxes. This may be significant formatting.
83 $this->textbox1
= rtrim( $request->getText( 'wpTextbox1' ) );
84 $this->textbox2
= rtrim( $request->getText( 'wpTextbox2' ) );
85 $this->summary
= trim( $request->getText( 'wpSummary' ) );
87 $this->edittime
= $request->getVal( 'wpEdittime' );
88 if( !preg_match( '/^\d{14}$/', $this->edittime
)) $this->edittime
= '';
90 $this->preview
= $request->getCheck( 'wpPreview' );
91 $this->save
= $request->wasPosted() && !$this->preview
;
92 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
93 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
95 $this->oldid
= $request->getInt( 'oldid' );
97 # Section edit can come from either the form or a link
98 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
102 * Since there is only one text field on the edit form,
103 * pressing <enter> will cause the form to be submitted, but
104 * the submit button value won't appear in the query, so we
105 * Fake it here before going back to edit(). This is kind of
106 * ugly, but it helps some old URLs to still work.
109 if( !$this->preview
) $this->save
= true;
115 * The edit form is self-submitting, so that when things like
116 * preview and edit conflicts occur, we get the same form back
117 * with the extra stuff added. Only when the final submission
118 * is made and all is well do we actually save and redirect to
119 * the newly-edited page.
121 * @param string $formtype Type of form either : save, initial or preview
123 function editForm( $formtype ) {
124 global $wgOut, $wgUser;
125 global $wgLang, $wgParser, $wgTitle;
126 global $wgAllowAnonymousMinor;
127 global $wgWhitelistEdit;
128 global $wgSpamRegex, $wgFilterCallback;
130 $sk = $wgUser->getSkin();
132 // css / js subpages of user pages get a special treatment
133 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
135 if(!$this->mTitle
->getArticleID()) { # new article
136 $wgOut->addWikiText(wfmsg('newarticletext'));
139 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
140 $wgOut->addWikiText(wfmsg('talkpagetext'));
143 # Attempt submission here. This will check for edit conflicts,
144 # and redundantly check for locked database, blocked IPs, etc.
145 # that edit() already checked just in case someone tries to sneak
146 # in the back door with a hand-edited submission URL.
148 if ( 'save' == $formtype ) {
150 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
, $matches ) ) {
151 $this->spamPage ( $matches );
154 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle
, $this->textbox1
, $this->section
) ) {
155 # Error messages or other handling should be performed by the filter function
158 if ( $wgUser->isBlocked() ) {
159 $this->blockedIPpage();
162 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
163 $this->userNotLoggedInPage();
166 if ( wfReadOnly() ) {
167 $wgOut->readOnlyPage();
171 # If article is new, insert it.
172 $aid = $this->mTitle
->getArticleID( GAID_FOR_UPDATE
);
174 # Don't save a new article if it's blank.
175 if ( ( '' == $this->textbox1
) ||
176 ( wfMsg( 'newarticletext' ) == $this->textbox1
) ) {
177 $wgOut->redirect( $this->mTitle
->getFullURL() );
180 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
184 # Article exists. Check for edit conflict.
186 $this->mArticle
->clear(); # Force reload of dates, etc.
187 $this->mArticle
->forUpdate( true ); # Lock the article
189 if( ( $this->section
!= 'new' ) &&
190 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
193 $userid = $wgUser->getID();
195 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
196 $this->section
, $this->textbox1
, $this->summary
);
197 # Suppress edit conflict with self
199 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
202 # switch from section editing to normal editing in edit conflict
205 if( $this->mergeChangesInto( $text ) ){
206 // Successful merge! Maybe we should tell the user the good news?
210 $this->textbox1
= $text;
214 if ( ! $isConflict ) {
217 if( $this->section
!= '' ) {
218 # Try to get a section anchor from the section source, redirect to edited section if header found
219 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
220 # for duplicate heading checking and maybe parsing
221 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
222 # we can't deal with anchors, includes, html etc in the header for now,
223 # headline would need to be parsed to improve this
224 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
225 if($hasmatch and strlen($matches[2]) > 0) {
226 global $wgInputEncoding;
227 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
229 $headline = preg_replace( "/<.*?" . ">/","",$headline );
230 $headline = trim( $headline );
231 $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
232 $replacearray = array(
236 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
240 # update the article here
241 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
247 # First time through: get contents, set time for conflict
250 if ( 'initial' == $formtype ) {
251 $this->edittime
= $this->mArticle
->getTimestamp();
252 $this->textbox1
= $this->mArticle
->getContent( true );
256 $wgOut->setRobotpolicy( 'noindex,nofollow' );
258 # Enabled article-related sidebar, toplinks, etc.
259 $wgOut->setArticleRelated( true );
262 $s = wfMsg( 'editconflict', $this->mTitle
->getPrefixedText() );
263 $wgOut->setPageTitle( $s );
264 $wgOut->addHTML( wfMsg( 'explainconflict' ) );
266 $this->textbox2
= $this->textbox1
;
267 $this->textbox1
= $this->mArticle
->getContent( true );
268 $this->edittime
= $this->mArticle
->getTimestamp();
270 $s = wfMsg( 'editing', $this->mTitle
->getPrefixedText() );
272 if( $this->section
!= '' ) {
273 if( $this->section
== 'new' ) {
274 $s.=wfMsg('commentedit');
276 $s.=wfMsg('sectionedit');
278 if(!$this->preview
) {
279 $sectitle=preg_match("/^=+(.*?)=+/mi",
282 if( !empty( $matches[1] ) ) {
283 $this->summary
= "/* ". trim($matches[1])." */ ";
287 $wgOut->setPageTitle( $s );
288 if ( $this->oldid
) {
289 $this->mArticle
->setOldSubtitle();
290 $wgOut->addHTML( wfMsg( 'editingold' ) );
295 $wgOut->addHTML( '<strong>' .
296 wfMsg( 'readonlywarning' ) .
298 } else if ( $isCssJsSubpage and 'preview' != $formtype) {
299 $wgOut->addHTML( wfMsg( 'usercssjsyoucanpreview' ));
301 if( $this->mTitle
->isProtected() ) {
302 $wgOut->addHTML( '<strong>' . wfMsg( 'protectedpagewarning' ) .
303 "</strong><br />\n" );
306 $kblength = (int)(strlen( $this->textbox1
) / 1024);
307 if( $kblength > 29 ) {
308 $wgOut->addHTML( '<strong>' .
309 wfMsg( 'longpagewarning', $wgLang->formatNum( $kblength ) )
313 $rows = $wgUser->getOption( 'rows' );
314 $cols = $wgUser->getOption( 'cols' );
316 $ew = $wgUser->getOption( 'editwidth' );
317 if ( $ew ) $ew = " style=\"width:100%\"";
320 $q = 'action=submit';
321 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
322 $action = $this->mTitle
->escapeLocalURL( $q );
324 $summary = wfMsg('summary');
325 $subject = wfMsg('subject');
326 $minor = wfMsg('minoredit');
327 $watchthis = wfMsg ('watchthis');
328 $save = wfMsg('savearticle');
329 $prev = wfMsg('showpreview');
331 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
333 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
334 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
335 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
336 htmlspecialchars( wfMsg( 'newwindow' ) );
338 global $wgRightsText;
339 $copywarn = "<div id=\"editpage-copywarn\">\n" .
340 wfMsg( $wgRightsText ?
'copyrightwarning' : 'copyrightwarning2',
341 '[[' . wfMsg( 'copyrightpage' ) . ']]',
342 $wgRightsText ) . "\n</div>";
344 if( $wgUser->getOption('showtoolbar') and !$isCssJsSubpage ) {
345 # prepare toolbar for edit buttons
346 $toolbar = $sk->getEditToolbar();
351 // activate checkboxes if user wants them to be always active
352 if( !$this->preview
) {
353 if( $wgUser->getOption( 'watchdefault' ) ) $this->watchthis
= true;
354 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit
= true;
356 // activate checkbox also if user is already watching the page,
357 // require wpWatchthis to be unset so that second condition is not
358 // checked unnecessarily
359 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
364 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
366 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
367 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
368 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
373 if ( 0 != $wgUser->getID() ) {
374 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
375 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
376 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
379 $checkboxhtml = $minoredithtml . $watchhtml . '<br />';
381 if ( 'preview' == $formtype) {
382 $previewhead='<h2>' . wfMsg( 'preview' ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
383 wfMsg( 'note' ) . wfMsg( 'previewnote' ) . "</font></center></p>\n";
385 $previewhead.='<h2>' . wfMsg( 'previewconflict' ) .
389 $parserOptions = ParserOptions
::newFromUser( $wgUser );
390 $parserOptions->setEditSection( false );
391 $parserOptions->setEditSectionOnRightClick( false );
393 # don't parse user css/js, show message about preview
394 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
396 if ( $isCssJsSubpage ) {
397 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
398 $previewtext = wfMsg('usercsspreview');
399 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
400 $previewtext = wfMsg('userjspreview');
402 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
403 $wgOut->addHTML( $parserOutput->mText
);
405 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
406 $wgTitle, $parserOptions );
407 $previewHTML = $parserOutput->mText
;
409 if($wgUser->getOption('previewontop')) {
410 $wgOut->addHTML($previewhead);
411 $wgOut->addHTML($previewHTML);
413 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
414 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
415 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
419 # if this is a comment, show a subject line at the top, which is also the edit summary.
420 # Otherwise, show a summary field at the bottom
421 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
422 if( $this->section
== 'new' ) {
423 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
426 $commentsubject = '';
427 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
430 if( !$this->preview
) {
431 # Don't select the edit box on preview; this interferes with seeing what's going on.
432 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
434 # Prepare a list of templates used by this page
435 $db =& wfGetDB( DB_SLAVE
);
436 $cur = $db->tableName( 'cur' );
437 $links = $db->tableName( 'links' );
438 $id = $this->mTitle
->getArticleID();
439 $sql = "SELECT cur_namespace,cur_title,cur_id ".
440 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
441 $res = $db->query( $sql, "EditPage::editform" );
443 if ( $db->numRows( $res ) ) {
444 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
445 while ( $row = $db->fetchObject( $res ) ) {
446 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
447 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
450 $templates .= '</ul>';
456 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
457 enctype=\"application/x-www-form-urlencoded\">
459 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
460 cols='{$cols}'{$ew}>" .
461 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
466 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
467 " title=\"".wfMsg('tooltip-save')."\"/>
468 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
469 " title=\"".wfMsg('tooltip-preview')."\"/>
470 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
471 $wgOut->addWikiText( $copywarn );
473 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
474 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
477 require_once( "DifferenceEngine.php" );
478 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
479 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
480 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
482 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
483 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
484 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
488 $wgOut->addHTML( "</form>\n" );
489 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
490 $wgOut->addHTML($previewhead);
491 $wgOut->addHTML($previewHTML);
498 function blockedIPpage() {
499 global $wgOut, $wgUser, $wgLang, $wgIP;
501 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
502 $wgOut->setRobotpolicy( 'noindex,nofollow' );
503 $wgOut->setArticleRelated( false );
505 $id = $wgUser->blockedBy();
506 $reason = $wgUser->blockedFor();
509 if ( is_numeric( $id ) ) {
510 $name = User
::whoIs( $id );
514 $link = '[[' . $wgLang->getNsText( Namespace::getUser() ) .
515 ":{$name}|{$name}]]";
517 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
518 $wgOut->returnToMain( false );
524 function userNotLoggedInPage() {
525 global $wgOut, $wgUser, $wgLang;
527 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
528 $wgOut->setRobotpolicy( 'noindex,nofollow' );
529 $wgOut->setArticleRelated( false );
531 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
532 $wgOut->returnToMain( false );
538 function spamPage ( $matches = array() )
541 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
542 $wgOut->setRobotpolicy( 'noindex,nofollow' );
543 $wgOut->setArticleRelated( false );
545 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
546 if ( isset ( $matches[0] ) ) {
547 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
549 $wgOut->returnToMain( false );
553 * Forks processes to scan the originating IP for an open proxy server
554 * MemCached can be used to skip IPs that have already been scanned
556 function proxyCheck() {
557 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
558 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
560 if ( !$wgBlockOpenProxies ) {
566 if ( $wgUseMemCached ) {
567 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
568 $mcValue = $wgMemc->get( $mcKey );
576 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
577 $iphash = md5( $wgIP . $wgProxyKey );
578 $url = $title->getFullURL( 'ip='.$iphash );
580 foreach ( $wgProxyPorts as $port ) {
581 $params = implode( ' ', array(
582 escapeshellarg( $wgProxyScriptPath ),
583 escapeshellarg( $wgIP ),
584 escapeshellarg( $port ),
585 escapeshellarg( $url )
587 exec( "php $params &>/dev/null &" );
590 if ( $wgUseMemCached ) {
591 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
600 function mergeChangesInto( &$text ){
601 $fname = 'EditPage::mergeChangesInto';
602 $oldDate = $this->edittime
;
603 $dbw =& wfGetDB( DB_MASTER
);
604 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
605 $fname, 'FOR UPDATE' );
607 $yourtext = $obj->cur_text
;
608 $ns = $this->mTitle
->getNamespace();
609 $title = $this->mTitle
->getDBkey();
610 $obj = $dbw->getArray( 'old',
611 array( 'old_text','old_flags'),
612 array( 'old_namespace' => $ns, 'old_title' => $title,
613 'old_timestamp' => $dbw->timestamp($oldDate)),
615 $oldText = Article
::getRevisionText( $obj );
617 if(wfMerge($oldText, $text, $yourtext, $result)){