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 $contentHandler = ContentHandler
::getForModelName( $rev1->getContentModelName() );
39 $de = $contentHandler->getDifferenceEngine( $this->getContext(),
47 if ( isset( $params['fromtitle'] ) ) {
48 $vals['fromtitle'] = $params['fromtitle'];
50 $vals['fromrevid'] = $rev1;
51 if ( isset( $params['totitle'] ) ) {
52 $vals['totitle'] = $params['totitle'];
54 $vals['torevid'] = $rev2;
56 $difftext = $de->getDiffBody();
58 if ( $difftext === false ) {
59 $this->dieUsage( 'The diff cannot be retrieved. ' .
60 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
62 ApiResult
::setContent( $vals, $difftext );
65 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
69 * @param $revision int
70 * @param $titleText string
73 private function revisionOrTitle( $revision, $titleText ) {
76 } elseif( $titleText ) {
77 $title = Title
::newFromText( $titleText );
79 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
81 return $title->getLatestRevID();
83 $this->dieUsage( 'inputneeded', 'A title or a revision number is needed for both the from and the to parameters' );
86 public function getAllowedParams() {
90 ApiBase
::PARAM_TYPE
=> 'integer'
94 ApiBase
::PARAM_TYPE
=> 'integer'
99 public function getParamDescription() {
101 'fromtitle' => 'First title to compare',
102 'fromrev' => 'First revision to compare',
103 'totitle' => 'Second title to compare',
104 'torev' => 'Second revision to compare',
107 public function getDescription() {
109 'Get the difference between 2 pages',
110 'You must pass a revision number or a page title for each part (1 and 2)'
114 public function getPossibleErrors() {
115 return array_merge( parent
::getPossibleErrors(), array(
116 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
117 array( 'invalidtitle', 'title' ),
118 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.' ),
122 public function getExamples() {
124 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
128 public function getVersion() {
129 return __CLASS__
. ': $Id$';