Merge "Ensure users are able to edit the page after changing the content model"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 23 Sep 2016 06:16:11 +0000 (06:16 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 23 Sep 2016 06:16:11 +0000 (06:16 +0000)
1  2 
includes/EditPage.php
includes/Title.php
includes/specials/SpecialChangeContentModel.php

diff --combined includes/EditPage.php
@@@ -21,7 -21,6 +21,7 @@@
   */
  
  use MediaWiki\Logger\LoggerFactory;
 +use MediaWiki\MediaWikiServices;
  
  /**
   * The edit page/HTML interface (split from Article)
@@@ -578,7 -577,7 +578,7 @@@ class EditPage 
                        ) {
                                $this->displayViewSourcePage(
                                        $this->getContentObject(),
 -                                      wfMessage(
 +                                      $this->context->msg(
                                                'contentmodelediterror',
                                                $revision->getContentModel(),
                                                $this->contentModel
                Hooks::run( 'EditPage::showReadOnlyForm:initial', [ $this, &$wgOut ] );
  
                $wgOut->setRobotPolicy( 'noindex,nofollow' );
 -              $wgOut->setPageTitle( wfMessage(
 +              $wgOut->setPageTitle( $this->context->msg(
                        'viewsource-title',
                        $this->getContextTitle()->getPrefixedText()
                ) );
                $this->showTextbox( $text, 'wpTextbox1', [ 'readonly' ] );
                $wgOut->addHTML( $this->editFormTextAfterContent );
  
 -              $wgOut->addHTML( Html::rawElement( 'div', [ 'class' => 'templatesUsed' ],
 -                      Linker::formatTemplates( $this->getTemplates() ) ) );
 +              $wgOut->addHTML( $this->makeTemplatesOnThisPageList( $this->getTemplates() ) );
  
                $wgOut->addModules( 'mediawiki.action.edit.collapsibleFooter' );
  
                // May be overridden by revision.
                $this->contentFormat = $request->getText( 'format', $this->contentFormat );
  
 -              if ( !ContentHandler::getForModelID( $this->contentModel )
 -                      ->isSupportedFormat( $this->contentFormat )
 -              ) {
 +              try {
 +                      $handler = ContentHandler::getForModelID( $this->contentModel );
 +              } catch ( MWUnknownContentModelException $e ) {
 +                      throw new ErrorPageError(
 +                              'editpage-invalidcontentmodel-title',
 +                              'editpage-invalidcontentmodel-text',
 +                              [ $this->contentModel ]
 +                      );
 +              }
 +
 +              if ( !$handler->isSupportedFormat( $this->contentFormat ) ) {
                        throw new ErrorPageError(
                                'editpage-notsupportedcontentformat-title',
                                'editpage-notsupportedcontentformat-text',
                                                                if ( $firstrev && $firstrev->getId() == $undo ) {
                                                                        $userText = $undorev->getUserText();
                                                                        if ( $userText === '' ) {
 -                                                                              $undoSummary = wfMessage(
 +                                                                              $undoSummary = $this->context->msg(
                                                                                        'undo-summary-username-hidden',
                                                                                        $undo
                                                                                )->inContentLanguage()->text();
                                                                        } else {
 -                                                                              $undoSummary = wfMessage(
 +                                                                              $undoSummary = $this->context->msg(
                                                                                        'undo-summary',
                                                                                        $undo,
                                                                                        $userText
                                                                        if ( $this->summary === '' ) {
                                                                                $this->summary = $undoSummary;
                                                                        } else {
 -                                                                              $this->summary = $undoSummary . wfMessage( 'colon-separator' )
 +                                                                              $this->summary = $undoSummary . $this->context->msg( 'colon-separator' )
                                                                                        ->inContentLanguage()->text() . $this->summary;
                                                                        }
                                                                        $this->undidRev = $undo;
                                        // Messages: undo-success, undo-failure, undo-norev, undo-nochange
                                        $class = ( $undoMsg == 'success' ? '' : 'error ' ) . "mw-undo-{$undoMsg}";
                                        $this->editFormPageTop .= $wgOut->parse( "<div class=\"{$class}\">" .
 -                                              wfMessage( 'undo-' . $undoMsg )->plain() . '</div>', true, /* interface */true );
 +                                              $this->context->msg( 'undo-' . $undoMsg )->plain() . '</div>', true, /* interface */true );
                                }
  
                                if ( $content === false ) {
                        // passed.
                        if ( $this->summary === '' ) {
                                $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
 -                              return wfMessage( 'newsectionsummary' )
 +                              return $this->context->msg( 'newsectionsummary' )
                                        ->rawParams( $cleanSectionTitle )->inContentLanguage()->text();
                        }
                } elseif ( $this->summary !== '' ) {
                        # This is a new section, so create a link to the new section
                        # in the revision summary.
                        $cleanSummary = $wgParser->stripSectionName( $this->summary );
 -                      return wfMessage( 'newsectionsummary' )
 +                      return $this->context->msg( 'newsectionsummary' )
                                ->rawParams( $cleanSummary )->inContentLanguage()->text();
                }
                return $this->summary;
                        } elseif ( !$wgUser->isAllowed( 'editcontentmodel' ) ) {
                                $status->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
                                return $status;
                        }
