f1083b4bd36b75fd0261889d806909d25bf47ac0
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 = '';
28 function EditPage( $article ) {
29 $this->mArticle
=& $article;
31 $this->mTitle
=& $wgTitle;
35 * This is the function that gets called for "action=edit".
38 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
39 // this is not an article
40 $wgOut->setArticleFlag(false);
42 $this->importFormData( $wgRequest );
44 if ( ! $this->mTitle
->userCanEdit() ) {
45 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
48 if ( $wgUser->isBlocked() ) {
49 $this->blockedIPpage();
52 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
53 $this->userNotLoggedInPage();
57 if( $this->save ||
$this->preview
) {
58 $this->editForm( 'preview' );
60 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
65 $this->editForm( 'save' );
66 } else if ( $this->preview
) {
67 $this->editForm( 'preview' );
68 } else { # First time through
69 $this->editForm( 'initial' );
76 function importFormData( &$request ) {
77 # These fields need to be checked for encoding.
78 # Also remove trailing whitespace, but don't remove _initial_
79 # whitespace from the text boxes. This may be significant formatting.
80 $this->textbox1
= rtrim( $request->getText( 'wpTextbox1' ) );
81 $this->textbox2
= rtrim( $request->getText( 'wpTextbox2' ) );
82 $this->summary
= trim( $request->getText( 'wpSummary' ) );
84 $this->edittime
= $request->getVal( 'wpEdittime' );
85 if( !preg_match( '/^\d{14}$/', $this->edittime
)) $this->edittime
= '';
87 $this->preview
= $request->getCheck( 'wpPreview' );
88 $this->save
= $request->wasPosted() && !$this->preview
;
89 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
90 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
92 $this->oldid
= $request->getInt( 'oldid' );
94 # Section edit can come from either the form or a link
95 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
99 * Since there is only one text field on the edit form,
100 * pressing <enter> will cause the form to be submitted, but
101 * the submit button value won't appear in the query, so we
102 * Fake it here before going back to edit(). This is kind of
103 * ugly, but it helps some old URLs to still work.
106 if( !$this->preview
) $this->save
= true;
112 * The edit form is self-submitting, so that when things like
113 * preview and edit conflicts occur, we get the same form back
114 * with the extra stuff added. Only when the final submission
115 * is made and all is well do we actually save and redirect to
116 * the newly-edited page.
118 * @param string $formtype Type of form either : save, initial or preview
120 function editForm( $formtype ) {
121 global $wgOut, $wgUser;
122 global $wgLang, $wgParser, $wgTitle;
123 global $wgAllowAnonymousMinor;
124 global $wgWhitelistEdit;
125 global $wgSpamRegex, $wgFilterCallback;
127 $sk = $wgUser->getSkin();
129 // css / js subpages of user pages get a special treatment
130 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
132 if(!$this->mTitle
->getArticleID()) { # new article
133 $wgOut->addWikiText(wfmsg('newarticletext'));
136 if( Namespace::isTalk( $this->mTitle
->getNamespace() ) ) {
137 $wgOut->addWikiText(wfmsg('talkpagetext'));
140 # Attempt submission here. This will check for edit conflicts,
141 # and redundantly check for locked database, blocked IPs, etc.
142 # that edit() already checked just in case someone tries to sneak
143 # in the back door with a hand-edited submission URL.
145 if ( 'save' == $formtype ) {
147 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
, $matches ) ) {
148 $this->spamPage ( $matches );
151 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle
, $this->textbox1
, $this->section
) ) {
152 # Error messages or other handling should be performed by the filter function
155 if ( $wgUser->isBlocked() ) {
156 $this->blockedIPpage();
159 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
160 $this->userNotLoggedInPage();
163 if ( wfReadOnly() ) {
164 $wgOut->readOnlyPage();
168 # If article is new, insert it.
169 $aid = $this->mTitle
->getArticleID( GAID_FOR_UPDATE
);
171 # Don't save a new article if it's blank.
172 if ( ( '' == $this->textbox1
) ||
173 ( wfMsg( 'newarticletext' ) == $this->textbox1
) ) {
174 $wgOut->redirect( $this->mTitle
->getFullURL() );
177 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
181 # Article exists. Check for edit conflict.
183 $this->mArticle
->clear(); # Force reload of dates, etc.
184 $this->mArticle
->forUpdate( true ); # Lock the article
186 if( ( $this->section
!= 'new' ) &&
187 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
190 $userid = $wgUser->getID();
192 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
193 $this->section
, $this->textbox1
, $this->summary
);
194 # Suppress edit conflict with self
196 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
199 # switch from section editing to normal editing in edit conflict
202 if( $this->mergeChangesInto( $text ) ){
203 // Successful merge! Maybe we should tell the user the good news?
207 $this->textbox1
= $text;
211 if ( ! $isConflict ) {
214 if( $this->section
!= '' ) {
215 # Try to get a section anchor from the section source, redirect to edited section if header found
216 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
217 # for duplicate heading checking and maybe parsing
218 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
219 # we can't deal with anchors, includes, html etc in the header for now,
220 # headline would need to be parsed to improve this
221 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
222 if($hasmatch and strlen($matches[2]) > 0) {
223 global $wgInputEncoding;
224 $headline = do_html_entity_decode( $matches[2], ENT_COMPAT
, $wgInputEncoding );
226 $headline = preg_replace( "/<.*?" . ">/","",$headline );
227 $headline = trim( $headline );
228 $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
229 $replacearray = array(
233 $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
237 # update the article here
238 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
, $this->watchthis
, '', $sectionanchor ))
244 # First time through: get contents, set time for conflict
247 if ( 'initial' == $formtype ) {
248 $this->edittime
= $this->mArticle
->getTimestamp();
249 $this->textbox1
= $this->mArticle
->getContent( true );
253 $wgOut->setRobotpolicy( 'noindex,nofollow' );
255 # Enabled article-related sidebar, toplinks, etc.
256 $wgOut->setArticleRelated( true );
259 $s = wfMsg( 'editconflict', $this->mTitle
->getPrefixedText() );
260 $wgOut->setPageTitle( $s );
261 $wgOut->addHTML( wfMsg( 'explainconflict' ) );
263 $this->textbox2
= $this->textbox1
;
264 $this->textbox1
= $this->mArticle
->getContent( true );
265 $this->edittime
= $this->mArticle
->getTimestamp();
267 $s = wfMsg( 'editing', $this->mTitle
->getPrefixedText() );
269 if( $this->section
!= '' ) {
270 if( $this->section
== 'new' ) {
271 $s.=wfMsg('commentedit');
273 $s.=wfMsg('sectionedit');
275 if(!$this->preview
) {
276 $sectitle=preg_match("/^=+(.*?)=+/mi",
279 if( !empty( $matches[1] ) ) {
280 $this->summary
= "/* ". trim($matches[1])." */ ";
284 $wgOut->setPageTitle( $s );
285 if ( $this->oldid
) {
286 $this->mArticle
->setOldSubtitle();
287 $wgOut->addHTML( wfMsg( 'editingold' ) );
292 $wgOut->addHTML( '<strong>' .
293 wfMsg( 'readonlywarning' ) .
295 } else if ( $isCssJsSubpage and 'preview' != $formtype) {
296 $wgOut->addHTML( wfMsg( 'usercssjsyoucanpreview' ));
298 if( $this->mTitle
->isProtected() ) {
299 $wgOut->addHTML( '<strong>' . wfMsg( 'protectedpagewarning' ) .
300 "</strong><br />\n" );
303 $kblength = (int)(strlen( $this->textbox1
) / 1024);
304 if( $kblength > 29 ) {
305 $wgOut->addHTML( '<strong>' .
306 wfMsg( 'longpagewarning', $wgLang->formatNum( $kblength ) )
310 $rows = $wgUser->getOption( 'rows' );
311 $cols = $wgUser->getOption( 'cols' );
313 $ew = $wgUser->getOption( 'editwidth' );
314 if ( $ew ) $ew = " style=\"width:100%\"";
317 $q = 'action=submit';
318 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
319 $action = $this->mTitle
->escapeLocalURL( $q );
321 $summary = wfMsg('summary');
322 $subject = wfMsg('subject');
323 $minor = wfMsg('minoredit');
324 $watchthis = wfMsg ('watchthis');
325 $save = wfMsg('savearticle');
326 $prev = wfMsg('showpreview');
328 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
330 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
331 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
332 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
333 htmlspecialchars( wfMsg( 'newwindow' ) );
335 global $wgRightsText;
336 $copywarn = "<div id=\"editpage-copywarn\">\n" .
337 wfMsg( $wgRightsText ?
'copyrightwarning' : 'copyrightwarning2',
338 '[[' . wfMsg( 'copyrightpage' ) . ']]',
339 $wgRightsText ) . "\n</div>";
341 if( $wgUser->getOption('showtoolbar') and !$isCssJsSubpage ) {
342 # prepare toolbar for edit buttons
343 $toolbar = $sk->getEditToolbar();
348 // activate checkboxes if user wants them to be always active
349 if( !$this->preview
) {
350 if( $wgUser->getOption( 'watchdefault' ) ) $this->watchthis
= true;
351 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit
= true;
353 // activate checkbox also if user is already watching the page,
354 // require wpWatchthis to be unset so that second condition is not
355 // checked unnecessarily
356 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
361 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
363 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
364 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
365 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
370 if ( 0 != $wgUser->getID() ) {
371 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
372 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
373 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
376 $checkboxhtml = $minoredithtml . $watchhtml . '<br />';
378 if ( 'preview' == $formtype) {
379 $previewhead='<h2>' . wfMsg( 'preview' ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
380 wfMsg( 'note' ) . wfMsg( 'previewnote' ) . "</font></center></p>\n";
382 $previewhead.='<h2>' . wfMsg( 'previewconflict' ) .
386 $parserOptions = ParserOptions
::newFromUser( $wgUser );
387 $parserOptions->setEditSection( false );
388 $parserOptions->setEditSectionOnRightClick( false );
390 # don't parse user css/js, show message about preview
391 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
393 if ( $isCssJsSubpage ) {
394 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
395 $previewtext = wfMsg('usercsspreview');
396 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
397 $previewtext = wfMsg('userjspreview');
399 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
400 $wgOut->addHTML( $parserOutput->mText
);
402 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $this->textbox1
) ."\n\n",
403 $wgTitle, $parserOptions );
404 $previewHTML = $parserOutput->mText
;
406 if($wgUser->getOption('previewontop')) {
407 $wgOut->addHTML($previewhead);
408 $wgOut->addHTML($previewHTML);
410 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
411 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
412 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
416 # if this is a comment, show a subject line at the top, which is also the edit summary.
417 # Otherwise, show a summary field at the bottom
418 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
419 if( $this->section
== 'new' ) {
420 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
423 $commentsubject = '';
424 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
427 if( !$this->preview
) {
428 # Don't select the edit box on preview; this interferes with seeing what's going on.
429 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
431 # Prepare a list of templates used by this page
432 $db =& wfGetDB( DB_SLAVE
);
433 $cur = $db->tableName( 'cur' );
434 $links = $db->tableName( 'links' );
435 $id = $this->mTitle
->getArticleID();
436 $sql = "SELECT cur_namespace,cur_title,cur_id ".
437 "FROM $cur,$links WHERE l_to=cur_id AND l_from={$id} and cur_namespace=".NS_TEMPLATE
;
438 $res = $db->query( $sql, "EditPage::editform" );
440 if ( $db->numRows( $res ) ) {
441 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
442 while ( $row = $db->fetchObject( $res ) ) {
443 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
444 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
447 $templates .= '</ul>';
453 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
454 enctype=\"application/x-www-form-urlencoded\">
456 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
457 cols='{$cols}'{$ew}>" .
458 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
463 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
464 " title=\"".wfMsg('tooltip-save')."\"/>
465 <input tabindex='6' id='wpPreview' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
466 " title=\"".wfMsg('tooltip-preview')."\"/>
467 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
468 $wgOut->addWikiText( $copywarn );
470 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
471 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
474 require_once( "DifferenceEngine.php" );
475 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
476 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
477 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
479 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
480 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
481 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2
) ) .
485 $wgOut->addHTML( "</form>\n" );
486 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
487 $wgOut->addHTML($previewhead);
488 $wgOut->addHTML($previewHTML);
495 function blockedIPpage() {
496 global $wgOut, $wgUser, $wgLang, $wgIP;
498 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
499 $wgOut->setRobotpolicy( 'noindex,nofollow' );
500 $wgOut->setArticleRelated( false );
502 $id = $wgUser->blockedBy();
503 $reason = $wgUser->blockedFor();
506 if ( is_numeric( $id ) ) {
507 $name = User
::whoIs( $id );
511 $link = '[[' . $wgLang->getNsText( Namespace::getUser() ) .
512 ":{$name}|{$name}]]";
514 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
515 $wgOut->returnToMain( false );
521 function userNotLoggedInPage() {
522 global $wgOut, $wgUser, $wgLang;
524 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
525 $wgOut->setRobotpolicy( 'noindex,nofollow' );
526 $wgOut->setArticleRelated( false );
528 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
529 $wgOut->returnToMain( false );
535 function spamPage ( $matches = array() )
538 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
539 $wgOut->setRobotpolicy( 'noindex,nofollow' );
540 $wgOut->setArticleRelated( false );
542 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
543 if ( isset ( $matches[0] ) ) {
544 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$matches[0]}</nowiki>" ) );
546 $wgOut->returnToMain( false );
550 * Forks processes to scan the originating IP for an open proxy server
551 * MemCached can be used to skip IPs that have already been scanned
553 function proxyCheck() {
554 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
555 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
557 if ( !$wgBlockOpenProxies ) {
563 if ( $wgUseMemCached ) {
564 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
565 $mcValue = $wgMemc->get( $mcKey );
573 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
574 $iphash = md5( $wgIP . $wgProxyKey );
575 $url = $title->getFullURL( 'ip='.$iphash );
577 foreach ( $wgProxyPorts as $port ) {
578 $params = implode( ' ', array(
579 escapeshellarg( $wgProxyScriptPath ),
580 escapeshellarg( $wgIP ),
581 escapeshellarg( $port ),
582 escapeshellarg( $url )
584 exec( "php $params &>/dev/null &" );
587 if ( $wgUseMemCached ) {
588 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
597 function mergeChangesInto( &$text ){
598 $fname = 'EditPage::mergeChangesInto';
599 $oldDate = $this->edittime
;
600 $dbw =& wfGetDB( DB_MASTER
);
601 $obj = $dbw->getArray( 'cur', array( 'cur_text' ), array( 'cur_id' => $this->mTitle
->getArticleID() ),
602 $fname, 'FOR UPDATE' );
604 $yourtext = $obj->cur_text
;
605 $ns = $this->mTitle
->getNamespace();
606 $title = $this->mTitle
->getDBkey();
607 $obj = $dbw->getArray( 'old',
608 array( 'old_text','old_flags'),
609 array( 'old_namespace' => $ns, 'old_title' => $title,
610 'old_timestamp' => $dbw->timestamp($oldDate)),
612 $oldText = Article
::getRevisionText( $obj );
614 if(wfMerge($oldText, $text, $yourtext, $result)){