<?php
/**
- * Copyright (C) 2010 Derk-Jan Hartman <hartman@videolan.org>
+ * Implements Special:ComparePages
+ *
+ * Copyright © 2010 Derk-Jan Hartman <hartman@videolan.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup SpecialPage
*/
/**
- * implements Special:ComparePages
+ * Implements Special:ComparePages
+ *
* @ingroup SpecialPage
*/
class SpecialComparePages extends SpecialPage {
public function __construct() {
parent::__construct( 'ComparePages' );
- $this->includable( false );
- }
-
- protected function setup( $par ) {
- global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
-
- // Options
- $opts = new FormOptions();
- $this->opts = $opts; // bind
- $opts->add( 'page1', '' );
- $opts->add( 'page2', '' );
- $opts->add( 'rev1', '' );
- $opts->add( 'rev2', '' );
- $opts->add( 'action', '' );
- $opts->add( 'diffonly', '' );
-
- // Set values
- $opts->fetchValuesFromRequest( $wgRequest );
-
- $title1 = Title::newFromText( $opts->getValue( 'page1' ) );
- $title2 = Title::newFromText( $opts->getValue( 'page2' ) );
-
- if( $title1 && $title1->exists() && $opts->getValue( 'rev1' ) == '' ) {
- $pda = new Article( $title1 );
- $pdi = $pda->getID();
- $pdLastRevision = Revision::loadFromPageId( wfGetDB( DB_SLAVE ), $pdi );
- $opts->setValue('rev1', $pdLastRevision->getId() );
- } elseif ( $opts->getValue( 'rev1' ) != '' ) {
- $pdrev = Revision::newFromId( $opts->getValue( 'rev1' ) );
- if( $pdrev ) $opts->setValue( 'page1', $pdrev->getTitle()->getPrefixedDBkey() );
- }
- if( $title2 && $title2->exists() && $opts->getValue( 'rev2' ) == '' ) {
- $pda = new Article( $title2 );
- $pdi = $pda->getID();
- $pdLastRevision = Revision::loadFromPageId( wfGetDB( DB_SLAVE ), $pdi );
- $opts->setValue('rev2', $pdLastRevision->getId() );
- } elseif ( $opts->getValue( 'rev2' ) != '' ) {
- $pdrev = Revision::newFromId( $opts->getValue( 'rev2' ) );
- if( $pdrev ) $opts->setValue( 'page2', $pdrev->getTitle()->getPrefixedDBkey() );
- }
-
- // Store some objects
- $this->skin = $wgUser->getSkin();
}
/**
* @return String
*/
public function execute( $par ) {
- global $wgLang, $wgOut;
-
$this->setHeaders();
$this->outputHeader();
- $this->setup( $par );
+ $form = new HTMLForm( array(
+ 'Page1' => array(
+ 'type' => 'text',
+ 'name' => 'page1',
+ 'label-message' => 'compare-page1',
+ 'size' => '40',
+ 'section' => 'page1',
+ 'validation-callback' => array( $this, 'checkExistingTitle' ),
+ ),
+ 'Revision1' => array(
+ 'type' => 'int',
+ 'name' => 'rev1',
+ 'label-message' => 'compare-rev1',
+ 'size' => '8',
+ 'section' => 'page1',
+ 'validation-callback' => array( $this, 'checkExistingRevision' ),
+ ),
+ 'Page2' => array(
+ 'type' => 'text',
+ 'name' => 'page2',
+ 'label-message' => 'compare-page2',
+ 'size' => '40',
+ 'section' => 'page2',
+ 'validation-callback' => array( $this, 'checkExistingTitle' ),
+ ),
+ 'Revision2' => array(
+ 'type' => 'int',
+ 'name' => 'rev2',
+ 'label-message' => 'compare-rev2',
+ 'size' => '8',
+ 'section' => 'page2',
+ 'validation-callback' => array( $this, 'checkExistingRevision' ),
+ ),
+ 'Action' => array(
+ 'type' => 'hidden',
+ 'name' => 'action',
+ ),
+ 'Diffonly' => array(
+ 'type' => 'hidden',
+ 'name' => 'diffonly',
+ ),
+ 'Unhide' => array(
+ 'type' => 'hidden',
+ 'name' => 'unhide',
+ ),
+ ), $this->getContext(), 'compare' );
+ $form->setSubmitTextMsg( 'compare-submit' );
+ $form->suppressReset();
+ $form->setMethod( 'get' );
+ $form->setSubmitCallback( array( __CLASS__, 'showDiff' ) );
+
+ $form->loadData();
+ $form->displayForm( '' );
+ $form->trySubmit();
+ }
- // Settings
- $this->form();
+ public static function showDiff( $data, HTMLForm $form ){
+ $rev1 = self::revOrTitle( $data['Revision1'], $data['Page1'] );
+ $rev2 = self::revOrTitle( $data['Revision2'], $data['Page2'] );
- if( $this->opts->getValue( 'rev1' ) && $this->opts->getValue( 'rev2' ) ) {
- $de = new DifferenceEngine( null,
- $this->opts->getValue( 'rev1' ),
- $this->opts->getValue( 'rev2' ),
+ if( $rev1 && $rev2 ) {
+ $de = new DifferenceEngine( $form->getContext(),
+ $rev1,
+ $rev2,
null, // rcid
- ($this->opts->getValue( 'action') == "purge" ? true : false ),
- false );
- $de->showDiffPage( (bool)$this->opts->getValue( 'diffonly' ) );
+ ( $data['Action'] == 'purge' ),
+ ( $data['Unhide'] == '1' )
+ );
+ $de->showDiffPage( true );
}
}
- protected function form() {
- global $wgOut, $wgScript;
-
- // Consume values
- $page1 = $this->opts->consumeValue( 'page1' );
- $page2 = $this->opts->consumeValue( 'page2' );
- $rev1 = $this->opts->consumeValue( 'rev1' );
- $rev2 = $this->opts->consumeValue( 'rev2' );
-
- // Store query values in hidden fields so that form submission doesn't lose them
- $hidden = array();
- foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
- $hidden[] = Xml::hidden( $key, $value );
+ public static function revOrTitle( $revision, $title ) {
+ if( $revision ){
+ return $revision;
+ } elseif( $title ) {
+ $title = Title::newFromText( $title );
+ if( $title instanceof Title ){
+ return $title->getLatestRevID();
+ }
}
- $hidden = implode( "\n", $hidden );
+ return null;
+ }
- $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
- Xml::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
- Xml::fieldset( wfMsg( 'compare-selector') ) .
- Xml::openElement( 'table', array( 'id' => 'mw-diff-table', 'width' => '100%' ) ) .
- "<tr>
- <td class='mw-label' width='10%'>" .
- Xml::label( wfMsg( 'compare-page1' ), 'page1' ) .
- "</td>
- <td class='mw-input' width='40%'>" .
- Xml::input( 'page1', 40, $page1, array( 'type' => 'text' ) ) .
- "</td>
- <td class='mw-label' width='10%'>" .
- Xml::label( wfMsg( 'compare-page2' ), 'page2' ) .
- "</td>
- <td class='mw-input' width='40%'>" .
- Xml::input( 'page2', 40, $page2, array( 'type' => 'text' ) ) .
- "</td>
- </tr>" .
- "<tr>
- <td class='mw-label'>" .
- Xml::label( wfMsg( 'compare-rev1' ), 'rev1' ) .
- "</td>
- <td class='mw-input'>" .
- Xml::input( 'rev1', 8, $rev1, array( 'type' => 'text' ) ) .
- "</td>
- <td class='mw-label'>" .
- Xml::label( wfMsg( 'compare-rev2' ), 'rev2' ) .
- "</td>
- <td class='mw-input'>" .
- Xml::input( 'rev2', 8, $rev2, array( 'type' => 'text' ) ) .
- "</td>
- </tr>" .
- "<tr> <td></td>
- <td class='mw-submit' colspan='3'>" .
- Xml::submitButton( wfMsg( 'compare-submit') ) .
- "</td>
- </tr>" .
- Xml::closeElement( 'table' ) .
- Xml::closeElement( 'fieldset' ) .
- $hidden .
- Xml::closeElement( 'form' );
+ public function checkExistingTitle( $value, $alldata ) {
+ if ( $value === '' || $value === null ) {
+ return true;
+ }
+ $title = Title::newFromText( $value );
+ if ( !$title instanceof Title ) {
+ return $this->msg( 'compare-invalid-title' )->parseAsBlock();
+ }
+ if ( !$title->exists() ) {
+ return $this->msg( 'compare-title-not-exists' )->parseAsBlock();
+ }
+ return true;
+ }
- $wgOut->addHTML( $form );
+ public function checkExistingRevision( $value, $alldata ) {
+ if ( $value === '' || $value === null ) {
+ return true;
+ }
+ $revision = Revision::newFromId( $value );
+ if ( $revision === null ) {
+ return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
+ }
+ return true;
}
}