+                       // Make sure the user can edit the page under the new content model too
+                       $titleWithNewContentModel = clone $this->mTitle;
+                       $titleWithNewContentModel->setContentModel( $this->contentModel );
+                       if ( !$titleWithNewContentModel->userCan( 'editcontentmodel', $wgUser )
+                               || !$titleWithNewContentModel->userCan( 'edit', $wgUser )
+                       ) {
+                               $status->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
+                               return $status;
+                       }
                        $changingContentModel = true;
                        $oldContentModel = $this->mTitle->getContentModel();
                }
                if ( $displayTitle === false ) {
                        $displayTitle = $contextTitle->getPrefixedText();
                }
 -              $wgOut->setPageTitle( wfMessage( $msg, $displayTitle ) );
 +              $wgOut->setPageTitle( $this->context->msg( $msg, $displayTitle ) );
                # Transmit the name of the message to JavaScript for live preview
                # Keep Resources.php/mediawiki.action.edit.preview in sync with the possible keys
                $wgOut->addJsConfigVars( [
                # Try to add a custom edit intro, or use the standard one if this is not possible.
                if ( !$this->showCustomIntro() && !$this->mTitle->exists() ) {
                        $helpLink = wfExpandUrl( Skin::makeInternalOrExternalUrl(
 -                              wfMessage( 'helppage' )->inContentLanguage()->text()
 +                              $this->context->msg( 'helppage' )->inContentLanguage()->text()
                        ) );
                        if ( $wgUser->isLoggedIn() ) {
                                $wgOut->wrapWikiMsg(
                        . Html::rawElement(
                                'label',
                                [ 'for' => 'wpAntispam' ],
 -                              wfMessage( 'simpleantispam-label' )->parse()
 +                              $this->context->msg( 'simpleantispam-label' )->parse()
                        )
                        . Xml::element(
                                'input',
                                : 'confirmrecreate';
                        $wgOut->addHTML(
                                '<div class="mw-confirm-recreate">' .
 -                                      wfMessage( $key, $username, "<nowiki>$comment</nowiki>" )->parse() .
 -                              Xml::checkLabel( wfMessage( 'recreate' )->text(), 'wpRecreate', 'wpRecreate', false,
 +                                      $this->context->msg( $key, $username, "<nowiki>$comment</nowiki>" )->parse() .
 +                              Xml::checkLabel( $this->context->msg( 'recreate' )->text(), 'wpRecreate', 'wpRecreate', false,
                                        [ 'title' => Linker::titleAttrib( 'recreate' ), 'tabindex' => 1, 'id' => 'wpRecreate' ]
                                ) .
                                '</div>'
  
                $wgOut->addHTML( $this->editFormTextAfterTools . "\n" );
  
 -              $wgOut->addHTML( Html::rawElement( 'div', [ 'class' => 'templatesUsed' ],
 -                      Linker::formatTemplates( $this->getTemplates(), $this->preview, $this->section != '' ) ) );
 +              $wgOut->addHTML( $this->makeTemplatesOnThisPageList( $this->getTemplates() ) );
  
                $wgOut->addHTML( Html::rawElement( 'div', [ 'class' => 'hiddencats' ],
                        Linker::formatHiddenCategories( $this->page->getHiddenCategories() ) ) );
                                $this->showConflict();
                        } catch ( MWContentSerializationException $ex ) {
                                // this can't really happen, but be nice if it does.
 -                              $msg = wfMessage(
 +                              $msg = $this->context->msg(
                                        'content-failed-to-parse',
                                        $this->contentModel,
                                        $this->contentFormat,
  
        }
  
 +      /**
 +       * Wrapper around TemplatesOnThisPageFormatter to make
 +       * a "templates on this page" list.
 +       *
 +       * @param Title[] $templates
 +       * @return string HTML
 +       */
 +      protected function makeTemplatesOnThisPageList( array $templates ) {
 +              $templateListFormatter = new TemplatesOnThisPageFormatter(
 +                      $this->context, MediaWikiServices::getInstance()->getLinkRenderer()
 +              );
 +
 +              // preview if preview, else section if section, else false
 +              $type = false;
 +              if ( $this->preview ) {
 +                      $type = 'preview';
 +              } elseif ( $this->section != '' ) {
 +                      $type = 'section';
 +              }
 +
 +              return Html::rawElement( 'div', [ 'class' => 'templatesUsed' ],
 +                      $templateListFormatter->format( $templates, $type )
 +              );
 +
 +      }
 +
        /**
         * Extract the section title from current section text, if any.
         *
                if ( count( $editNotices ) ) {
                        $wgOut->addHTML( implode( "\n", $editNotices ) );
                } else {
 -                      $msg = wfMessage( 'editnotice-notext' );
 +                      $msg = $this->context->msg( 'editnotice-notext' );
                        if ( !$msg->isDisabled() ) {
                                $wgOut->addHTML(
                                        '<div class="mw-editnotice-notext">'
                                ]
                        );
                } else {
 -                      if ( !wfMessage( 'longpage-hint' )->isDisabled() ) {
 +                      if ( !$this->context->msg( 'longpage-hint' )->isDisabled() ) {
                                $wgOut->wrapWikiMsg( "<div id='mw-edit-longpage-hint'>\n$1\n</div>",
                                        [
                                                'longpage-hint',
         * subclasses may reorganize the form.
         * Note that you do not need to worry about the label's for=, it will be
         * inferred by the id given to the input. You can remove them both by
 -       * passing array( 'id' => false ) to $userInputAttrs.
 +       * passing [ 'id' => false ] to $userInputAttrs.
         *
         * @param string $summary The value of the summary input
         * @param string $labelText The html to place inside the label
         * @param array $inputAttrs Array of attrs to use on the input
         * @param array $spanLabelAttrs Array of attrs to use on the span inside the label
         *
 -       * @return array An array in the format array( $label, $input )
 +       * @return array An array in the format [ $label, $input ]
         */
        function getSummaryInput( $summary = "", $labelText = null,
                $inputAttrs = null, $spanLabelAttrs = null
                                return;
                        }
                }
 -              $labelText = wfMessage( $isSubjectPreview ? 'subject' : 'summary' )->parse();
 +              $labelText = $this->context->msg( $isSubjectPreview ? 'subject' : 'summary' )->parse();
                list( $label, $input ) = $this->getSummaryInput(
                        $summary,
                        $labelText,
                global $wgParser;
  
                if ( $isSubjectPreview ) {
 -                      $summary = wfMessage( 'newsectionsummary' )->rawParams( $wgParser->stripSectionName( $summary ) )
 +                      $summary = $this->context->msg( 'newsectionsummary' )
 +                              ->rawParams( $wgParser->stripSectionName( $summary ) )
                                ->inContentLanguage()->text();
                }
  
                $message = $isSubjectPreview ? 'subject-preview' : 'summary-preview';
  
 -              $summary = wfMessage( $message )->parse()
 +              $summary = $this->context->msg( $message )->parse()
                        . Linker::commentBlock( $summary, $this->mTitle, $isSubjectPreview );
                return Xml::tags( 'div', [ 'class' => 'mw-summary-preview' ], $summary );
        }
@@@ -3334,7 -3309,7 +3343,7 @@@ HTM
                        try {
                                $this->showDiff();
                        } catch ( MWContentSerializationException $ex ) {
 -                              $msg = wfMessage(
 +                              $msg = $this->context->msg(
                                        'content-failed-to-parse',
                                        $this->contentModel,
                                        $this->contentFormat,
                }
  
                if ( ( $oldContent && !$oldContent->isEmpty() ) || ( $newContent && !$newContent->isEmpty() ) ) {
 -                      $oldtitle = wfMessage( $oldtitlemsg )->parse();
 -                      $newtitle = wfMessage( 'yourtext' )->parse();
 +                      $oldtitle = $this->context->msg( $oldtitlemsg )->parse();
 +                      $newtitle = $this->context->msg( 'yourtext' )->parse();
  
                        if ( !$oldContent ) {
                                $oldContent = $newContent->getContentHandler()->makeEmptyContent();
         */
        protected function showHeaderCopyrightWarning() {
                $msg = 'editpage-head-copy-warn';
 -              if ( !wfMessage( $msg )->isDisabled() ) {
 +              if ( !$this->context->msg( $msg )->isDisabled() ) {
                        global $wgOut;
                        $wgOut->wrapWikiMsg( "<div class='editpage-head-copywarn'>\n$1\n</div>",
                                'editpage-head-copy-warn' );
        protected function showTosSummary() {
                $msg = 'editpage-tos-summary';
                Hooks::run( 'EditPageTosSummary', [ $this->mTitle, &$msg ] );
 -              if ( !wfMessage( $msg )->isDisabled() ) {
 +              if ( !$this->context->msg( $msg )->isDisabled() ) {
                        global $wgOut;
                        $wgOut->addHTML( '<div class="mw-tos-summary">' );
                        $wgOut->addWikiMsg( $msg );
        protected function showEditTools() {
                global $wgOut;
                $wgOut->addHTML( '<div class="mw-editTools">' .
 -                      wfMessage( 'edittools' )->inContentLanguage()->parse() .
 +                      $this->context->msg( 'edittools' )->inContentLanguage()->parse() .
                        '</div>' );
        }
  
                Hooks::run( 'EditPageCopyrightWarning', [ $title, &$copywarnMsg ] );
  
                return "<div id=\"editpage-copywarn\">\n" .
 -                      call_user_func_array( 'wfMessage', $copywarnMsg )->$format() . "\n</div>";
 +                      call_user_func_array( 'wfMessage', $copywarnMsg )->title( $title )->$format() . "\n</div>";
        }
  
        /**
                if ( $cancel !== '' ) {
                        $cancel .= Html::element( 'span',
                                [ 'class' => 'mw-editButtons-pipe-separator' ],
 -                              wfMessage( 'pipe-separator' )->text() );
 +                              $this->context->msg( 'pipe-separator' )->text() );
                }
  
 -              $message = wfMessage( 'edithelppage' )->inContentLanguage()->text();
 +              $message = $this->context->msg( 'edithelppage' )->inContentLanguage()->text();
                $edithelpurl = Skin::makeInternalOrExternalUrl( $message );
                $attrs = [
                        'target' => 'helpwindow',
                        'href' => $edithelpurl,
                ];
 -              $edithelp = Html::linkButton( wfMessage( 'edithelp' )->text(),
 +              $edithelp = Html::linkButton( $this->context->msg( 'edithelp' )->text(),
                        $attrs, [ 'mw-ui-quiet' ] ) .
 -                      wfMessage( 'word-separator' )->escaped() .
 -                      wfMessage( 'newwindow' )->parse();
 +                      $this->context->msg( 'word-separator' )->escaped() .
 +                      $this->context->msg( 'newwindow' )->parse();
  
                $wgOut->addHTML( "      <span class='cancelLink'>{$cancel}</span>\n" );
                $wgOut->addHTML( "      <span class='editHelp'>{$edithelp}</span>\n" );
                        $de = $handler->createDifferenceEngine( $this->mArticle->getContext() );
                        $de->setContent( $content2, $content1 );
                        $de->showDiff(
 -                              wfMessage( 'yourtext' )->parse(),
 -                              wfMessage( 'storedversion' )->text()
 +                              $this->context->msg( 'yourtext' )->parse(),
 +                              $this->context->msg( 'storedversion' )->text()
                        );
  
                        $wgOut->wrapWikiMsg( '<h2>$1</h2>', "yourtext" );
  
                return Linker::linkKnown(
                        $this->getContextTitle(),
 -                      wfMessage( 'cancel' )->parse(),
 +                      $this->context->msg( 'cancel' )->parse(),
                        Html::buttonAttributes( $attrs, [ 'mw-ui-quiet' ] ),
                        $cancelParams
                );
                // Quick paranoid permission checks...
                if ( is_object( $data ) ) {
                        if ( $data->log_deleted & LogPage::DELETED_USER ) {
 -                              $data->user_name = wfMessage( 'rev-deleted-user' )->escaped();
 +                              $data->user_name = $this->context->msg( 'rev-deleted-user' )->escaped();
                        }
  
                        if ( $data->log_deleted & LogPage::DELETED_COMMENT ) {
 -                              $data->log_comment = wfMessage( 'rev-deleted-comment' )->escaped();
 +                              $data->log_comment = $this->context->msg( 'rev-deleted-comment' )->escaped();
                        }
                }
  
                                // string, which happens when you initially edit
                                // a category page, due to automatic preview-on-open.
                                $parsedNote = $wgOut->parse( "<div class='previewnote'>" .
 -                                      wfMessage( 'session_fail_preview_html' )->text() . "</div>", true, /* interface */true );
 +                                      $this->context->msg( 'session_fail_preview_html' )->text() . "</div>",
 +                                      true, /* interface */true );
                        }
                        $stats->increment( 'edit.failures.session_loss' );
                        return $parsedNote;
                        # provide a anchor link to the editform
                        $continueEditing = '<span class="mw-continue-editing">' .
                                '[[#' . self::EDITFORM_ID . '|' . $wgLang->getArrow() . ' ' .
 -                              wfMessage( 'continue-editing' )->text() . ']]</span>';
 +                              $this->context->msg( 'continue-editing' )->text() . ']]</span>';
                        if ( $this->mTriedSave && !$this->mTokenOk ) {
                                if ( $this->mTokenOkExceptSuffix ) {
 -                                      $note = wfMessage( 'token_suffix_mismatch' )->plain();
 +                                      $note = $this->context->msg( 'token_suffix_mismatch' )->plain();
                                        $stats->increment( 'edit.failures.bad_token' );
                                } else {
 -                                      $note = wfMessage( 'session_fail_preview' )->plain();
 +                                      $note = $this->context->msg( 'session_fail_preview' )->plain();
                                        $stats->increment( 'edit.failures.session_loss' );
                                }
                        } elseif ( $this->incompleteForm ) {
 -                              $note = wfMessage( 'edit_form_incomplete' )->plain();
 +                              $note = $this->context->msg( 'edit_form_incomplete' )->plain();
                                if ( $this->mTriedSave ) {
                                        $stats->increment( 'edit.failures.incomplete_form' );
                                }
                        } else {
 -                              $note = wfMessage( 'previewnote' )->plain() . ' ' . $continueEditing;
 +                              $note = $this->context->msg( 'previewnote' )->plain() . ' ' . $continueEditing;
                        }
  
                        # don't parse non-wikitext pages, show message about preview
                                # Messages: usercsspreview, userjspreview, sitecsspreview, sitejspreview
                                if ( $level && $format ) {
                                        $note = "<div id='mw-{$level}{$format}preview'>" .
 -                                              wfMessage( "{$level}{$format}preview" )->text() .
 +                                              $this->context->msg( "{$level}{$format}preview" )->text() .
                                                ' ' . $continueEditing . "</div>";
                                }
                        }
                        }
  
                } catch ( MWContentSerializationException $ex ) {
 -                      $m = wfMessage(
 +                      $m = $this->context->msg(
                                'content-failed-to-parse',
                                $this->contentModel,
                                $this->contentFormat,
  
                if ( $this->isConflict ) {
                        $conflict = '<h2 id="mw-previewconflict">'
 -                              . wfMessage( 'previewconflict' )->escaped() . "</h2>\n";
 +                              . $this->context->msg( 'previewconflict' )->escaped() . "</h2>\n";
                } else {
                        $conflict = '<hr />';
                }
  
                $previewhead = "<div class='previewnote'>\n" .
 -                      '<h2 id="mw-previewheader">' . wfMessage( 'preview' )->escaped() . "</h2>" .
 +                      '<h2 id="mw-previewheader">' . $this->context->msg( 'preview' )->escaped() . "</h2>" .
                        $wgOut->parse( $note, true, /* interface */true ) . $conflict . "</div>\n";
  
                $pageViewLang = $this->mTitle->getPageViewLanguage();
                // don't show the minor edit checkbox if it's a new page or section
                if ( !$this->isNew ) {
                        $checkboxes['minor'] = '';
 -                      $minorLabel = wfMessage( 'minoredit' )->parse();
 +                      $minorLabel = $this->context->msg( 'minoredit' )->parse();
                        if ( $wgUser->isAllowed( 'minoredit' ) ) {
                                $attribs = [
                                        'tabindex' => ++$tabindex,
 -                                      'accesskey' => wfMessage( 'accesskey-minoredit' )->text(),
 +                                      'accesskey' => $this->context->msg( 'accesskey-minoredit' )->text(),
                                        'id' => 'wpMinoredit',
                                ];
                                $minorEditHtml =
                        }
                }
  
 -              $watchLabel = wfMessage( 'watchthis' )->parse();
 +              $watchLabel = $this->context->msg( 'watchthis' )->parse();
                $checkboxes['watch'] = '';
                if ( $wgUser->isLoggedIn() ) {
                        $attribs = [
                                'tabindex' => ++$tabindex,
 -                              'accesskey' => wfMessage( 'accesskey-watch' )->text(),
 +                              'accesskey' => $this->context->msg( 'accesskey-watch' )->text(),
                                'id' => 'wpWatchthis',
                        ];
                        $watchThisHtml =
                } else {
                        $buttonLabelKey = !$this->mTitle->exists() ? 'savearticle' : 'savechanges';
                }
 -              $buttonLabel = wfMessage( $buttonLabelKey )->text();
 +              $buttonLabel = $this->context->msg( $buttonLabelKey )->text();
                $attribs = [
                        'id' => 'wpSave',
                        'name' => 'wpSave',
                        'name' => 'wpPreview',
                        'tabindex' => $tabindex,
                ] + Linker::tooltipAndAccesskeyAttribs( 'preview' );
 -              $buttons['preview'] = Html::submitButton( wfMessage( 'showpreview' )->text(),
 +              $buttons['preview'] = Html::submitButton( $this->context->msg( 'showpreview' )->text(),
                        $attribs );
                $buttons['live'] = '';
  
                        'name' => 'wpDiff',
                        'tabindex' => ++$tabindex,
                ] + Linker::tooltipAndAccesskeyAttribs( 'diff' );
 -              $buttons['diff'] = Html::submitButton( wfMessage( 'showdiff' )->text(),
 +              $buttons['diff'] = Html::submitButton( $this->context->msg( 'showdiff' )->text(),
                        $attribs );
  
                Hooks::run( 'EditPageBeforeEditButtons', [ &$this, &$buttons, &$tabindex ] );
        function noSuchSectionPage() {
                global $wgOut;
  
 -              $wgOut->prepareErrorPage( wfMessage( 'nosuchsectiontitle' ) );
 +              $wgOut->prepareErrorPage( $this->context->msg( 'nosuchsectiontitle' ) );
  
 -              $res = wfMessage( 'nosuchsectiontext', $this->section )->parseAsBlock();
 +              $res = $this->context->msg( 'nosuchsectiontext', $this->section )->parseAsBlock();
                Hooks::run( 'EditPageNoSuchSection', [ &$this, &$res ] );
                $wgOut->addHTML( $res );
  
                if ( is_array( $match ) ) {
                        $match = $wgLang->listToText( $match );
                }
 -              $wgOut->prepareErrorPage( wfMessage( 'spamprotectiontitle' ) );
 +              $wgOut->prepareErrorPage( $this->context->msg( 'spamprotectiontitle' ) );
  
                $wgOut->addHTML( '<div id="spamprotected">' );
                $wgOut->addWikiMsg( 'spamprotectiontext' );
diff --combined includes/Title.php
@@@ -91,7 -91,13 +91,13 @@@ class Title implements LinkTarget 
         * @var bool|string ID of the page's content model, i.e. one of the
         *   CONTENT_MODEL_XXX constants
         */
-       public $mContentModel = false;
+       private $mContentModel = false;
+       /**
+        * @var bool If a content model was forced via setContentModel()
+        *   this will be true to avoid having other code paths reset it
+        */
+       private $mForcedContentModel = false;
  
        /** @var int Estimated number of revisions; null of not loaded */
        private $mEstimateRevisions;
                        if ( isset( $row->page_latest ) ) {
                                $this->mLatestID = (int)$row->page_latest;
                        }
-                       if ( isset( $row->page_content_model ) ) {
+                       if ( !$this->mForcedContentModel && isset( $row->page_content_model ) ) {
                                $this->mContentModel = strval( $row->page_content_model );
-                       } else {
+                       } elseif ( !$this->mForcedContentModel ) {
                                $this->mContentModel = false; # initialized lazily in getContentModel()
                        }
                        if ( isset( $row->page_lang ) ) {
                        $this->mLength = 0;
                        $this->mRedirect = false;
                        $this->mLatestID = 0;
-                       $this->mContentModel = false; # initialized lazily in getContentModel()
+                       if ( !$this->mForcedContentModel ) {
+                               $this->mContentModel = false; # initialized lazily in getContentModel()
+                       }
                }
        }
  
         * @return string Content model id
         */
        public function getContentModel( $flags = 0 ) {
-               if ( ( !$this->mContentModel || $flags === Title::GAID_FOR_UPDATE ) &&
-                       $this->getArticleID( $flags )
+               if ( !$this->mForcedContentModel
+                       && ( !$this->mContentModel || $flags === Title::GAID_FOR_UPDATE )
+                       && $this->getArticleID( $flags )
                ) {
                        $linkCache = LinkCache::singleton();
                        $linkCache->addLinkObj( $this ); # in case we already had an article ID
                return $this->getContentModel() == $id;
        }
  
+       /**
+        * Set a proposed content model for the page for permissions
+        * checking. This does not actually change the content model
+        * of a title!
+        *
+        * Additionally, you should make sure you've checked
+        * ContentHandler::canBeUsedOn() first.
+        *
+        * @since 1.28
+        * @param string $model CONTENT_MODEL_XXX constant
+        */
+       public function setContentModel( $model ) {
+               $this->mContentModel = $model;
+               $this->mForcedContentModel = true;
+       }
        /**
         * Get the namespace text
         *
        /**
         * Returns true if the title is inside one of the specified namespaces.
         *
 -       * @param int $namespaces,... The namespaces to check for
 +       * @param int|int[] $namespaces,... The namespaces to check for
         * @return bool
         * @since 1.19
         */
                return $this->mCascadeRestriction;
        }
  
 -      /**
 -       * Loads a string into mRestrictions array
 -       *
 -       * @param ResultWrapper $res Resource restrictions as an SQL result.
 -       * @param string $oldFashionedRestrictions Comma-separated list of page
 -       *        restrictions from page table (pre 1.10)
 -       */
 -      private function loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions = null ) {
 -              $rows = [];
 -
 -              foreach ( $res as $row ) {
 -                      $rows[] = $row;
 -              }
 -
 -              $this->loadRestrictionsFromRows( $rows, $oldFashionedRestrictions );
 -      }
 -
        /**
         * Compiles list of active page restrictions from both page table (pre 1.10)
         * and page_restrictions table for this existing page.
         *   restrictions from page table (pre 1.10)
         */
        public function loadRestrictions( $oldFashionedRestrictions = null ) {
 -              if ( !$this->mRestrictionsLoaded ) {
 -                      $dbr = wfGetDB( DB_REPLICA );
 -                      if ( $this->exists() ) {
 -                              $res = $dbr->select(
 -                                      'page_restrictions',
 -                                      [ 'pr_type', 'pr_expiry', 'pr_level', 'pr_cascade' ],
 -                                      [ 'pr_page' => $this->getArticleID() ],
 -                                      __METHOD__
 -                              );
 +              if ( $this->mRestrictionsLoaded ) {
 +                      return;
 +              }
  
 -                              $this->loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions );
 -                      } else {
 -                              $title_protection = $this->getTitleProtection();
 -
 -                              if ( $title_protection ) {
 -                                      $now = wfTimestampNow();
 -                                      $expiry = $dbr->decodeExpiry( $title_protection['expiry'] );
 -
 -                                      if ( !$expiry || $expiry > $now ) {
 -                                              // Apply the restrictions
 -                                              $this->mRestrictionsExpiry['create'] = $expiry;
 -                                              $this->mRestrictions['create'] = explode( ',', trim( $title_protection['permission'] ) );
 -                                      } else { // Get rid of the old restrictions
 -                                              $this->mTitleProtection = false;
 -                                      }
 -                              } else {
 -                                      $this->mRestrictionsExpiry['create'] = 'infinity';
 +              $id = $this->getArticleID();
 +              if ( $id ) {
 +                      $cache = ObjectCache::getMainWANInstance();
 +                      $rows = $cache->getWithSetCallback(
 +                              // Page protections always leave a new null revision
 +                              $cache->makeKey( 'page-restrictions', $id, $this->getLatestRevID() ),
 +                              $cache::TTL_DAY,
 +                              function ( $curValue, &$ttl, array &$setOpts ) {
 +                                      $dbr = wfGetDB( DB_REPLICA );
 +
 +                                      $setOpts += Database::getCacheSetOptions( $dbr );
 +
 +                                      return iterator_to_array(
 +                                              $dbr->select(
 +                                                      'page_restrictions',
 +                                                      [ 'pr_type', 'pr_expiry', 'pr_level', 'pr_cascade' ],
 +                                                      [ 'pr_page' => $this->getArticleID() ],
 +                                                      __METHOD__
 +                                              )
 +                                      );
                                }
 -                              $this->mRestrictionsLoaded = true;
 +                      );
 +
 +                      $this->loadRestrictionsFromRows( $rows, $oldFashionedRestrictions );
 +              } else {
 +                      $title_protection = $this->getTitleProtection();
 +
 +                      if ( $title_protection ) {
 +                              $now = wfTimestampNow();
 +                              $expiry = wfGetDB( DB_REPLICA )->decodeExpiry( $title_protection['expiry'] );
 +
 +                              if ( !$expiry || $expiry > $now ) {
 +                                      // Apply the restrictions
 +                                      $this->mRestrictionsExpiry['create'] = $expiry;
 +                                      $this->mRestrictions['create'] =
 +                                              explode( ',', trim( $title_protection['permission'] ) );
 +                              } else { // Get rid of the old restrictions
 +                                      $this->mTitleProtection = false;
 +                              }
 +                      } else {
 +                              $this->mRestrictionsExpiry['create'] = 'infinity';
                        }
 +                      $this->mRestrictionsLoaded = true;
                }
        }
  
         * This clears some fields in this object, and clears any associated
         * keys in the "bad links" section of the link cache.
         *
 -       * - This is called from WikiPage::doEdit() and WikiPage::insertOn() to allow
 +       * - This is called from WikiPage::doEditContent() and WikiPage::insertOn() to allow
         * loading of the new page_id. It's also called from
         * WikiPage::doDeleteArticleReal()
         *
@@@ -156,10 -156,20 +156,20 @@@ class SpecialChangeContentModel extend
                }
  
                $this->title = Title::newFromText( $data['pagetitle'] );
+               $titleWithNewContentModel = clone $this->title;
+               $titleWithNewContentModel->setContentModel( $data['model'] );
                $user = $this->getUser();
-               // Check permissions and make sure the user has permission to edit the specific page
-               $errors = $this->title->getUserPermissionsErrors( 'editcontentmodel', $user );
-               $errors = wfMergeErrorArrays( $errors, $this->title->getUserPermissionsErrors( 'edit', $user ) );
+               // Check permissions and make sure the user has permission to:
+               $errors = wfMergeErrorArrays(
+                       // edit the contentmodel of the page
+                       $this->title->getUserPermissionsErrors( 'editcontentmodel', $user ),
+                       // edit the page under the old content model
+                       $this->title->getUserPermissionsErrors( 'edit', $user ),
+                       // edit the contentmodel under the new content model
+                       $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
+                       // edit the page under the new content model
+                       $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
+               );
                if ( $errors ) {
                        $out = $this->getOutput();
                        $wikitext = $out->formatPermissionsErrorMessage( $errors );
                # Truncate for whole multibyte characters.
                $reason = $wgContLang->truncate( $reason, 255 );
  
 +              // Run edit filters
 +              $derivativeContext = new DerivativeContext( $this->getContext() );
 +              $derivativeContext->setTitle( $this->title );
 +              $derivativeContext->setWikiPage( $page );
 +              $status = new Status();
 +              if ( !Hooks::run( 'EditFilterMergedContent',
 +                              [ $derivativeContext, $newContent, $status, $reason,
 +                              $user, false ] )
 +              ) {
 +                      if ( $status->isGood() ) {
 +                              // TODO: extensions should really specify an error message
 +                              $status->fatal( 'hookaborted' );
 +                      }
 +                      return $status;
 +              }
 +
                $status = $page->doEditContent(
                        $newContent,
                        $reason,