CologneBlue rewrite: output the sidebar items as <ul> lists
[lhc/web/wiklou.git] / includes / EditPage.php
index cc41b02..67f0b46 100644 (file)
@@ -409,10 +409,13 @@ class EditPage {
                                wfProfileOut( __METHOD__ );
                                return;
                        }
-                       if ( !$this->mTitle->getArticleID() )
+
+                       if ( !$this->mTitle->getArticleID() ) {
                                wfRunHooks( 'EditFormPreloadText', array( &$this->textbox1, &$this->mTitle ) );
-                       else
+                       } else {
                                wfRunHooks( 'EditFormInitialText', array( $this ) );
+                       }
+
                }
 
                $this->showEditForm();
@@ -471,7 +474,7 @@ class EditPage {
                $content = $this->getContentObject();
 
                # Use the normal message if there's nothing to display
-               if ( $this->firsttime && $content->isEmpty() ) {
+               if ( $this->firsttime && ( !$content || $content->isEmpty() ) ) {
                        $action = $this->mTitle->exists() ? 'edit' :
                                ( $this->mTitle->isTalkPage() ? 'createtalk' : 'createpage' );
                        throw new PermissionsError( $action, $permErrors );
@@ -606,8 +609,10 @@ class EditPage {
                                // modified by subclasses
                                wfProfileIn( get_class( $this ) . "::importContentFormData" );
                                $textbox1 = $this->importContentFormData( $request );
-                               if ( isset( $textbox1 ) )
+                               if ( isset( $textbox1 ) ) {
                                        $this->textbox1 = $textbox1;
+                               }
+
                                wfProfileOut( get_class( $this ) . "::importContentFormData" );
                        }
 
@@ -776,6 +781,9 @@ class EditPage {
                $this->edittime = $this->mArticle->getTimestamp();
 
                $content = $this->getContentObject( false ); #TODO: track content object?!
+               if ( $content === false ) {
+                       return false;
+               }
                $this->textbox1 = $this->toEditText( $content );
 
                // activate checkboxes if user wants them to be always active
@@ -803,7 +811,7 @@ class EditPage {
        /**
         * Fetch initial editing page content.
         *
-        * @param $def_text string
+        * @param $def_text string|bool
         * @return mixed string on success, $def_text for invalid sections
         * @private
         * @deprecated since 1.21, get WikiPage::getContent() instead.
@@ -824,7 +832,7 @@ class EditPage {
        }
 
        /**
-        * @param Content|false $def_content The default value to return
+        * @param Content|null $def_content The default value to return
         *
         * @return mixed Content on success, $def_content for invalid sections
         *
@@ -937,11 +945,11 @@ class EditPage {
         * to the original text of the edit.
         *
         * This difers from Article::getContent() that when a missing revision is
-        * encountered the result will be an empty string and not the
+        * encountered the result will be null and not the
         * 'missing-revision' message.
         *
         * @since 1.19
-        * @return string
+        * @return Content|null
         */
        private function getOriginalContent() {
                if ( $this->section == 'new' ) {
@@ -1054,7 +1062,7 @@ class EditPage {
 
                $title = Title::newFromText( $preload );
                # Check for existence to avoid getting MediaWiki:Noarticletext
-               if ( $title === null || !$title->exists() || !$title->userCan( 'read' ) ) {
+               if ( $title === null || !$title->exists() || !$title->userCan( 'read', $wgUser ) ) {
                        return $handler->makeEmptyContent();
                }
 
@@ -1062,7 +1070,7 @@ class EditPage {
                if ( $page->isRedirect() ) {
                        $title = $page->getRedirectTarget();
                        # Same as before
-                       if ( $title === null || !$title->exists() || !$title->userCan( 'read' ) ) {
+                       if ( $title === null || !$title->exists() || !$title->userCan( 'read', $wgUser ) ) {
                                return $handler->makeEmptyContent();
                        }
                        $page = WikiPage::factory( $title );
@@ -1071,6 +1079,10 @@ class EditPage {
                $parserOptions = ParserOptions::newFromUser( $wgUser );
                $content = $page->getContent( Revision::RAW );
 
+               if ( !$content ) {
+                       return $handler->makeEmptyContent();
+               }
+
                return $content->preloadTransform( $title, $parserOptions );
        }
 
@@ -1340,7 +1352,7 @@ class EditPage {
 
                if ( $new ) {
                        // Late check for create permission, just in case *PARANOIA*
-                       if ( !$this->mTitle->userCan( 'create' ) ) {
+                       if ( !$this->mTitle->userCan( 'create', $wgUser ) ) {
                                $status->fatal( 'nocreatetext' );
                                $status->value = self::AS_NO_CREATE_PERMISSION;
                                wfDebug( __METHOD__ . ": no create permission\n" );
@@ -1386,8 +1398,8 @@ class EditPage {
                                        // passed.
                                        if ( $this->summary === '' ) {
                                                $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
-                                               $this->summary = wfMessage( 'newsectionsummary', $cleanSectionTitle )
-                                                       ->inContentLanguage()->text() ;
+                                               $this->summary = wfMessage( 'newsectionsummary' )
+                                                       ->rawParams( $cleanSectionTitle )->inContentLanguage()->text();
                                        }
                                } elseif ( $this->summary !== '' ) {
                                        // Insert the section title above the content.
@@ -1398,8 +1410,8 @@ class EditPage {
 
                                        // Create a link to the new section from the edit summary.
                                        $cleanSummary = $wgParser->stripSectionName( $this->summary );
-                                       $this->summary = wfMessage( 'newsectionsummary', $cleanSummary )
-                                               ->inContentLanguage()->text();
+                                       $this->summary = wfMessage( 'newsectionsummary' )
+                                               ->rawParams( $cleanSummary )->inContentLanguage()->text();
                                }
                        }
 
@@ -1461,15 +1473,16 @@ class EditPage {
                                $this->isConflict = true;
                                $content = $textbox_content; // do not try to merge here!
                        } elseif ( $this->isConflict ) {
+                               $contentObj = $content;
                                # Attempt merge
-                               if ( $this->mergeChangesIntoContent( $textbox_content ) ) {
+                               if ( $this->mergeChangesInto( $content ) ) {
                                        // Successful merge! Maybe we should tell the user the good news?
                                        $this->isConflict = false;
-                                       $content = $textbox_content;
+                                       $content = $this->toEditContent( $content );
                                        wfDebug( __METHOD__ . ": Suppressing edit conflict, successful merge.\n" );
                                } else {
                                        $this->section = '';
-                                       #$this->textbox1 = $text; #redundant, nothing to do here?
+                                       $this->textbox1 = ContentHandler::getContentText( $contentObj );
                                        wfDebug( __METHOD__ . ": Keeping edit conflict, failed merge.\n" );
                                }
                        }
@@ -1542,16 +1555,16 @@ class EditPage {
                                        // passed.
                                        if ( $this->summary === '' ) {
                                                $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
-                                               $this->summary = wfMessage( 'newsectionsummary', $cleanSectionTitle )
-                                                       ->inContentLanguage()->text();
+                                               $this->summary = wfMessage( 'newsectionsummary' )
+                                                       ->rawParams( $cleanSectionTitle )->inContentLanguage()->text();
                                        }
                                } elseif ( $this->summary !== '' ) {
                                        $sectionanchor = $wgParser->guessLegacySectionNameFromWikiText( $this->summary );
                                        # This is a new section, so create a link to the new section
                                        # in the revision summary.
                                        $cleanSummary = $wgParser->stripSectionName( $this->summary );
-                                       $this->summary = wfMessage( 'newsectionsummary', $cleanSummary )
-                                               ->inContentLanguage()->text();
+                                       $this->summary = wfMessage( 'newsectionsummary' )
+                                               ->rawParams( $cleanSummary )->inContentLanguage()->text();
                                }
                        } elseif ( $this->section != '' ) {
                                # Try to get a section anchor from the section source, redirect to edited section if header found
@@ -1596,7 +1609,7 @@ class EditPage {
 
                if ( $doEditStatus->isOK() ) {
                                $result['redirect'] = $content->isRedirect();
-                       $this->commitWatch();
+                       $this->updateWatchlist();
                        wfProfileOut( __METHOD__ );
                        return $status;
                } else {
@@ -1617,19 +1630,27 @@ class EditPage {
        }
 
        /**
-        * Commit the change of watch status
+        * Register the change of watch status
         */
-       protected function commitWatch() {
+       protected function updateWatchlist() {
                global $wgUser;
+
                if ( $wgUser->isLoggedIn() && $this->watchthis != $wgUser->isWatched( $this->mTitle ) ) {
+                       $fname = __METHOD__;
+                       $title = $this->mTitle;
+                       $watch = $this->watchthis;
+
+                       // Do this in its own transaction to reduce contention...
                        $dbw = wfGetDB( DB_MASTER );
-                       $dbw->begin( __METHOD__ );
-                       if ( $this->watchthis ) {
-                               WatchAction::doWatch( $this->mTitle, $wgUser );
-                       } else {
-                               WatchAction::doUnwatch( $this->mTitle, $wgUser );
-                       }
-                       $dbw->commit( __METHOD__ );
+                       $dbw->onTransactionIdle( function() use ( $dbw, $title, $watch, $wgUser, $fname ) {
+                               $dbw->begin( $fname );
+                               if ( $watch ) {
+                                       WatchAction::doWatch( $title, $wgUser );
+                               } else {
+                                       WatchAction::doUnwatch( $title, $wgUser );
+                               }
+                               $dbw->commit( $fname );
+                       } );
                }
        }
 
@@ -1645,14 +1666,35 @@ class EditPage {
        function mergeChangesInto( &$editText ){
                ContentHandler::deprecated( __METHOD__, "1.21" );
 
-               $editContent = $this->toEditContent( $editText );
+               wfProfileIn( __METHOD__ );
 
-               $ok = $this->mergeChangesIntoContent( $editContent );
+               $db = wfGetDB( DB_MASTER );
+
+               // This is the revision the editor started from
+               $baseRevision = $this->getBaseRevision();
+               if ( is_null( $baseRevision ) ) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+               $baseText = $baseRevision->getText();
+
+               // The current state, we want to merge updates into it
+               $currentRevision = Revision::loadFromTitle( $db, $this->mTitle );
+               if ( is_null( $currentRevision ) ) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+               $currentText = $currentRevision->getText();
+
+               $result = '';
+               $editText = $this->toEditText( $editText );
 
-               if ( $ok ) {
-                       $editText = $this->toEditText( $editContent );
+               if ( wfMerge( $baseText, $editText, $currentText, $result ) ) {
+                       $editText = $result;
+                       wfProfileOut( __METHOD__ );
                        return true;
                } else {
+                       wfProfileOut( __METHOD__ );
                        return false;
                }
        }
@@ -1661,8 +1703,7 @@ class EditPage {
         * @private
         * @todo document
         *
-        * @parma $editText string
-        *
+        * @param $editContent
         * @return bool
         * @since since 1.WD
         */
@@ -1673,19 +1714,21 @@ class EditPage {
 
                // This is the revision the editor started from
                $baseRevision = $this->getBaseRevision();
-               if ( is_null( $baseRevision ) ) {
+               $baseContent = $baseRevision ? $baseRevision->getContent() : null;
+
+               if ( is_null( $baseContent ) ) {
                        wfProfileOut( __METHOD__ );
                        return false;
                }
-               $baseContent = $baseRevision->getContent();
 
                // The current state, we want to merge updates into it
                $currentRevision = Revision::loadFromTitle( $db, $this->mTitle );
-               if ( is_null( $currentRevision ) ) {
+               $currentContent = $currentRevision ? $currentRevision->getContent() : null;
+
+               if ( is_null( $currentContent ) ) {
                        wfProfileOut( __METHOD__ );
                        return false;
                }
-               $currentContent = $currentRevision->getContent();
 
                $handler = ContentHandler::getForModelID( $baseContent->getModel() );
 
@@ -1763,7 +1806,7 @@ class EditPage {
                $wgOut->addModules( 'mediawiki.action.edit' );
 
                if ( $wgUser->getOption( 'uselivepreview', false ) ) {
-                       $wgOut->addModules( 'mediawiki.legacy.preview' );
+                       $wgOut->addModules( 'mediawiki.action.edit.preview' );
                }
                // Bug #19334: textarea jumps when editing articles in IE8
                $wgOut->addStyle( 'common/IE80Fixes.css', 'screen', 'IE 8' );
@@ -1862,10 +1905,13 @@ class EditPage {
                # Give a notice if the user is editing a deleted/moved page...
                if ( !$this->mTitle->exists() ) {
                        LogEventsList::showLogExtract( $wgOut, array( 'delete', 'move' ), $this->mTitle,
-                               '', array( 'lim' => 10,
-                                          'conds' => array( "log_action != 'revision'" ),
-                                          'showIfEmpty' => false,
-                                          'msgKey' => array( 'recreate-moveddeleted-warn' ) )
+                               '',
+                               array(
+                                       'lim' => 10,
+                                       'conds' => array( "log_action != 'revision'" ),
+                                       'showIfEmpty' => false,
+                                       'msgKey' => array( 'recreate-moveddeleted-warn' )
+                               )
                        );
                }
        }
@@ -1883,29 +1929,36 @@ class EditPage {
                                // Added using template syntax, to take <noinclude>'s into account.
                                $wgOut->addWikiTextTitleTidy( '{{:' . $title->getFullText() . '}}', $this->mTitle );
                                return true;
-                       } else {
-                               return false;
                        }
-               } else {
-                       return false;
                }
+               return false;
        }
 
        /**
-        * Gets an editable textual representation of the given Content object.
+        * Gets an editable textual representation of $content.
         * The textual representation can be turned by into a Content object by the
         * toEditContent() method.
         *
+        * If $content is null or false or a string, $content is returned unchanged.
+        *
         * If the given Content object is not of a type that can be edited using the text base EditPage,
         * an exception will be raised. Set $this->allowNonTextContent to true to allow editing of non-textual
         * content.
         *
-        * @param Content $content
+        * @param Content|null|false|string $content
         * @return String the editable text form of the content.
         *
         * @throws MWException if $content is not an instance of TextContent and $this->allowNonTextContent is not true.
         */
-       protected function toEditText( Content $content ) {
+       protected function toEditText( $content ) {
+               if ( $content === null || $content === false ) {
+                       return $content;
+               }
+
+               if ( is_string( $content ) ) {
+                       return $content;
+               }
+
                if ( !$this->allowNonTextContent && !( $content instanceof TextContent ) ) {
                        throw new MWException( "This content model can not be edited as text: "
                                                                . ContentHandler::getLocalizedName( $content->getModel() ) );
@@ -1921,7 +1974,7 @@ class EditPage {
         * an exception will be raised. Set $this->allowNonTextContent to true to allow editing of non-textual
         * content.
         *
-        * @param String|null|false $text Text to unserialize
+        * @param String|null|bool $text Text to unserialize
         * @return Content The content object created from $text. If $text was false or null, false resp. null will be
         *                 returned instead.
         *
@@ -1946,7 +1999,7 @@ class EditPage {
 
        /**
         * Send the edit form and related headers to $wgOut
-        * @param $formCallback Callback that takes an OutputPage parameter; will be called
+        * @param $formCallback Callback|null that takes an OutputPage parameter; will be called
         *     during form output near the top, for captchas and the like.
         */
        function showEditForm( $formCallback = null ) {
@@ -2218,7 +2271,7 @@ class EditPage {
                                if ( $revision ) {
                                        // Let sysop know that this will make private content public if saved
 
-                                       if ( !$revision->userCan( Revision::DELETED_TEXT ) ) {
+                                       if ( !$revision->userCan( Revision::DELETED_TEXT, $wgUser ) ) {
                                                $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
                                        } elseif ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
                                                $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
@@ -2252,10 +2305,13 @@ class EditPage {
                                        $wgOut->wrapWikiMsg( "<div class='error' id='mw-userinvalidcssjstitle'>\n$1\n</div>", array( 'userinvalidcssjstitle', $this->mTitle->getSkinFromCssJsSubpage() ) );
                                }
                                if ( $this->formtype !== 'preview' ) {
-                                       if ( $this->isCssSubpage )
+                                       if ( $this->isCssSubpage ) {
                                                $wgOut->wrapWikiMsg( "<div id='mw-usercssyoucanpreview'>\n$1\n</div>", array( 'usercssyoucanpreview' ) );
-                                       if ( $this->isJsSubpage )
+                                       }
+
+                                       if ( $this->isJsSubpage ) {
                                                $wgOut->wrapWikiMsg( "<div id='mw-userjsyoucanpreview'>\n$1\n</div>", array( 'userjsyoucanpreview' ) );
+                                       }
                                }
                        }
                }
@@ -2386,14 +2442,16 @@ class EditPage {
         * @return String
         */
        protected function getSummaryPreview( $isSubjectPreview, $summary = "" ) {
-               if ( !$summary || ( !$this->preview && !$this->diff ) )
+               if ( !$summary || ( !$this->preview && !$this->diff ) ) {
                        return "";
+               }
 
                global $wgParser;
 
-               if ( $isSubjectPreview )
+               if ( $isSubjectPreview ) {
                        $summary = wfMessage( 'newsectionsummary', $wgParser->stripSectionName( $summary ) )
                                ->inContentLanguage()->text();
+               }
 
                $message = $isSubjectPreview ? 'subject-preview' : 'summary-preview';
 
@@ -2412,8 +2470,9 @@ class EditPage {
 
 HTML
                );
-               if ( !$this->checkUnicodeCompliantBrowser() )
+               if ( !$this->checkUnicodeCompliantBrowser() ) {
                        $wgOut->addHTML( Html::hidden( 'safemode', '1' ) );
+               }
        }
 
        protected function showFormAfterText() {
@@ -2523,13 +2582,15 @@ HTML
        protected function displayPreviewArea( $previewOutput, $isOnTop = false ) {
                global $wgOut;
                $classes = array();
-               if ( $isOnTop )
+               if ( $isOnTop ) {
                        $classes[] = 'ontop';
+               }
 
                $attribs = array( 'id' => 'wikiPreview', 'class' => implode( ' ', $classes ) );
 
-               if ( $this->formtype != 'preview' )
+               if ( $this->formtype != 'preview' ) {
                        $attribs['style'] = 'display: none;';
+               }
 
                $wgOut->addHTML( Xml::openElement( 'div', $attribs ) );
 
@@ -2577,7 +2638,7 @@ HTML
         * save and then make a comparison.
         */
        function showDiff() {
-               global $wgUser, $wgContLang, $wgParser, $wgOut;
+               global $wgUser, $wgContLang, $wgOut;
 
                $oldtitlemsg = 'currentrev';
                # if message does not exist, show diff against the preloaded default
@@ -2590,7 +2651,7 @@ HTML
                                $oldContent = null;
                        }
                } else {
-                       $oldContent = $this->getOriginalContent();
+                       $oldContent = $this->getCurrentContent();
                }
 
                $textboxContent = $this->toEditContent( $this->textbox1 );
@@ -2599,16 +2660,26 @@ HTML
                                                        $this->section, $textboxContent,
                                                        $this->summary, $this->edittime );
 
-               ContentHandler::runLegacyHooks( 'EditPageGetDiffText', array( $this, &$newContent ) );
-               wfRunHooks( 'EditPageGetDiffContent', array( $this, &$newContent ) );
+               if ( $newContent ) {
+                       ContentHandler::runLegacyHooks( 'EditPageGetDiffText', array( $this, &$newContent ) );
+                       wfRunHooks( 'EditPageGetDiffContent', array( $this, &$newContent ) );
 
-               $popts = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
-               $newContent = $newContent->preSaveTransform( $this->mTitle, $wgUser, $popts );
+                       $popts = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
+                       $newContent = $newContent->preSaveTransform( $this->mTitle, $wgUser, $popts );
+               }
 
                if ( ( $oldContent && !$oldContent->isEmpty() ) || ( $newContent && !$newContent->isEmpty() ) ) {
                        $oldtitle = wfMessage( $oldtitlemsg )->parse();
                        $newtitle = wfMessage( 'yourtext' )->parse();
 
+                       if ( !$oldContent ) {
+                               $oldContent = $newContent->getContentHandler()->makeEmptyContent();
+                       }
+
+                       if ( !$newContent ) {
+                               $newContent = $oldContent->getContentHandler()->makeEmptyContent();
+                       }
+
                        $de = $oldContent->getContentHandler()->createDifferenceEngine( $this->mArticle->getContext() );
                        $de->setContent( $oldContent, $newContent );
 
@@ -2824,10 +2895,13 @@ HTML
                );
                // Quick paranoid permission checks...
                if ( is_object( $data ) ) {
-                       if ( $data->log_deleted & LogPage::DELETED_USER )
+                       if ( $data->log_deleted & LogPage::DELETED_USER ) {
                                $data->user_name = wfMessage( 'rev-deleted-user' )->escaped();
-                       if ( $data->log_deleted & LogPage::DELETED_COMMENT )
+                       }
+
+                       if ( $data->log_deleted & LogPage::DELETED_COMMENT ) {
                                $data->log_comment = wfMessage( 'rev-deleted-comment' )->escaped();
+                       }
                }
                return $data;
        }
@@ -2838,7 +2912,7 @@ HTML
         * @return string
         */
        function getPreviewText() {
-               global $wgOut, $wgUser, $wgParser, $wgRawHtml, $wgLang;
+               global $wgOut, $wgUser, $wgRawHtml, $wgLang;
 
                wfProfileIn( __METHOD__ );
 
@@ -2903,11 +2977,7 @@ HTML
                                # Messages: usercsspreview, userjspreview, sitecsspreview, sitejspreview
                                if( $level && $format ) {
                                        $note = "<div id='mw-{$level}{$format}preview'>" . wfMessage( "{$level}{$format}preview" )->text()  . "</div>";
-                               } else {
-                                       $note = wfMessage( 'previewnote' )->text() ;
                                }
-                       } else {
-                               $note = wfMessage( 'previewnote' )->text() ;
                        }
 
                        $rt = $content->getRedirectChain();
@@ -3336,7 +3406,7 @@ HTML
        /**
         * Produce the stock "your edit contains spam" page
         *
-        * @param $match string Text which triggered one or more filters
+        * @param $match string|bool Text which triggered one or more filters
         * @deprecated since 1.17 Use method spamPageWithContent() instead
         */
        static function spamPage( $match = false ) {