It helps when you don't break things...
[lhc/web/wiklou.git] / includes / EditPage.php
index 0b9a476..867431b 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;
@@ -22,6 +20,7 @@ class EditPage {
        var $firsttime;
        var $lastDelete;
        var $mTokenOk = false;
+       var $mTokenOkExceptSuffix = false;
        var $mTriedSave = false;
        var $tooBig = false;
        var $kblength = false;
@@ -67,14 +66,14 @@ 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' );
-               $undoto = $wgRequest->getVal( 'undoto' );
+               $undo = $wgRequest->getVal( 'undo' );
 
                wfProfileIn( __METHOD__ );
 
@@ -99,18 +98,28 @@ class EditPage {
 
                        $text = $this->mArticle->getContent();
 
-                       if ( $undoafter > 0 && $undoto > $undoafter ) {
-                               #Undoing a specific edit overrides section editing; section-editing
+                       if ($undo > 0 && $undoafter > 0 && $undo < $undoafter) {
+                               # If they got undoafter and undo round the wrong way, switch them
+                               list( $undo, $undoafter ) = array( $undoafter, $undo );
+                       }
+
+                       if ( $undo > 0 && $undo > $undoafter ) {
+                               # Undoing a specific edit overrides section editing; section-editing
                                # doesn't work with undoing.
-                               $undorev = Revision::newFromId($undoto);
-                               $oldrev = Revision::newFromId($undoafter);
+                               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) && !is_null($oldrev) && $undorev->getPage()==$oldrev->getPage() && $undorev->getPage()==$this->mArticle->getID() ) {
                                        $undorev_text = $undorev->getText();
-                    $oldrev_text = $oldrev->getText();
-                    $currev_text = $text;
+                                       $oldrev_text = $oldrev->getText();
+                                       $currev_text = $text;
 
                                        #No use doing a merge if it's just a straight revert.
                                        if ( $currev_text != $undorev_text ) {
@@ -131,22 +140,22 @@ class EditPage {
                                        $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-success' ) );
                                        $firstrev = $oldrev->getNext();
                                        # If we just undid one rev, use an autosummary
-                                       if ( $firstrev->mId == $undoto ) {
-                                                       $this->summary = wfMsgForContent('undo-summary', $undoto, $undorev->getUserText());
+                                       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 != '' ) {
-                       if( $section == 'new' ) {
-                               $text = $this->getPreloadedText( $preload );
-                       } else {
-                               $text = $wgParser->getSection( $text, $section );
+                       } else if( $section != '' ) {
+                               if( $section == 'new' ) {
+                                       $text = $this->getPreloadedText( $preload );
+                               } else {
+                                       $text = $wgParser->getSection( $text, $section, $def_text );
+                               }
                        }
                }
-       }
 
                wfProfileOut( __METHOD__ );
                return $text;
@@ -291,7 +300,7 @@ class EditPage {
                global $wgOut, $wgUser, $wgRequest, $wgTitle;
                global $wgEmailConfirmToEdit;
 
-               if ( ! wfRunHooks( 'AlternateEdit', array( &$this  ) ) )
+               if ( ! wfRunHooks( 'AlternateEdit', array( &$this ) ) )
                        return;
 
                $fname = 'EditPage::edit';
@@ -407,9 +416,10 @@ class EditPage {
                        }
                }
 
-               if(!$this->mTitle->getArticleID() && ('initial' == $this->formtype || $this->firsttime )) { # new article
+               # Show applicable editing introductions
+               if( $this->formtype == 'initial' || $this->firsttime )
                        $this->showIntro();
-               }
+       
                if( $this->mTitle->isTalkPage() ) {
                        $wgOut->addWikiText( wfMsg( 'talkpagetext' ) );
                }
@@ -430,7 +440,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 ) );
                }
@@ -441,17 +456,30 @@ class EditPage {
        }
 
        /**
-        * Return true if this page should be previewed when the edit form
-        * is initially opened.
+        * Should we show a preview when the edit form is first shown?
+        *
         * @return bool
-        * @private
         */
-       function previewOnOpen() {
-               global $wgUser;
-               return $this->section != 'new' &&
-                       ( ( $wgUser->getOption( 'previewonfirst' ) && $this->mTitle->exists() ) ||
-                               ( $this->mTitle->getNamespace() == NS_CATEGORY &&
-                                       !$this->mTitle->exists() ) );
+       private function previewOnOpen() {
+               global $wgRequest, $wgUser;
+               if( $wgRequest->getVal( 'preview' ) == 'yes' ) {
+                       // Explicit override from request
+                       return true;
+               } elseif( $wgRequest->getVal( 'preview' ) == 'no' ) {
+                       // Explicit override from request
+                       return false;
+               } elseif( $this->section == 'new' ) {
+                       // Nothing *to* preview for new sections
+                       return false;
+               } elseif( $this->mTitle->exists() && $wgUser->getOption( 'previewonfirst' ) ) {
+                       // Standard preference behaviour
+                       return true;
+               } elseif( !$this->mTitle->exists() && $this->mTitle->getNamespace() == NS_CATEGORY ) {
+                       // Categories are special
+                       return true;
+               } else {
+                       return false;
+               }
        }
 
        /**
@@ -567,35 +595,45 @@ class EditPage {
         */
        function tokenOk( &$request ) {
                global $wgUser;
-               if( $wgUser->isAnon() ) {
-                       # Anonymous users may not have a session
-                       # open. Check for suffix anyway.
-                       $this->mTokenOk = ( EDIT_TOKEN_SUFFIX == $request->getVal( 'wpEditToken' ) );
-               } else {
-                       $this->mTokenOk = $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
-               }
+               $token = $request->getVal( 'wpEditToken' );
+               $this->mTokenOk = $wgUser->matchEditToken( $token );
+               $this->mTokenOkExceptSuffix = $wgUser->matchEditTokenNoSuffix( $token );
                return $this->mTokenOk;
        }
 
-       /** */
-       function showIntro() {
+       /**
+        * Show all applicable editing introductions
+        */
+       private function showIntro() {
                global $wgOut, $wgUser;
-               $addstandardintro=true;
-               if($this->editintro) {
-                       $introtitle=Title::newFromText($this->editintro);
-                       if(isset($introtitle) && $introtitle->userCanRead()) {
-                               $rev=Revision::newFromTitle($introtitle);
-                               if($rev) {
-                                       $wgOut->addSecondaryWikiText($rev->getText());
-                                       $addstandardintro=false;
-                               }
-                       }
-               }
-               if($addstandardintro) {
-                       if ( $wgUser->isLoggedIn() )
+               if( !$this->showCustomIntro() && !$this->mTitle->exists() ) {
+                       if( $wgUser->isLoggedIn() ) {
                                $wgOut->addWikiText( wfMsg( 'newarticletext' ) );
-                       else
+                       } else {
                                $wgOut->addWikiText( wfMsg( 'newarticletextanon' ) );
+                       }
+                       $this->showDeletionLog( $wgOut );
+               }
+       }
+       
+       /**
+        * Attempt to show a custom editing introduction, if supplied
+        *
+        * @return bool
+        */
+       private function showCustomIntro() {
+               if( $this->editintro ) {
+                       $title = Title::newFromText( $this->editintro );
+                       if( $title instanceof Title && $title->exists() && $title->userCanRead() ) {
+                               global $wgOut;
+                               $revision = Revision::newFromTitle( $title );
+                               $wgOut->addSecondaryWikiText( $revision->getText() );
+                               return true;
+                       } else {
+                               return false;
+                       }
+               } else {
+                       return false;
                }
        }
 
@@ -732,6 +770,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' ) {
@@ -803,7 +843,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 );
@@ -869,10 +909,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;
        }
 
        /**
@@ -934,9 +977,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 );
@@ -982,20 +1025,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);
@@ -1074,7 +1119,7 @@ class EditPage {
                        }
 
                        if ( 'diff' == $this->formtype ) {
-                               $wgOut->addHTML( $this->getDiff() );
+                               $this->showDiff();
                        }
                }
 
@@ -1100,7 +1145,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 ;
@@ -1152,6 +1197,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\" />
@@ -1212,10 +1259,7 @@ END
                 * include the constant suffix to prevent editing from
                 * broken text-mangling proxies.
                 */
-               if ( $wgUser->isLoggedIn() )
-                       $token = htmlspecialchars( $wgUser->editToken() );
-               else
-                       $token = EDIT_TOKEN_SUFFIX;
+               $token = htmlspecialchars( $wgUser->editToken() );
                $wgOut->addHTML( "\n<input type='hidden' value=\"$token\" name=\"wpEditToken\" />\n" );
 
 
@@ -1254,7 +1298,7 @@ END
                        }
 
                        if ( $this->formtype == 'diff') {
-                               $wgOut->addHTML( $this->getDiff() );
+                               $this->showDiff();
                        }
 
                }
