MediaWiki
REL1_24
|
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( 00069 'The diff cannot be retrieved. Maybe one or both revisions do ' . 00070 'not exist or you do not have permission to view them.', 00071 'baddiff' 00072 ); 00073 } 00074 00075 ApiResult::setContent( $vals, $difftext ); 00076 00077 $this->getResult()->addValue( null, $this->getModuleName(), $vals ); 00078 } 00079 00086 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) { 00087 if ( $revision ) { 00088 return $revision; 00089 } elseif ( $titleText ) { 00090 $title = Title::newFromText( $titleText ); 00091 if ( !$title || $title->isExternal() ) { 00092 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) ); 00093 } 00094 00095 return $title->getLatestRevID(); 00096 } elseif ( $titleId ) { 00097 $title = Title::newFromID( $titleId ); 00098 if ( !$title ) { 00099 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) ); 00100 } 00101 00102 return $title->getLatestRevID(); 00103 } 00104 $this->dieUsage( 00105 'A title, a page ID, or a revision number is needed for both the from and the to parameters', 00106 'inputneeded' 00107 ); 00108 } 00109 00110 public function getAllowedParams() { 00111 return array( 00112 'fromtitle' => null, 00113 'fromid' => array( 00114 ApiBase::PARAM_TYPE => 'integer' 00115 ), 00116 'fromrev' => array( 00117 ApiBase::PARAM_TYPE => 'integer' 00118 ), 00119 'totitle' => null, 00120 'toid' => array( 00121 ApiBase::PARAM_TYPE => 'integer' 00122 ), 00123 'torev' => array( 00124 ApiBase::PARAM_TYPE => 'integer' 00125 ), 00126 ); 00127 } 00128 00129 public function getParamDescription() { 00130 return array( 00131 'fromtitle' => 'First title to compare', 00132 'fromid' => 'First page ID to compare', 00133 'fromrev' => 'First revision to compare', 00134 'totitle' => 'Second title to compare', 00135 'toid' => 'Second page ID to compare', 00136 'torev' => 'Second revision to compare', 00137 ); 00138 } 00139 00140 public function getDescription() { 00141 return array( 00142 'Get the difference between 2 pages.', 00143 'You must pass a revision number or a page title or a page ID id for each part (1 and 2).' 00144 ); 00145 } 00146 00147 public function getExamples() { 00148 return array( 00149 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2', 00150 ); 00151 } 00152 }