4 * Created on May 1, 2011
6 * Copyright © 2011 Sam Reed
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 class ApiComparePages
extends ApiBase
{
28 public function __construct( $main, $action ) {
29 parent
::__construct( $main, $action );
32 public function execute() {
33 $params = $this->extractRequestParams();
35 $rev1 = $this->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
36 $rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
38 $de = new DifferenceEngine( $this->getContext(),
46 if ( isset( $params['fromtitle'] ) ) {
47 $vals['fromtitle'] = $params['fromtitle'];
49 if ( isset( $params['fromid'] ) ) {
50 $vals['fromid'] = $params['fromid'];
52 $vals['fromrevid'] = $rev1;
53 if ( isset( $params['totitle'] ) ) {
54 $vals['totitle'] = $params['totitle'];
56 if ( isset( $params['toid'] ) ) {
57 $vals['toid'] = $params['toid'];
59 $vals['torevid'] = $rev2;
61 $difftext = $de->getDiffBody();
63 if ( $difftext === false ) {
64 $this->dieUsage( 'The diff cannot be retrieved. ' .
65 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
67 ApiResult
::setContent( $vals, $difftext );
70 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
74 * @param $revision int
75 * @param $titleText string
79 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
82 } elseif( $titleText ) {
83 $title = Title
::newFromText( $titleText );
85 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
87 return $title->getLatestRevID();
88 } elseif ( $titleId ) {
89 $title = Title
::newFromID( $titleId );
91 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
93 return $title->getLatestRevID();
95 $this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
98 public function getAllowedParams() {
102 ApiBase
::PARAM_TYPE
=> 'integer'
105 ApiBase
::PARAM_TYPE
=> 'integer'
109 ApiBase
::PARAM_TYPE
=> 'integer'
112 ApiBase
::PARAM_TYPE
=> 'integer'
117 public function getParamDescription() {
119 'fromtitle' => 'First title to compare',
120 'fromid' => 'First page ID to compare',
121 'fromrev' => 'First revision to compare',
122 'totitle' => 'Second title to compare',
123 'toid' => 'Second page ID to compare',
124 'torev' => 'Second revision to compare',
127 public function getDescription() {
129 'Get the difference between 2 pages',
130 'You must pass a revision number or a page title or a page ID id for each part (1 and 2)'
134 public function getPossibleErrors() {
135 return array_merge( parent
::getPossibleErrors(), array(
136 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
137 array( 'invalidtitle', 'title' ),
138 array( 'nosuchpageid', 'pageid' ),
139 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.' ),
143 public function getExamples() {
145 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
149 public function getVersion() {
150 return __CLASS__
. ': $Id$';