X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fcontent%2FContentHandler.php;h=4e50c8ee799b090784c7d2bb6a1d1ed4606e8e00;hb=386bed1ffd9da7eb5830e0c2386e5dc79e261dfe;hp=1ecd6145c740d3b538279ecc9a268471f4ee2d1c;hpb=2b8d66c4cd4836b44464e0b33134dd6a9d27212c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index 1ecd6145c7..4e50c8ee79 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -1,4 +1,7 @@ mModelID = $modelId; $this->mSupportedFormats = $formats; - - $this->mModelName = preg_replace( '/(Content)?Handler$/', '', get_class( $this ) ); - $this->mModelName = preg_replace( '/[_\\\\]/', '', $this->mModelName ); - $this->mModelName = strtolower( $this->mModelName ); } /** @@ -897,7 +896,7 @@ abstract class ContentHandler { * have it / want it. */ public function getAutoDeleteReason( Title $title, &$hasHistory ) { - $dbr = wfGetDB( DB_SLAVE ); + $dbr = wfGetDB( DB_REPLICA ); // Get the last revision $rev = Revision::newFromTitle( $title ); @@ -1015,9 +1014,21 @@ abstract class ContentHandler { return false; // no content to undo } - $this->checkModelID( $cur_content->getModel() ); - $this->checkModelID( $undo_content->getModel() ); - $this->checkModelID( $undoafter_content->getModel() ); + try { + $this->checkModelID( $cur_content->getModel() ); + $this->checkModelID( $undo_content->getModel() ); + if ( $current->getId() !== $undo->getId() ) { + // If we are undoing the most recent revision, + // its ok to revert content model changes. However + // if we are undoing a revision in the middle, then + // doing that will be confusing. + $this->checkModelID( $undoafter_content->getModel() ); + } + } catch ( MWException $e ) { + // If the revisions have different content models + // just return false + return false; + } if ( $cur_content->equals( $undo_content ) ) { // No use doing a merge if it's just a straight revert. @@ -1156,62 +1167,19 @@ abstract class ContentHandler { * * @param string $event Event name * @param array $args Parameters passed to hook functions - * @param bool $warn Whether to log a warning. - * Default to self::$enableDeprecationWarnings. - * May be set to false for testing. + * @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 - * - * @see ContentHandler::$enableDeprecationWarnings */ public static function runLegacyHooks( $event, $args = [], - $warn = null + $deprecatedVersion = null ) { - if ( $warn === null ) { - $warn = self::$enableDeprecationWarnings; - } - if ( !Hooks::isRegistered( $event ) ) { return true; // nothing to do here } - if ( $warn ) { - // Log information about which handlers are registered for the legacy hook, - // so we can find and fix them. - - $handlers = Hooks::getHandlers( $event ); - $handlerInfo = []; - - MediaWiki\suppressWarnings(); - - foreach ( $handlers as $handler ) { - if ( is_array( $handler ) ) { - if ( is_object( $handler[0] ) ) { - $info = get_class( $handler[0] ); - } else { - $info = $handler[0]; - } - - if ( isset( $handler[1] ) ) { - $info .= '::' . $handler[1]; - } - } elseif ( is_object( $handler ) ) { - $info = get_class( $handler[0] ); - $info .= '::on' . $event; - } else { - $info = $handler; - } - - $handlerInfo[] = $info; - } - - MediaWiki\restoreWarnings(); - - wfWarn( "Using obsolete hook $event via ContentHandler::runLegacyHooks()! Handlers: " . - implode( ', ', $handlerInfo ), 2 ); - } - // convert Content objects to text $contentObjects = []; $contentTexts = []; @@ -1229,7 +1197,7 @@ abstract class ContentHandler { } // call the hook functions - $ok = Hooks::run( $event, $args ); + $ok = Hooks::run( $event, $args, $deprecatedVersion ); // see if the hook changed the text foreach ( $contentTexts as $k => $orig ) { @@ -1251,23 +1219,114 @@ abstract class ContentHandler { /** * Get fields definition for search index + * + * @todo Expose title, redirect, namespace, text, source_text, text_bytes + * field mappings here. (see T142670 and T143409) + * * @param SearchEngine $engine * @return SearchIndexField[] List of fields this content handler can provide. * @since 1.28 */ public function getFieldsForSearchIndex( SearchEngine $engine ) { - /* Default fields: - /* - * namespace - * namespace_text - * redirect - * source_text - * suggest - * timestamp - * title - * text - * text_bytes - */ - return []; + $fields['category'] = $engine->makeSearchFieldMapping( + 'category', + SearchIndexField::INDEX_TYPE_TEXT + ); + + $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD ); + + $fields['external_link'] = $engine->makeSearchFieldMapping( + 'external_link', + SearchIndexField::INDEX_TYPE_KEYWORD + ); + + $fields['outgoing_link'] = $engine->makeSearchFieldMapping( + 'outgoing_link', + SearchIndexField::INDEX_TYPE_KEYWORD + ); + + $fields['template'] = $engine->makeSearchFieldMapping( + 'template', + SearchIndexField::INDEX_TYPE_KEYWORD + ); + + $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD ); + + return $fields; + } + + /** + * Add new field definition to array. + * @param SearchIndexField[] $fields + * @param SearchEngine $engine + * @param string $name + * @param int $type + * @return SearchIndexField[] new field defs + * @since 1.28 + */ + protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) { + $fields[$name] = $engine->makeSearchFieldMapping( $name, $type ); + return $fields; + } + + /** + * Return fields to be indexed by search engine + * as representation of this document. + * Overriding class should call parent function or take care of calling + * the SearchDataForIndex hook. + * @param WikiPage $page Page to index + * @param ParserOutput $output + * @param SearchEngine $engine Search engine for which we are indexing + * @return array Map of name=>value for fields + * @since 1.28 + */ + public function getDataForSearchIndex( WikiPage $page, ParserOutput $output, + SearchEngine $engine ) { + $fieldData = []; + $content = $page->getContent(); + + if ( $content ) { + $searchDataExtractor = new ParserOutputSearchDataExtractor(); + + $fieldData['category'] = $searchDataExtractor->getCategories( $output ); + $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output ); + $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output ); + $fieldData['template'] = $searchDataExtractor->getTemplates( $output ); + + $text = $content->getTextForSearchIndex(); + + $fieldData['text'] = $text; + $fieldData['source_text'] = $text; + $fieldData['text_bytes'] = $content->getSize(); + } + + Hooks::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] ); + return $fieldData; + } + + /** + * Produce page output suitable for indexing. + * + * Specific content handlers may override it if they need different content handling. + * + * @param WikiPage $page + * @param ParserCache $cache + * @return ParserOutput + */ + public function getParserOutputForIndexing( WikiPage $page, ParserCache $cache = null ) { + $parserOptions = $page->makeParserOptions( 'canonical' ); + $revId = $page->getRevision()->getId(); + if ( $cache ) { + $parserOutput = $cache->get( $page, $parserOptions ); + } + if ( empty( $parserOutput ) ) { + $parserOutput = + $page->getContent()->getParserOutput( $page->getTitle(), $revId, $parserOptions ); + if ( $cache ) { + $cache->save( $parserOutput, $page, $parserOptions ); + } + } + return $parserOutput; } + }