ResourceLoaderUserTokensModule.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * ResourceLoader module for user tokens.
  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. * @author Krinkle
  22. */
  23. /**
  24. * Module for user tokens
  25. */
  26. class ResourceLoaderUserTokensModule extends ResourceLoaderModule {
  27. protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
  28. protected $targets = [ 'desktop', 'mobile' ];
  29. /**
  30. * Fetch the tokens for the current user.
  31. *
  32. * @param ResourceLoaderContext $context
  33. * @return array List of tokens keyed by token type
  34. */
  35. protected function contextUserTokens( ResourceLoaderContext $context ) {
  36. $user = $context->getUserObj();
  37. return [
  38. 'editToken' => $user->getEditToken(),
  39. 'patrolToken' => $user->getEditToken( 'patrol' ),
  40. 'watchToken' => $user->getEditToken( 'watch' ),
  41. 'csrfToken' => $user->getEditToken(),
  42. ];
  43. }
  44. /**
  45. * @param ResourceLoaderContext $context
  46. * @return string JavaScript code
  47. */
  48. public function getScript( ResourceLoaderContext $context ) {
  49. // Use FILTER_NOMIN annotation to prevent needless minification and caching (T84960).
  50. return ResourceLoader::FILTER_NOMIN . Xml::encodeJsCall(
  51. 'mw.user.tokens.set',
  52. [ $this->contextUserTokens( $context ) ],
  53. ResourceLoader::inDebugMode()
  54. );
  55. }
  56. /**
  57. * @return bool
  58. */
  59. public function supportsURLLoading() {
  60. return false;
  61. }
  62. /**
  63. * @return string
  64. */
  65. public function getGroup() {
  66. return 'private';
  67. }
  68. }