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