2006-06-29 08:07:00 +00:00
|
|
|
<?php
|
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
class SpecialCiteThisPage extends FormSpecialPage {
|
2016-09-19 23:27:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Parser
|
|
|
|
*/
|
|
|
|
private $citationParser;
|
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
/**
|
|
|
|
* @var Title|bool
|
|
|
|
*/
|
|
|
|
protected $title = false;
|
|
|
|
|
2014-09-22 16:54:40 +00:00
|
|
|
public function __construct() {
|
2014-07-26 21:44:55 +00:00
|
|
|
parent::__construct( 'CiteThisPage' );
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
/**
|
|
|
|
* @param $par string
|
|
|
|
*/
|
2014-09-22 16:54:40 +00:00
|
|
|
public function execute( $par ) {
|
2006-06-29 08:07:00 +00:00
|
|
|
$this->setHeaders();
|
2016-09-30 08:25:14 +00:00
|
|
|
parent::execute( $par );
|
2016-10-25 19:43:35 +00:00
|
|
|
if ( $this->title instanceof Title ) {
|
2016-09-30 08:25:14 +00:00
|
|
|
$id = $this->getRequest()->getInt( 'id' );
|
|
|
|
$this->showCitations( $this->title, $id );
|
|
|
|
}
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
protected function alterForm( HTMLForm $form ) {
|
|
|
|
$form->setMethod( 'get' );
|
|
|
|
}
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
protected function getFormFields() {
|
|
|
|
if ( isset( $this->par ) ) {
|
|
|
|
$default = $this->par;
|
|
|
|
} else {
|
|
|
|
$default = '';
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
2016-09-30 08:25:14 +00:00
|
|
|
return [
|
|
|
|
'page' => [
|
|
|
|
'name' => 'page',
|
|
|
|
'type' => 'title',
|
|
|
|
'default' => $default,
|
|
|
|
'label-message' => 'citethispage-change-target'
|
|
|
|
]
|
|
|
|
];
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
public function onSubmit( array $data ) {
|
2016-10-25 19:43:35 +00:00
|
|
|
// GET forms are "submitted" on every view, so check
|
|
|
|
// that some data was put in for page, as empty string
|
|
|
|
// will pass validation
|
|
|
|
if ( strlen( $data['page'] ) ) {
|
|
|
|
$this->title = Title::newFromText( $data['page'] );
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
2015-05-15 20:00:37 +00:00
|
|
|
|
2016-01-30 20:08:53 +00:00
|
|
|
/**
|
|
|
|
* Return an array of subpages beginning with $search that this special page will accept.
|
|
|
|
*
|
|
|
|
* @param string $search Prefix to search for
|
|
|
|
* @param int $limit Maximum number of results to return (usually 10)
|
|
|
|
* @param int $offset Number of results to skip (usually 0)
|
|
|
|
* @return string[] Matching subpages
|
|
|
|
*/
|
|
|
|
public function prefixSearchSubpages( $search, $limit, $offset ) {
|
|
|
|
$title = Title::newFromText( $search );
|
|
|
|
if ( !$title || !$title->canExist() ) {
|
|
|
|
// No prefix suggestion in special and media namespace
|
2016-05-09 23:36:56 +00:00
|
|
|
return [];
|
2016-01-30 20:08:53 +00:00
|
|
|
}
|
|
|
|
// Autocomplete subpage the same as a normal search
|
|
|
|
$prefixSearcher = new StringPrefixSearch;
|
2016-05-09 23:36:56 +00:00
|
|
|
$result = $prefixSearcher->search( $search, $limit, [], $offset );
|
2016-01-30 20:08:53 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:00:37 +00:00
|
|
|
protected function getGroupName() {
|
|
|
|
return 'pagetools';
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
private function showCitations( Title $title, $revId ) {
|
|
|
|
if ( !$revId ) {
|
|
|
|
$revId = $title->getLatestRevID();
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
$out = $this->getOutput();
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
$revision = Revision::newFromTitle( $title, $revId );
|
|
|
|
if ( !$revision ) {
|
|
|
|
$out->wrapWikiMsg( '<div class="errorbox">$1</div>',
|
|
|
|
[ 'citethispage-badrevision', $title->getPrefixedText(), $revId ] );
|
|
|
|
return;
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
// Set the overall timestamp to the revision's timestamp
|
|
|
|
$parserOptions->setTimestamp( $revision->getTimestamp() );
|
|
|
|
|
|
|
|
$parser = $this->getParser();
|
|
|
|
// Register our <citation> tag which just parses using a different
|
|
|
|
// context
|
|
|
|
$parser->setHook( 'citation', [ $this, 'citationTag' ] );
|
|
|
|
// Also hold on to a separate Parser instance for <citation> tag parsing
|
|
|
|
// since we can't parse in a parse using the same Parser
|
|
|
|
$this->citationParser = $this->getParser();
|
|
|
|
|
|
|
|
$ret = $parser->parse(
|
|
|
|
$this->getContentText(),
|
|
|
|
$title,
|
|
|
|
$parserOptions,
|
|
|
|
/* $linestart = */ false,
|
|
|
|
/* $clearstate = */ true,
|
|
|
|
$revId
|
|
|
|
);
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
$this->getOutput()->addModuleStyles( 'ext.citeThisPage' );
|
|
|
|
$this->getOutput()->addParserOutputContent( $ret );
|
2006-06-29 08:07:00 +00:00
|
|
|
|
|
|
|
}
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
/**
|
|
|
|
* @return Parser
|
|
|
|
*/
|
|
|
|
private function getParser() {
|
|
|
|
$parserConf = $this->getConfig()->get( 'ParserConf' );
|
|
|
|
return new $parserConf['class']( $parserConf );
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
/**
|
|
|
|
* Get the content to parse
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getContentText() {
|
|
|
|
$msg = $this->msg( 'citethispage-content' )->inContentLanguage()->plain();
|
2010-04-17 21:07:37 +00:00
|
|
|
if ( $msg == '' ) {
|
2016-01-16 12:41:27 +00:00
|
|
|
# With MediaWiki 1.20 the plain text files were deleted
|
|
|
|
# and the text moved into SpecialCite.i18n.php
|
2014-07-26 21:44:55 +00:00
|
|
|
# This code is kept for b/c in case an installation has its own file "citethispage-content-xx"
|
2012-04-26 10:13:54 +00:00
|
|
|
# for a previously not supported language.
|
|
|
|
global $wgContLang, $wgContLanguageCode;
|
2016-01-16 12:41:27 +00:00
|
|
|
$dir = __DIR__ . DIRECTORY_SEPARATOR;
|
2012-04-26 10:13:54 +00:00
|
|
|
$code = $wgContLang->lc( $wgContLanguageCode );
|
2014-07-26 21:44:55 +00:00
|
|
|
if ( file_exists( "${dir}citethispage-content-$code" ) ) {
|
|
|
|
$msg = file_get_contents( "${dir}citethispage-content-$code" );
|
2016-05-09 23:36:56 +00:00
|
|
|
} elseif ( file_exists( "${dir}citethispage-content" ) ){
|
2014-07-26 21:44:55 +00:00
|
|
|
$msg = file_get_contents( "${dir}citethispage-content" );
|
2012-04-26 10:13:54 +00:00
|
|
|
}
|
2006-07-03 13:11:35 +00:00
|
|
|
}
|
2014-05-04 23:14:55 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
return $msg;
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
/**
|
|
|
|
* Get the common ParserOptions for both parses
|
|
|
|
*
|
|
|
|
* @return ParserOptions
|
|
|
|
*/
|
|
|
|
private function getParserOptions() {
|
|
|
|
$parserOptions = ParserOptions::newFromUser( $this->getUser() );
|
|
|
|
$parserOptions->setDateFormat( 'default' );
|
|
|
|
$parserOptions->setEditSection( false );
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
// Having tidy on causes whitespace and <pre> tags to
|
|
|
|
// be generated around the output of the CiteThisPageOutput
|
|
|
|
// class TODO FIXME.
|
|
|
|
$parserOptions->setTidy( false );
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
return $parserOptions;
|
2011-05-28 20:44:24 +00:00
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
/**
|
|
|
|
* Implements the <citation> tag.
|
|
|
|
*
|
|
|
|
* This is a hack to allow content that is typically parsed
|
|
|
|
* using the page's timestamp/pagetitle to use the current
|
|
|
|
* request's time and title
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param array $params
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function citationTag( $text, $params, Parser $parser ) {
|
|
|
|
$ret = $this->citationParser->parse(
|
|
|
|
$text,
|
|
|
|
$this->getPageTitle(),
|
|
|
|
$this->getParserOptions(),
|
|
|
|
/* $linestart = */ false
|
|
|
|
);
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
return $ret->getText();
|
2011-11-13 14:22:24 +00:00
|
|
|
|
|
|
|
}
|
2016-09-30 08:25:14 +00:00
|
|
|
|
|
|
|
protected function getDisplayFormat() {
|
|
|
|
return 'ooui';
|
|
|
|
}
|
2016-11-15 19:35:17 +00:00
|
|
|
|
|
|
|
public function requiresUnblock() {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-15 19:55:41 +00:00
|
|
|
|
|
|
|
public function requiresWrite() {
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|