MediaWiki  master
WikitextContentHandler.php
Go to the documentation of this file.
1 <?php
28 
35 
36  public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
37  parent::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT ] );
38  }
39 
40  protected function getContentClass() {
41  return WikitextContent::class;
42  }
43 
54  public function makeRedirectContent( Title $destination, $text = '' ) {
55  $optionalColon = '';
56 
57  $services = MediaWikiServices::getInstance();
58  if ( $destination->getNamespace() == NS_CATEGORY ) {
59  $optionalColon = ':';
60  } else {
61  $iw = $destination->getInterwiki();
62  if ( $iw && $services
63  ->getLanguageNameUtils()
64  ->getLanguageName( $iw,
65  LanguageNameUtils::AUTONYMS,
66  LanguageNameUtils::DEFINED )
67  ) {
68  $optionalColon = ':';
69  }
70  }
71 
72  $mwRedir = $services->getMagicWordFactory()->get( 'redirect' );
73  $redirectText = $mwRedir->getSynonym( 0 ) .
74  ' [[' . $optionalColon . $destination->getFullText() . ']]';
75 
76  if ( $text != '' ) {
77  $redirectText .= "\n" . $text;
78  }
79 
80  $class = $this->getContentClass();
81  return new $class( $redirectText );
82  }
83 
91  public function supportsRedirects() {
92  return true;
93  }
94 
102  public function supportsSections() {
103  return true;
104  }
105 
116  public function isParserCacheSupported() {
117  return true;
118  }
119 
124  protected function getFileHandler() {
125  return new FileContentHandler();
126  }
127 
128  public function getFieldsForSearchIndex( SearchEngine $engine ) {
129  $fields = parent::getFieldsForSearchIndex( $engine );
130 
131  $fields['heading'] =
133  $fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
134 
135  $fields['auxiliary_text'] =
136  $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
137 
138  $fields['opening_text'] =
139  $engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
140  $fields['opening_text']->setFlag(
142  );
143  // Until we have full first-class content handler for files, we invoke it explicitly here
144  $fields = array_merge( $fields, $this->getFileHandler()->getFieldsForSearchIndex( $engine ) );
145 
146  return $fields;
147  }
148 
149  public function getDataForSearchIndex(
150  WikiPage $page,
151  ParserOutput $parserOutput,
152  SearchEngine $engine
153  ) {
154  $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
155 
156  $structure = new WikiTextStructure( $parserOutput );
157  $fields['heading'] = $structure->headings();
158  // text fields
159  $fields['opening_text'] = $structure->getOpeningText();
160  $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
161  $fields['auxiliary_text'] = $structure->getAuxiliaryText();
162  $fields['defaultsort'] = $structure->getDefaultSort();
163 
164  // Until we have full first-class content handler for files, we invoke it explicitly here
165  if ( NS_FILE == $page->getTitle()->getNamespace() ) {
166  $fields = array_merge( $fields,
167  $this->getFileHandler()->getDataForSearchIndex( $page, $parserOutput, $engine ) );
168  }
169  return $fields;
170  }
171 
180  public function serializeContent( Content $content, $format = null ) {
181  $this->checkFormat( $format );
182 
183  // NOTE: MessageContent also uses CONTENT_MODEL_WIKITEXT, but it's not a TextContent!
184  // Perhaps MessageContent should use a separate ContentHandler instead.
185  if ( $content instanceof MessageContent ) {
186  return $content->getMessage()->plain();
187  }
188 
189  return parent::serializeContent( $content, $format );
190  }
191 
192 }
ParserOutput
Definition: ParserOutput.php:25
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:134
SearchIndexField\FLAG_SCORING
const FLAG_SCORING
This field contains secondary information, which is already present in other fields,...
Definition: SearchIndexField.php:51
WikitextContentHandler\getFileHandler
getFileHandler()
Get file handler.
Definition: WikitextContentHandler.php:124
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:48
NS_FILE
const NS_FILE
Definition: Defines.php:66
WikitextContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: WikitextContentHandler.php:128
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:215
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:41
WikitextContentHandler\getDataForSearchIndex
getDataForSearchIndex(WikiPage $page, ParserOutput $parserOutput, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
Definition: WikitextContentHandler.php:149
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1053
Title\getInterwiki
getInterwiki()
Get the interwiki prefix.
Definition: Title.php:942
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:307
Title\getFullText
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1895
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:230
SearchIndexField\FLAG_NO_HIGHLIGHT
const FLAG_NO_HIGHLIGHT
This field does not need highlight handling.
Definition: SearchIndexField.php:56
$content
$content
Definition: router.php:78
WikitextContentHandler
Content handler for wiki text pages.
Definition: WikitextContentHandler.php:34
WikitextContentHandler\isParserCacheSupported
isParserCacheSupported()
Returns true, because wikitext supports caching using the ParserCache mechanism.
Definition: WikitextContentHandler.php:116
WikitextContentHandler\__construct
__construct( $modelId=CONTENT_MODEL_WIKITEXT)
Definition: WikitextContentHandler.php:36
FileContentHandler
Content handler for File: files TODO: this handler s not used directly now, but instead manually call...
Definition: FileContentHandler.php:11
TextContentHandler
Base content handler implementation for flat text contents.
Definition: TextContentHandler.php:31
SearchEngine
Contain a class for special pages.
Definition: SearchEngine.php:34
Content
Base interface for content objects.
Definition: Content.php:34
SearchEngine\makeSearchFieldMapping
makeSearchFieldMapping( $name, $type)
Create a search field definition.
Definition: SearchEngine.php:744
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ContentHandler\checkFormat
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
Definition: ContentHandler.php:525
MessageContent
Wrapper allowing us to handle a system message as a Content object.
Definition: MessageContent.php:36
WikitextContentHandler\supportsRedirects
supportsRedirects()
Returns true because wikitext supports redirects.
Definition: WikitextContentHandler.php:91
WikiTextStructure
Class allowing to explore structure of parsed wikitext.
Definition: WikiTextStructure.php:8
WikitextContentHandler\supportsSections
supportsSections()
Returns true because wikitext supports sections.
Definition: WikitextContentHandler.php:102
WikitextContentHandler\makeRedirectContent
makeRedirectContent(Title $destination, $text='')
Returns a WikitextContent object representing a redirect to the given destination page.
Definition: WikitextContentHandler.php:54
WikitextContentHandler\serializeContent
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
Definition: WikitextContentHandler.php:180
WikitextContentHandler\getContentClass
getContentClass()
Returns the name of the associated Content class, to be used when creating new objects.
Definition: WikitextContentHandler.php:40
SearchIndexField\INDEX_TYPE_TEXT
const INDEX_TYPE_TEXT
TEXT fields are suitable for natural language and may be subject to analysis such as stemming.
Definition: SearchIndexField.php:18