SpecialGoToInterwiki.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Implements Special:GoToInterwiki
  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. * Landing page for non-local interwiki links.
  25. *
  26. * Meant to warn people that the site they're visiting
  27. * is not the local wiki (In case of phishing tricks).
  28. * Only meant to be used for things that directly
  29. * redirect from url (e.g. Special:Search/google:foo )
  30. * Not meant for general interwiki linking (e.g.
  31. * [[google:foo]] should still directly link)
  32. *
  33. * @ingroup SpecialPage
  34. */
  35. class SpecialGoToInterwiki extends UnlistedSpecialPage {
  36. public function __construct( $name = 'GoToInterwiki' ) {
  37. parent::__construct( $name );
  38. }
  39. public function execute( $par ) {
  40. // Allow forcing an interstitial for local interwikis. This is used
  41. // when a redirect page is reached via a special page which resolves
  42. // to a user-dependent value (as defined by
  43. // RedirectSpecialPage::personallyIdentifiableTarget). See the hack
  44. // for avoiding T109724 in MediaWiki::performRequest (which also
  45. // explains why we can't use a query parameter instead).
  46. //
  47. // HHVM dies when substr_compare is used on an empty string so ensure it's not.
  48. $force = ( substr_compare( $par ?: 'x', 'force/', 0, 6 ) === 0 );
  49. if ( $force ) {
  50. $par = substr( $par, 6 );
  51. }
  52. $this->setHeaders();
  53. $target = Title::newFromText( $par );
  54. // Disallow special pages as a precaution against
  55. // possible redirect loops.
  56. if ( !$target || $target->isSpecialPage() ) {
  57. $this->getOutput()->setStatusCode( 404 );
  58. $this->getOutput()->addWikiMsg( 'gotointerwiki-invalid' );
  59. return;
  60. }
  61. $url = $target->getFullURL();
  62. if ( !$target->isExternal() || ( $target->isLocal() && !$force ) ) {
  63. // Either a normal page, or a local interwiki.
  64. // Just redirect.
  65. $this->getOutput()->redirect( $url, '301' );
  66. } else {
  67. $this->getOutput()->addWikiMsg(
  68. 'gotointerwiki-external',
  69. $url,
  70. $target->getFullText()
  71. );
  72. }
  73. }
  74. /**
  75. * @return bool
  76. */
  77. public function requiresWrite() {
  78. return false;
  79. }
  80. /**
  81. * @return string
  82. */
  83. protected function getGroupName() {
  84. return 'redirects';
  85. }
  86. }