MediaWiki  master
SpecialRedirectWithAction.php
Go to the documentation of this file.
1 <?php
2 
5 
32  protected $action;
33 
35  protected $msgPrefix;
36 
38  private $searchEngineFactory;
39 
49  public function __construct(
50  $name,
51  $action,
52  $msgPrefix,
53  SearchEngineFactory $searchEngineFactory = null
54  ) {
55  parent::__construct( $name );
56  $this->action = $action;
57  $this->msgPrefix = $msgPrefix;
58  if ( !$searchEngineFactory ) {
59  // Fallback to global state if the new parameter was not provided
60  wfDeprecated( __METHOD__ . ' without providing SearchEngineFactory', '1.39' );
61  $searchEngineFactory = MediaWikiServices::getInstance()->getSearchEngineFactory();
62  }
63  $this->searchEngineFactory = $searchEngineFactory;
64  }
65 
69  public function getRedirect( $subpage ) {
70  if ( $subpage === null || $subpage === '' ) {
71  return false;
72  }
73  $this->mAddedRedirectParams['title'] = $subpage;
74  $this->mAddedRedirectParams['action'] = $this->action;
75  return true;
76  }
77 
81  protected function showNoRedirectPage() {
82  $this->setHeaders();
83  $this->outputHeader();
84  $this->showForm();
85  }
86 
87  private function showForm() {
88  // Dynamic messages used:
89  // 'special' . $this->msgPrefix . '-page'
90  // 'special' . $this->msgPrefix . '-submit'
91  // Each special page that extends this should include those as comments for grep
92  $form = HTMLForm::factory( 'ooui', [
93  'page' => [
94  'type' => 'text',
95  'name' => 'page',
96  'label-message' => 'special' . $this->msgPrefix . '-page',
97  'required' => true,
98  ],
99  ], $this->getContext(), $this->msgPrefix );
100  $form->setSubmitTextMsg( 'special' . $this->msgPrefix . '-submit' );
101  $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
102  $form->show();
103  }
104 
112  public function onFormSubmit( $formData ) {
113  $title = $formData['page'];
114  try {
115  $page = Title::newFromTextThrow( $title );
116  } catch ( MalformedTitleException $e ) {
117  return Status::newFatal( $e->getMessageObject() );
118  }
119  $query = [ 'action' => $this->action ];
120  $url = $page->getFullUrlForRedirect( $query );
121  $this->getOutput()->redirect( $url );
122  }
123 
128  public function isListed() {
129  return true;
130  }
131 
140  public function prefixSearchSubpages( $search, $limit, $offset ) {
141  return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
142  }
143 
148  protected function getGroupName() {
149  return 'redirects';
150  }
151 }
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:354
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:82
Shortcut to construct a special page alias.
Factory class for SearchEngine.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
prefixSearchString( $search, $limit, $offset, SearchEngineFactory $searchEngineFactory=null)
Perform a regular substring search for prefixSearchSubpages.
__construct( $name, $action, $msgPrefix, SearchEngineFactory $searchEngineFactory=null)
getRedirect( $subpage)
If the special page is a redirect, then get the Title object it redirects to.False otherwise....
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73