2012-10-16 18:20:43 +00:00
|
|
|
<?php
|
2012-11-18 14:34:00 +00:00
|
|
|
/**
|
2012-12-20 19:44:47 +00:00
|
|
|
* Content handler for wiki text pages.
|
|
|
|
|
*
|
2012-11-18 14:34:00 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-12-20 19:44:47 +00:00
|
|
|
* @since 1.21
|
|
|
|
|
*
|
2012-11-18 14:34:00 +00:00
|
|
|
* @file
|
2012-12-20 19:44:47 +00:00
|
|
|
* @ingroup Content
|
2012-11-18 14:34:00 +00:00
|
|
|
*/
|
2012-10-16 18:20:43 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-12-20 19:44:47 +00:00
|
|
|
* Content handler for wiki text pages.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Content
|
2012-10-16 18:20:43 +00:00
|
|
|
*/
|
|
|
|
|
class WikitextContentHandler extends TextContentHandler {
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2012-10-16 18:20:43 +00:00
|
|
|
public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
parent::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT ] );
|
2012-10-16 18:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-17 06:04:08 +00:00
|
|
|
protected function getContentClass() {
|
|
|
|
|
return 'WikitextContent';
|
2012-10-16 18:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a WikitextContent object representing a redirect to the given destination page.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param Title $destination The page to redirect to.
|
|
|
|
|
* @param string $text Text to include in the redirect, if possible.
|
2012-10-16 18:20:43 +00:00
|
|
|
*
|
|
|
|
|
* @return Content
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
|
|
|
|
* @see ContentHandler::makeRedirectContent
|
2012-10-16 18:20:43 +00:00
|
|
|
*/
|
2013-10-03 15:09:08 +00:00
|
|
|
public function makeRedirectContent( Title $destination, $text = '' ) {
|
2013-06-30 08:02:37 +00:00
|
|
|
$optionalColon = '';
|
|
|
|
|
|
|
|
|
|
if ( $destination->getNamespace() == NS_CATEGORY ) {
|
|
|
|
|
$optionalColon = ':';
|
|
|
|
|
} else {
|
|
|
|
|
$iw = $destination->getInterwiki();
|
|
|
|
|
if ( $iw && Language::fetchLanguageName( $iw, null, 'mw' ) ) {
|
|
|
|
|
$optionalColon = ':';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-16 18:20:43 +00:00
|
|
|
$mwRedir = MagicWord::get( 'redirect' );
|
2013-11-20 04:49:59 +00:00
|
|
|
$redirectText = $mwRedir->getSynonym( 0 ) .
|
|
|
|
|
' [[' . $optionalColon . $destination->getFullText() . ']]';
|
|
|
|
|
|
2013-10-03 15:09:08 +00:00
|
|
|
if ( $text != '' ) {
|
|
|
|
|
$redirectText .= "\n" . $text;
|
|
|
|
|
}
|
2012-10-16 18:20:43 +00:00
|
|
|
|
2014-08-17 06:04:08 +00:00
|
|
|
$class = $this->getContentClass();
|
|
|
|
|
return new $class( $redirectText );
|
2012-10-16 18:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-13 16:26:43 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true because wikitext supports redirects.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return bool Always true.
|
2012-12-13 16:26:43 +00:00
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @see ContentHandler::supportsRedirects
|
2012-12-13 16:26:43 +00:00
|
|
|
*/
|
|
|
|
|
public function supportsRedirects() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-16 18:20:43 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true because wikitext supports sections.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return bool Always true.
|
|
|
|
|
*
|
|
|
|
|
* @see ContentHandler::supportsSections
|
2012-10-16 18:20:43 +00:00
|
|
|
*/
|
|
|
|
|
public function supportsSections() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true, because wikitext supports caching using the
|
|
|
|
|
* ParserCache mechanism.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.21
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
|
|
|
|
* @return bool Always true.
|
|
|
|
|
*
|
|
|
|
|
* @see ContentHandler::isParserCacheSupported
|
2012-10-16 18:20:43 +00:00
|
|
|
*/
|
|
|
|
|
public function isParserCacheSupported() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2016-05-13 00:10:52 +00:00
|
|
|
public function getFieldsForSearchIndex( SearchEngine $engine ) {
|
|
|
|
|
$fields = [];
|
|
|
|
|
|
|
|
|
|
$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['heading'] =
|
|
|
|
|
$engine->makeSearchFieldMapping( 'heading', SearchIndexField::INDEX_TYPE_TEXT );
|
|
|
|
|
$fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
|
|
|
|
|
|
|
|
|
|
$fields['auxiliary_text'] =
|
|
|
|
|
$engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
|
|
|
|
|
|
|
|
|
|
$fields['opening_text'] =
|
|
|
|
|
$engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
|
2016-07-08 18:52:55 +00:00
|
|
|
$fields['opening_text']->setFlag( SearchIndexField::FLAG_SCORING |
|
|
|
|
|
SearchIndexField::FLAG_NO_HIGHLIGHT );
|
2016-05-13 00:10:52 +00:00
|
|
|
|
|
|
|
|
$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 );
|
|
|
|
|
|
|
|
|
|
// FIXME: this really belongs in separate file handler but files
|
|
|
|
|
// do not have separate handler. Sadness.
|
|
|
|
|
$fields['file_text'] =
|
|
|
|
|
$engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
|
|
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 20:24:10 +00:00
|
|
|
/**
|
|
|
|
|
* Extract text of the file
|
|
|
|
|
* TODO: probably should go to file handler?
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getFileText( Title $title ) {
|
|
|
|
|
$file = wfLocalFile( $title );
|
|
|
|
|
if ( $file && $file->exists() ) {
|
|
|
|
|
return $file->getHandler()->getEntireText( $file );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDataForSearchIndex( WikiPage $page, ParserOutput $parserOutput,
|
|
|
|
|
SearchEngine $engine ) {
|
|
|
|
|
$fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
|
|
|
|
|
|
|
|
|
|
$structure = new WikiTextStructure( $parserOutput );
|
|
|
|
|
$fields['external_link'] = array_keys( $parserOutput->getExternalLinks() );
|
|
|
|
|
$fields['category'] = $structure->categories();
|
|
|
|
|
$fields['heading'] = $structure->headings();
|
|
|
|
|
$fields['outgoing_link'] = $structure->outgoingLinks();
|
|
|
|
|
$fields['template'] = $structure->templates();
|
|
|
|
|
// text fields
|
|
|
|
|
$fields['opening_text'] = $structure->getOpeningText();
|
|
|
|
|
$fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
|
|
|
|
|
$fields['auxiliary_text'] = $structure->getAuxiliaryText();
|
|
|
|
|
|
|
|
|
|
$title = $page->getTitle();
|
|
|
|
|
if ( NS_FILE == $title->getNamespace() ) {
|
|
|
|
|
$fileText = $this->getFileText( $title );
|
|
|
|
|
if ( $fileText ) {
|
|
|
|
|
$fields['file_text'] = $fileText;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $fields;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-18 14:34:00 +00:00
|
|
|
}
|