| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\CiteThisPage; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLForm; |
| 7 |
use MediaWiki\Parser\Parser; |
| 8 |
use MediaWiki\Parser\ParserFactory; |
| 9 |
use MediaWiki\Parser\ParserOptions; |
| 10 |
use MediaWiki\Revision\RevisionLookup; |
| 11 |
use MediaWiki\SpecialPage\FormSpecialPage; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use SearchEngineFactory; |
| 14 |
|
| 15 |
class SpecialCiteThisPage extends FormSpecialPage { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private $citationParser; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $title = false; |
| 26 |
|
| 27 |
|
| 28 |
private $searchEngineFactory; |
| 29 |
|
| 30 |
|
| 31 |
private $revisionLookup; |
| 32 |
|
| 33 |
|
| 34 |
private $parserFactory; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function __construct( |
| 42 |
SearchEngineFactory $searchEngineFactory, |
| 43 |
RevisionLookup $revisionLookup, |
| 44 |
ParserFactory $parserFactory |
| 45 |
) { |
| 46 |
parent::__construct( 'CiteThisPage' ); |
| 47 |
$this->searchEngineFactory = $searchEngineFactory; |
| 48 |
$this->revisionLookup = $revisionLookup; |
| 49 |
$this->parserFactory = $parserFactory; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function execute( $par ) { |
| 56 |
$this->setHeaders(); |
| 57 |
$this->addHelpLink( 'Extension:CiteThisPage' ); |
| 58 |
parent::execute( $par ); |
| 59 |
if ( $this->title instanceof Title ) { |
| 60 |
$id = $this->getRequest()->getInt( 'id' ); |
| 61 |
$this->showCitations( $this->title, $id ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected function alterForm( HTMLForm $form ) { |
| 69 |
$form->setMethod( 'get' ); |
| 70 |
$form->setFormIdentifier( 'titleform' ); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected function getFormFields() { |
| 77 |
return [ |
| 78 |
'page' => [ |
| 79 |
'name' => 'page', |
| 80 |
'type' => 'title', |
| 81 |
'exists' => true, |
| 82 |
'default' => $this->par ?? '', |
| 83 |
'label-message' => 'citethispage-change-target' |
| 84 |
] |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function onSubmit( array $data ) { |
| 93 |
|
| 94 |
|
| 95 |
if ( strlen( $data['page'] ) ) { |
| 96 |
$this->title = Title::newFromText( $data['page'] ); |
| 97 |
} |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 110 |
return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
protected function getGroupName() { |
| 115 |
return 'pagetools'; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private function showCitations( Title $title, $revId ) { |
| 123 |
if ( !$revId ) { |
| 124 |
$revId = $title->getLatestRevID(); |
| 125 |
} |
| 126 |
|
| 127 |
$out = $this->getOutput(); |
| 128 |
|
| 129 |
$revTimestamp = $this->revisionLookup->getTimestampFromId( $revId ); |
| 130 |
|
| 131 |
if ( !$revTimestamp ) { |
| 132 |
$out->addHTML( |
| 133 |
Html::errorBox( |
| 134 |
$out->msg( 'citethispage-badrevision', $title->getPrefixedText(), $revId )->parse() |
| 135 |
) |
| 136 |
); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$parserOptions = $this->getParserOptions(); |
| 141 |
|
| 142 |
$parserOptions->setTimestamp( $revTimestamp ); |
| 143 |
|
| 144 |
$parser = $this->parserFactory->create(); |
| 145 |
|
| 146 |
|
| 147 |
$parser->setHook( 'citation', [ $this, 'citationTag' ] ); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
$this->citationParser = $this->parserFactory->create(); |
| 152 |
|
| 153 |
$ret = $parser->parse( |
| 154 |
$this->getContentText(), |
| 155 |
$title, |
| 156 |
$parserOptions, |
| 157 |
false, |
| 158 |
true, |
| 159 |
$revId |
| 160 |
); |
| 161 |
|
| 162 |
$this->getOutput()->addModuleStyles( 'ext.citeThisPage' ); |
| 163 |
$this->getOutput()->addParserOutputContent( $ret, [ |
| 164 |
'enableSectionEditLinks' => false, |
| 165 |
] ); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private function getContentText() { |
| 174 |
$msg = $this->msg( 'citethispage-content' )->inContentLanguage()->plain(); |
| 175 |
if ( $msg == '' ) { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
$dir = __DIR__ . '/../'; |
| 181 |
$contentLang = $this->getContentLanguage(); |
| 182 |
$code = $contentLang->lc( $contentLang->getCode() ); |
| 183 |
if ( file_exists( "{$dir}citethispage-content-$code" ) ) { |
| 184 |
$msg = file_get_contents( "{$dir}citethispage-content-$code" ); |
| 185 |
} elseif ( file_exists( "{$dir}citethispage-content" ) ) { |
| 186 |
$msg = file_get_contents( "{$dir}citethispage-content" ); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $msg; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private function getParserOptions() { |
| 199 |
$parserOptions = ParserOptions::newFromContext( $this->getContext() ); |
| 200 |
$parserOptions->setDateFormat( 'default' ); |
| 201 |
$parserOptions->setInterfaceMessage( true ); |
| 202 |
return $parserOptions; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function citationTag( $text, $params, Parser $parser ) { |
| 218 |
$parserOptions = $this->getParserOptions(); |
| 219 |
|
| 220 |
$ret = $this->citationParser->parse( |
| 221 |
$text, |
| 222 |
$this->getPageTitle(), |
| 223 |
$parserOptions, |
| 224 |
false |
| 225 |
); |
| 226 |
|
| 227 |
return Parser::stripOuterParagraph( $ret->getText( [ |
| 228 |
'enableSectionEditLinks' => false, |
| 229 |
|
| 230 |
'unwrap' => true, |
| 231 |
'wrapperDivClass' => '', |
| 232 |
] ) ); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
protected function getDisplayFormat() { |
| 237 |
return 'ooui'; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
public function requiresUnblock() { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
public function requiresWrite() { |
| 247 |
return false; |
| 248 |
} |
| 249 |
} |