PreferencesFactory.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. namespace MediaWiki\Preferences;
  21. use HTMLForm;
  22. use IContextSource;
  23. use User;
  24. /**
  25. * A PreferencesFactory is a MediaWiki service that provides the definitions of preferences for a
  26. * given user. These definitions are in the form of an HTMLForm descriptor.
  27. *
  28. * PreferencesForm (a subclass of HTMLForm) is used to generate the Preferences form, and handles
  29. * generic submission, CSRF protection, layout and other logic in a reusable manner.
  30. *
  31. * In order to generate the form, the HTMLForm object needs an array structure detailing the
  32. * form fields available, and that's what this implementations of this interface provide. Each
  33. * element of the array is a basic property-list, including the type of field, the label it is to be
  34. * given in the form, callbacks for validation and 'filtering', and other pertinent information.
  35. * Note that the 'default' field is named for generic forms, and does not represent the preference's
  36. * default (which is stored in $wgDefaultUserOptions), but the default for the form field, which
  37. * should be whatever the user has set for that preference. There is no need to override it unless
  38. * you have some special storage logic (for instance, those not presently stored as options, but
  39. * which are best set from the user preferences view).
  40. *
  41. * Field types are implemented as subclasses of the generic HTMLFormField object, and typically
  42. * implement at least getInputHTML, which generates the HTML for the input field to be placed in the
  43. * table.
  44. *
  45. * Once fields have been retrieved and validated, submission logic is handed over to the
  46. * submitForm() method of this interface.
  47. */
  48. interface PreferencesFactory {
  49. /**
  50. * Get the preferences form for a given user. This method retrieves the form descriptor for the
  51. * user, instantiates a new form using the descriptor and returns the instantiated form object.
  52. * @param User $user
  53. * @param IContextSource $contextSource
  54. * @param string $formClass
  55. * @param array $remove
  56. * @return HTMLForm
  57. */
  58. public function getForm(
  59. User $user,
  60. IContextSource $contextSource,
  61. $formClass = \PreferencesForm::class,
  62. array $remove = []
  63. );
  64. /**
  65. * Get the preferences form descriptor.
  66. * @param User $user
  67. * @param IContextSource $contextSource
  68. * @return mixed[][] An HTMLForm descriptor array.
  69. */
  70. public function getFormDescriptor( User $user, IContextSource $contextSource );
  71. /**
  72. * Get the names of preferences that should never be saved
  73. * (such as 'realname' and 'emailaddress').
  74. * @return string[]
  75. */
  76. public function getSaveBlacklist();
  77. }