JavaScriptContentHandler.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /**
  21. * Content handler for JavaScript pages.
  22. *
  23. * @todo Create a ScriptContentHandler base class, do highlighting stuff there?
  24. *
  25. * @since 1.21
  26. * @ingroup Content
  27. */
  28. class JavaScriptContentHandler extends CodeContentHandler {
  29. /**
  30. * @param string $modelId
  31. */
  32. public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
  33. parent::__construct( $modelId, [ CONTENT_FORMAT_JAVASCRIPT ] );
  34. }
  35. /**
  36. * @return string
  37. */
  38. protected function getContentClass() {
  39. return JavaScriptContent::class;
  40. }
  41. public function supportsRedirects() {
  42. return true;
  43. }
  44. /**
  45. * Create a redirect that is also valid JavaScript
  46. *
  47. * @param Title $destination
  48. * @param string $text ignored
  49. * @return JavaScriptContent
  50. */
  51. public function makeRedirectContent( Title $destination, $text = '' ) {
  52. // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
  53. $url = $destination->getFullURL( 'action=raw&ctype=text/javascript', false, PROTO_RELATIVE );
  54. $class = $this->getContentClass();
  55. return new $class( '/* #REDIRECT */' . Xml::encodeJsCall( 'mw.loader.load', [ $url ] ) );
  56. }
  57. }