Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / content / ContentHandler.php
index db20f51..8603360 100644 (file)
@@ -136,7 +136,7 @@ abstract class ContentHandler {
                        $modelId = $title->getContentModel();
                }
 
-               $handler = ContentHandler::getForModelID( $modelId );
+               $handler = self::getForModelID( $modelId );
 
                return $handler->unserializeContent( $text, $format );
        }
@@ -198,9 +198,6 @@ abstract class ContentHandler {
                        $ext = $m[1];
                }
 
-               // Hook can force JS/CSS
-               Hooks::run( 'TitleIsCssOrJsPage', [ $title, &$isCodePage ], '1.21' );
-
                // Is this a user subpage containing code?
                $isCodeSubpage = NS_USER == $ns
                        && !$isCodePage
@@ -213,9 +210,6 @@ abstract class ContentHandler {
                $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
                $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
 
-               // Hook can override $isWikitext
-               Hooks::run( 'TitleIsWikitextPage', [ $title, &$isWikitext ], '1.21' );
-
                if ( !$isWikitext ) {
                        switch ( $ext ) {
                                case 'js':
@@ -246,7 +240,7 @@ abstract class ContentHandler {
        public static function getForTitle( Title $title ) {
                $modelId = $title->getContentModel();
 
-               return ContentHandler::getForModelID( $modelId );
+               return self::getForModelID( $modelId );
        }
 
        /**
@@ -262,7 +256,7 @@ abstract class ContentHandler {
        public static function getForContent( Content $content ) {
                $modelId = $content->getModel();
 
-               return ContentHandler::getForModelID( $modelId );
+               return self::getForModelID( $modelId );
        }
 
        /**
@@ -299,8 +293,8 @@ abstract class ContentHandler {
        public static function getForModelID( $modelId ) {
                global $wgContentHandlers;
 
-               if ( isset( ContentHandler::$handlers[$modelId] ) ) {
-                       return ContentHandler::$handlers[$modelId];
+               if ( isset( self::$handlers[$modelId] ) ) {
+                       return self::$handlers[$modelId];
                }
 
                if ( empty( $wgContentHandlers[$modelId] ) ) {
@@ -333,9 +327,9 @@ abstract class ContentHandler {
                wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
                        . ': ' . get_class( $handler ) );
 
-               ContentHandler::$handlers[$modelId] = $handler;
+               self::$handlers[$modelId] = $handler;
 
-               return ContentHandler::$handlers[$modelId];
+               return self::$handlers[$modelId];
        }
 
        /**
@@ -367,7 +361,9 @@ abstract class ContentHandler {
        public static function getContentModels() {
                global $wgContentHandlers;
 
-               return array_keys( $wgContentHandlers );
+               $models = array_keys( $wgContentHandlers );
+               Hooks::run( 'GetContentModels', [ &$models ] );
+               return $models;
        }
 
        public static function getAllContentFormats() {
@@ -376,7 +372,7 @@ abstract class ContentHandler {
                $formats = [];
 
                foreach ( $wgContentHandlers as $model => $class ) {
-                       $handler = ContentHandler::getForModelID( $model );
+                       $handler = self::getForModelID( $model );
                        $formats = array_merge( $formats, $handler->getSupportedFormats() );
                }
 
@@ -623,8 +619,8 @@ abstract class ContentHandler {
         */
        public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0,
                $rcid = 0, // FIXME: Deprecated, no longer used
-               $refreshCache = false, $unhide = false ) {
-
+               $refreshCache = false, $unhide = false
+       ) {
                // hook: get difference engine
                $differenceEngine = null;
                if ( !Hooks::run( 'GetDifferenceEngine',
@@ -903,7 +899,7 @@ abstract class ContentHandler {
                        $onlyAuthor = $row->rev_user_text;
                        // Try to find a second contributor
                        foreach ( $res as $row ) {
-                               if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
+                               if ( $row->rev_user_text != $onlyAuthor ) { // T24999
                                        $onlyAuthor = false;
                                        break;
                                }
@@ -1011,22 +1007,22 @@ abstract class ContentHandler {
         * @return ParserOptions
         */
        public function makeParserOptions( $context ) {
-               global $wgContLang, $wgEnableParserLimitReporting;
+               global $wgContLang;
 
                if ( $context instanceof IContextSource ) {
-                       $options = ParserOptions::newFromContext( $context );
+                       $user = $context->getUser();
+                       $lang = $context->getLanguage();
                } elseif ( $context instanceof User ) { // settings per user (even anons)
-                       $options = ParserOptions::newFromUser( $context );
+                       $user = $context;
+                       $lang = null;
                } elseif ( $context === 'canonical' ) { // canonical settings
-                       $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
+                       $user = new User;
+                       $lang = $wgContLang;
                } else {
                        throw new MWException( "Bad context for parser options: $context" );
                }
 
-               $options->enableLimitReport( $wgEnableParserLimitReporting ); // show inclusion/loop reports
-               $options->setTidy( true ); // fix bad HTML
-
-               return $options;
+               return ParserOptions::newCanonical( $user, $lang );
        }
 
        /**
@@ -1095,65 +1091,6 @@ abstract class ContentHandler {
                return $this->supportsDirectEditing();
        }
 
-       /**
-        * Call a legacy hook that uses text instead of Content objects.
-        * Will log a warning when a matching hook function is registered.
-        * If the textual representation of the content is changed by the
-        * hook function, a new Content object is constructed from the new
-        * text.
-        *
-        * @param string $event Event name
-        * @param array $args Parameters passed to hook functions
-        * @param string|null $deprecatedVersion Emit a deprecation notice
-        *   when the hook is run for the provided version
-        *
-        * @return bool True if no handler aborted the hook
-        */
-       public static function runLegacyHooks( $event, $args = [],
-               $deprecatedVersion = null
-       ) {
-
-               if ( !Hooks::isRegistered( $event ) ) {
-                       return true; // nothing to do here
-               }
-
-               // convert Content objects to text
-               $contentObjects = [];
-               $contentTexts = [];
-
-               foreach ( $args as $k => $v ) {
-                       if ( $v instanceof Content ) {
-                               /* @var Content $v */
-
-                               $contentObjects[$k] = $v;
-
-                               $v = $v->serialize();
-                               $contentTexts[$k] = $v;
-                               $args[$k] = $v;
-                       }
-               }
-
-               // call the hook functions
-               $ok = Hooks::run( $event, $args, $deprecatedVersion );
-
-               // see if the hook changed the text
-               foreach ( $contentTexts as $k => $orig ) {
-                       /* @var Content $content */
-
-                       $modified = $args[$k];
-                       $content = $contentObjects[$k];
-
-                       if ( $modified !== $orig ) {
-                               // text was changed, create updated Content object
-                               $content = $content->getContentHandler()->unserializeContent( $modified );
-                       }
-
-                       $args[$k] = $content;
-               }
-
-               return $ok;
-       }
-
        /**
         * Get fields definition for search index
         *
@@ -1169,7 +1106,6 @@ abstract class ContentHandler {
                        'category',
                        SearchIndexField::INDEX_TYPE_TEXT
                );
-
                $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD );
 
                $fields['external_link'] = $engine->makeSearchFieldMapping(
@@ -1186,9 +1122,13 @@ abstract class ContentHandler {
                        'template',
                        SearchIndexField::INDEX_TYPE_KEYWORD
                );
-
                $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD );
 
+               $fields['content_model'] = $engine->makeSearchFieldMapping(
+                       'content_model',
+                       SearchIndexField::INDEX_TYPE_KEYWORD
+               );
+
                return $fields;
        }
 
@@ -1217,8 +1157,11 @@ abstract class ContentHandler {
         * @return array Map of name=>value for fields
         * @since 1.28
         */
-       public function getDataForSearchIndex( WikiPage $page, ParserOutput $output,
-                                              SearchEngine $engine ) {
+       public function getDataForSearchIndex(
+               WikiPage $page,
+               ParserOutput $output,
+               SearchEngine $engine
+       ) {
                $fieldData = [];
                $content = $page->getContent();
 
@@ -1235,6 +1178,7 @@ abstract class ContentHandler {
                        $fieldData['text'] = $text;
                        $fieldData['source_text'] = $text;
                        $fieldData['text_bytes'] = $content->getSize();
+                       $fieldData['content_model'] = $content->getModel();
                }
 
                Hooks::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] );