FormSpecialPage.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * @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 $form
  137. * @return bool|string|array|Status As documented for HTMLForm::trySubmit.
  138. */
  139. abstract public function onSubmit( array $data /* $form = null */ );
  140. /**
  141. * Do something exciting on successful processing of the form, most likely to show a
  142. * confirmation message
  143. * @since 1.22 Default is to do nothing
  144. */
  145. public function onSuccess() {
  146. }
  147. /**
  148. * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
  149. *
  150. * @param string $par Subpage string if one was specified
  151. */
  152. public function execute( $par ) {
  153. $this->setParameter( $par );
  154. $this->setHeaders();
  155. // This will throw exceptions if there's a problem
  156. $this->checkExecutePermissions( $this->getUser() );
  157. $securityLevel = $this->getLoginSecurityLevel();
  158. if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) {
  159. return;
  160. }
  161. $form = $this->getForm();
  162. if ( $form->show() ) {
  163. $this->onSuccess();
  164. }
  165. }
  166. /**
  167. * Maybe do something interesting with the subpage parameter
  168. * @param string $par
  169. */
  170. protected function setParameter( $par ) {
  171. $this->par = $par;
  172. }
  173. /**
  174. * Called from execute() to check if the given user can perform this action.
  175. * Failures here must throw subclasses of ErrorPageError.
  176. * @param User $user
  177. * @throws UserBlockedError
  178. */
  179. protected function checkExecutePermissions( User $user ) {
  180. $this->checkPermissions();
  181. if ( $this->requiresUnblock() && $user->isBlocked() ) {
  182. $block = $user->getBlock();
  183. throw new UserBlockedError( $block );
  184. }
  185. if ( $this->requiresWrite() ) {
  186. $this->checkReadOnly();
  187. }
  188. }
  189. /**
  190. * Whether this action requires the wiki not to be locked
  191. * @return bool
  192. */
  193. public function requiresWrite() {
  194. return true;
  195. }
  196. /**
  197. * Whether this action cannot be executed by a blocked user
  198. * @return bool
  199. */
  200. public function requiresUnblock() {
  201. return true;
  202. }
  203. /**
  204. * Preserve POST data across reauthentication
  205. *
  206. * @since 1.32
  207. * @param array $data
  208. */
  209. protected function setReauthPostData( array $data ) {
  210. $this->reauthPostData = $data;
  211. }
  212. }