MediaWiki  REL1_19
ApiComparePages.php
Go to the documentation of this file.
00001 <?php
00026 class ApiComparePages extends ApiBase {
00027 
00028         public function __construct( $main, $action ) {
00029                 parent::__construct( $main, $action );
00030         }
00031 
00032         public function execute() {
00033                 $params = $this->extractRequestParams();
00034 
00035                 $rev1 = $this->revisionOrTitle( $params['fromrev'], $params['fromtitle'] );
00036                 $rev2 = $this->revisionOrTitle( $params['torev'], $params['totitle'] );
00037 
00038                 $de = new DifferenceEngine( $this->getContext(),
00039                         $rev1,
00040                         $rev2,
00041                         null, // rcid
00042                         true,
00043                         false );
00044 
00045                 $vals = array();
00046                 if ( isset( $params['fromtitle'] ) ) {
00047                         $vals['fromtitle'] = $params['fromtitle'];
00048                 }
00049                 $vals['fromrevid'] = $rev1;
00050                 if ( isset( $params['totitle'] ) ) {
00051                         $vals['totitle'] = $params['totitle'];
00052                 }
00053                 $vals['torevid'] = $rev2;
00054 
00055                 $difftext = $de->getDiffBody();
00056 
00057                 if ( $difftext === false ) {
00058                         $this->dieUsage( 'The diff cannot be retrieved. ' .
00059                                 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
00060                 } else {
00061                         ApiResult::setContent( $vals, $difftext );
00062                 }
00063 
00064                 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
00065         }
00066 
00072         private function revisionOrTitle( $revision, $titleText ) {
00073                 if( $revision ){
00074                         return $revision;
00075                 } elseif( $titleText ) {
00076                         $title = Title::newFromText( $titleText );
00077                         if( !$title ){
00078                                 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
00079                         }
00080                         return $title->getLatestRevID();
00081                 }
00082                 $this->dieUsage( 'inputneeded', 'A title or a revision number is needed for both the from and the to parameters' );
00083         }
00084 
00085         public function getAllowedParams() {
00086                 return array(
00087                         'fromtitle' => null,
00088                         'fromrev' => array(
00089                                 ApiBase::PARAM_TYPE => 'integer'
00090                         ),
00091                         'totitle' => null,
00092                         'torev' => array(
00093                                 ApiBase::PARAM_TYPE => 'integer'
00094                         ),
00095                 );
00096         }
00097 
00098         public function getParamDescription() {
00099                 return array(
00100                         'fromtitle' => 'First title to compare',
00101                         'fromrev' => 'First revision to compare',
00102                         'totitle' => 'Second title to compare',
00103                         'torev' => 'Second revision to compare',
00104                 );
00105         }
00106         public function getDescription() {
00107                 return array(
00108                         'Get the difference between 2 pages',
00109                         'You must pass a revision number or a page title for each part (1 and 2)'
00110                 );
00111         }
00112 
00113         public function getPossibleErrors() {
00114                 return array_merge( parent::getPossibleErrors(), array(
00115                         array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
00116                         array( 'invalidtitle', 'title' ),
00117                         array( 'code' => 'baddiff', 'info' => 'The diff cannot be retrieved. Maybe one or both revisions do not exist or you do not have permission to view them.' ),
00118                 ) );
00119         }
00120 
00121         public function getExamples() {
00122                 return array(
00123                         'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
00124                 );
00125         }
00126 
00127         public function getVersion() {
00128                 return __CLASS__ . ': $Id$';
00129         }
00130 }