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
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
or $wgUser->getOption('previewonfirst')) {
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 # if user want to see preview when he edit an article
406 if( $wgUser->getOption('previewonfirst') and ($this->textbox1
== '')) {
407 $this->textbox1
= $this->mArticle
->getContent(true);
410 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
411 $wgTitle, $parserOptions );
413 $previewHTML = $parserOutput->mText
;
415 if($wgUser->getOption('previewontop')) {
416 $wgOut->addHTML($previewhead);
417 $wgOut->addHTML($previewHTML);
419 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
420 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
421 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
425 # if this is a comment, show a subject line at the top, which is also the edit summary.
426 # Otherwise, show a summary field at the bottom
427 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
428 if( $this->section
== 'new' ) {
429 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
432 $commentsubject = '';
433 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
436 if( !$this->preview
) {
437 # Don't select the edit box on preview; this interferes with seeing what's going on.
438 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
440 # Prepare a list of templates used by this page
441 $db =& wfGetDB( DB_SLAVE
);
442 $cur = $db->tableName( 'cur' );
443 $links = $db->tableName( 'links' );
444 $id = $this->mTitle
->getArticleID();
445 $sql = "SELECT cur_namespace,cur_title,cur_id ".
446 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
447 $res = $db->query( $sql, "EditPage::editform" );
449 if ( $db->numRows( $res ) ) {
450 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
451 while ( $row = $db->fetchObject( $res ) ) {
452 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
453 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
456 $templates .= '</ul>';
462 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
463 enctype=\"application/x-www-form-urlencoded\">
465 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
466 cols='{$cols}'{$ew}>" .
467 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
472 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
473 " title=\"".wfMsg('tooltip-save')."\"/>
474 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
475 " title=\"".wfMsg('tooltip-preview')."\"/>
476 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
477 $wgOut->addWikiText( $copywarn );
479 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
480 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
483 require_once( "DifferenceEngine.php" );
484 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
485 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
486 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
488 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
489 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
490 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
494 $wgOut->addHTML( "</form>\n" );
495 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
496 $wgOut->addHTML($previewhead);
497 $wgOut->addHTML($previewHTML);
504 function blockedIPpage() {
505 global $wgOut, $wgUser, $wgLang, $wgIP;
507 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
508 $wgOut->setRobotpolicy( 'noindex,nofollow' );
509 $wgOut->setArticleRelated( false );
511 $id = $wgUser->blockedBy();
512 $reason = $wgUser->blockedFor();
515 if ( is_numeric( $id ) ) {
516 $name = User
::whoIs( $id );
520 $link = '[[' . $wgLang->getNsText( Namespace::getUser() ) .
521 ":{$name}|{$name}]]";
523 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
524 $wgOut->returnToMain( false );
530 function userNotLoggedInPage() {
531 global $wgOut, $wgUser, $wgLang;
533 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
534 $wgOut->setRobotpolicy( 'noindex,nofollow' );
535 $wgOut->setArticleRelated( false );
537 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
538 $wgOut->returnToMain( false );
544 function spamPage ( $matches = array() )
547 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
548 $wgOut->setRobotpolicy( 'noindex,nofollow' );
549 $wgOut->setArticleRelated( false );
551 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
552 if ( isset ( $matches[0] ) ) {
553 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
555 $wgOut->returnToMain( false );
559 * Forks processes to scan the originating IP for an open proxy server
560 * MemCached can be used to skip IPs that have already been scanned
562 function proxyCheck() {
563 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
564 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
566 if ( !$wgBlockOpenProxies ) {
572 if ( $wgUseMemCached ) {
573 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
574 $mcValue = $wgMemc->get( $mcKey );
582 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
583 $iphash = md5( $wgIP . $wgProxyKey );
584 $url = $title->getFullURL( 'ip='.$iphash );
586 foreach ( $wgProxyPorts as $port ) {
587 $params = implode( ' ', array(
588 escapeshellarg( $wgProxyScriptPath ),
589 escapeshellarg( $wgIP ),
590 escapeshellarg( $port ),
591 escapeshellarg( $url )
593 exec( "php $params &>/dev/null &" );
596 if ( $wgUseMemCached ) {
597 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
606 function mergeChangesInto( &$text ){
607 $fname = 'EditPage::mergeChangesInto';
608 $oldDate = $this->edittime
;
609 $dbw =& wfGetDB( DB_MASTER
);
610 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
611 $fname, 'FOR UPDATE' );
613 $yourtext = $obj->cur_text
;
614 $ns = $this->mTitle
->getNamespace();
615 $title = $this->mTitle
->getDBkey();
616 $obj = $dbw->getArray( 'old',
617 array( 'old_text','old_flags'),
618 array( 'old_namespace' => $ns, 'old_title' => $title,
619 'old_timestamp' => $dbw->timestamp($oldDate)),
621 $oldText = Article
::getRevisionText( $obj );
623 if(wfMerge($oldText, $text, $yourtext, $result)){