* (bug 10181) Support the XCache object caching mechanism [patch from Kurt Radwanski]
[lhc/web/wiklou.git] / includes / EditPage.php
index a515862..888726c 100644 (file)
@@ -1,16 +1,14 @@
 <?php
 /**
- * Contain the EditPage class
+ * Contains the EditPage class
  */
 
 /**
- * Splitting edit page/HTML interface from Article...
+ * The edit page/HTML interface (split from Article)
  * The actual database and text munging is still in Article,
  * but it should get easier to call those from alternate
  * interfaces.
- *
  */
-
 class EditPage {
        var $mArticle;
        var $mTitle;
@@ -67,12 +65,13 @@ class EditPage {
        /**
         * Fetch initial editing page content.
         */
-       private function getContent() {
+       private function getContent( $def_text = '' ) {
                global $wgOut, $wgRequest, $wgParser;
 
                # Get variables from query string :P
                $section = $wgRequest->getVal( 'section' );
                $preload = $wgRequest->getVal( 'preload' );
+               $undoafter = $wgRequest->getVal( 'undoafter' );
                $undo = $wgRequest->getVal( 'undo' );
 
                wfProfileIn( __METHOD__ );
@@ -98,44 +97,56 @@ class EditPage {
 
                        $text = $this->mArticle->getContent();
 
-                       if ( $undo > 0 ) {
-                               #Undoing a specific edit overrides section editing; section-editing
+                       if ( $undo > 0 && $undo > $undoafter ) {
+                               # Undoing a specific edit overrides section editing; section-editing
                                # doesn't work with undoing.
-                               $undorev = Revision::newFromId($undo);
+                               if ( $undoafter ) {
+                                       $undorev = Revision::newFromId($undo);
+                                       $oldrev = Revision::newFromId($undoafter);
+                               } else {
+                                       $undorev = Revision::newFromId($undo);
+                                       $oldrev = $undorev ? $undorev->getPrevious() : null;
+                               }
 
                                #Sanity check, make sure it's the right page.
                                # Otherwise, $text will be left as-is.
-                               if (!is_null($undorev) && $undorev->getPage() == $this->mArticle->getID()) {
-                                       $oldrev = $undorev->getPrevious();
+                               if ( !is_null($undorev) && !is_null($oldrev) && $undorev->getPage()==$oldrev->getPage() && $undorev->getPage()==$this->mArticle->getID() ) {
                                        $undorev_text = $undorev->getText();
                                        $oldrev_text = $oldrev->getText();
                                        $currev_text = $text;
 
                                        #No use doing a merge if it's just a straight revert.
-                                       if ($currev_text != $undorev_text) {
+                                       if ( $currev_text != $undorev_text ) {
                                                $result = wfMerge($undorev_text, $oldrev_text, $currev_text, $text);
                                        } else {
                                                $text = $oldrev_text;
                                                $result = true;
                                        }
+                               } else {
+                                       // Failed basic sanity checks.
+                                       // Older revisions may have been removed since the link
+                                       // was created, or we may simply have got bogus input.
+                                       $result = false;
+                               }
 
-                                       if( $result ) {
-                                               # Inform the user of our success and set an automatic edit summary
-                                               $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-success' ) );
-                                               $this->summary = wfMsgForContent( 'undo-summary', $undo, $undorev->getUserText() );
-                                               $this->formtype = 'diff';
-                                       } else {
-                                               # Warn the user that something went wrong
-                                               $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-failure' ) );
+                               if( $result ) {
+                                       # Inform the user of our success and set an automatic edit summary
+                                       $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-success' ) );
+                                       $firstrev = $oldrev->getNext();
+                                       # If we just undid one rev, use an autosummary
+                                       if ( $firstrev->mId == $undo ) {
+                                               $this->summary = wfMsgForContent('undo-summary', $undo, $undorev->getUserText());
                                        }
-
+                                       $this->formtype = 'diff';
+                               } else {
+                                       # Warn the user that something went wrong
+                                       $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-failure' ) );
                                }
-                       }
-                       else if( $section != '' ) {
+                       } else if( $section != '' ) {
                                if( $section == 'new' ) {
                                        $text = $this->getPreloadedText( $preload );
                                } else {
-                                       $text = $wgParser->getSection( $text, $section );
+                                       $text = $wgParser->getSection( $text, $section, $def_text );
                                }
                        }
                }
@@ -283,7 +294,7 @@ class EditPage {
                global $wgOut, $wgUser, $wgRequest, $wgTitle;
                global $wgEmailConfirmToEdit;
 
-               if ( ! wfRunHooks( 'AlternateEdit', array( &$this  ) ) )
+               if ( ! wfRunHooks( 'AlternateEdit', array( &$this ) ) )
                        return;
 
                $fname = 'EditPage::edit';
@@ -422,7 +433,12 @@ class EditPage {
                # First time through: get contents, set time for conflict
                # checking, etc.
                if ( 'initial' == $this->formtype || $this->firsttime ) {
-                       $this->initialiseForm();
+                       if ($this->initialiseForm() === false) {
+                               $this->noSuchSectionPage();
+                               wfProfileOut( "$fname-business-end" );
+                               wfProfileOut( $fname );
+                               return;
+                       }
                        if( !$this->mTitle->getArticleId() ) 
                                wfRunHooks( 'EditFormPreloadText', array( &$this->textbox1, &$this->mTitle ) );
                }
@@ -588,6 +604,8 @@ class EditPage {
                                $wgOut->addWikiText( wfMsg( 'newarticletext' ) );
                        else
                                $wgOut->addWikiText( wfMsg( 'newarticletextanon' ) );
+                               # Let the user know about previous deletions if applicable
+                               $this->showDeletionLog( $wgOut );
                }
        }
 
@@ -724,6 +742,8 @@ class EditPage {
                $this->mArticle->clear(); # Force reload of dates, etc.
                $this->mArticle->forUpdate( true ); # Lock the article
 
+               wfDebug("timestamp: {$this->mArticle->getTimestamp()}, edittime: {$this->edittime}\n");
+
                if( $this->mArticle->getTimestamp() != $this->edittime ) {
                        $this->isConflict = true;
                        if( $this->section == 'new' ) {
@@ -795,7 +815,7 @@ class EditPage {
                }
 
                #And a similar thing for new sections
-                if( $this->section == 'new' && !$this->allowBlankSummary && $wgUser->getOption( 'forceeditsummary' ) ) {
+               if( $this->section == 'new' && !$this->allowBlankSummary && $wgUser->getOption( 'forceeditsummary' ) ) {
                        if (trim($this->summary) == '') {
                                $this->missingSummary = true;
                                wfProfileOut( $fname );
@@ -861,10 +881,13 @@ class EditPage {
        function initialiseForm() {
                $this->edittime = $this->mArticle->getTimestamp();
                $this->summary = '';
-               $this->textbox1 = $this->getContent();
+               $this->textbox1 = $this->getContent(false);
+               if ($this->textbox1 === false) return false;
+
                if ( !$this->mArticle->exists() && $this->mArticle->mTitle->getNamespace() == NS_MEDIAWIKI )
                        $this->textbox1 = wfMsgWeirdKey( $this->mArticle->mTitle->getText() );
                wfProxyCheck();
+               return true;
        }
 
        /**
@@ -926,9 +949,9 @@ class EditPage {
                                $wgOut->addWikiText( wfMsg( 'missingsummary' ) );
                        }
 
-                        if( $this->missingSummary && $this->section == 'new' ) {
-                                $wgOut->addWikiText( wfMsg( 'missingcommentheader' ) );
-                        }
+                       if( $this->missingSummary && $this->section == 'new' ) {
+                               $wgOut->addWikiText( wfMsg( 'missingcommentheader' ) );
+                       }
 
                        if( !$this->hookError == '' ) {
                                $wgOut->addWikiText( $this->hookError );
@@ -974,20 +997,22 @@ class EditPage {
                                if( wfEmptyMsg( 'semiprotectedpagewarning', $notice ) || $notice == '-' )
                                        $notice = '';
                        } else {
-                               # It's either cascading protection or regular protection; work out which
-                               $cascadeSources = $this->mTitle->getCascadeProtectionSources();
-                               if( $cascadeSources && count( $cascadeSources ) > 0 ) {
-                                       # Cascading protection; explain, and list the titles responsible
-                                       $notice = wfMsg( 'cascadeprotectedwarning' ) . "\n";
-                                       foreach( $cascadeSources as $source )
-                                               $notice .= '* [[:' . $source->getPrefixedText() . "]]\n";
-                               } else {
-                                       # Regular protection
-                                       $notice = wfMsg( 'protectedpagewarning' );
-                               }
+                       # Then it must be protected based on static groups (regular)
+                               $notice = wfMsg( 'protectedpagewarning' );
                        }
                        $wgOut->addWikiText( $notice );
                }
+               if ( $this->mTitle->isCascadeProtected() ) {
+                       # Is this page under cascading protection from some source pages?
+                       list($cascadeSources, /* $restrictions */) = $this->mTitle->getCascadeProtectionSources();
+                       if ( count($cascadeSources) > 0 ) {
+                               # Explain, and list the titles responsible
+                               $notice = wfMsgExt( 'cascadeprotectedwarning', array('parsemag'), count($cascadeSources) ) . "\n";
+                               foreach( $cascadeSources as $id => $page )
+                                       $notice .= '* [[:' . $page->getPrefixedText() . "]]\n";
+                               }
+                       $wgOut->addWikiText( $notice );
+               }
 
                if ( $this->kblength === false ) {
                        $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
@@ -1066,6 +1091,7 @@ class EditPage {
                        }
 
                        if ( 'diff' == $this->formtype ) {
+                               $wgOut->addStyle( 'common/diff.css' );
                                $wgOut->addHTML( $this->getDiff() );
                        }
                }
@@ -1092,7 +1118,7 @@ class EditPage {
                if( !$this->preview && !$this->diff ) {
                        $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
                }
-               $templates = ($this->preview || $this->section) ? $this->mPreviewTemplates : $this->mArticle->getUsedTemplates();
+               $templates = ($this->preview || $this->section != '') ? $this->mPreviewTemplates : $this->mArticle->getUsedTemplates();
                $formattedtemplates = $sk->formatTemplates( $templates, $this->preview, $this->section != '');
 
                global $wgUseMetadataEdit ;
@@ -1144,6 +1170,8 @@ END
                        call_user_func_array( $formCallback, array( &$wgOut ) );
                }
 
+               wfRunHooks( 'EditPage::showEditForm:fields', array( &$this, &$wgOut ) );
+
                // Put these up at the top to ensure they aren't lost on early form submission
                $wgOut->addHTML( "
 <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
@@ -1246,6 +1274,7 @@ END
                        }
 
                        if ( $this->formtype == 'diff') {
+                               $wgOut->addStyle( 'common/diff.css' );
                                $wgOut->addHTML( $this->getDiff() );
                        }
 
@@ -1460,6 +1489,21 @@ END
                $wgOut->returnToMain( false );
        }
 
+       /**
+        * Creates a basic error page which informs the user that
+        * they have attempted to edit a nonexistant section.
+        */
+       function noSuchSectionPage() {
+               global $wgOut;
+
+               $wgOut->setPageTitle( wfMsg( 'nosuchsectiontitle' ) );
+               $wgOut->setRobotPolicy( 'noindex,nofollow' );
+               $wgOut->setArticleRelated( false );
+
+               $wgOut->addWikiText( wfMsg( 'nosuchsectiontext', $this->section ) );
+               $wgOut->returnToMain( false, $this->mTitle->getPrefixedUrl() );
+       }
+
        /**
         * Produce the stock "your edit contains spam" page
         *
@@ -1581,90 +1625,102 @@ END
                 * can figure out a way to make them work in IE. However, we should make
                 * sure these keys are not defined on the edit page.
                 */
-               $toolarray=array(
-                       array(  'image'=>'button_bold.png',
-                                       'open'  =>      '\\\'\\\'\\\'',
-                                       'close' =>      '\\\'\\\'\\\'',
-                                       'sample'=>      wfMsg('bold_sample'),
-                                       'tip'   =>      wfMsg('bold_tip'),
-                                       'key'   =>      'B'
-                               ),
-                       array(  'image'=>'button_italic.png',
-                                       'open'  =>      '\\\'\\\'',
-                                       'close' =>      '\\\'\\\'',
-                                       'sample'=>      wfMsg('italic_sample'),
-                                       'tip'   =>      wfMsg('italic_tip'),
-                                       'key'   =>      'I'
-                               ),
-                       array(  'image'=>'button_link.png',
-                                       'open'  =>      '[[',
-                                       'close' =>      ']]',
-                                       'sample'=>      wfMsg('link_sample'),
-                                       'tip'   =>      wfMsg('link_tip'),
-                                       'key'   =>      'L'
-                               ),
-                       array(  'image'=>'button_extlink.png',
-                                       'open'  =>      '[',
-                                       'close' =>      ']',
-                                       'sample'=>      wfMsg('extlink_sample'),
-                                       'tip'   =>      wfMsg('extlink_tip'),
-                                       'key'   =>      'X'
-                               ),
-                       array(  'image'=>'button_headline.png',
-                                       'open'  =>      "\\n== ",
-                                       'close' =>      " ==\\n",
-                                       'sample'=>      wfMsg('headline_sample'),
-                                       'tip'   =>      wfMsg('headline_tip'),
-                                       'key'   =>      'H'
-                               ),
-                       array(  'image'=>'button_image.png',
-                                       'open'  =>      '[['.$wgContLang->getNsText(NS_IMAGE).":",
-                                       'close' =>      ']]',
-                                       'sample'=>      wfMsg('image_sample'),
-                                       'tip'   =>      wfMsg('image_tip'),
-                                       'key'   =>      'D'
-                               ),
-                       array(  'image' =>'button_media.png',
-                                       'open'  =>      '[['.$wgContLang->getNsText(NS_MEDIA).':',
-                                       'close' =>      ']]',
-                                       'sample'=>      wfMsg('media_sample'),
-                                       'tip'   =>      wfMsg('media_tip'),
-                                       'key'   =>      'M'
-                               ),
-                       array(  'image' =>'button_math.png',
-                                       'open'  =>      "<math>",
-                                       'close' =>      "<\\/math>",
-                                       'sample'=>      wfMsg('math_sample'),
-                                       'tip'   =>      wfMsg('math_tip'),
-                                       'key'   =>      'C'
-                               ),
-                       array(  'image' =>'button_nowiki.png',
-                                       'open'  =>      "<nowiki>",
-                                       'close' =>      "<\\/nowiki>",
-                                       'sample'=>      wfMsg('nowiki_sample'),
-                                       'tip'   =>      wfMsg('nowiki_tip'),
-                                       'key'   =>      'N'
-                               ),
-                       array(  'image' =>'button_sig.png',
-                                       'open'  =>      '--~~~~',
-                                       'close' =>      '',
-                                       'sample'=>      '',
-                                       'tip'   =>      wfMsg('sig_tip'),
-                                       'key'   =>      'Y'
-                               ),
-                       array(  'image' =>'button_hr.png',
-                                       'open'  =>      "\\n----\\n",
-                                       'close' =>      '',
-                                       'sample'=>      '',
-                                       'tip'   =>      wfMsg('hr_tip'),
-                                       'key'   =>      'R'
-                               )
+               $toolarray = array(
+                       array(  'image' => 'button_bold.png',
+                               'id'    => 'mw-editbutton-bold',
+                               'open'  => '\\\'\\\'\\\'',
+                               'close' => '\\\'\\\'\\\'',
+                               'sample'=> wfMsg('bold_sample'),
+                               'tip'   => wfMsg('bold_tip'),
+                               'key'   => 'B'
+                       ),
+                       array(  'image' => 'button_italic.png',
+                               'id'    => 'mw-editbutton-italic',
+                               'open'  => '\\\'\\\'',
+                               'close' => '\\\'\\\'',
+                               'sample'=> wfMsg('italic_sample'),
+                               'tip'   => wfMsg('italic_tip'),
+                               'key'   => 'I'
+                       ),
+                       array(  'image' => 'button_link.png',
+                               'id'    => 'mw-editbutton-link',
+                               'open'  => '[[',
+                               'close' => ']]',
+                               'sample'=> wfMsg('link_sample'),
+                               'tip'   => wfMsg('link_tip'),
+                               'key'   => 'L'
+                       ),
+                       array(  'image' => 'button_extlink.png',
+                               'id'    => 'mw-editbutton-extlink',
+                               'open'  => '[',
+                               'close' => ']',
+                               'sample'=> wfMsg('extlink_sample'),
+                               'tip'   => wfMsg('extlink_tip'),
+                               'key'   => 'X'
+                       ),
+                       array(  'image' => 'button_headline.png',
+                               'id'    => 'mw-editbutton-headline',
+                               'open'  => "\\n== ",
+                               'close' => " ==\\n",
+                               'sample'=> wfMsg('headline_sample'),
+                               'tip'   => wfMsg('headline_tip'),
+                               'key'   => 'H'
+                       ),
+                       array(  'image' => 'button_image.png',
+                               'id'    => 'mw-editbutton-image',
+                               'open'  => '[['.$wgContLang->getNsText(NS_IMAGE).":",
+                               'close' => ']]',
+                               'sample'=> wfMsg('image_sample'),
+                               'tip'   => wfMsg('image_tip'),
+                               'key'   => 'D'
+                       ),
+                       array(  'image' => 'button_media.png',
+                               'id'    => 'mw-editbutton-media',
+                               'open'  => '[['.$wgContLang->getNsText(NS_MEDIA).':',
+                               'close' => ']]',
+                               'sample'=> wfMsg('media_sample'),
+                               'tip'   => wfMsg('media_tip'),
+                               'key'   => 'M'
+                       ),
+                       array(  'image' => 'button_math.png',
+                               'id'    => 'mw-editbutton-math',
+                               'open'  => "<math>",
+                               'close' => "<\\/math>",
+                               'sample'=> wfMsg('math_sample'),
+                               'tip'   => wfMsg('math_tip'),
+                               'key'   => 'C'
+                       ),
+                       array(  'image' => 'button_nowiki.png',
+                               'id'    => 'mw-editbutton-nowiki',
+                               'open'  => "<nowiki>",
+                               'close' => "<\\/nowiki>",
+                               'sample'=> wfMsg('nowiki_sample'),
+                               'tip'   => wfMsg('nowiki_tip'),
+                               'key'   => 'N'
+                       ),
+                       array(  'image' => 'button_sig.png',
+                               'id'    => 'mw-editbutton-signature',
+                               'open'  => '--~~~~',
+                               'close' => '',
+                               'sample'=> '',
+                               'tip'   => wfMsg('sig_tip'),
+                               'key'   => 'Y'
+                       ),
+                       array(  'image' => 'button_hr.png',
+                               'id'    => 'mw-editbutton-hr',
+                               'open'  => "\\n----\\n",
+                               'close' => '',
+                               'sample'=> '',
+                               'tip'   => wfMsg('hr_tip'),
+                               'key'   => 'R'
+                       )
                );
                $toolbar = "<div id='toolbar'>\n";
                $toolbar.="<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
 
                foreach($toolarray as $tool) {
 
+                       $cssId = $tool['id'];
                        $image=$wgStylePath.'/common/images/'.$tool['image'];
                        $open=$tool['open'];
                        $close=$tool['close'];
@@ -1678,7 +1734,7 @@ END
 
                        #$key = $tool["key"];
 
-                       $toolbar.="addButton('$image','$tip','$open','$close','$sample');\n";
+                       $toolbar.="addButton('$image','$tip','$open','$close','$sample','$cssId');\n";
                }
 
                $toolbar.="/*]]>*/\n</script>";
@@ -1822,7 +1878,7 @@ END
        function livePreview() {
                global $wgOut;
                $wgOut->disable();
-               header( 'Content-type: text/xml' );
+               header( 'Content-type: text/xml; charset=utf-8' );
                header( 'Cache-control: no-cache' );
 
                $s =
@@ -1982,6 +2038,46 @@ END
                $wgOut->setPageTitle( wfMsg( 'nocreatetitle' ) );
                $wgOut->addWikiText( wfMsg( 'nocreatetext' ) );
        }
+       
+       /**
+        * If there are rows in the deletion log for this page, show them,
+        * along with a nice little note for the user
+        *
+        * @param OutputPage $out
+        */
+       private function showDeletionLog( $out ) {
+               $title = $this->mArticle->getTitle();
+               $reader = new LogReader(
+                       new FauxRequest(
+                               array(
+                                       'page' => $title->getPrefixedText(),
+                                       'type' => 'delete',
+                                       )
+                       )
+               );
+               if( $reader->hasRows() ) {
+                       $out->addHtml( '<div id="mw-recreate-deleted-warn">' );
+                       $out->addHtml( $this->buildWarningDismisser() );
+                       $out->addWikiText( wfMsg( 'recreate-deleted-warn' ) );
+                       $viewer = new LogViewer( $reader );
+                       $viewer->showList( $out );
+                       $out->addHtml( '</div>' );                      
+               }                               
+       }
+       
+       /**
+        * Builds a JavaScript fragment that injects a link to dismiss the
+        * "recreating deleted" warning
+        *
+        * @return string
+        */
+       private function buildWarningDismisser() {
+               return '<script type="text/javascript">'
+                       . 'document.write( \'<div class="mw-recreate-deleted-control">'
+                       . '<a href="javascript:dismissRecreateWarning();">'
+                       . wfMsgHtml( 'recreate-deleted-dismiss' ) . '</a></div>\' );'
+                       . '</script>';                  
+       }
 
 }