@@ -1346,7 +1390,11 @@ END
                wfProfileIn( $fname );
 
                if ( $this->mTriedSave && !$this->mTokenOk ) {
-                       $msg = 'session_fail_preview';
+                       if ( $this->mTokenOkExceptSuffix ) {
+                               $msg = 'token_suffix_mismatch';
+                       } else {
+                               $msg = 'session_fail_preview';
+                       }
                } else {
                        $msg = 'previewnote';
                }
@@ -1397,6 +1445,9 @@ END
 
                        $previewHTML = $parserOutput->getText();
                        $wgOut->addParserOutputNoText( $parserOutput );
+                       
+                       # ParserOutput might have altered the page title, so reset it
+                       $wgOut->setPageTitle( wfMsg( 'editing', $this->mTitle->getPrefixedText() ) );                   
 
                        foreach ( $parserOutput->getTemplates() as $ns => $template)
                                foreach ( array_keys( $template ) as $dbk)
@@ -1468,6 +1519,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
         *
@@ -1861,10 +1927,8 @@ END
         *
         * If this is a section edit, we'll replace the section as for final
         * save and then make a comparison.
-        *
-        * @return string HTML
         */
-       function getDiff() {
+       function showDiff() {
                $oldtext = $this->mArticle->fetchContent();
                $newtext = $this->mArticle->replaceSection(
                        $this->section, $this->textbox1, $this->summary, $this->edittime );
@@ -1875,11 +1939,13 @@ END
                        $de = new DifferenceEngine( $this->mTitle );
                        $de->setText( $oldtext, $newtext );
                        $difftext = $de->getDiff( $oldtitle, $newtitle );
+                       $de->showDiffStyle();
                } else {
                        $difftext = '';
                }
 
-               return '<div id="wikiDiff">' . $difftext . '</div>';
+               global $wgOut;
+               $wgOut->addHtml( '<div id="wikiDiff">' . $difftext . '</div>' );
        }
 
        /**
@@ -2002,7 +2068,32 @@ 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->addWikiText( wfMsg( 'recreate-deleted-warn' ) );
+                       $viewer = new LogViewer( $reader );
+                       $viewer->showList( $out );
+                       $out->addHtml( '</div>' );                      
+               }                               
+       }
+       
 }
 
-?>
+