Merge "LivePreview: Wrap content preview with mw-content-*"
[lhc/web/wiklou.git] / includes / EditPage.php
index 7f5a9c0..a8a17cf 100644 (file)
@@ -467,13 +467,11 @@ class EditPage {
                        return;
                }
 
-               wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__ . ": enter\n" );
 
                // If they used redlink=1 and the page exists, redirect to the main article
                if ( $wgRequest->getBool( 'redlink' ) && $this->mTitle->exists() ) {
                        $wgOut->redirect( $this->mTitle->getFullURL() );
-                       wfProfileOut( __METHOD__ );
                        return;
                }
 
@@ -482,7 +480,6 @@ class EditPage {
 
                if ( $this->live ) {
                        $this->livePreview();
-                       wfProfileOut( __METHOD__ );
                        return;
                }
 
@@ -507,7 +504,7 @@ class EditPage {
                        }
                }
 
-               $permErrors = $this->getEditPermissionErrors();
+               $permErrors = $this->getEditPermissionErrors( $this->save ? 'secure' : 'full' );
                if ( $permErrors ) {
                        wfDebug( __METHOD__ . ": User can't edit\n" );
                        // Auto-block user's IP if the account was "hard" blocked
@@ -515,12 +512,9 @@ class EditPage {
 
                        $this->displayPermissionsError( $permErrors );
 
-                       wfProfileOut( __METHOD__ );
                        return;
                }
 
-               wfProfileIn( __METHOD__ . "-business-end" );
-
                $this->isConflict = false;
                // css / js subpages of user pages get a special treatment
                $this->isCssJsSubpage = $this->mTitle->isCssJsSubpage();
@@ -540,9 +534,9 @@ class EditPage {
                # in the back door with a hand-edited submission URL.
 
                if ( 'save' == $this->formtype ) {
-                       if ( !$this->attemptSave() ) {
-                               wfProfileOut( __METHOD__ . "-business-end" );
-                               wfProfileOut( __METHOD__ );
+                       $resultDetails = null;
+                       $status = $this->attemptSave( $resultDetails );
+                       if ( !$this->handleStatus( $status, $resultDetails ) ) {
                                return;
                        }
                }
@@ -552,8 +546,6 @@ class EditPage {
                if ( 'initial' == $this->formtype || $this->firsttime ) {
                        if ( $this->initialiseForm() === false ) {
                                $this->noSuchSectionPage();
-                               wfProfileOut( __METHOD__ . "-business-end" );
-                               wfProfileOut( __METHOD__ );
                                return;
                        }
 
@@ -566,20 +558,25 @@ class EditPage {
                }
 
                $this->showEditForm();
-               wfProfileOut( __METHOD__ . "-business-end" );
-               wfProfileOut( __METHOD__ );
        }
 
        /**
+        * @param string $rigor Same format as Title::getUserPermissionErrors()
         * @return array
         */
-       protected function getEditPermissionErrors() {
+       protected function getEditPermissionErrors( $rigor = 'secure' ) {
                global $wgUser;
-               $permErrors = $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser );
+
+               $permErrors = $this->mTitle->getUserPermissionsErrors( 'edit', $wgUser, $rigor );
                # Can this title be created?
                if ( !$this->mTitle->exists() ) {
-                       $permErrors = array_merge( $permErrors,
-                               wfArrayDiff2( $this->mTitle->getUserPermissionsErrors( 'create', $wgUser ), $permErrors ) );
+                       $permErrors = array_merge(
+                               $permErrors,
+                               wfArrayDiff2(
+                                       $this->mTitle->getUserPermissionsErrors( 'create', $wgUser, $rigor ),
+                                       $permErrors
+                               )
+                       );
                }
                # Ignore some permissions errors when a user is just previewing/viewing diffs
                $remove = array();
@@ -591,6 +588,7 @@ class EditPage {
                        }
                }
                $permErrors = wfArrayDiff2( $permErrors, $remove );
+
                return $permErrors;
        }
 
@@ -732,13 +730,10 @@ class EditPage {
        function importFormData( &$request ) {
                global $wgContLang, $wgUser;
 
-               wfProfileIn( __METHOD__ );
-
                # Section edit can come from either the form or a link
                $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
 
                if ( $this->section !== null && $this->section !== '' && !$this->isSectionEditSupported() ) {
-                       wfProfileOut( __METHOD__ );
                        throw new ErrorPageError( 'sectioneditnotsupported-title', 'sectioneditnotsupported-text' );
                }
 
@@ -753,13 +748,10 @@ class EditPage {
                                // Skip this if wpTextbox2 has input, it indicates that we came
                                // from a conflict page with raw page text, not a custom form
                                // modified by subclasses
-                               wfProfileIn( get_class( $this ) . "::importContentFormData" );
                                $textbox1 = $this->importContentFormData( $request );
                                if ( $textbox1 !== null ) {
                                        $this->textbox1 = $textbox1;
                                }
-
-                               wfProfileOut( get_class( $this ) . "::importContentFormData" );
                        }
 
                        # Truncate for whole multibyte characters
@@ -931,7 +923,6 @@ class EditPage {
                // Allow extensions to modify form data
                Hooks::run( 'EditPage::importFormData', array( $this, $request ) );
 
-               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -992,8 +983,6 @@ class EditPage {
        protected function getContentObject( $def_content = null ) {
                global $wgOut, $wgRequest, $wgUser, $wgContLang;
 
-               wfProfileIn( __METHOD__ );
-
                $content = false;
 
                // For message page not locally set, use the i18n message.
@@ -1105,7 +1094,6 @@ class EditPage {
                        }
                }
 
-               wfProfileOut( __METHOD__ );
                return $content;
        }
 
@@ -1298,18 +1286,20 @@ class EditPage {
 
        /**
         * Attempt submission
+        * @param array $resultDetails See docs for $result in internalAttemptSave
         * @throws UserBlockedError|ReadOnlyError|ThrottledError|PermissionsError
-        * @return bool False if output is done, true if the rest of the form should be displayed
+        * @return Status The resulting status object.
         */
-       public function attemptSave() {
+       public function attemptSave( &$resultDetails = false ) {
                global $wgUser;
 
-               $resultDetails = false;
                # Allow bots to exempt some edits from bot flagging
                $bot = $wgUser->isAllowed( 'bot' ) && $this->bot;
                $status = $this->internalAttemptSave( $resultDetails, $bot );
 
-               return $this->handleStatus( $status, $resultDetails );
+               Hooks::run( 'EditPage::attemptSave:after', array( $this, $status, $resultDetails ) );
+
+               return $status;
        }
 
        /**
@@ -1538,15 +1528,10 @@ class EditPage {
 
                $status = Status::newGood();
 
-               wfProfileIn( __METHOD__ );
-               wfProfileIn( __METHOD__ . '-checks' );
-
                if ( !Hooks::run( 'EditPage::attemptSave', array( $this ) ) ) {
                        wfDebug( "Hook 'EditPage::attemptSave' aborted article saving\n" );
                        $status->fatal( 'hookaborted' );
                        $status->value = self::AS_HOOK_ERROR;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1563,8 +1548,6 @@ class EditPage {
                        );
                        $status->fatal( 'spamprotectionmatch', false );
                        $status->value = self::AS_SPAM_ERROR;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1579,8 +1562,6 @@ class EditPage {
                                $ex->getMessage()
                        );
                        $status->value = self::AS_PARSE_ERROR;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1592,9 +1573,6 @@ class EditPage {
                                $code = $wgUser->isAnon() ? self::AS_IMAGE_REDIRECT_ANON : self::AS_IMAGE_REDIRECT_LOGGED;
                                $status->setResult( false, $code );
 
-                               wfProfileOut( __METHOD__ . '-checks' );
-                               wfProfileOut( __METHOD__ );
-
                                return $status;
                }
 
@@ -1623,8 +1601,6 @@ class EditPage {
                        wfDebugLog( 'SpamRegex', "$ip spam regex hit [[$pdbk]]: \"$match\"" );
                        $status->fatal( 'spamprotectionmatch', $match );
                        $status->value = self::AS_SPAM_ERROR;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
                if ( !Hooks::run(
@@ -1634,15 +1610,11 @@ class EditPage {
                        # Error messages etc. could be handled within the hook...
                        $status->fatal( 'hookaborted' );
                        $status->value = self::AS_HOOK_ERROR;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                } elseif ( $this->hookError != '' ) {
                        # ...or the hook could be expecting us to produce an error
                        $status->fatal( 'hookaborted' );
                        $status->value = self::AS_HOOK_ERROR_EXPECTED;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1651,8 +1623,6 @@ class EditPage {
                        $wgUser->spreadAnyEditBlock();
                        # Check block state against master, thus 'false'.
                        $status->setResult( false, self::AS_BLOCKED_PAGE_FOR_USER );
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1661,22 +1631,16 @@ class EditPage {
                        // Error will be displayed by showEditForm()
                        $this->tooBig = true;
                        $status->setResult( false, self::AS_CONTENT_TOO_BIG );
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
                if ( !$wgUser->isAllowed( 'edit' ) ) {
                        if ( $wgUser->isAnon() ) {
                                $status->setResult( false, self::AS_READ_ONLY_PAGE_ANON );
-                               wfProfileOut( __METHOD__ . '-checks' );
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        } else {
                                $status->fatal( 'readonlytext' );
                                $status->value = self::AS_READ_ONLY_PAGE_LOGGED;
-                               wfProfileOut( __METHOD__ . '-checks' );
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
                }
@@ -1685,23 +1649,17 @@ class EditPage {
                        && !$wgUser->isAllowed( 'editcontentmodel' )
                ) {
                        $status->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
                if ( wfReadOnly() ) {
                        $status->fatal( 'readonlytext' );
                        $status->value = self::AS_READ_ONLY_PAGE;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
                if ( $wgUser->pingLimiter() || $wgUser->pingLimiter( 'linkpurge', 0 ) ) {
                        $status->fatal( 'actionthrottledtext' );
                        $status->value = self::AS_RATE_LIMITED;
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1709,13 +1667,9 @@ class EditPage {
                # confirmation
                if ( $this->wasDeletedSinceLastEdit() && !$this->recreate ) {
                        $status->setResult( false, self::AS_ARTICLE_WAS_DELETED );
-                       wfProfileOut( __METHOD__ . '-checks' );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
-               wfProfileOut( __METHOD__ . '-checks' );
-
                # Load the page data from the master. If anything changes in the meantime,
                # we detect it by using page_latest like a token in a 1 try compare-and-swap.
                $this->mArticle->loadPageData( 'fromdbmaster' );
@@ -1727,7 +1681,6 @@ class EditPage {
                                $status->fatal( 'nocreatetext' );
                                $status->value = self::AS_NO_CREATE_PERMISSION;
                                wfDebug( __METHOD__ . ": no create permission\n" );
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
 
@@ -1745,12 +1698,10 @@ class EditPage {
                                $this->blankArticle = true;
                                $status->fatal( 'blankarticle' );
                                $status->setResult( false, self::AS_BLANK_ARTICLE );
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
 
                        if ( !$this->runPostMergeFilters( $textbox_content, $status, $wgUser ) ) {
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
 
@@ -1855,12 +1806,10 @@ class EditPage {
 
                        if ( $this->isConflict ) {
                                $status->setResult( false, self::AS_CONFLICT_DETECTED );
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
 
                        if ( !$this->runPostMergeFilters( $content, $status, $wgUser ) ) {
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
 
@@ -1870,7 +1819,6 @@ class EditPage {
                                        $this->missingSummary = true;
                                        $status->fatal( 'missingsummary' ); // or 'missingcommentheader' if $section == 'new'. Blegh
                                        $status->value = self::AS_SUMMARY_NEEDED;
-                                       wfProfileOut( __METHOD__ );
                                        return $status;
                                }
 
@@ -1879,7 +1827,6 @@ class EditPage {
                                        $this->missingComment = true;
                                        $status->fatal( 'missingcommenttext' );
                                        $status->value = self::AS_TEXTBOX_EMPTY;
-                                       wfProfileOut( __METHOD__ );
                                        return $status;
                                }
                        } elseif ( !$this->allowBlankSummary
@@ -1890,12 +1837,10 @@ class EditPage {
                                $this->missingSummary = true;
                                $status->fatal( 'missingsummary' );
                                $status->value = self::AS_SUMMARY_NEEDED;
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
 
                        # All's well
-                       wfProfileIn( __METHOD__ . '-sectionanchor' );
                        $sectionanchor = '';
                        if ( $this->section == 'new' ) {
                                $this->summary = $this->newSectionSummary( $sectionanchor );
@@ -1912,7 +1857,6 @@ class EditPage {
                                }
                        }
                        $result['sectionanchor'] = $sectionanchor;
-                       wfProfileOut( __METHOD__ . '-sectionanchor' );
 
                        // Save errors may fall down to the edit form, but we've now
                        // merged the section into full text. Clear the section field
@@ -1934,7 +1878,6 @@ class EditPage {
                                $this->selfRedirect = true;
                                $status->fatal( 'selfredirect' );
                                $status->value = self::AS_SELF_REDIRECT;
-                               wfProfileOut( __METHOD__ );
                                return $status;
                        }
                }
@@ -1944,7 +1887,6 @@ class EditPage {
                if ( $this->kblength > $wgMaxArticleSize ) {
                        $this->tooBig = true;
                        $status->setResult( false, self::AS_MAX_ARTICLE_SIZE_EXCEEDED );
-                       wfProfileOut( __METHOD__ );
                        return $status;
                }
 
@@ -1974,7 +1916,6 @@ class EditPage {
                                // Destroys data doEdit() put in $status->value but who cares
                                $doEditStatus->value = self::AS_END;
                        }
-                       wfProfileOut( __METHOD__ );
                        return $doEditStatus;
                }
 
@@ -1985,7 +1926,6 @@ class EditPage {
                }
                $result['redirect'] = $content->isRedirect();
                $this->updateWatchlist();
-               wfProfileOut( __METHOD__ );
                return $status;
        }
 
@@ -2022,7 +1962,6 @@ class EditPage {
         * @return bool
         */
        private function mergeChangesIntoContent( &$editContent ) {
-               wfProfileIn( __METHOD__ );
 
                $db = wfGetDB( DB_MASTER );
 
@@ -2031,7 +1970,6 @@ class EditPage {
                $baseContent = $baseRevision ? $baseRevision->getContent() : null;
 
                if ( is_null( $baseContent ) ) {
-                       wfProfileOut( __METHOD__ );
                        return false;
                }
 
@@ -2040,7 +1978,6 @@ class EditPage {
                $currentContent = $currentRevision ? $currentRevision->getContent() : null;
 
                if ( is_null( $currentContent ) ) {
-                       wfProfileOut( __METHOD__ );
                        return false;
                }
 
@@ -2050,11 +1987,9 @@ class EditPage {
 
                if ( $result ) {
                        $editContent = $result;
-                       wfProfileOut( __METHOD__ );
                        return true;
                }
 
-               wfProfileOut( __METHOD__ );
                return false;
        }
 
@@ -2373,8 +2308,6 @@ class EditPage {
        function showEditForm( $formCallback = null ) {
                global $wgOut, $wgUser;
 
-               wfProfileIn( __METHOD__ );
-
                # need to parse the preview early so that we know which templates are used,
                # otherwise users with "show preview after edit box" will get a blank list
                # we parse this near the beginning so that setHeaders can do the title
@@ -2389,7 +2322,6 @@ class EditPage {
                $this->setHeaders();
 
                if ( $this->showHeader() === false ) {
-                       wfProfileOut( __METHOD__ );
                        return;
                }
 
@@ -2593,7 +2525,6 @@ class EditPage {
                        $this->displayPreviewArea( $previewOutput, false );
                }
 
-               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -3065,6 +2996,12 @@ HTML
 
                if ( $this->formtype == 'preview' ) {
                        $this->showPreview( $previewOutput );
+               } else {
+                       // Empty content container for LivePreview
+                       $pageViewLang = $this->mTitle->getPageViewLanguage();
+                       $attribs = array( 'lang' => $pageViewLang->getHtmlCode(), 'dir' => $pageViewLang->getDir(),
+                               'class' => 'mw-content-' . $pageViewLang->getDir() );
+                       $wgOut->addHTML( Html::rawElement( 'div', $attribs ) );
                }
 
                $wgOut->addHTML( '</div>' );
@@ -3250,8 +3187,6 @@ HTML
                        return '';
                }
 
-               wfProfileIn( __METHOD__ );
-
                $limitReport = Html::rawElement( 'div', array( 'class' => 'mw-limitReportExplanation' ),
                        wfMessage( 'limitreport-title' )->parseAsBlock()
                );
@@ -3286,8 +3221,6 @@ HTML
                        Html::closeElement( 'table' ) .
                        Html::closeElement( 'div' );
 
-               wfProfileOut( __METHOD__ );
-
                return $limitReport;
        }
 
@@ -3473,8 +3406,6 @@ HTML
                global $wgOut, $wgUser, $wgRawHtml, $wgLang;
                global $wgAllowUserCss, $wgAllowUserJs;
 
-               wfProfileIn( __METHOD__ );
-
                if ( $wgRawHtml && !$this->mTokenOk ) {
                        // Could be an offsite preview attempt. This is very unsafe if
                        // HTML is enabled, as it could be an attack.
@@ -3486,7 +3417,6 @@ HTML
                                $parsedNote = $wgOut->parse( "<div class='previewnote'>" .
                                        wfMessage( 'session_fail_preview_html' )->text() . "</div>", true, /* interface */true );
                        }
-                       wfProfileOut( __METHOD__ );
                        return $parsedNote;
                }
 
@@ -3500,7 +3430,6 @@ HTML
                                'AlternateEditPreview',
                                array( $this, &$content, &$previewHTML, &$this->mParserOutput ) )
                        ) {
-                               wfProfileOut( __METHOD__ );
                                return $previewHTML;
                        }
 
@@ -3573,6 +3502,8 @@ HTML
                        # But it's now deprecated, so never mind
 
                        $pstContent = $content->preSaveTransform( $this->mTitle, $wgUser, $parserOptions );
+                       $scopedCallback = $parserOptions->setupFakeRevision(
+                               $this->mTitle, $pstContent, $wgUser );
                        $parserOutput = $pstContent->getParserOutput( $this->mTitle, null, $parserOptions );
 
                        # Try to stash the edit for the final submission step
@@ -3617,7 +3548,6 @@ HTML
                        'class' => 'mw-content-' . $pageViewLang->getDir() );
                $previewHTML = Html::rawElement( 'div', $attribs, $previewHTML );
 
-               wfProfileOut( __METHOD__ );
                return $previewhead . $previewHTML . $this->previewTextAfterContent;
        }