2006-06-29 08:07:00 +00:00
|
|
|
<?php
|
|
|
|
|
2021-02-11 02:03:06 +00:00
|
|
|
namespace MediaWiki\Extension\CiteThisPage;
|
|
|
|
|
2023-12-10 22:17:18 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2024-06-08 20:44:14 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLForm;
|
|
|
|
use MediaWiki\Parser\Parser;
|
2024-10-19 21:29:00 +00:00
|
|
|
use MediaWiki\Parser\ParserFactory;
|
|
|
|
use MediaWiki\Parser\ParserOptions;
|
2021-03-12 20:25:22 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2023-12-10 22:17:18 +00:00
|
|
|
use MediaWiki\SpecialPage\FormSpecialPage;
|
2023-08-19 04:13:59 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-03-12 20:25:22 +00:00
|
|
|
use SearchEngineFactory;
|
2019-09-02 09:32:55 +00:00
|
|
|
|
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;
|
|
|
|
|
2021-03-12 20:25:22 +00:00
|
|
|
/** @var SearchEngineFactory */
|
|
|
|
private $searchEngineFactory;
|
|
|
|
|
|
|
|
/** @var RevisionLookup */
|
|
|
|
private $revisionLookup;
|
|
|
|
|
|
|
|
/** @var ParserFactory */
|
|
|
|
private $parserFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SearchEngineFactory $searchEngineFactory
|
|
|
|
* @param RevisionLookup $revisionLookup
|
|
|
|
* @param ParserFactory $parserFactory
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
SearchEngineFactory $searchEngineFactory,
|
|
|
|
RevisionLookup $revisionLookup,
|
|
|
|
ParserFactory $parserFactory
|
|
|
|
) {
|
2014-07-26 21:44:55 +00:00
|
|
|
parent::__construct( 'CiteThisPage' );
|
2021-03-12 20:25:22 +00:00
|
|
|
$this->searchEngineFactory = $searchEngineFactory;
|
|
|
|
$this->revisionLookup = $revisionLookup;
|
|
|
|
$this->parserFactory = $parserFactory;
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2016-09-30 08:25:14 +00:00
|
|
|
/**
|
2017-10-06 19:29:48 +00:00
|
|
|
* @param string $par
|
2016-09-30 08:25:14 +00:00
|
|
|
*/
|
2014-09-22 16:54:40 +00:00
|
|
|
public function execute( $par ) {
|
2006-06-29 08:07:00 +00:00
|
|
|
$this->setHeaders();
|
2019-08-15 01:45:53 +00:00
|
|
|
$this->addHelpLink( 'Extension:CiteThisPage' );
|
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
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* @param HTMLForm $form
|
|
|
|
*/
|
2016-09-30 08:25:14 +00:00
|
|
|
protected function alterForm( HTMLForm $form ) {
|
|
|
|
$form->setMethod( 'get' );
|
2019-11-16 17:44:17 +00:00
|
|
|
$form->setFormIdentifier( 'titleform' );
|
2016-09-30 08:25:14 +00:00
|
|
|
}
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-09-30 08:25:14 +00:00
|
|
|
protected function getFormFields() {
|
|
|
|
return [
|
|
|
|
'page' => [
|
|
|
|
'name' => 'page',
|
|
|
|
'type' => 'title',
|
2021-03-06 20:51:09 +00:00
|
|
|
'exists' => true,
|
2021-02-11 02:03:06 +00:00
|
|
|
'default' => $this->par ?? '',
|
2016-09-30 08:25:14 +00:00
|
|
|
'label-message' => 'citethispage-change-target'
|
|
|
|
]
|
|
|
|
];
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
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
|
2019-11-16 17:44:17 +00:00
|
|
|
// that some data was put in for page
|
2016-10-25 19:43:35 +00:00
|
|
|
if ( strlen( $data['page'] ) ) {
|
|
|
|
$this->title = Title::newFromText( $data['page'] );
|
|
|
|
}
|
2017-12-25 15:41:12 +00:00
|
|
|
return true;
|
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 ) {
|
2021-03-12 20:25:22 +00:00
|
|
|
return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
|
2016-01-30 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/** @inheritDoc */
|
2015-05-15 20:00:37 +00:00
|
|
|
protected function getGroupName() {
|
|
|
|
return 'pagetools';
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @param int $revId
|
|
|
|
*/
|
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
|
|
|
|
2021-03-12 20:25:22 +00:00
|
|
|
$revTimestamp = $this->revisionLookup->getTimestampFromId( $revId );
|
2020-04-02 00:35:01 +00:00
|
|
|
|
|
|
|
if ( !$revTimestamp ) {
|
2022-03-23 17:59:56 +00:00
|
|
|
$out->addHTML(
|
|
|
|
Html::errorBox(
|
|
|
|
$out->msg( 'citethispage-badrevision', $title->getPrefixedText(), $revId )->parse()
|
|
|
|
)
|
|
|
|
);
|
2016-09-19 23:27:03 +00:00
|
|
|
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
|
2020-04-02 00:35:01 +00:00
|
|
|
$parserOptions->setTimestamp( $revTimestamp );
|
2016-09-19 23:27:03 +00:00
|
|
|
|
2021-03-12 20:25:22 +00:00
|
|
|
$parser = $this->parserFactory->create();
|
2016-09-19 23:27:03 +00:00
|
|
|
// Register our <citation> tag which just parses using a different
|
|
|
|
// context
|
|
|
|
$parser->setHook( 'citation', [ $this, 'citationTag' ] );
|
2021-03-12 20:25:22 +00:00
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
// Also hold on to a separate Parser instance for <citation> tag parsing
|
|
|
|
// since we can't parse in a parse using the same Parser
|
2021-03-12 20:25:22 +00:00
|
|
|
$this->citationParser = $this->parserFactory->create();
|
2016-09-19 23:27:03 +00:00
|
|
|
|
|
|
|
$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' );
|
2017-11-22 20:53:27 +00:00
|
|
|
$this->getOutput()->addParserOutputContent( $ret, [
|
|
|
|
'enableSectionEditLinks' => false,
|
|
|
|
] );
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|
2008-01-09 20:52:38 +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.
|
2018-02-09 07:44:48 +00:00
|
|
|
$dir = __DIR__ . '/../';
|
2021-03-12 20:25:22 +00:00
|
|
|
$contentLang = $this->getContentLanguage();
|
|
|
|
$code = $contentLang->lc( $contentLang->getCode() );
|
2022-11-04 22:17:09 +00:00
|
|
|
if ( file_exists( "{$dir}citethispage-content-$code" ) ) {
|
|
|
|
$msg = file_get_contents( "{$dir}citethispage-content-$code" );
|
|
|
|
} elseif ( file_exists( "{$dir}citethispage-content" ) ) {
|
|
|
|
$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() {
|
2023-02-23 20:59:01 +00:00
|
|
|
$parserOptions = ParserOptions::newFromContext( $this->getContext() );
|
2016-09-19 23:27:03 +00:00
|
|
|
$parserOptions->setDateFormat( 'default' );
|
2019-03-09 18:47:37 +00:00
|
|
|
$parserOptions->setInterfaceMessage( true );
|
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 ) {
|
2017-10-13 21:17:40 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
2016-09-19 23:27:03 +00:00
|
|
|
$ret = $this->citationParser->parse(
|
|
|
|
$text,
|
|
|
|
$this->getPageTitle(),
|
2017-10-13 21:17:40 +00:00
|
|
|
$parserOptions,
|
2016-09-19 23:27:03 +00:00
|
|
|
/* $linestart = */ false
|
|
|
|
);
|
2008-01-09 20:52:38 +00:00
|
|
|
|
2020-04-15 21:27:06 +00:00
|
|
|
return Parser::stripOuterParagraph( $ret->getText( [
|
2017-11-22 20:53:27 +00:00
|
|
|
'enableSectionEditLinks' => false,
|
2017-12-22 18:42:36 +00:00
|
|
|
// This will be inserted into the output of another parser, so there will actually be a wrapper
|
|
|
|
'unwrap' => true,
|
2020-04-15 21:27:06 +00:00
|
|
|
'wrapperDivClass' => '',
|
|
|
|
] ) );
|
2011-11-13 14:22:24 +00:00
|
|
|
}
|
2016-09-30 08:25:14 +00:00
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/** @inheritDoc */
|
2016-09-30 08:25:14 +00:00
|
|
|
protected function getDisplayFormat() {
|
|
|
|
return 'ooui';
|
|
|
|
}
|
2016-11-15 19:35:17 +00:00
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/** @inheritDoc */
|
2016-11-15 19:35:17 +00:00
|
|
|
public function requiresUnblock() {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-15 19:55:41 +00:00
|
|
|
|
2021-03-31 13:18:05 +00:00
|
|
|
/** @inheritDoc */
|
2016-11-15 19:55:41 +00:00
|
|
|
public function requiresWrite() {
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-29 08:07:00 +00:00
|
|
|
}
|