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