MediaWiki  REL1_20
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->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
00036                 $rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
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                 if ( isset( $params['fromid'] ) ) {
00050                         $vals['fromid'] = $params['fromid'];
00051                 }
00052                 $vals['fromrevid'] = $rev1;
00053                 if ( isset( $params['totitle'] ) ) {
00054                         $vals['totitle'] = $params['totitle'];
00055                 }
00056                 if ( isset( $params['toid'] ) ) {
00057                         $vals['toid'] = $params['toid'];
00058                 }
00059                 $vals['torevid'] = $rev2;
00060 
00061                 $difftext = $de->getDiffBody();
00062 
00063                 if ( $difftext === false ) {
00064                         $this->dieUsage( 'The diff cannot be retrieved. ' .
00065                                 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
00066                 } else {
00067                         ApiResult::setContent( $vals, $difftext );
00068                 }
00069 
00070                 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
00071         }
00072 
00079         private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
00080                 if( $revision ){
00081                         return $revision;
00082                 } elseif( $titleText ) {
00083                         $title = Title::newFromText( $titleText );
00084                         if( !$title ){
00085                                 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
00086                         }
00087                         return $title->getLatestRevID();
00088                 } elseif ( $titleId ) {
00089                         $title = Title::newFromID( $titleId );
00090                         if( !$title ) {
00091                                 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
00092                         }
00093                         return $title->getLatestRevID();
00094                 }
00095                 $this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
00096         }
00097 
00098         public function getAllowedParams() {
00099                 return array(
00100                         'fromtitle' => null,
00101                         'fromid' => array(
00102                                 ApiBase::PARAM_TYPE => 'integer'
00103                         ),
00104                         'fromrev' => array(
00105                                 ApiBase::PARAM_TYPE => 'integer'
00106                         ),
00107                         'totitle' => null,
00108                         'toid' => array(
00109                                 ApiBase::PARAM_TYPE => 'integer'
00110                         ),
00111                         'torev' => array(
00112                                 ApiBase::PARAM_TYPE => 'integer'
00113                         ),
00114                 );
00115         }
00116 
00117         public function getParamDescription() {
00118                 return array(
00119                         'fromtitle' => 'First title to compare',
00120                         'fromid' => 'First page ID to compare',
00121                         'fromrev' => 'First revision to compare',
00122                         'totitle' => 'Second title to compare',
00123                         'toid' => 'Second page ID to compare',
00124                         'torev' => 'Second revision to compare',
00125                 );
00126         }
00127 
00128         public function getResultProperties() {
00129                 return array(
00130                         '' => array(
00131                                 'fromtitle' => array(
00132                                         ApiBase::PROP_TYPE => 'string',
00133                                         ApiBase::PROP_NULLABLE => true
00134                                 ),
00135                                 'fromrevid' => 'integer',
00136                                 'totitle' => array(
00137                                         ApiBase::PROP_TYPE => 'string',
00138                                         ApiBase::PROP_NULLABLE => true
00139                                 ),
00140                                 'torevid' => 'integer',
00141                                 '*' => 'string'
00142                         )
00143                 );
00144         }
00145 
00146         public function getDescription() {
00147                 return array(
00148                         'Get the difference between 2 pages',
00149                         'You must pass a revision number or a page title or a page ID id for each part (1 and 2)'
00150                 );
00151         }
00152 
00153         public function getPossibleErrors() {
00154                 return array_merge( parent::getPossibleErrors(), array(
00155                         array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
00156                         array( 'invalidtitle', 'title' ),
00157                         array( 'nosuchpageid', 'pageid' ),
00158                         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.' ),
00159                 ) );
00160         }
00161 
00162         public function getExamples() {
00163                 return array(
00164                         'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
00165                 );
00166         }
00167 
00168         public function getVersion() {
00169                 return __CLASS__ . ': $Id$';
00170         }
00171 }