Merge "Remove last remnants of pre-1.16 live preview"
[lhc/web/wiklou.git] / includes / EditPage.php
index 2321bde..745f8de 100644 (file)
@@ -22,6 +22,7 @@
 
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
+use Wikimedia\ScopedCallback;
 
 /**
  * The edit page/HTML interface (split from Article)
@@ -407,6 +408,11 @@ class EditPage {
         */
        protected $context;
 
+       /**
+        * @var bool Whether an old revision is edited
+        */
+       private $isOldRev = false;
+
        /**
         * @param Article $article
         */
@@ -1038,7 +1044,6 @@ class EditPage {
 
                // Allow extensions to modify form data
                Hooks::run( 'EditPage::importFormData', [ $this, $request ] );
-
        }
 
        /**
@@ -1613,8 +1618,9 @@ class EditPage {
        protected function runPostMergeFilters( Content $content, Status $status, User $user ) {
                // Run old style post-section-merge edit filter
                if ( !ContentHandler::runLegacyHooks( 'EditFilterMerged',
-                       [ $this, $content, &$this->hookError, $this->summary ] )
-               ) {
+                       [ $this, $content, &$this->hookError, $this->summary ],
+                       '1.21'
+               ) ) {
                        # Error messages etc. could be handled within the hook...
                        $status->fatal( 'hookaborted' );
                        $status->value = self::AS_HOOK_ERROR;
@@ -2230,7 +2236,6 @@ class EditPage {
         * @return bool
         */
        private function mergeChangesIntoContent( &$editContent ) {
-
                $db = wfGetDB( DB_MASTER );
 
                // This is the revision the editor started from
@@ -2321,9 +2326,12 @@ class EditPage {
        }
 
        function setHeaders() {
-               global $wgOut, $wgUser, $wgAjaxEditStash;
+               global $wgOut, $wgUser, $wgAjaxEditStash, $wgCookieSetOnAutoblock;
 
                $wgOut->addModules( 'mediawiki.action.edit' );
+               if ( $wgCookieSetOnAutoblock === true ) {
+                       $wgOut->addModules( 'mediawiki.user.blockcookie' );
+               }
                $wgOut->addModuleStyles( 'mediawiki.action.edit.styles' );
 
                if ( $wgUser->getOption( 'showtoolbar' ) ) {
@@ -2593,10 +2601,21 @@ class EditPage {
 
                $this->setHeaders();
 
-               if ( $this->showHeader() === false ) {
+               $this->addTalkPageText();
+               $this->addEditNotices();
+
+               if ( !$this->isConflict &&
+                       $this->section != '' &&
+                       !$this->isSectionEditSupported() ) {
+                       // We use $this->section to much before this and getVal('wgSection') directly in other places
+                       // at this point we can't reset $this->section to '' to fallback to non-section editing.
+                       // Someone is welcome to try refactoring though
+                       $wgOut->showErrorPage( 'sectioneditnotsupported-title', 'sectioneditnotsupported-text' );
                        return;
                }
 
+               $this->showHeader();
+
                $wgOut->addHTML( $this->editFormPageTop );
 
                if ( $wgUser->getOption( 'previewontop' ) ) {
@@ -2766,9 +2785,8 @@ class EditPage {
                $wgOut->addHTML( Html::rawElement( 'div', [ 'class' => 'hiddencats' ],
                        Linker::formatHiddenCategories( $this->page->getHiddenCategories() ) ) );
 
-               if ( $this->mParserOutput ) {
-                       $wgOut->setLimitReportData( $this->mParserOutput->getLimitReportData() );
-               }
+               $wgOut->addHTML( Html::rawElement( 'div', [ 'class' => 'limitreport' ],
+                       self::getPreviewLimitReport( $this->mParserOutput ) ) );
 
                $wgOut->addModules( 'mediawiki.action.edit.collapsibleFooter' );
 
@@ -2807,7 +2825,6 @@ class EditPage {
                if ( !$wgUser->getOption( 'previewontop' ) ) {
                        $this->displayPreviewArea( $previewOutput, false );
                }
-
        }
 
        /**
@@ -2833,7 +2850,6 @@ class EditPage {
                return Html::rawElement( 'div', [ 'class' => 'templatesUsed' ],
                        $templateListFormatter->format( $templates, $type )
                );
-
        }
 
        /**
@@ -2852,44 +2868,14 @@ class EditPage {
                }
        }
 
-       /**
-        * @return bool
-        */
        protected function showHeader() {
-               global $wgOut, $wgUser, $wgMaxArticleSize, $wgLang;
+               global $wgOut, $wgUser;
                global $wgAllowUserCss, $wgAllowUserJs;
 
-               if ( $this->mTitle->isTalkPage() ) {
-                       $wgOut->addWikiMsg( 'talkpagetext' );
-               }
-
-               // Add edit notices
-               $editNotices = $this->mTitle->getEditNotices( $this->oldid );
-               if ( count( $editNotices ) ) {
-                       $wgOut->addHTML( implode( "\n", $editNotices ) );
-               } else {
-                       $msg = $this->context->msg( 'editnotice-notext' );
-                       if ( !$msg->isDisabled() ) {
-                               $wgOut->addHTML(
-                                       '<div class="mw-editnotice-notext">'
-                                       . $msg->parseAsBlock()
-                                       . '</div>'
-                               );
-                       }
-               }
-
                if ( $this->isConflict ) {
-                       $wgOut->wrapWikiMsg( "<div class='mw-explainconflict'>\n$1\n</div>", 'explainconflict' );
+                       $this->addExplainConflictHeader( $wgOut );
                        $this->editRevId = $this->page->getLatest();
                } else {
-                       if ( $this->section != '' && !$this->isSectionEditSupported() ) {
-                               // We use $this->section to much before this and getVal('wgSection') directly in other places
-                               // at this point we can't reset $this->section to '' to fallback to non-section editing.
-                               // Someone is welcome to try refactoring though
-                               $wgOut->showErrorPage( 'sectioneditnotsupported-title', 'sectioneditnotsupported-text' );
-                               return false;
-                       }
-
                        if ( $this->section != '' && $this->section != 'new' ) {
                                if ( !$this->summary && !$this->preview && !$this->diff ) {
                                        $sectionTitle = self::extractSectionTitle( $this->textbox1 ); // FIXME: use Content object
@@ -2947,6 +2933,7 @@ class EditPage {
                                        if ( !$revision->isCurrent() ) {
                                                $this->mArticle->setOldSubtitle( $revision->getId() );
                                                $wgOut->addWikiMsg( 'editingold' );
+                                               $this->isOldRev = true;
                                        }
                                } elseif ( $this->mTitle->exists() ) {
                                        // Something went wrong
@@ -3014,69 +3001,12 @@ class EditPage {
                        }
                }
 
-               if ( $this->mTitle->isProtected( 'edit' ) &&
-                       MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace() ) !== [ '' ]
-               ) {
-                       # Is the title semi-protected?
-                       if ( $this->mTitle->isSemiProtected() ) {
-                               $noticeMsg = 'semiprotectedpagewarning';
-                       } else {
-                               # Then it must be protected based on static groups (regular)
-                               $noticeMsg = 'protectedpagewarning';
-                       }
-                       LogEventsList::showLogExtract( $wgOut, 'protect', $this->mTitle, '',
-                               [ 'lim' => 1, 'msgKey' => [ $noticeMsg ] ] );
-               }
-               if ( $this->mTitle->isCascadeProtected() ) {
-                       # Is this page under cascading protection from some source pages?
-                       /** @var Title[] $cascadeSources */
-                       list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
-                       $notice = "<div class='mw-cascadeprotectedwarning'>\n$1\n";
-                       $cascadeSourcesCount = count( $cascadeSources );
-                       if ( $cascadeSourcesCount > 0 ) {
-                               # Explain, and list the titles responsible
-                               foreach ( $cascadeSources as $page ) {
-                                       $notice .= '* [[:' . $page->getPrefixedText() . "]]\n";
-                               }
-                       }
-                       $notice .= '</div>';
-                       $wgOut->wrapWikiMsg( $notice, [ 'cascadeprotectedwarning', $cascadeSourcesCount ] );
-               }
-               if ( !$this->mTitle->exists() && $this->mTitle->getRestrictions( 'create' ) ) {
-                       LogEventsList::showLogExtract( $wgOut, 'protect', $this->mTitle, '',
-                               [ 'lim' => 1,
-                                       'showIfEmpty' => false,
-                                       'msgKey' => [ 'titleprotectedwarning' ],
-                                       'wrap' => "<div class=\"mw-titleprotectedwarning\">\n$1</div>" ] );
-               }
+               $this->addPageProtectionWarningHeaders();
 
-               if ( $this->contentLength === false ) {
-                       $this->contentLength = strlen( $this->textbox1 );
-               }
+               $this->addLongPageWarningHeader();
 
-               if ( $this->tooBig || $this->contentLength > $wgMaxArticleSize * 1024 ) {
-                       $wgOut->wrapWikiMsg( "<div class='error' id='mw-edit-longpageerror'>\n$1\n</div>",
-                               [
-                                       'longpageerror',
-                                       $wgLang->formatNum( round( $this->contentLength / 1024, 3 ) ),
-                                       $wgLang->formatNum( $wgMaxArticleSize )
-                               ]
-                       );
-               } else {
-                       if ( !$this->context->msg( 'longpage-hint' )->isDisabled() ) {
-                               $wgOut->wrapWikiMsg( "<div id='mw-edit-longpage-hint'>\n$1\n</div>",
-                                       [
-                                               'longpage-hint',
-                                               $wgLang->formatSize( strlen( $this->textbox1 ) ),
-                                               strlen( $this->textbox1 )
-                                       ]
-                               );
-                       }
-               }
                # Add header copyright warning
                $this->showHeaderCopyrightWarning();
-
-               return true;
        }
 
        /**
@@ -3257,6 +3187,10 @@ HTML
                                        $classes[] = 'mw-textarea-cprotected';
                                }
                        }
+                       # Is an old revision being edited?
+                       if ( $this->isOldRev ) {
+                               $classes[] = 'mw-textarea-oldrev';
+                       }
 
                        $attribs = [ 'tabindex' => 1 ];
 
@@ -3287,27 +3221,9 @@ HTML
                global $wgOut, $wgUser;
 
                $wikitext = $this->safeUnicodeOutput( $text );
-               if ( strval( $wikitext ) !== '' ) {
-                       // Ensure there's a newline at the end, otherwise adding lines
-                       // is awkward.
-                       // But don't add a newline if the ext is empty, or Firefox in XHTML
-                       // mode will show an extra newline. A bit annoying.
-                       $wikitext .= "\n";
-               }
+               $wikitext = $this->addNewLineAtEnd( $wikitext );
 
-               $attribs = $customAttribs + [
-                       'accesskey' => ',',
-                       'id' => $name,
-                       'cols' => $wgUser->getIntOption( 'cols' ),
-                       'rows' => $wgUser->getIntOption( 'rows' ),
-                       // Avoid PHP notices when appending preferences
-                       // (appending allows customAttribs['style'] to still work).
-                       'style' => ''
-               ];
-
-               $pageLang = $this->mTitle->getPageLanguage();
-               $attribs['lang'] = $pageLang->getHtmlCode();
-               $attribs['dir'] = $pageLang->getDir();
+               $attribs = $this->buildTextboxAttribs( $name, $customAttribs, $wgUser );
 
                $wgOut->addHTML( Html::textarea( $name, $wikitext, $attribs ) );
        }
@@ -3410,7 +3326,7 @@ HTML
                }
 
                if ( $newContent ) {
-                       ContentHandler::runLegacyHooks( 'EditPageGetDiffText', [ $this, &$newContent ] );
+                       ContentHandler::runLegacyHooks( 'EditPageGetDiffText', [ $this, &$newContent ], '1.21' );
                        Hooks::run( 'EditPageGetDiffContent', [ $this, &$newContent ] );
 
                        $popts = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
@@ -3494,9 +3410,10 @@ HTML
         *
         * @param Title $title
         * @param string $format Output format, valid values are any function of a Message object
+        * @param Language|string|null $langcode Language code or Language object.
         * @return string
         */
-       public static function getCopyrightWarning( $title, $format = 'plain' ) {
+       public static function getCopyrightWarning( $title, $format = 'plain', $langcode = null ) {
                global $wgRightsText;
                if ( $wgRightsText ) {
                        $copywarnMsg = [ 'copyrightwarning',
@@ -3509,8 +3426,12 @@ HTML
                // Allow for site and per-namespace customization of contribution/copyright notice.
                Hooks::run( 'EditPageCopyrightWarning', [ $title, &$copywarnMsg ] );
 
+               $msg = call_user_func_array( 'wfMessage', $copywarnMsg )->title( $title );
+               if ( $langcode ) {
+                       $msg->inLanguage( $langcode );
+               }
                return "<div id=\"editpage-copywarn\">\n" .
-                       call_user_func_array( 'wfMessage', $copywarnMsg )->title( $title )->$format() . "\n</div>";
+                       $msg->$format() . "\n</div>";
        }
 
        /**
@@ -3525,12 +3446,41 @@ HTML
                        return '';
                }
 
-               return ResourceLoader::makeInlineScript(
-                       ResourceLoader::makeConfigSetScript(
-                               [ 'wgPageParseReport' => $output->getLimitReportData() ],
-                               true
-                       )
+               $limitReport = Html::rawElement( 'div', [ 'class' => 'mw-limitReportExplanation' ],
+                       wfMessage( 'limitreport-title' )->parseAsBlock()
                );
+
+               // Show/hide animation doesn't work correctly on a table, so wrap it in a div.
+               $limitReport .= Html::openElement( 'div', [ 'class' => 'preview-limit-report-wrapper' ] );
+
+               $limitReport .= Html::openElement( 'table', [
+                       'class' => 'preview-limit-report wikitable'
+               ] ) .
+                       Html::openElement( 'tbody' );
+
+               foreach ( $output->getLimitReportData() as $key => $value ) {
+                       if ( Hooks::run( 'ParserLimitReportFormat',
+                               [ $key, &$value, &$limitReport, true, true ]
+                       ) ) {
+                               $keyMsg = wfMessage( $key );
+                               $valueMsg = wfMessage( [ "$key-value-html", "$key-value" ] );
+                               if ( !$valueMsg->exists() ) {
+                                       $valueMsg = new RawMessage( '$1' );
+                               }
+                               if ( !$keyMsg->isDisabled() && !$valueMsg->isDisabled() ) {
+                                       $limitReport .= Html::openElement( 'tr' ) .
+                                               Html::rawElement( 'th', null, $keyMsg->parse() ) .
+                                               Html::rawElement( 'td', null, $valueMsg->params( $value )->parse() ) .
+                                               Html::closeElement( 'tr' );
+                               }
+                       }
+               }
+
+               $limitReport .= Html::closeElement( 'tbody' ) .
+                       Html::closeElement( 'table' ) .
+                       Html::closeElement( 'div' );
+
+               return $limitReport;
        }
 
        protected function showStandardInputs( &$tabindex = 2 ) {
@@ -3588,7 +3538,7 @@ HTML
                global $wgOut;
 
                if ( Hooks::run( 'EditPageBeforeConflictDiff', [ &$this, &$wgOut ] ) ) {
-                       $stats = $wgOut->getContext()->getStats();
+                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
                        $stats->increment( 'edit.failures.conflict' );
                        // Only include 'standard' namespaces to avoid creating unknown numbers of statsd metrics
                        if (
@@ -3727,7 +3677,7 @@ HTML
                global $wgOut, $wgRawHtml, $wgLang;
                global $wgAllowUserCss, $wgAllowUserJs;
 
-               $stats = $wgOut->getContext()->getStats();
+               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
 
                if ( $wgRawHtml && !$this->mTokenOk ) {
                        // Could be an offsite preview attempt. This is very unsafe if
@@ -3819,7 +3769,7 @@ HTML
                        }
 
                        $hook_args = [ $this, &$content ];
-                       ContentHandler::runLegacyHooks( 'EditPageGetPreviewText', $hook_args );
+                       ContentHandler::runLegacyHooks( 'EditPageGetPreviewText', $hook_args, '1.25' );
                        Hooks::run( 'EditPageGetPreviewContent', $hook_args );
 
                        $parserResult = $this->doPreviewParse( $content );
@@ -4145,7 +4095,7 @@ HTML
                        'name' => 'wpSave',
                        'tabindex' => ++$tabindex,
                ] + Linker::tooltipAndAccesskeyAttribs( 'save' );
-               $buttons['save'] = Html::submitButton( $buttonLabel, $attribs, [ 'mw-ui-constructive' ] );
+               $buttons['save'] = Html::submitButton( $buttonLabel, $attribs, [ 'mw-ui-progressive' ] );
 
                ++$tabindex; // use the same for preview and live preview
                $attribs = [
@@ -4262,7 +4212,7 @@ HTML
        protected function safeUnicodeOutput( $text ) {
                return $this->checkUnicodeCompliantBrowser()
                        ? $text
-                       : $this->makesafe( $text );
+                       : $this->makeSafe( $text );
        }
 
        /**
@@ -4347,4 +4297,177 @@ HTML
                // reverse the transform that we made for reversibility reasons.
                return strtr( $result, [ "&#x0" => "&#x" ] );
        }
+
+       /**
+        * @since 1.29
+        */
+       protected function addEditNotices() {
+               global $wgOut;
+
+               $editNotices = $this->mTitle->getEditNotices( $this->oldid );
+               if ( count( $editNotices ) ) {
+                       $wgOut->addHTML( implode( "\n", $editNotices ) );
+               } else {
+                       $msg = $this->context->msg( 'editnotice-notext' );
+                       if ( !$msg->isDisabled() ) {
+                               $wgOut->addHTML(
+                                       '<div class="mw-editnotice-notext">'
+                                       . $msg->parseAsBlock()
+                                       . '</div>'
+                               );
+                       }
+               }
+       }
+
+       /**
+        * @since 1.29
+        */
+       protected function addTalkPageText() {
+               global $wgOut;
+
+               if ( $this->mTitle->isTalkPage() ) {
+                       $wgOut->addWikiMsg( 'talkpagetext' );
+               }
+       }
+
+       /**
+        * @since 1.29
+        */
+       protected function addLongPageWarningHeader() {
+               global $wgMaxArticleSize, $wgOut, $wgLang;
+
+               if ( $this->contentLength === false ) {
+                       $this->contentLength = strlen( $this->textbox1 );
+               }
+
+               if ( $this->tooBig || $this->contentLength > $wgMaxArticleSize * 1024 ) {
+                       $wgOut->wrapWikiMsg( "<div class='error' id='mw-edit-longpageerror'>\n$1\n</div>",
+                               [
+                                       'longpageerror',
+                                       $wgLang->formatNum( round( $this->contentLength / 1024, 3 ) ),
+                                       $wgLang->formatNum( $wgMaxArticleSize )
+                               ]
+                       );
+               } else {
+                       if ( !$this->context->msg( 'longpage-hint' )->isDisabled() ) {
+                               $wgOut->wrapWikiMsg( "<div id='mw-edit-longpage-hint'>\n$1\n</div>",
+                                       [
+                                               'longpage-hint',
+                                               $wgLang->formatSize( strlen( $this->textbox1 ) ),
+                                               strlen( $this->textbox1 )
+                                       ]
+                               );
+                       }
+               }
+       }
+
+       /**
+        * @since 1.29
+        */
+       protected function addPageProtectionWarningHeaders() {
+               global $wgOut;
+
+               if ( $this->mTitle->isProtected( 'edit' ) &&
+                       MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace() ) !== [ '' ]
+               ) {
+                       # Is the title semi-protected?
+                       if ( $this->mTitle->isSemiProtected() ) {
+                               $noticeMsg = 'semiprotectedpagewarning';
+                       } else {
+                               # Then it must be protected based on static groups (regular)
+                               $noticeMsg = 'protectedpagewarning';
+                       }
+                       LogEventsList::showLogExtract( $wgOut, 'protect', $this->mTitle, '',
+                               [ 'lim' => 1, 'msgKey' => [ $noticeMsg ] ] );
+               }
+               if ( $this->mTitle->isCascadeProtected() ) {
+                       # Is this page under cascading protection from some source pages?
+                       /** @var Title[] $cascadeSources */
+                       list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
+                       $notice = "<div class='mw-cascadeprotectedwarning'>\n$1\n";
+                       $cascadeSourcesCount = count( $cascadeSources );
+                       if ( $cascadeSourcesCount > 0 ) {
+                               # Explain, and list the titles responsible
+                               foreach ( $cascadeSources as $page ) {
+                                       $notice .= '* [[:' . $page->getPrefixedText() . "]]\n";
+                               }
+                       }
+                       $notice .= '</div>';
+                       $wgOut->wrapWikiMsg( $notice, [ 'cascadeprotectedwarning', $cascadeSourcesCount ] );
+               }
+               if ( !$this->mTitle->exists() && $this->mTitle->getRestrictions( 'create' ) ) {
+                       LogEventsList::showLogExtract( $wgOut, 'protect', $this->mTitle, '',
+                               [ 'lim' => 1,
+                                       'showIfEmpty' => false,
+                                       'msgKey' => [ 'titleprotectedwarning' ],
+                                       'wrap' => "<div class=\"mw-titleprotectedwarning\">\n$1</div>" ] );
+               }
+       }
+
+       /**
+        * @param OutputPage $out
+        * @since 1.29
+        */
+       protected function addExplainConflictHeader( OutputPage $out ) {
+               $out->wrapWikiMsg( "<div class='mw-explainconflict'>\n$1\n</div>", 'explainconflict' );
+       }
+
+       /**
+        * @param string $name
+        * @param mixed[] $customAttribs
+        * @param User $user
+        * @return mixed[]
+        * @since 1.29
+        */
+       protected function buildTextboxAttribs( $name, array $customAttribs, User $user ) {
+               $attribs = $customAttribs + [
+                               'accesskey' => ',',
+                               'id' => $name,
+                               'cols' => $user->getIntOption( 'cols' ),
+                               'rows' => $user->getIntOption( 'rows' ),
+                               // Avoid PHP notices when appending preferences
+                               // (appending allows customAttribs['style'] to still work).
+                               'style' => ''
+                       ];
+
+               // The following classes can be used here:
+               // * mw-editfont-default
+               // * mw-editfont-monospace
+               // * mw-editfont-sans-serif
+               // * mw-editfont-serif
+               $class = 'mw-editfont-' . $user->getOption( 'editfont' );
+
+               if ( isset( $attribs['class'] ) ) {
+                       if ( is_string( $attribs['class'] ) ) {
+                               $attribs['class'] .= ' ' . $class;
+                       } elseif ( is_array( $attribs['class'] ) ) {
+                               $attribs['class'][] = $class;
+                       }
+               } else {
+                       $attribs['class'] = $class;
+               }
+
+               $pageLang = $this->mTitle->getPageLanguage();
+               $attribs['lang'] = $pageLang->getHtmlCode();
+               $attribs['dir'] = $pageLang->getDir();
+
+               return $attribs;
+       }
+
+       /**
+        * @param string $wikitext
+        * @return string
+        * @since 1.29
+        */
+       protected function addNewLineAtEnd( $wikitext ) {
+               if ( strval( $wikitext ) !== '' ) {
+                       // Ensure there's a newline at the end, otherwise adding lines
+                       // is awkward.
+                       // But don't add a newline if the text is empty, or Firefox in XHTML
+                       // mode will show an extra newline. A bit annoying.
+                       $wikitext .= "\n";
+                       return $wikitext;
+               }
+               return $wikitext;
+       }
 }