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->revisionOrTitle( $params['fromrev'], $params['fromtitle'] );
36 $rev2 = $this->revisionOrTitle( $params['torev'], $params['totitle'] );
38 $de = new DifferenceEngine( $this->getContext(),
46 if ( isset( $params['fromtitle'] ) ) {
47 $vals['fromtitle'] = $params['fromtitle'];
49 $vals['fromrevid'] = $rev1;
50 if ( isset( $params['totitle'] ) ) {
51 $vals['totitle'] = $params['totitle'];
53 $vals['torevid'] = $rev2;
55 $difftext = $de->getDiffBody();
57 if ( $difftext === false ) {
58 $this->dieUsage( 'The diff cannot be retrieved. ' .
59 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
61 ApiResult
::setContent( $vals, $difftext );
64 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
68 * @param $revision int
69 * @param $titleText string
72 private function revisionOrTitle( $revision, $titleText ) {
75 } elseif( $titleText ) {
76 $title = Title
::newFromText( $titleText );
78 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
80 return $title->getLatestRevID();
82 $this->dieUsage( 'inputneeded', 'A title or a revision number is needed for both the from and the to parameters' );
85 public function getAllowedParams() {
89 ApiBase
::PARAM_TYPE
=> 'integer'
93 ApiBase
::PARAM_TYPE
=> 'integer'
98 public function getParamDescription() {
100 'fromtitle' => 'First title to compare',
101 'fromrev' => 'First revision to compare',
102 'totitle' => 'Second title to compare',
103 'torev' => 'Second revision to compare',
106 public function getDescription() {
108 'Get the difference between 2 pages',
109 'You must pass a revision number or a page title for each part (1 and 2)'
113 public function getPossibleErrors() {
114 return array_merge( parent
::getPossibleErrors(), array(
115 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
116 array( 'invalidtitle', 'title' ),
117 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.' ),
121 public function getExamples() {
123 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
127 public function getVersion() {
128 return __CLASS__
. ': $Id$';