3 * 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
22 var $save = false, $preview = false;
23 var $minoredit = false, $watchthis = false;
24 var $textbox1 = '', $textbox2 = '', $summary = '';
25 var $edittime = '', $section = '';
32 function EditPage( $article ) {
33 $this->mArticle
=& $article;
35 $this->mTitle
=& $wgTitle;
39 * This is the function that gets called for "action=edit".
42 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
43 // this is not an article
44 $wgOut->setArticleFlag(false);
46 $this->importFormData( $wgRequest );
48 if ( ! $this->mTitle
->userCanEdit() ) {
49 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
52 if ( $wgUser->isBlocked() ) {
53 $this->blockedIPpage();
56 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
57 $this->userNotLoggedInPage();
61 if( $this->save ||
$this->preview
) {
62 $this->editForm( 'preview' );
64 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
69 $this->editForm( 'save' );
70 } else if ( $this->preview
or $wgUser->getOption('previewonfirst')) {
71 $this->editForm( 'preview' );
72 } else { # First time through
73 $this->editForm( 'initial' );
80 function importFormData( &$request ) {
81 # These fields need to be checked for encoding.
82 # Also remove trailing whitespace, but don't remove _initial_
83 # whitespace from the text boxes. This may be significant formatting.
84 $this->textbox1
= rtrim( $request->getText( 'wpTextbox1' ) );
85 $this->textbox2
= rtrim( $request->getText( 'wpTextbox2' ) );
86 $this->summary
= trim( $request->getText( 'wpSummary' ) );
88 $this->edittime
= $request->getVal( 'wpEdittime' );
89 if( !preg_match( '/^\d{14}$/', $this->edittime
)) $this->edittime
= '';
91 $this->preview
= $request->getCheck( 'wpPreview' );
92 $this->save
= $request->wasPosted() && !$this->preview
;
93 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
94 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
96 $this->oldid
= $request->getInt( 'oldid' );
98 # Section edit can come from either the form or a link
99 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
103 * Since there is only one text field on the edit form,
104 * pressing <enter> will cause the form to be submitted, but
105 * the submit button value won't appear in the query, so we
106 * Fake it here before going back to edit(). This is kind of
107 * ugly, but it helps some old URLs to still work.
110 if( !$this->preview
) $this->save
= true;
116 * The edit form is self-submitting, so that when things like
117 * preview and edit conflicts occur, we get the same form back
118 * with the extra stuff added. Only when the final submission
119 * is made and all is well do we actually save and redirect to
120 * the newly-edited page.
122 * @param string $formtype Type of form either : save, initial or preview
124 function editForm( $formtype ) {
125 global $wgOut, $wgUser;
126 global $wgLang, $wgContLang, $wgParser, $wgTitle;
127 global $wgAllowAnonymousMinor;
128 global $wgWhitelistEdit;
129 global $wgSpamRegex, $wgFilterCallback;
132 $sk = $wgUser->getSkin();
134 // css / js subpages of user pages get a special treatment
135 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
137 if(!$this->mTitle
->getArticleID()) { # new article
138 $wgOut->addWikiText(wfmsg('newarticletext'));
141 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
142 $wgOut->addWikiText(wfmsg('talkpagetext'));
145 # Attempt submission here. This will check for edit conflicts,
146 # and redundantly check for locked database, blocked IPs, etc.
147 # that edit() already checked just in case someone tries to sneak
148 # in the back door with a hand-edited submission URL.
150 if ( 'save' == $formtype ) {
152 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
, $matches ) ) {
153 $this->spamPage ( $matches );
156 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle
, $this->textbox1
, $this->section
) ) {
157 # Error messages or other handling should be performed by the filter function
160 if ( $wgUser->isBlocked() ) {
161 $this->blockedIPpage();
164 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
165 $this->userNotLoggedInPage();
168 if ( wfReadOnly() ) {
169 $wgOut->readOnlyPage();
173 # If article is new, insert it.
174 $aid = $this->mTitle
->getArticleID( GAID_FOR_UPDATE
);
176 # Don't save a new article if it's blank.
177 if ( ( '' == $this->textbox1
) ||
178 ( wfMsg( 'newarticletext' ) == $this->textbox1
) ) {
179 $wgOut->redirect( $this->mTitle
->getFullURL() );
182 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
186 # Article exists. Check for edit conflict.
188 $this->mArticle
->clear(); # Force reload of dates, etc.
189 $this->mArticle
->forUpdate( true ); # Lock the article
191 if( ( $this->section
!= 'new' ) &&
192 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
195 $userid = $wgUser->getID();
198 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
199 $this->section
, $this->textbox1
, $this->summary
, $this->edittime
);
202 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
203 $this->section
, $this->textbox1
, $this->summary
);
205 # Suppress edit conflict with self
207 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
210 # switch from section editing to normal editing in edit conflict
213 if( $this->mergeChangesInto( $text ) ){
214 // Successful merge! Maybe we should tell the user the good news?
218 $this->textbox1
= $text;
222 if ( ! $isConflict ) {
225 if( $this->section
!= '' ) {
226 # Try to get a section anchor from the section source, redirect to edited section if header found
227 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
228 # for duplicate heading checking and maybe parsing
229 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
230 # we can't deal with anchors, includes, html etc in the header for now,
231 # headline would need to be parsed to improve this
232 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
233 if($hasmatch and strlen($matches[2]) > 0) {
234 global $wgInputEncoding;
235 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
237 $headline = preg_replace( "/<.*?" . ">/","",$headline );
238 $headline = trim( $headline );
239 $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
240 $replacearray = array(
244 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
248 # update the article here
249 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
255 # First time through: get contents, set time for conflict
258 if ( 'initial' == $formtype ) {
259 $this->edittime
= $this->mArticle
->getTimestamp();
260 $this->textbox1
= $this->mArticle
->getContent( true );
264 $wgOut->setRobotpolicy( 'noindex,nofollow' );
266 # Enabled article-related sidebar, toplinks, etc.
267 $wgOut->setArticleRelated( true );
270 $s = wfMsg( 'editconflict', $this->mTitle
->getPrefixedText() );
271 $wgOut->setPageTitle( $s );
272 $wgOut->addHTML( wfMsg( 'explainconflict' ) );
274 $this->textbox2
= $this->textbox1
;
275 $this->textbox1
= $this->mArticle
->getContent( true );
276 $this->edittime
= $this->mArticle
->getTimestamp();
278 $s = wfMsg( 'editing', $this->mTitle
->getPrefixedText() );
280 if( $this->section
!= '' ) {
281 if( $this->section
== 'new' ) {
282 $s.=wfMsg('commentedit');
284 $s.=wfMsg('sectionedit');
286 if(!$this->preview
) {
287 $sectitle=preg_match("/^=+(.*?)=+/mi",
290 if( !empty( $matches[1] ) ) {
291 $this->summary
= "/* ". trim($matches[1])." */ ";
295 $wgOut->setPageTitle( $s );
296 if ( !$wgUseLatin1 && !$this->checkUnicodeCompliantBrowser() ) {
297 $this->mArticle
->setOldSubtitle();
298 $wgOut->addWikiText( wfMsg( 'nonunicodebrowser') );
300 if ( $this->oldid
) {
301 $this->mArticle
->setOldSubtitle();
302 $wgOut->addHTML( wfMsg( 'editingold' ) );
307 $wgOut->addHTML( '<strong>' .
308 wfMsg( 'readonlywarning' ) .
310 } else if ( $isCssJsSubpage and 'preview' != $formtype) {
311 $wgOut->addHTML( wfMsg( 'usercssjsyoucanpreview' ));
313 if( $this->mTitle
->isProtected() ) {
314 $wgOut->addHTML( '<strong>' . wfMsg( 'protectedpagewarning' ) .
315 "</strong><br />\n" );
318 $kblength = (int)(strlen( $this->textbox1
) / 1024);
319 if( $kblength > 29 ) {
320 $wgOut->addHTML( '<strong>' .
321 wfMsg( 'longpagewarning', $wgLang->formatNum( $kblength ) )
325 $rows = $wgUser->getOption( 'rows' );
326 $cols = $wgUser->getOption( 'cols' );
328 $ew = $wgUser->getOption( 'editwidth' );
329 if ( $ew ) $ew = " style=\"width:100%\"";
332 $q = 'action=submit';
333 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
334 $action = $this->mTitle
->escapeLocalURL( $q );
336 $summary = wfMsg('summary');
337 $subject = wfMsg('subject');
338 $minor = wfMsg('minoredit');
339 $watchthis = wfMsg ('watchthis');
340 $save = wfMsg('savearticle');
341 $prev = wfMsg('showpreview');
343 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
345 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
346 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
347 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
348 htmlspecialchars( wfMsg( 'newwindow' ) );
350 global $wgRightsText;
351 $copywarn = "<div id=\"editpage-copywarn\">\n" .
352 wfMsg( $wgRightsText ?
'copyrightwarning' : 'copyrightwarning2',
353 '[[' . wfMsg( 'copyrightpage' ) . ']]',
354 $wgRightsText ) . "\n</div>";
356 if( $wgUser->getOption('showtoolbar') and !$isCssJsSubpage ) {
357 # prepare toolbar for edit buttons
358 $toolbar = $sk->getEditToolbar();
363 // activate checkboxes if user wants them to be always active
364 if( !$this->preview
) {
365 if( $wgUser->getOption( 'watchdefault' ) ) $this->watchthis
= true;
366 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit
= true;
368 // activate checkbox also if user is already watching the page,
369 // require wpWatchthis to be unset so that second condition is not
370 // checked unnecessarily
371 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
376 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
378 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
379 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
380 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
385 if ( 0 != $wgUser->getID() ) {
386 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
387 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
388 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
391 $checkboxhtml = $minoredithtml . $watchhtml . '<br />';
393 if ( 'preview' == $formtype) {
394 $previewhead='<h2>' . wfMsg( 'preview' ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
395 wfMsg( 'note' ) . wfMsg( 'previewnote' ) . "</font></center></p>\n";
397 $previewhead.='<h2>' . wfMsg( 'previewconflict' ) .
401 $parserOptions = ParserOptions
::newFromUser( $wgUser );
402 $parserOptions->setEditSection( false );
403 $parserOptions->setEditSectionOnRightClick( false );
405 # don't parse user css/js, show message about preview
406 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
408 if ( $isCssJsSubpage ) {
409 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
410 $previewtext = wfMsg('usercsspreview');
411 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
412 $previewtext = wfMsg('userjspreview');
414 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
415 $wgOut->addHTML( $parserOutput->mText
);
417 # if user want to see preview when he edit an article
418 if( $wgUser->getOption('previewonfirst') and ($this->textbox1
== '')) {
419 $this->textbox1
= $this->mArticle
->getContent(true);
422 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
423 $wgTitle, $parserOptions );
425 $previewHTML = $parserOutput->mText
;
427 if($wgUser->getOption('previewontop')) {
428 $wgOut->addHTML($previewhead);
429 $wgOut->addHTML($previewHTML);
431 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
432 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
433 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
437 # if this is a comment, show a subject line at the top, which is also the edit summary.
438 # Otherwise, show a summary field at the bottom
439 $summarytext = htmlspecialchars( $wgContLang->recodeForEdit( $this->summary
) ); # FIXME
440 if( $this->section
== 'new' ) {
441 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
444 $commentsubject = '';
445 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
448 if( !$this->preview
) {
449 # Don't select the edit box on preview; this interferes with seeing what's going on.
450 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
452 # Prepare a list of templates used by this page
453 $db =& wfGetDB( DB_SLAVE
);
454 $cur = $db->tableName( 'cur' );
455 $links = $db->tableName( 'links' );
456 $id = $this->mTitle
->getArticleID();
457 $sql = "SELECT cur_namespace,cur_title,cur_id ".
458 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
459 $res = $db->query( $sql, "EditPage::editform" );
461 if ( $db->numRows( $res ) ) {
462 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
463 while ( $row = $db->fetchObject( $res ) ) {
464 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
465 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
468 $templates .= '</ul>';
474 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
475 enctype=\"application/x-www-form-urlencoded\">
477 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
478 cols='{$cols}'{$ew}>" .
479 htmlspecialchars( $wgContLang->recodeForEdit( $this->textbox1
) ) .
484 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
485 " title=\"".wfMsg('tooltip-save')."\"/>
486 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
487 " title=\"".wfMsg('tooltip-preview')."\"/>
488 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
489 $wgOut->addWikiText( $copywarn );
491 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
492 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
495 require_once( "DifferenceEngine.php" );
496 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
497 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
498 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
500 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
501 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
502 . htmlspecialchars( $wgContLang->recodeForEdit( $this->textbox2
) ) .
506 $wgOut->addHTML( "</form>\n" );
507 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
508 $wgOut->addHTML($previewhead);
509 $wgOut->addHTML($previewHTML);
516 function blockedIPpage() {
517 global $wgOut, $wgUser, $wgContLang, $wgIP;
519 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
520 $wgOut->setRobotpolicy( 'noindex,nofollow' );
521 $wgOut->setArticleRelated( false );
523 $id = $wgUser->blockedBy();
524 $reason = $wgUser->blockedFor();
527 if ( is_numeric( $id ) ) {
528 $name = User
::whoIs( $id );
532 $link = '[[' . $wgContLang->getNsText( Namespace::getUser() ) .
533 ":{$name}|{$name}]]";
535 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
536 $wgOut->returnToMain( false );
542 function userNotLoggedInPage() {
543 global $wgOut, $wgUser;
545 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
546 $wgOut->setRobotpolicy( 'noindex,nofollow' );
547 $wgOut->setArticleRelated( false );
549 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
550 $wgOut->returnToMain( false );
556 function spamPage ( $matches = array() )
559 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
560 $wgOut->setRobotpolicy( 'noindex,nofollow' );
561 $wgOut->setArticleRelated( false );
563 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
564 if ( isset ( $matches[0] ) ) {
565 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
567 $wgOut->returnToMain( false );
571 * Forks processes to scan the originating IP for an open proxy server
572 * MemCached can be used to skip IPs that have already been scanned
574 function proxyCheck() {
575 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
576 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
578 if ( !$wgBlockOpenProxies ) {
584 if ( $wgUseMemCached ) {
585 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
586 $mcValue = $wgMemc->get( $mcKey );
594 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
595 $iphash = md5( $wgIP . $wgProxyKey );
596 $url = $title->getFullURL( 'ip='.$iphash );
598 foreach ( $wgProxyPorts as $port ) {
599 $params = implode( ' ', array(
600 escapeshellarg( $wgProxyScriptPath ),
601 escapeshellarg( $wgIP ),
602 escapeshellarg( $port ),
603 escapeshellarg( $url )
605 exec( "php $params &>/dev/null &" );
608 if ( $wgUseMemCached ) {
609 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
618 function mergeChangesInto( &$text ){
619 $fname = 'EditPage::mergeChangesInto';
620 $oldDate = $this->edittime
;
621 $dbw =& wfGetDB( DB_MASTER
);
622 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
623 $fname, 'FOR UPDATE' );
625 $yourtext = $obj->cur_text
;
626 $ns = $this->mTitle
->getNamespace();
627 $title = $this->mTitle
->getDBkey();
628 $obj = $dbw->getArray( 'old',
629 array( 'old_text','old_flags'),
630 array( 'old_namespace' => $ns, 'old_title' => $title,
631 'old_timestamp' => $dbw->timestamp($oldDate)),
633 $oldText = Article
::getRevisionText( $obj );
635 if(wfMerge($oldText, $text, $yourtext, $result)){
644 function checkUnicodeCompliantBrowser() {
645 global $wgBrowserBlackList;
646 $currentbrowser = $_SERVER["HTTP_USER_AGENT"];
647 foreach ( $wgBrowserBlackList as $browser ) {
648 if ( preg_match($browser, $currentbrowser) ) {