MediaWiki REL1_34
SpecialCiteThisPage.php
Go to the documentation of this file.
1<?php
2
4
6
11
15 protected $title = false;
16
17 public function __construct() {
18 parent::__construct( 'CiteThisPage' );
19 }
20
24 public function execute( $par ) {
25 $this->setHeaders();
26 $this->addHelpLink( 'Extension:CiteThisPage' );
27 parent::execute( $par );
28 if ( $this->title instanceof Title ) {
29 $id = $this->getRequest()->getInt( 'id' );
30 $this->showCitations( $this->title, $id );
31 }
32 }
33
34 protected function alterForm( HTMLForm $form ) {
35 $form->setMethod( 'get' );
36 }
37
38 protected function getFormFields() {
39 if ( isset( $this->par ) ) {
40 $default = $this->par;
41 } else {
42 $default = '';
43 }
44 return [
45 'page' => [
46 'name' => 'page',
47 'type' => 'title',
48 'default' => $default,
49 'label-message' => 'citethispage-change-target'
50 ]
51 ];
52 }
53
54 public function onSubmit( array $data ) {
55 // GET forms are "submitted" on every view, so check
56 // that some data was put in for page, as empty string
57 // will pass validation
58 if ( strlen( $data['page'] ) ) {
59 $this->title = Title::newFromText( $data['page'] );
60 }
61 return true;
62 }
63
72 public function prefixSearchSubpages( $search, $limit, $offset ) {
73 $title = Title::newFromText( $search );
74 if ( !$title || !$title->canExist() ) {
75 // No prefix suggestion in special and media namespace
76 return [];
77 }
78 // Autocomplete subpage the same as a normal search
79 $result = SearchEngine::completionSearch( $search );
80 return array_map( function ( $sub ) {
81 return $sub->getSuggestedTitle();
82 }, $result->getSuggestions() );
83 }
84
85 protected function getGroupName() {
86 return 'pagetools';
87 }
88
89 private function showCitations( Title $title, $revId ) {
90 if ( !$revId ) {
91 $revId = $title->getLatestRevID();
92 }
93
94 $out = $this->getOutput();
95
96 $revision = Revision::newFromTitle( $title, $revId );
97 if ( !$revision ) {
98 $out->wrapWikiMsg( '<div class="errorbox">$1</div>',
99 [ 'citethispage-badrevision', $title->getPrefixedText(), $revId ] );
100 return;
101 }
102
103 $parserOptions = $this->getParserOptions();
104 // Set the overall timestamp to the revision's timestamp
105 $parserOptions->setTimestamp( $revision->getTimestamp() );
106
107 $parser = $this->getParser();
108 // Register our <citation> tag which just parses using a different
109 // context
110 $parser->setHook( 'citation', [ $this, 'citationTag' ] );
111 // Also hold on to a separate Parser instance for <citation> tag parsing
112 // since we can't parse in a parse using the same Parser
113 $this->citationParser = $this->getParser();
114
115 $ret = $parser->parse(
116 $this->getContentText(),
117 $title,
118 $parserOptions,
119 /* $linestart = */ false,
120 /* $clearstate = */ true,
121 $revId
122 );
123
124 $this->getOutput()->addModuleStyles( 'ext.citeThisPage' );
125 $this->getOutput()->addParserOutputContent( $ret, [
126 'enableSectionEditLinks' => false,
127 ] );
128 }
129
133 private function getParser() {
134 $parserConf = $this->getConfig()->get( 'ParserConf' );
135 return new $parserConf['class']( $parserConf );
136 }
137
143 private function getContentText() {
144 $msg = $this->msg( 'citethispage-content' )->inContentLanguage()->plain();
145 if ( $msg == '' ) {
146 # With MediaWiki 1.20 the plain text files were deleted
147 # and the text moved into SpecialCite.i18n.php
148 # This code is kept for b/c in case an installation has its own file "citethispage-content-xx"
149 # for a previously not supported language.
150 global $wgContLanguageCode;
151 $dir = __DIR__ . '/../';
152 $code = MediaWikiServices::getInstance()->getContentLanguage()
153 ->lc( $wgContLanguageCode );
154 if ( file_exists( "${dir}citethispage-content-$code" ) ) {
155 $msg = file_get_contents( "${dir}citethispage-content-$code" );
156 } elseif ( file_exists( "${dir}citethispage-content" ) ) {
157 $msg = file_get_contents( "${dir}citethispage-content" );
158 }
159 }
160
161 return $msg;
162 }
163
169 private function getParserOptions() {
170 $parserOptions = ParserOptions::newFromUser( $this->getUser() );
171 $parserOptions->setDateFormat( 'default' );
172 $parserOptions->setInterfaceMessage( true );
173
174 // Having tidy on causes whitespace and <pre> tags to
175 // be generated around the output of the CiteThisPageOutput
176 // class TODO FIXME.
177 $parserOptions->setTidy( false );
178
179 return $parserOptions;
180 }
181
194 public function citationTag( $text, $params, Parser $parser ) {
195 $parserOptions = $this->getParserOptions();
196
197 $ret = $this->citationParser->parse(
198 $text,
199 $this->getPageTitle(),
200 $parserOptions,
201 /* $linestart = */ false
202 );
203
204 return $ret->getText( [
205 'enableSectionEditLinks' => false,
206 // This will be inserted into the output of another parser, so there will actually be a wrapper
207 'unwrap' => true,
208 ] );
209 }
210
211 protected function getDisplayFormat() {
212 return 'ooui';
213 }
214
215 public function requiresUnblock() {
216 return false;
217 }
218
219 public function requiresWrite() {
220 return false;
221 }
222}
foreach(LanguageCode::getNonstandardLanguageCodeMapping() as $code=> $bcp47) $wgContLanguageCode
Definition Setup.php:498
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:131
setMethod( $method='post')
Set the method used to submit the form.
MediaWikiServices is the service locator for the application scope of MediaWiki.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:74
onSubmit(array $data)
Process the form on POST submission.
getFormFields()
Get an HTMLForm descriptor array.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getParserOptions()
Get the common ParserOptions for both parses.
showCitations(Title $title, $revId)
requiresUnblock()
Whether this action cannot be executed by a blocked user.
requiresWrite()
Whether this action requires the wiki not to be locked.
getContentText()
Get the content to parse.
getDisplayFormat()
Get display format for the form.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
citationTag( $text, $params, Parser $parser)
Implements the <citation> tag.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Represents a title within MediaWiki.
Definition Title.php:42
canExist()
Is this in a namespace that allows actual pages?
Definition Title.php:1186
getLatestRevID( $flags=0)
What is the page_latest field for this page?
Definition Title.php:3211
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1818