SpecialDiff.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Redirect from Special:Diff/### to index.php?diff=### and
  4. * from Special:Diff/###/### to index.php?oldid=###&diff=###.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup SpecialPage
  23. */
  24. /**
  25. * Redirect from Special:Diff/### to index.php?diff=### and
  26. * from Special:Diff/###/### to index.php?oldid=###&diff=###.
  27. *
  28. * All of the following are valid usages:
  29. * - [[Special:Diff/12345]] (diff of a revision with the previous one)
  30. * - [[Special:Diff/12345/prev]] (diff of a revision with the previous one as well)
  31. * - [[Special:Diff/12345/next]] (diff of a revision with the next one)
  32. * - [[Special:Diff/12345/cur]] (diff of a revision with the latest one of that page)
  33. * - [[Special:Diff/12345/98765]] (diff between arbitrary two revisions)
  34. *
  35. * @ingroup SpecialPage
  36. * @since 1.23
  37. */
  38. class SpecialDiff extends RedirectSpecialPage {
  39. public function __construct() {
  40. parent::__construct( 'Diff' );
  41. $this->mAllowedRedirectParams = [];
  42. }
  43. /**
  44. * @param string|null $subpage
  45. * @return Title|bool
  46. */
  47. public function getRedirect( $subpage ) {
  48. $parts = explode( '/', $subpage );
  49. // Try to parse the values given, generating somewhat pretty URLs if possible
  50. if ( count( $parts ) === 1 && $parts[0] !== '' ) {
  51. $this->mAddedRedirectParams['diff'] = $parts[0];
  52. } elseif ( count( $parts ) === 2 ) {
  53. $this->mAddedRedirectParams['oldid'] = $parts[0];
  54. $this->mAddedRedirectParams['diff'] = $parts[1];
  55. } else {
  56. return false;
  57. }
  58. return true;
  59. }
  60. protected function showNoRedirectPage() {
  61. $this->addHelpLink( 'Help:Diff' );
  62. $this->setHeaders();
  63. $this->outputHeader();
  64. $this->showForm();
  65. }
  66. private function showForm() {
  67. $form = HTMLForm::factory( 'ooui', [
  68. 'oldid' => [
  69. 'name' => 'oldid',
  70. 'type' => 'int',
  71. 'label-message' => 'diff-form-oldid',
  72. ],
  73. 'diff' => [
  74. 'name' => 'diff',
  75. 'class' => HTMLTextField::class,
  76. 'label-message' => 'diff-form-revid',
  77. ],
  78. ], $this->getContext(), 'diff-form' );
  79. $form->setSubmitTextMsg( 'diff-form-submit' );
  80. $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
  81. $form->show();
  82. }
  83. public function onFormSubmit( $formData ) {
  84. $params = [];
  85. if ( $formData['oldid'] ) {
  86. $params[] = $formData['oldid'];
  87. }
  88. if ( $formData['diff'] ) {
  89. $params[] = $formData['diff'];
  90. }
  91. $title = $this->getPageTitle( $params ? implode( '/', $params ) : null );
  92. $url = $title->getFullUrlForRedirect();
  93. $this->getOutput()->redirect( $url );
  94. }
  95. public function getDescription() {
  96. // 'diff' message is in lowercase, using own message
  97. return $this->msg( 'diff-form' )->text();
  98. }
  99. public function getName() {
  100. return 'diff-form';
  101. }
  102. public function isListed() {
  103. return true;
  104. }
  105. protected function getGroupName() {
  106. return 'redirects';
  107. }
  108. }