SpecialComparePages.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Implements Special:ComparePages
  4. *
  5. * Copyright © 2010 Derk-Jan Hartman <hartman@videolan.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup SpecialPage
  24. */
  25. /**
  26. * Implements Special:ComparePages
  27. *
  28. * @ingroup SpecialPage
  29. */
  30. class SpecialComparePages extends SpecialPage {
  31. // Stored objects
  32. protected $opts, $skin;
  33. // Some internal settings
  34. protected $showNavigation = false;
  35. public function __construct() {
  36. parent::__construct( 'ComparePages' );
  37. }
  38. /**
  39. * Show a form for filtering namespace and username
  40. *
  41. * @param string|null $par
  42. */
  43. public function execute( $par ) {
  44. $this->setHeaders();
  45. $this->outputHeader();
  46. $this->getOutput()->addModuleStyles( 'mediawiki.special' );
  47. $this->addHelpLink( 'Help:Diff' );
  48. $form = HTMLForm::factory( 'ooui', [
  49. 'Page1' => [
  50. 'type' => 'title',
  51. 'name' => 'page1',
  52. 'label-message' => 'compare-page1',
  53. 'size' => '40',
  54. 'section' => 'page1',
  55. 'validation-callback' => [ $this, 'checkExistingTitle' ],
  56. 'required' => false,
  57. ],
  58. 'Revision1' => [
  59. 'type' => 'int',
  60. 'name' => 'rev1',
  61. 'label-message' => 'compare-rev1',
  62. 'size' => '8',
  63. 'section' => 'page1',
  64. 'validation-callback' => [ $this, 'checkExistingRevision' ],
  65. ],
  66. 'Page2' => [
  67. 'type' => 'title',
  68. 'name' => 'page2',
  69. 'label-message' => 'compare-page2',
  70. 'size' => '40',
  71. 'section' => 'page2',
  72. 'validation-callback' => [ $this, 'checkExistingTitle' ],
  73. 'required' => false,
  74. ],
  75. 'Revision2' => [
  76. 'type' => 'int',
  77. 'name' => 'rev2',
  78. 'label-message' => 'compare-rev2',
  79. 'size' => '8',
  80. 'section' => 'page2',
  81. 'validation-callback' => [ $this, 'checkExistingRevision' ],
  82. ],
  83. 'Action' => [
  84. 'type' => 'hidden',
  85. 'name' => 'action',
  86. ],
  87. 'Diffonly' => [
  88. 'type' => 'hidden',
  89. 'name' => 'diffonly',
  90. ],
  91. 'Unhide' => [
  92. 'type' => 'hidden',
  93. 'name' => 'unhide',
  94. ],
  95. ], $this->getContext(), 'compare' );
  96. $form->setSubmitTextMsg( 'compare-submit' );
  97. $form->suppressReset();
  98. $form->setMethod( 'get' );
  99. $form->setSubmitCallback( [ __CLASS__, 'showDiff' ] );
  100. $form->loadData();
  101. $form->displayForm( '' );
  102. $form->trySubmit();
  103. }
  104. public static function showDiff( $data, HTMLForm $form ) {
  105. $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
  106. $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
  107. if ( $rev1 && $rev2 ) {
  108. $revision = Revision::newFromId( $rev1 );
  109. if ( $revision ) { // NOTE: $rev1 was already checked, should exist.
  110. $contentHandler = $revision->getContentHandler();
  111. $de = $contentHandler->createDifferenceEngine( $form->getContext(),
  112. $rev1,
  113. $rev2,
  114. null, // rcid
  115. ( $data['Action'] == 'purge' ),
  116. ( $data['Unhide'] == '1' )
  117. );
  118. $de->showDiffPage( true );
  119. }
  120. }
  121. }
  122. public static function revOrTitle( $revision, $title ) {
  123. if ( $revision ) {
  124. return $revision;
  125. } elseif ( $title ) {
  126. $title = Title::newFromText( $title );
  127. if ( $title instanceof Title ) {
  128. return $title->getLatestRevID();
  129. }
  130. }
  131. return null;
  132. }
  133. public function checkExistingTitle( $value, $alldata ) {
  134. if ( $value === '' || $value === null ) {
  135. return true;
  136. }
  137. $title = Title::newFromText( $value );
  138. if ( !$title instanceof Title ) {
  139. return $this->msg( 'compare-invalid-title' )->parseAsBlock();
  140. }
  141. if ( !$title->exists() ) {
  142. return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
  143. }
  144. return true;
  145. }
  146. public function checkExistingRevision( $value, $alldata ) {
  147. if ( $value === '' || $value === null ) {
  148. return true;
  149. }
  150. $revision = Revision::newFromId( $value );
  151. if ( $revision === null ) {
  152. return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
  153. }
  154. return true;
  155. }
  156. protected function getGroupName() {
  157. return 'pagetools';
  158. }
  159. }