MediaWiki REL1_33
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 return true;
59 }
60
69 public function prefixSearchSubpages( $search, $limit, $offset ) {
70 $title = Title::newFromText( $search );
71 if ( !$title || !$title->canExist() ) {
72 // No prefix suggestion in special and media namespace
73 return [];
74 }
75 // Autocomplete subpage the same as a normal search
76 $result = SearchEngine::completionSearch( $search );
77 return array_map( function ( $sub ) {
78 return $sub->getSuggestedTitle();
79 }, $result->getSuggestions() );
80 }
81
82 protected function getGroupName() {
83 return 'pagetools';
84 }
85
86 private function showCitations( Title $title, $revId ) {
87 if ( !$revId ) {
88 $revId = $title->getLatestRevID();
89 }
90
91 $out = $this->getOutput();
92
93 $revision = Revision::newFromTitle( $title, $revId );
94 if ( !$revision ) {
95 $out->wrapWikiMsg( '<div class="errorbox">$1</div>',
96 [ 'citethispage-badrevision', $title->getPrefixedText(), $revId ] );
97 return;
98 }
99
100 $parserOptions = $this->getParserOptions();
101 // Set the overall timestamp to the revision's timestamp
102 $parserOptions->setTimestamp( $revision->getTimestamp() );
103
104 $parser = $this->getParser();
105 // Register our <citation> tag which just parses using a different
106 // context
107 $parser->setHook( 'citation', [ $this, 'citationTag' ] );
108 // Also hold on to a separate Parser instance for <citation> tag parsing
109 // since we can't parse in a parse using the same Parser
110 $this->citationParser = $this->getParser();
111
112 $ret = $parser->parse(
113 $this->getContentText(),
114 $title,
115 $parserOptions,
116 /* $linestart = */ false,
117 /* $clearstate = */ true,
118 $revId
119 );
120
121 $this->getOutput()->addModuleStyles( 'ext.citeThisPage' );
122 $this->getOutput()->addParserOutputContent( $ret, [
123 'enableSectionEditLinks' => false,
124 ] );
125 }
126
130 private function getParser() {
131 $parserConf = $this->getConfig()->get( 'ParserConf' );
132 return new $parserConf['class']( $parserConf );
133 }
134
140 private function getContentText() {
141 $msg = $this->msg( 'citethispage-content' )->inContentLanguage()->plain();
142 if ( $msg == '' ) {
143 # With MediaWiki 1.20 the plain text files were deleted
144 # and the text moved into SpecialCite.i18n.php
145 # This code is kept for b/c in case an installation has its own file "citethispage-content-xx"
146 # for a previously not supported language.
148 $dir = __DIR__ . '/../';
150 if ( file_exists( "${dir}citethispage-content-$code" ) ) {
151 $msg = file_get_contents( "${dir}citethispage-content-$code" );
152 } elseif ( file_exists( "${dir}citethispage-content" ) ) {
153 $msg = file_get_contents( "${dir}citethispage-content" );
154 }
155 }
156
157 return $msg;
158 }
159
165 private function getParserOptions() {
166 $parserOptions = ParserOptions::newFromUser( $this->getUser() );
167 $parserOptions->setDateFormat( 'default' );
168 $parserOptions->setInterfaceMessage( true );
169
170 // Having tidy on causes whitespace and <pre> tags to
171 // be generated around the output of the CiteThisPageOutput
172 // class TODO FIXME.
173 $parserOptions->setTidy( false );
174
175 return $parserOptions;
176 }
177
190 public function citationTag( $text, $params, Parser $parser ) {
191 $parserOptions = $this->getParserOptions();
192
193 $ret = $this->citationParser->parse(
194 $text,
195 $this->getPageTitle(),
196 $parserOptions,
197 /* $linestart = */ false
198 );
199
200 return $ret->getText( [
201 'enableSectionEditLinks' => false,
202 // This will be inserted into the output of another parser, so there will actually be a wrapper
203 'unwrap' => true,
204 ] );
205 }
206
207 protected function getDisplayFormat() {
208 return 'ooui';
209 }
210
211 public function requiresUnblock() {
212 return false;
213 }
214
215 public function requiresWrite() {
216 return false;
217 }
218}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$wgContLang
Definition Setup.php:790
foreach(LanguageCode::getNonstandardLanguageCodeMapping() as $code=> $bcp47) $wgContLanguageCode
Definition Setup.php:519
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.
Definition HTMLForm.php:133
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:69
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:137
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.
Represents a title within MediaWiki.
Definition Title.php:40
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition hooks.txt:1834
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:855
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:856
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:2003
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params
title