FormSpecialPage.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Special page which uses an HTMLForm to handle processing.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. /**
  24. * Special page which uses an HTMLForm to handle processing. This is mostly a
  25. * clone of FormAction. More special pages should be built this way; maybe this could be
  26. * a new structure for SpecialPages.
  27. *
  28. * @ingroup SpecialPage
  29. */
  30. abstract class FormSpecialPage extends SpecialPage {
  31. /**
  32. * The sub-page of the special page.
  33. * @var string
  34. */
  35. protected $par = null;
  36. /**
  37. * Get an HTMLForm descriptor array
  38. * @return array
  39. */
  40. abstract protected function getFormFields();
  41. /**
  42. * Add pre-text to the form
  43. * @return string HTML which will be sent to $form->addPreText()
  44. */
  45. protected function preText() {
  46. return '';
  47. }
  48. /**
  49. * Add post-text to the form
  50. * @return string HTML which will be sent to $form->addPostText()
  51. */
  52. protected function postText() {
  53. return '';
  54. }
  55. /**
  56. * Play with the HTMLForm if you need to more substantially
  57. * @param HTMLForm $form
  58. */
  59. protected function alterForm( HTMLForm $form ) {
  60. }
  61. /**
  62. * Get message prefix for HTMLForm
  63. *
  64. * @since 1.21
  65. * @return string
  66. */
  67. protected function getMessagePrefix() {
  68. return strtolower( $this->getName() );
  69. }
  70. /**
  71. * Get display format for the form. See HTMLForm documentation for available values.
  72. *
  73. * @since 1.25
  74. * @return string
  75. */
  76. protected function getDisplayFormat() {
  77. return 'table';
  78. }
  79. /**
  80. * Get the HTMLForm to control behavior
  81. * @return HTMLForm|null
  82. */
  83. protected function getForm() {
  84. $form = HTMLForm::factory(
  85. $this->getDisplayFormat(),
  86. $this->getFormFields(),
  87. $this->getContext(),
  88. $this->getMessagePrefix()
  89. );
  90. $form->setSubmitCallback( [ $this, 'onSubmit' ] );
  91. if ( $this->getDisplayFormat() !== 'ooui' ) {
  92. // No legend and wrapper by default in OOUI forms, but can be set manually
  93. // from alterForm()
  94. $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
  95. }
  96. $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
  97. if ( !$headerMsg->isDisabled() ) {
  98. $form->addHeaderText( $headerMsg->parseAsBlock() );
  99. }
  100. $form->addPreText( $this->preText() );
  101. $form->addPostText( $this->postText() );
  102. $this->alterForm( $form );
  103. if ( $form->getMethod() == 'post' ) {
  104. // Retain query parameters (uselang etc) on POST requests
  105. $params = array_diff_key(
  106. $this->getRequest()->getQueryValues(), [ 'title' => null ] );
  107. $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
  108. }
  109. // Give hooks a chance to alter the form, adding extra fields or text etc
  110. Hooks::run( 'SpecialPageBeforeFormDisplay', [ $this->getName(), &$form ] );
  111. return $form;
  112. }
  113. /**
  114. * Process the form on POST submission.
  115. * @param array $data
  116. * @param HTMLForm $form
  117. * @return bool|string|array|Status As documented for HTMLForm::trySubmit.
  118. */
  119. abstract public function onSubmit( array $data /* $form = null */ );
  120. /**
  121. * Do something exciting on successful processing of the form, most likely to show a
  122. * confirmation message
  123. * @since 1.22 Default is to do nothing
  124. */
  125. public function onSuccess() {
  126. }
  127. /**
  128. * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
  129. *
  130. * @param string $par Subpage string if one was specified
  131. */
  132. public function execute( $par ) {
  133. $this->setParameter( $par );
  134. $this->setHeaders();
  135. // This will throw exceptions if there's a problem
  136. $this->checkExecutePermissions( $this->getUser() );
  137. $form = $this->getForm();
  138. if ( $form->show() ) {
  139. $this->onSuccess();
  140. }
  141. }
  142. /**
  143. * Maybe do something interesting with the subpage parameter
  144. * @param string $par
  145. */
  146. protected function setParameter( $par ) {
  147. $this->par = $par;
  148. }
  149. /**
  150. * Called from execute() to check if the given user can perform this action.
  151. * Failures here must throw subclasses of ErrorPageError.
  152. * @param User $user
  153. * @throws UserBlockedError
  154. */
  155. protected function checkExecutePermissions( User $user ) {
  156. $this->checkPermissions();
  157. if ( $this->requiresUnblock() && $user->isBlocked() ) {
  158. $block = $user->getBlock();
  159. throw new UserBlockedError( $block );
  160. }
  161. if ( $this->requiresWrite() ) {
  162. $this->checkReadOnly();
  163. }
  164. }
  165. /**
  166. * Whether this action requires the wiki not to be locked
  167. * @return bool
  168. */
  169. public function requiresWrite() {
  170. return true;
  171. }
  172. /**
  173. * Whether this action cannot be executed by a blocked user
  174. * @return bool
  175. */
  176. public function requiresUnblock() {
  177. return true;
  178. }
  179. }