MediaWiki REL1_30
SpecialCiteThisPage.php
Go to the documentation of this file.
1<?php
2
4
9
13 protected $title = false;
14
15 public function __construct() {
16 parent::__construct( 'CiteThisPage' );
17 }
18
22 public function execute( $par ) {
23 $this->setHeaders();
24 parent::execute( $par );
25 if ( $this->title instanceof Title ) {
26 $id = $this->getRequest()->getInt( 'id' );
27 $this->showCitations( $this->title, $id );
28 }
29 }
30
31 protected function alterForm( HTMLForm $form ) {
32 $form->setMethod( 'get' );
33 }
34
35 protected function getFormFields() {
36 if ( isset( $this->par ) ) {
37 $default = $this->par;
38 } else {
39 $default = '';
40 }
41 return [
42 'page' => [
43 'name' => 'page',
44 'type' => 'title',
45 'default' => $default,
46 'label-message' => 'citethispage-change-target'
47 ]
48 ];
49 }
50
51 public function onSubmit( array $data ) {
52 // GET forms are "submitted" on every view, so check
53 // that some data was put in for page, as empty string
54 // will pass validation
55 if ( strlen( $data['page'] ) ) {
56 $this->title = Title::newFromText( $data['page'] );
57 }
58 }
59
68 public function prefixSearchSubpages( $search, $limit, $offset ) {
69 $title = Title::newFromText( $search );
70 if ( !$title || !$title->canExist() ) {
71 // No prefix suggestion in special and media namespace
72 return [];
73 }
74 // Autocomplete subpage the same as a normal search
75 $prefixSearcher = new StringPrefixSearch;
76 $result = $prefixSearcher->search( $search, $limit, [], $offset );
77 return $result;
78 }
79
80 protected function getGroupName() {
81 return 'pagetools';
82 }
83
84 private function showCitations( Title $title, $revId ) {
85 if ( !$revId ) {
86 $revId = $title->getLatestRevID();
87 }
88
89 $out = $this->getOutput();
90
91 $revision = Revision::newFromTitle( $title, $revId );
92 if ( !$revision ) {
93 $out->wrapWikiMsg( '<div class="errorbox">$1</div>',
94 [ 'citethispage-badrevision', $title->getPrefixedText(), $revId ] );
95 return;
96 }
97
98 $parserOptions = $this->getParserOptions();
99 // Set the overall timestamp to the revision's timestamp
100 $parserOptions->setTimestamp( $revision->getTimestamp() );
101
102 $parser = $this->getParser();
103 // Register our <citation> tag which just parses using a different
104 // context
105 $parser->setHook( 'citation', [ $this, 'citationTag' ] );
106 // Also hold on to a separate Parser instance for <citation> tag parsing
107 // since we can't parse in a parse using the same Parser
108 $this->citationParser = $this->getParser();
109
110 $ret = $parser->parse(
111 $this->getContentText(),
112 $title,
113 $parserOptions,
114 /* $linestart = */ false,
115 /* $clearstate = */ true,
116 $revId
117 );
118
119 $this->getOutput()->addModuleStyles( 'ext.citeThisPage' );
120 $this->getOutput()->addParserOutputContent( $ret );
121 }
122
126 private function getParser() {
127 $parserConf = $this->getConfig()->get( 'ParserConf' );
128 return new $parserConf['class']( $parserConf );
129 }
130
136 private function getContentText() {
137 $msg = $this->msg( 'citethispage-content' )->inContentLanguage()->plain();
138 if ( $msg == '' ) {
139 # With MediaWiki 1.20 the plain text files were deleted
140 # and the text moved into SpecialCite.i18n.php
141 # This code is kept for b/c in case an installation has its own file "citethispage-content-xx"
142 # for a previously not supported language.
144 $dir = __DIR__ . DIRECTORY_SEPARATOR;
146 if ( file_exists( "${dir}citethispage-content-$code" ) ) {
147 $msg = file_get_contents( "${dir}citethispage-content-$code" );
148 } elseif ( file_exists( "${dir}citethispage-content" ) ) {
149 $msg = file_get_contents( "${dir}citethispage-content" );
150 }
151 }
152
153 return $msg;
154 }
155
161 private function getParserOptions() {
162 $parserOptions = ParserOptions::newFromUser( $this->getUser() );
163 $parserOptions->setDateFormat( 'default' );
164 $parserOptions->setEditSection( false );
165
166 // Having tidy on causes whitespace and <pre> tags to
167 // be generated around the output of the CiteThisPageOutput
168 // class TODO FIXME.
169 $parserOptions->setTidy( false );
170
171 return $parserOptions;
172 }
173
186 public function citationTag( $text, $params, Parser $parser ) {
187 $ret = $this->citationParser->parse(
188 $text,
189 $this->getPageTitle(),
190 $this->getParserOptions(),
191 /* $linestart = */ false
192 );
193
194 return $ret->getText();
195 }
196
197 protected function getDisplayFormat() {
198 return 'ooui';
199 }
200
201 public function requiresUnblock() {
202 return false;
203 }
204
205 public function requiresWrite() {
206 return false;
207 }
208}
$dir
Definition Autoload.php:8
$wgContLanguageCode
Definition Setup.php:437
Special page which uses an HTMLForm to handle processing.
string $par
The sub-page of the special page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:128
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:70
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition Revision.php:134
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)
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.
Performs prefix search, returning strings.
Represents a title within MediaWiki.
Definition Title.php:39
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
namespace being checked & $result
Definition hooks.txt:2293
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2572
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:863
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1975
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:862
$params