MediaWiki  1.34.0
SpecialCiteThisPage.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
10  private $citationParser;
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 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:672
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
SpecialCiteThisPage\getParser
getParser()
Definition: SpecialCiteThisPage.php:133
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
SpecialCiteThisPage\showCitations
showCitations(Title $title, $revId)
Definition: SpecialCiteThisPage.php:89
SpecialCiteThisPage\getContentText
getContentText()
Get the content to parse.
Definition: SpecialCiteThisPage.php:143
SpecialCiteThisPage\citationTag
citationTag( $text, $params, Parser $parser)
Implements the <citation> tag.
Definition: SpecialCiteThisPage.php:194
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1818
SpecialCiteThisPage\onSubmit
onSubmit(array $data)
Process the form on POST submission.
Definition: SpecialCiteThisPage.php:54
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialCiteThisPage\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: SpecialCiteThisPage.php:219
HTMLForm\setMethod
setMethod( $method='post')
Set the method used to submit the form.
Definition: HTMLForm.php:1629
Revision\newFromTitle
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:138
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:758
SpecialCiteThisPage
Definition: SpecialCiteThisPage.php:5
SpecialCiteThisPage\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: SpecialCiteThisPage.php:211
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
SpecialCiteThisPage\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.
Definition: SpecialCiteThisPage.php:215
Title\canExist
canExist()
Is this in a namespace that allows actual pages?
Definition: Title.php:1186
SpecialCiteThisPage\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialCiteThisPage.php:38
FormSpecialPage\$par
string null $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
SpecialCiteThisPage\$title
Title bool $title
Definition: SpecialCiteThisPage.php:15
Title\getLatestRevID
getLatestRevID( $flags=0)
What is the page_latest field for this page?
Definition: Title.php:3211
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
SpecialCiteThisPage\__construct
__construct()
Definition: SpecialCiteThisPage.php:17
SpecialCiteThisPage\getParserOptions
getParserOptions()
Get the common ParserOptions for both parses.
Definition: SpecialCiteThisPage.php:169
SearchEngine\completionSearch
completionSearch( $search)
Perform a completion search.
Definition: SearchEngine.php:573
Title
Represents a title within MediaWiki.
Definition: Title.php:42
SpecialCiteThisPage\$citationParser
Parser $citationParser
Definition: SpecialCiteThisPage.php:10
$wgContLanguageCode
foreach(LanguageCode::getNonstandardLanguageCodeMapping() as $code=> $bcp47) $wgContLanguageCode
Definition: Setup.php:499
SpecialCiteThisPage\execute
execute( $par)
Definition: SpecialCiteThisPage.php:24
SpecialCiteThisPage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialCiteThisPage.php:85
SpecialCiteThisPage\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialCiteThisPage.php:72
SpecialCiteThisPage\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialCiteThisPage.php:34
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:1027
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131