MediaWiki  REL1_22
SpecialComparePages.php
Go to the documentation of this file.
00001 <?php
00031 class SpecialComparePages extends SpecialPage {
00032 
00033     // Stored objects
00034     protected $opts, $skin;
00035 
00036     // Some internal settings
00037     protected $showNavigation = false;
00038 
00039     public function __construct() {
00040         parent::__construct( 'ComparePages' );
00041     }
00042 
00049     public function execute( $par ) {
00050         $this->setHeaders();
00051         $this->outputHeader();
00052 
00053         $form = new HTMLForm( array(
00054             'Page1' => array(
00055                 'type' => 'text',
00056                 'name' => 'page1',
00057                 'label-message' => 'compare-page1',
00058                 'size' => '40',
00059                 'section' => 'page1',
00060                 'validation-callback' => array( $this, 'checkExistingTitle' ),
00061             ),
00062             'Revision1' => array(
00063                 'type' => 'int',
00064                 'name' => 'rev1',
00065                 'label-message' => 'compare-rev1',
00066                 'size' => '8',
00067                 'section' => 'page1',
00068                 'validation-callback' => array( $this, 'checkExistingRevision' ),
00069             ),
00070             'Page2' => array(
00071                 'type' => 'text',
00072                 'name' => 'page2',
00073                 'label-message' => 'compare-page2',
00074                 'size' => '40',
00075                 'section' => 'page2',
00076                 'validation-callback' => array( $this, 'checkExistingTitle' ),
00077             ),
00078             'Revision2' => array(
00079                 'type' => 'int',
00080                 'name' => 'rev2',
00081                 'label-message' => 'compare-rev2',
00082                 'size' => '8',
00083                 'section' => 'page2',
00084                 'validation-callback' => array( $this, 'checkExistingRevision' ),
00085             ),
00086             'Action' => array(
00087                 'type' => 'hidden',
00088                 'name' => 'action',
00089             ),
00090             'Diffonly' => array(
00091                 'type' => 'hidden',
00092                 'name' => 'diffonly',
00093             ),
00094             'Unhide' => array(
00095                 'type' => 'hidden',
00096                 'name' => 'unhide',
00097             ),
00098         ), $this->getContext(), 'compare' );
00099         $form->setSubmitTextMsg( 'compare-submit' );
00100         $form->suppressReset();
00101         $form->setMethod( 'get' );
00102         $form->setSubmitCallback( array( __CLASS__, 'showDiff' ) );
00103 
00104         $form->loadData();
00105         $form->displayForm( '' );
00106         $form->trySubmit();
00107     }
00108 
00109     public static function showDiff( $data, HTMLForm $form ) {
00110         $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
00111         $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
00112 
00113         if ( $rev1 && $rev2 ) {
00114             $revision = Revision::newFromId( $rev1 );
00115 
00116             if ( $revision ) { // NOTE: $rev1 was already checked, should exist.
00117                 $contentHandler = $revision->getContentHandler();
00118                 $de = $contentHandler->createDifferenceEngine( $form->getContext(),
00119                     $rev1,
00120                     $rev2,
00121                     null, // rcid
00122                     ( $data['Action'] == 'purge' ),
00123                     ( $data['Unhide'] == '1' )
00124                 );
00125                 $de->showDiffPage( true );
00126             }
00127         }
00128     }
00129 
00130     public static function revOrTitle( $revision, $title ) {
00131         if ( $revision ) {
00132             return $revision;
00133         } elseif ( $title ) {
00134             $title = Title::newFromText( $title );
00135             if ( $title instanceof Title ) {
00136                 return $title->getLatestRevID();
00137             }
00138         }
00139 
00140         return null;
00141     }
00142 
00143     public function checkExistingTitle( $value, $alldata ) {
00144         if ( $value === '' || $value === null ) {
00145             return true;
00146         }
00147         $title = Title::newFromText( $value );
00148         if ( !$title instanceof Title ) {
00149             return $this->msg( 'compare-invalid-title' )->parseAsBlock();
00150         }
00151         if ( !$title->exists() ) {
00152             return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
00153         }
00154 
00155         return true;
00156     }
00157 
00158     public function checkExistingRevision( $value, $alldata ) {
00159         if ( $value === '' || $value === null ) {
00160             return true;
00161         }
00162         $revision = Revision::newFromId( $value );
00163         if ( $revision === null ) {
00164             return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
00165         }
00166 
00167         return true;
00168     }
00169 
00170     protected function getGroupName() {
00171         return 'pagetools';
00172     }
00173 }