FormSpecialPage.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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|null
  34. */
  35. protected $par = null;
  36. /**
  37. * @var array|null POST data preserved across re-authentication
  38. * @since 1.32
  39. */
  40. protected $reauthPostData = null;
  41. /**
  42. * Get an HTMLForm descriptor array
  43. * @return array
  44. */
  45. abstract protected function getFormFields();
  46. /**
  47. * Add pre-text to the form
  48. * @return string HTML which will be sent to $form->addPreText()
  49. */
  50. protected function preText() {
  51. return '';
  52. }
  53. /**
  54. * Add post-text to the form
  55. * @return string HTML which will be sent to $form->addPostText()
  56. */
  57. protected function postText() {
  58. return '';
  59. }
  60. /**
  61. * Play with the HTMLForm if you need to more substantially
  62. * @param HTMLForm $form
  63. */
  64. protected function alterForm( HTMLForm $form ) {
  65. }
  66. /**
  67. * Get message prefix for HTMLForm
  68. *
  69. * @since 1.21
  70. * @return string
  71. */
  72. protected function getMessagePrefix() {
  73. return strtolower( $this->getName() );
  74. }
  75. /**
  76. * Get display format for the form. See HTMLForm documentation for available values.
  77. *
  78. * @since 1.25
  79. * @return string
  80. */
  81. protected function getDisplayFormat() {
  82. return 'table';
  83. }
  84. /**
  85. * Get the HTMLForm to control behavior
  86. * @return HTMLForm|null
  87. */
  88. protected function getForm() {
  89. $context = $this->getContext();
  90. $onSubmit = [ $this, 'onSubmit' ];
  91. if ( $this->reauthPostData ) {
  92. // Restore POST data
  93. $context = new DerivativeContext( $context );
  94. $oldRequest = $this->getRequest();
  95. $context->setRequest( new DerivativeRequest(
  96. $oldRequest, $this->reauthPostData + $oldRequest->getQueryValues(), true
  97. ) );
  98. // But don't treat it as a "real" submission just in case of some
  99. // crazy kind of CSRF.
  100. $onSubmit = function () {
  101. return false;
  102. };
  103. }
  104. $form = HTMLForm::factory(
  105. $this->getDisplayFormat(),
  106. $this->getFormFields(),
  107. $context,
  108. $this->getMessagePrefix()
  109. );
  110. $form->setSubmitCallback( $onSubmit );
  111. if ( $this->getDisplayFormat() !== 'ooui' ) {
  112. // No legend and wrapper by default in OOUI forms, but can be set manually
  113. // from alterForm()
  114. $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
  115. }
  116. $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
  117. if ( !$headerMsg->isDisabled() ) {
  118. $form->addHeaderText( $headerMsg->parseAsBlock() );
  119. }
  120. $form->addPreText( $this->preText() );
  121. $form->addPostText( $this->postText() );
  122. $this->alterForm( $form );
  123. if ( $form->getMethod() == 'post' ) {
  124. // Retain query parameters (uselang etc) on POST requests
  125. $params = array_diff_key(
  126. $this->getRequest()->getQueryValues(), [ 'title' => null ] );
  127. $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
  128. }
  129. // Give hooks a chance to alter the form, adding extra fields or text etc
  130. Hooks::run( 'SpecialPageBeforeFormDisplay', [ $this->getName(), &$form ] );
  131. return $form;
  132. }
  133. /**
  134. * Process the form on POST submission.
  135. * @param array $data
  136. * @param HTMLForm|null $form
  137. * @suppress PhanCommentParamWithoutRealParam Many implementations don't have $form
  138. * @return bool|string|array|Status As documented for HTMLForm::trySubmit.
  139. */
  140. abstract public function onSubmit( array $data /* HTMLForm $form = null */ );
  141. /**
  142. * Do something exciting on successful processing of the form, most likely to show a
  143. * confirmation message
  144. * @since 1.22 Default is to do nothing
  145. */
  146. public function onSuccess() {
  147. }
  148. /**
  149. * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
  150. *
  151. * @param string|null $par Subpage string if one was specified
  152. */
  153. public function execute( $par ) {
  154. $this->setParameter( $par );
  155. $this->setHeaders();
  156. // This will throw exceptions if there's a problem
  157. $this->checkExecutePermissions( $this->getUser() );
  158. $securityLevel = $this->getLoginSecurityLevel();
  159. if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) {
  160. return;
  161. }
  162. $form = $this->getForm();
  163. if ( $form->show() ) {
  164. $this->onSuccess();
  165. }
  166. }
  167. /**
  168. * Maybe do something interesting with the subpage parameter
  169. * @param string|null $par
  170. */
  171. protected function setParameter( $par ) {
  172. $this->par = $par;
  173. }
  174. /**
  175. * Called from execute() to check if the given user can perform this action.
  176. * Failures here must throw subclasses of ErrorPageError.
  177. * @param User $user
  178. * @throws UserBlockedError
  179. */
  180. protected function checkExecutePermissions( User $user ) {
  181. $this->checkPermissions();
  182. if ( $this->requiresUnblock() ) {
  183. $block = $user->getBlock();
  184. if ( $block && $block->isSitewide() ) {
  185. throw new UserBlockedError( $block );
  186. }
  187. }
  188. if ( $this->requiresWrite() ) {
  189. $this->checkReadOnly();
  190. }
  191. }
  192. /**
  193. * Whether this action requires the wiki not to be locked
  194. * @return bool
  195. */
  196. public function requiresWrite() {
  197. return true;
  198. }
  199. /**
  200. * Whether this action cannot be executed by a blocked user
  201. * @return bool
  202. */
  203. public function requiresUnblock() {
  204. return true;
  205. }
  206. /**
  207. * Preserve POST data across reauthentication
  208. *
  209. * @since 1.32
  210. * @param array $data
  211. */
  212. protected function setReauthPostData( array $data ) {
  213. $this->reauthPostData = $data;
  214. }
  215. }