wiki.techinc.nl/includes/search/RevisionSearchResultTrait.php
Tim Starling 68c433bd23 Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.

General principles:
* Use DI if it is already used. We're not changing the way state is
  managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
  is a service, it's a more generic interface, it is the only
  thing that provides isRegistered() which is needed in some cases,
  and a HookRunner can be efficiently constructed from it
  (confirmed by benchmark). Because HookContainer is needed
  for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
  SpecialPage and ApiBase have getHookContainer() and getHookRunner()
  methods in the base class, and classes that extend that base class
  are not expected to know or care where the base class gets its
  HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
  getHookRunner() methods, getting them from the global service
  container. The point of this is to ease migration to DI by ensuring
  that call sites ask their local friendly base class rather than
  getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
  methods did not seem warranted, there is a private HookRunner property
  which is accessed directly. Very rarely (two cases), there is a
  protected property, for consistency with code that conventionally
  assumes protected=private, but in cases where the class might actually
  be overridden, a protected accessor is preferred over a protected
  property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
  global code. In a few cases it was used for objects with broken
  construction schemes, out of horror or laziness.

Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore

Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router

setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine

Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-05-30 14:23:28 +00:00

207 lines
4.5 KiB
PHP

<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
/**
* Transitional trait used to share the methods between SearchResult and RevisionSearchResult.
* All the content of this trait can be moved to RevisionSearchResult once SearchResult is finally
* refactored into an abstract class.
* NOTE: This trait MUST NOT be used by something else than SearchResult and RevisionSearchResult.
* It will be removed without deprecation period once SearchResult
*/
trait RevisionSearchResultTrait {
/**
* @var RevisionRecord
*/
protected $mRevisionRecord = null;
/**
* @var File
*/
protected $mImage = null;
/**
* @var Title|null
*/
protected $mTitle;
/**
* @var string
*/
protected $mText;
/**
* Initialize from a Title and if possible initializes a corresponding
* RevisionRecord and File.
*
* @param Title|null $title
*/
protected function initFromTitle( $title ) {
$this->mTitle = $title;
if ( $title !== null ) {
$services = MediaWikiServices::getInstance();
$id = false;
Hooks::runner()->onSearchResultInitFromTitle( $title, $id );
$this->mRevisionRecord = $services->getRevisionLookup()->getRevisionByTitle(
$title,
$id,
RevisionLookup::READ_NORMAL
);
if ( $title->getNamespace() === NS_FILE ) {
$this->mImage = $services->getRepoGroup()->findFile( $title );
}
}
}
/**
* Check if this is result points to an invalid title
*
* @return bool
*/
public function isBrokenTitle() {
return $this->mTitle === null;
}
/**
* Check if target page is missing, happens when index is out of date
*
* @return bool
*/
public function isMissingRevision() {
return !$this->mRevisionRecord && !$this->mImage;
}
/**
* @return Title|null
*/
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->mRevisionRecord != null ) {
$content = $this->mRevisionRecord->getContent( SlotRecord::MAIN );
$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->mRevisionRecord ) {
return $this->mRevisionRecord->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;
}
}