X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fsearch%2FSearchResult.php;h=3d32de776dbde954ba7256f6aa4af0a29be3413d;hb=38190f6608f3d2807cb93b1bae7edb770af36ef8;hp=b924b29cffe6b9f7d5bda92ff16c90868fc5bddc;hpb=7af7bbe7476996bf60d8d8e48a84687ccd7505df;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/search/SearchResult.php b/includes/search/SearchResult.php index b924b29cff..3d32de776d 100644 --- a/includes/search/SearchResult.php +++ b/includes/search/SearchResult.php @@ -21,46 +21,29 @@ * @ingroup Search */ -use MediaWiki\MediaWikiServices; - /** - * @todo FIXME: This class is horribly factored. It would probably be better to - * have a useful base class to which you pass some standard information, then - * let the fancy self-highlighters extend that. + * NOTE: this class is being refactored into an abstract base class. + * If you extend this class directly, please implement all the methods declared + * in RevisionSearchResultTrait or extend RevisionSearchResult. + * + * Once the hard-deprecation period is over (1.36?): + * - all methods declared in RevisionSearchResultTrait should be declared + * as abstract in this class + * - RevisionSearchResultTrait body should be moved to RevisionSearchResult and then removed without + * deprecation + * - caveat: all classes extending this one may potentially break if they did not properly implement + * all the methods. * @ingroup Search */ class SearchResult { + use SearchResultTrait; + use RevisionSearchResultTrait; - /** - * @var Revision - */ - protected $mRevision = null; - - /** - * @var File - */ - protected $mImage = null; - - /** - * @var Title - */ - protected $mTitle; - - /** - * @var string - */ - protected $mText; - - /** - * @var SearchEngine - */ - protected $searchEngine; - - /** - * A function returning a set of extension data. - * @var Closure|null - */ - protected $extensionData; + public function __construct() { + if ( self::class === static::class ) { + wfDeprecated( __METHOD__, '1.34' ); + } + } /** * Return a new SearchResult and initializes it with a title. @@ -70,217 +53,10 @@ class SearchResult { * @return SearchResult */ public static function newFromTitle( $title, ISearchResultSet $parentSet = null ) { - $result = new static(); - $result->initFromTitle( $title ); + $result = new RevisionSearchResult( $title ); if ( $parentSet ) { $parentSet->augmentResult( $result ); } return $result; } - - /** - * Initialize from a Title and if possible initializes a corresponding - * Revision and File. - * - * @param Title $title - */ - protected function initFromTitle( $title ) { - $this->mTitle = $title; - $services = MediaWikiServices::getInstance(); - if ( !is_null( $this->mTitle ) ) { - $id = false; - Hooks::run( 'SearchResultInitFromTitle', [ $title, &$id ] ); - $this->mRevision = Revision::newFromTitle( - $this->mTitle, $id, Revision::READ_NORMAL ); - if ( $this->mTitle->getNamespace() === NS_FILE ) { - $this->mImage = $services->getRepoGroup()->findFile( $this->mTitle ); - } - } - $this->searchEngine = $services->newSearchEngine(); - } - - /** - * Check if this is result points to an invalid title - * - * @return bool - */ - public function isBrokenTitle() { - return is_null( $this->mTitle ); - } - - /** - * Check if target page is missing, happens when index is out of date - * - * @return bool - */ - public function isMissingRevision() { - return !$this->mRevision && !$this->mImage; - } - - /** - * @return Title - */ - public function getTitle() { - return $this->mTitle; - } - - /** - * Get the file for this page, if one exists - * @return File|null - */ - public function getFile() { - return $this->mImage; - } - - /** - * Lazy initialization of article text from DB - */ - protected function initText() { - if ( !isset( $this->mText ) ) { - if ( $this->mRevision != null ) { - $content = $this->mRevision->getContent(); - $this->mText = $content !== null ? $content->getTextForSearchIndex() : ''; - } else { // TODO: can we fetch raw wikitext for commons images? - $this->mText = ''; - } - } - } - - /** - * @param string[] $terms Terms to highlight (this parameter is deprecated and ignored) - * @return string Highlighted text snippet, null (and not '') if not supported - */ - public function getTextSnippet( $terms = [] ) { - return ''; - } - - /** - * @return string Highlighted title, '' if not supported - */ - public function getTitleSnippet() { - return ''; - } - - /** - * @return string Highlighted redirect name (redirect to this page), '' if none or not supported - */ - public function getRedirectSnippet() { - return ''; - } - - /** - * @return Title|null Title object for the redirect to this page, null if none or not supported - */ - public function getRedirectTitle() { - return null; - } - - /** - * @return string Highlighted relevant section name, null if none or not supported - */ - public function getSectionSnippet() { - return ''; - } - - /** - * @return Title|null Title object (pagename+fragment) for the section, - * null if none or not supported - */ - public function getSectionTitle() { - return null; - } - - /** - * @return string Highlighted relevant category name or '' if none or not supported - */ - public function getCategorySnippet() { - return ''; - } - - /** - * @return string Timestamp - */ - public function getTimestamp() { - if ( $this->mRevision ) { - return $this->mRevision->getTimestamp(); - } elseif ( $this->mImage ) { - return $this->mImage->getTimestamp(); - } - return ''; - } - - /** - * @return int Number of words - */ - public function getWordCount() { - $this->initText(); - return str_word_count( $this->mText ); - } - - /** - * @return int Size in bytes - */ - public function getByteSize() { - $this->initText(); - return strlen( $this->mText ); - } - - /** - * @return string Interwiki prefix of the title (return iw even if title is broken) - */ - public function getInterwikiPrefix() { - return ''; - } - - /** - * @return string Interwiki namespace of the title (since we likely can't resolve it locally) - */ - public function getInterwikiNamespaceText() { - return ''; - } - - /** - * Did this match file contents (eg: PDF/DJVU)? - * @return bool - */ - public function isFileMatch() { - return false; - } - - /** - * Get the extension data as: - * augmentor name => data - * @return array[] - */ - public function getExtensionData() { - if ( $this->extensionData ) { - return call_user_func( $this->extensionData ); - } else { - return []; - } - } - - /** - * Set extension data for this result. - * The data is: - * augmentor name => data - * @param Closure|array $extensionData Takes no arguments, returns - * either array of extension data or null. - */ - public function setExtensionData( $extensionData ) { - if ( $extensionData instanceof Closure ) { - $this->extensionData = $extensionData; - } elseif ( is_array( $extensionData ) ) { - wfDeprecated( __METHOD__ . ' with array argument', '1.32' ); - $this->extensionData = function () use ( $extensionData ) { - return $extensionData; - }; - } else { - $type = is_object( $extensionData ) - ? get_class( $extensionData ) - : gettype( $extensionData ); - throw new \InvalidArgumentException( - __METHOD__ . " must be called with Closure|array, but received $type" ); - } - } }