02cba8786a9c7525f24148ab11fe4e41a78d279f
4 * Contain the EditPage class
8 * Splitting edit page/HTML interface from Article...
9 * The actual database and text munging is still in Article,
10 * but it should get easier to call those from alternate
18 var $save = false, $preview = false;
19 var $minoredit = false, $watchthis = false;
20 var $textbox1 = '', $textbox2 = '', $summary = '';
21 var $edittime = '', $section = '';
24 function EditPage( $article ) {
25 $this->mArticle
=& $article;
27 $this->mTitle
=& $wgTitle;
30 # This is the function that gets called for "action=edit".
34 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
35 // this is not an article
36 $wgOut->setArticleFlag(false);
38 $this->importFormData( $wgRequest );
40 if ( ! $this->mTitle
->userCanEdit() ) {
41 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
44 if ( $wgUser->isBlocked() ) {
45 $this->blockedIPpage();
48 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
49 $this->userNotLoggedInPage();
53 if( $this->save ||
$this->preview
) {
54 $this->editForm( 'preview' );
56 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
61 $this->editForm( 'save' );
62 } else if ( $this->preview
) {
63 $this->editForm( 'preview' );
64 } else { # First time through
65 $this->editForm( 'initial' );
69 function importFormData( &$request ) {
70 # These fields need to be checked for encoding.
71 # Also remove trailing whitespace, but don't remove _initial_
72 # whitespace from the text boxes. This may be significant formatting.
73 $this->textbox1
= rtrim( $request->getText( 'wpTextbox1' ) );
74 $this->textbox2
= rtrim( $request->getText( 'wpTextbox2' ) );
75 $this->summary
= trim( $request->getText( 'wpSummary' ) );
77 $this->edittime
= $request->getVal( 'wpEdittime' );
78 if( !preg_match( '/^\d{14}$/', $this->edittime
)) $this->edittime
= '';
80 $this->preview
= $request->getCheck( 'wpPreview' );
81 $this->save
= $request->wasPosted() && !$this->preview
;
82 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
83 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
85 $this->oldid
= $request->getInt( 'oldid' );
87 # Section edit can come from either the form or a link
88 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
91 # Since there is only one text field on the edit form,
92 # pressing <enter> will cause the form to be submitted, but
93 # the submit button value won't appear in the query, so we
94 # Fake it here before going back to edit(). This is kind of
95 # ugly, but it helps some old URLs to still work.
99 if( !$this->preview
) $this->save
= true;
104 # The edit form is self-submitting, so that when things like
105 # preview and edit conflicts occur, we get the same form back
106 # with the extra stuff added. Only when the final submission
107 # is made and all is well do we actually save and redirect to
108 # the newly-edited page.
110 function editForm( $formtype )
112 global $wgOut, $wgUser;
113 global $wgLang, $wgParser, $wgTitle;
114 global $wgAllowAnonymousMinor;
115 global $wgWhitelistEdit;
116 global $wgSpamRegex, $wgFilterCallback;
118 $sk = $wgUser->getSkin();
120 // css / js subpages of user pages get a special treatment
121 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
123 if(!$this->mTitle
->getArticleID()) { # new article
124 $wgOut->addWikiText(wfmsg('newarticletext'));
127 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
128 $wgOut->addWikiText(wfmsg('talkpagetext'));
131 # Attempt submission here. This will check for edit conflicts,
132 # and redundantly check for locked database, blocked IPs, etc.
133 # that edit() already checked just in case someone tries to sneak
134 # in the back door with a hand-edited submission URL.
136 if ( 'save' == $formtype ) {
138 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
, $matches ) ) {
139 $this->spamPage ( $matches );
142 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle
, $this->textbox1
, $this->section
) ) {
143 # Error messages or other handling should be performed by the filter function
146 if ( $wgUser->isBlocked() ) {
147 $this->blockedIPpage();
150 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
151 $this->userNotLoggedInPage();
154 if ( wfReadOnly() ) {
155 $wgOut->readOnlyPage();
159 # If article is new, insert it.
160 $aid = $this->mTitle
->getArticleID( GAID_FOR_UPDATE
);
162 # Don't save a new article if it's blank.
163 if ( ( '' == $this->textbox1
) ||
164 ( wfMsg( 'newarticletext' ) == $this->textbox1
) ) {
165 $wgOut->redirect( $this->mTitle
->getFullURL() );
168 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
172 # Article exists. Check for edit conflict.
174 $this->mArticle
->clear(); # Force reload of dates, etc.
175 $this->mArticle
->forUpdate( true ); # Lock the article
177 if( ( $this->section
!= 'new' ) &&
178 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
181 $userid = $wgUser->getID();
183 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
184 $this->section
, $this->textbox1
, $this->summary
);
185 # Suppress edit conflict with self
187 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
190 # switch from section editing to normal editing in edit conflict
193 if( $this->mergeChangesInto( $text ) ){
194 // Successful merge! Maybe we should tell the user the good news?
198 $this->textbox1
= $text;
202 if ( ! $isConflict ) {
205 if( $this->section
!= '' ) {
206 # Try to get a section anchor from the section source, redirect to edited section if header found
207 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
208 # for duplicate heading checking and maybe parsing
209 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
210 # we can't deal with anchors, includes, html etc in the header for now,
211 # headline would need to be parsed to improve this
212 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
213 if($hasmatch and strlen($matches[2]) > 0) {
214 global $wgInputEncoding;
215 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
217 $headline = preg_replace( "/<.*?" . ">/","",$headline );
218 $headline = trim( $headline );
219 $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
220 $replacearray = array(
224 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
228 # update the article here
229 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
235 # First time through: get contents, set time for conflict
238 if ( 'initial' == $formtype ) {
239 $this->edittime
= $this->mArticle
->getTimestamp();
240 $this->textbox1
= $this->mArticle
->getContent( true );
244 $wgOut->setRobotpolicy( 'noindex,nofollow' );
246 # Enabled article-related sidebar, toplinks, etc.
247 $wgOut->setArticleRelated( true );
250 $s = wfMsg( 'editconflict', $this->mTitle
->getPrefixedText() );
251 $wgOut->setPageTitle( $s );
252 $wgOut->addHTML( wfMsg( 'explainconflict' ) );
254 $this->textbox2
= $this->textbox1
;
255 $this->textbox1
= $this->mArticle
->getContent( true );
256 $this->edittime
= $this->mArticle
->getTimestamp();
258 $s = wfMsg( 'editing', $this->mTitle
->getPrefixedText() );
260 if( $this->section
!= '' ) {
261 if( $this->section
== 'new' ) {
262 $s.=wfMsg('commentedit');
264 $s.=wfMsg('sectionedit');
266 if(!$this->preview
) {
267 $sectitle=preg_match("/^=+(.*?)=+/mi",
270 if( !empty( $matches[1] ) ) {
271 $this->summary
= "/* ". trim($matches[1])." */ ";
275 $wgOut->setPageTitle( $s );
276 if ( $this->oldid
) {
277 $this->mArticle
->setOldSubtitle();
278 $wgOut->addHTML( wfMsg( 'editingold' ) );
283 $wgOut->addHTML( '<strong>' .
284 wfMsg( 'readonlywarning' ) .
286 } else if ( $isCssJsSubpage and 'preview' != $formtype) {
287 $wgOut->addHTML( wfMsg( 'usercssjsyoucanpreview' ));
289 if( $this->mTitle
->isProtected() ) {
290 $wgOut->addHTML( '<strong>' . wfMsg( 'protectedpagewarning' ) .
291 "</strong><br />\n" );
294 $kblength = (int)(strlen( $this->textbox1
) / 1024);
295 if( $kblength > 29 ) {
296 $wgOut->addHTML( '<strong>' .
297 wfMsg( 'longpagewarning', $wgLang->formatNum( $kblength ) )
301 $rows = $wgUser->getOption( 'rows' );
302 $cols = $wgUser->getOption( 'cols' );
304 $ew = $wgUser->getOption( 'editwidth' );
305 if ( $ew ) $ew = " style=\"width:100%\"";
308 $q = 'action=submit';
309 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
310 $action = $this->mTitle
->escapeLocalURL( $q );
312 $summary = wfMsg('summary');
313 $subject = wfMsg('subject');
314 $minor = wfMsg('minoredit');
315 $watchthis = wfMsg ('watchthis');
316 $save = wfMsg('savearticle');
317 $prev = wfMsg('showpreview');
319 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
321 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
322 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
323 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
324 htmlspecialchars( wfMsg( 'newwindow' ) );
326 global $wgRightsText;
327 $copywarn = "<div id=\"editpage-copywarn\">\n" .
328 wfMsg( $wgRightsText ?
'copyrightwarning' : 'copyrightwarning2',
329 '[[' . wfMsg( 'copyrightpage' ) . ']]',
330 $wgRightsText ) . "\n</div>";
332 if( $wgUser->getOption('showtoolbar') and !$isCssJsSubpage ) {
333 # prepare toolbar for edit buttons
334 $toolbar = $sk->getEditToolbar();
339 // activate checkboxes if user wants them to be always active
340 if( !$this->preview
) {
341 if( $wgUser->getOption( 'watchdefault' ) ) $this->watchthis
= true;
342 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit
= true;
344 // activate checkbox also if user is already watching the page,
345 // require wpWatchthis to be unset so that second condition is not
346 // checked unnecessarily
347 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
352 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
354 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
355 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
356 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
361 if ( 0 != $wgUser->getID() ) {
362 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
363 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
364 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
367 $checkboxhtml = $minoredithtml . $watchhtml . '<br />';
369 if ( 'preview' == $formtype) {
370 $previewhead='<h2>' . wfMsg( 'preview' ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
371 wfMsg( 'note' ) . wfMsg( 'previewnote' ) . "</font></center></p>\n";
373 $previewhead.='<h2>' . wfMsg( 'previewconflict' ) .
377 $parserOptions = ParserOptions
::newFromUser( $wgUser );
378 $parserOptions->setEditSection( false );
379 $parserOptions->setEditSectionOnRightClick( false );
381 # don't parse user css/js, show message about preview
382 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
384 if ( $isCssJsSubpage ) {
385 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
386 $previewtext = wfMsg('usercsspreview');
387 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
388 $previewtext = wfMsg('userjspreview');
390 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
391 $wgOut->addHTML( $parserOutput->mText
);
393 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
394 $wgTitle, $parserOptions );
395 $previewHTML = $parserOutput->mText
;
397 if($wgUser->getOption('previewontop')) {
398 $wgOut->addHTML($previewhead);
399 $wgOut->addHTML($previewHTML);
401 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
402 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
403 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
407 # if this is a comment, show a subject line at the top, which is also the edit summary.
408 # Otherwise, show a summary field at the bottom
409 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
410 if( $this->section
== 'new' ) {
411 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
414 $commentsubject = '';
415 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
418 if( !$this->preview
) {
419 # Don't select the edit box on preview; this interferes with seeing what's going on.
420 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
422 # Prepare a list of templates used by this page
423 $db =& wfGetDB( DB_SLAVE
);
424 $cur = $db->tableName( 'cur' );
425 $links = $db->tableName( 'links' );
426 $id = $this->mTitle
->getArticleID();
427 $sql = "SELECT cur_namespace,cur_title,cur_id ".
428 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
429 $res = $db->query( $sql, "EditPage::editform" );
431 if ( $db->numRows( $res ) ) {
432 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
433 while ( $row = $db->fetchObject( $res ) ) {
434 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
435 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
438 $templates .= '</ul>';
444 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
445 enctype=\"application/x-www-form-urlencoded\">
447 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
448 cols='{$cols}'{$ew}>" .
449 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
454 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
455 " title=\"".wfMsg('tooltip-save')."\"/>
456 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
457 " title=\"".wfMsg('tooltip-preview')."\"/>
458 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
459 $wgOut->addWikiText( $copywarn );
461 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
462 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
465 require_once( "DifferenceEngine.php" );
466 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
467 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
468 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
470 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
471 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
472 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
476 $wgOut->addHTML( "</form>\n" );
477 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
478 $wgOut->addHTML($previewhead);
479 $wgOut->addHTML($previewHTML);
483 function blockedIPpage()
485 global $wgOut, $wgUser, $wgLang, $wgIP;
487 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
488 $wgOut->setRobotpolicy( 'noindex,nofollow' );
489 $wgOut->setArticleRelated( false );
491 $id = $wgUser->blockedBy();
492 $reason = $wgUser->blockedFor();
495 if ( is_numeric( $id ) ) {
496 $name = User
::whoIs( $id );
500 $link = '[[' . $wgLang->getNsText( Namespace::getUser() ) .
501 ":{$name}|{$name}]]";
503 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
504 $wgOut->returnToMain( false );
509 function userNotLoggedInPage()
511 global $wgOut, $wgUser, $wgLang;
513 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
514 $wgOut->setRobotpolicy( 'noindex,nofollow' );
515 $wgOut->setArticleRelated( false );
517 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
518 $wgOut->returnToMain( false );
521 function spamPage ( $matches = array() )
524 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
525 $wgOut->setRobotpolicy( 'noindex,nofollow' );
526 $wgOut->setArticleRelated( false );
528 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
529 if ( isset ( $matches[0] ) ) {
530 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
532 $wgOut->returnToMain( false );
535 # Forks processes to scan the originating IP for an open proxy server
536 # MemCached can be used to skip IPs that have already been scanned
537 function proxyCheck()
539 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
540 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
542 if ( !$wgBlockOpenProxies ) {
548 if ( $wgUseMemCached ) {
549 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
550 $mcValue = $wgMemc->get( $mcKey );
558 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
559 $iphash = md5( $wgIP . $wgProxyKey );
560 $url = $title->getFullURL( 'ip='.$iphash );
562 foreach ( $wgProxyPorts as $port ) {
563 $params = implode( ' ', array(
564 escapeshellarg( $wgProxyScriptPath ),
565 escapeshellarg( $wgIP ),
566 escapeshellarg( $port ),
567 escapeshellarg( $url )
569 exec( "php $params &>/dev/null &" );
572 if ( $wgUseMemCached ) {
573 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
578 /* private */ function mergeChangesInto( &$text ){
579 $fname = 'EditPage::mergeChangesInto';
580 $oldDate = $this->edittime
;
581 $dbw =& wfGetDB( DB_MASTER
);
582 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
583 $fname, 'FOR UPDATE' );
585 $yourtext = $obj->cur_text
;
586 $ns = $this->mTitle
->getNamespace();
587 $title = $this->mTitle
->getDBkey();
588 $obj = $dbw->getArray( 'old',
589 array( 'old_text','old_flags'),
590 array( 'old_namespace' => $ns, 'old_title' => $title,
591 'old_timestamp' => $dbw->timestamp($oldDate)),
593 $oldText = Article
::getRevisionText( $obj );
595 if(wfMerge($oldText, $text, $yourtext, $result)){