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 
123  protected function getFileHandler() {
124  return new FileContentHandler();
125  }
126 
127  public function getFieldsForSearchIndex( SearchEngine $engine ) {
128  $fields = parent::getFieldsForSearchIndex( $engine );
129 
130  $fields['heading'] =
132  $fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
133 
134  $fields['auxiliary_text'] =
135  $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
136 
137  $fields['opening_text'] =
138  $engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
139  $fields['opening_text']->setFlag(
141  );
142  // Until we have full first-class content handler for files, we invoke it explicitly here
143  $fields = array_merge( $fields, $this->getFileHandler()->getFieldsForSearchIndex( $engine ) );
144 
145  return $fields;
146  }
147 
148  public function getDataForSearchIndex(
149  WikiPage $page,
150  ParserOutput $parserOutput,
151  SearchEngine $engine
152  ) {
153  $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
154 
155  $structure = new WikiTextStructure( $parserOutput );
156  $fields['heading'] = $structure->headings();
157  // text fields
158  $fields['opening_text'] = $structure->getOpeningText();
159  $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
160  $fields['auxiliary_text'] = $structure->getAuxiliaryText();
161  $fields['defaultsort'] = $structure->getDefaultSort();
162 
163  // Until we have full first-class content handler for files, we invoke it explicitly here
164  if ( $page->getTitle()->getNamespace() === NS_FILE ) {
165  $fields = array_merge( $fields,
166  $this->getFileHandler()->getDataForSearchIndex( $page, $parserOutput, $engine ) );
167  }
168  return $fields;
169  }
170 
179  public function serializeContent( Content $content, $format = null ) {
180  $this->checkFormat( $format );
181 
182  // NOTE: MessageContent also uses CONTENT_MODEL_WIKITEXT, but it's not a TextContent!
183  // Perhaps MessageContent should use a separate ContentHandler instead.
184  if ( $content instanceof MessageContent ) {
185  return $content->getMessage()->plain();
186  }
187 
188  return parent::serializeContent( $content, $format );
189  }
190 
191 }
ParserOutput
Definition: ParserOutput.php:31
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
SearchIndexField\FLAG_SCORING
const FLAG_SCORING
This field contains secondary information, which is already present in other fields,...
Definition: SearchIndexField.php:52
WikitextContentHandler\getFileHandler
getFileHandler()
Definition: WikitextContentHandler.php:123
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:55
WikitextContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: WikitextContentHandler.php:127
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Wikitext.
Definition: Defines.php:234
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:42
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:148
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1066
Title\getInterwiki
getInterwiki()
Get the interwiki prefix.
Definition: Title.php:967
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:295
Title\getFullText
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1908
SearchIndexField\FLAG_NO_HIGHLIGHT
const FLAG_NO_HIGHLIGHT
This field does not need highlight handling.
Definition: SearchIndexField.php:57
$content
$content
Definition: router.php:76
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
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:218
SearchEngine
Contain a class for special pages Stable to extend.
Definition: SearchEngine.php:37
Content
Base interface for content objects.
Definition: Content.php:35
SearchEngine\makeSearchFieldMapping
makeSearchFieldMapping( $name, $type)
Create a search field definition.
Definition: SearchEngine.php:775
Title
Represents a title within MediaWiki.
Definition: Title.php:46
ContentHandler\checkFormat
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
Definition: ContentHandler.php:544
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
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:77
WikiTextStructure
Class allowing to explore structure of parsed wikitext.
Definition: WikiTextStructure.php:8
NS_FILE
const NS_FILE
Definition: Defines.php:69
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:179
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:19