Merge "Add missing wfProfileOut()"
[mediawiki.git] / includes / api / ApiComparePages.php
blob67412598a86ae29bf022450d4607891aa66504d8
1 <?php
2 /**
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
23 * @file
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 $revision = Revision::newFromId( $rev1 );
40 if ( !$revision ) {
41 $this->dieUsage( 'The diff cannot be retrieved, ' .
42 'one revision does not exist or you do not have permission to view it.', 'baddiff' );
45 $contentHandler = $revision->getContentHandler();
46 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
47 $rev1,
48 $rev2,
49 null, // rcid
50 true,
51 false );
53 $vals = array();
54 if ( isset( $params['fromtitle'] ) ) {
55 $vals['fromtitle'] = $params['fromtitle'];
57 if ( isset( $params['fromid'] ) ) {
58 $vals['fromid'] = $params['fromid'];
60 $vals['fromrevid'] = $rev1;
61 if ( isset( $params['totitle'] ) ) {
62 $vals['totitle'] = $params['totitle'];
64 if ( isset( $params['toid'] ) ) {
65 $vals['toid'] = $params['toid'];
67 $vals['torevid'] = $rev2;
69 $difftext = $de->getDiffBody();
71 if ( $difftext === false ) {
72 $this->dieUsage( 'The diff cannot be retrieved. ' .
73 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
74 } else {
75 ApiResult::setContent( $vals, $difftext );
78 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
81 /**
82 * @param $revision int
83 * @param $titleText string
84 * @param $titleId int
85 * @return int
87 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
88 if( $revision ){
89 return $revision;
90 } elseif( $titleText ) {
91 $title = Title::newFromText( $titleText );
92 if( !$title ){
93 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
95 return $title->getLatestRevID();
96 } elseif ( $titleId ) {
97 $title = Title::newFromID( $titleId );
98 if( !$title ) {
99 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
101 return $title->getLatestRevID();
103 $this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
106 public function getAllowedParams() {
107 return array(
108 'fromtitle' => null,
109 'fromid' => array(
110 ApiBase::PARAM_TYPE => 'integer'
112 'fromrev' => array(
113 ApiBase::PARAM_TYPE => 'integer'
115 'totitle' => null,
116 'toid' => array(
117 ApiBase::PARAM_TYPE => 'integer'
119 'torev' => array(
120 ApiBase::PARAM_TYPE => 'integer'
125 public function getParamDescription() {
126 return array(
127 'fromtitle' => 'First title to compare',
128 'fromid' => 'First page ID to compare',
129 'fromrev' => 'First revision to compare',
130 'totitle' => 'Second title to compare',
131 'toid' => 'Second page ID to compare',
132 'torev' => 'Second revision to compare',
136 public function getResultProperties() {
137 return array(
138 '' => array(
139 'fromtitle' => array(
140 ApiBase::PROP_TYPE => 'string',
141 ApiBase::PROP_NULLABLE => true
143 'fromrevid' => 'integer',
144 'totitle' => array(
145 ApiBase::PROP_TYPE => 'string',
146 ApiBase::PROP_NULLABLE => true
148 'torevid' => 'integer',
149 '*' => 'string'
154 public function getDescription() {
155 return array(
156 'Get the difference between 2 pages',
157 'You must pass a revision number or a page title or a page ID id for each part (1 and 2)'
161 public function getPossibleErrors() {
162 return array_merge( parent::getPossibleErrors(), array(
163 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
164 array( 'invalidtitle', 'title' ),
165 array( 'nosuchpageid', 'pageid' ),
166 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.' ),
167 ) );
170 public function getExamples() {
171 return array(
172 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
176 public function getVersion() {
177 return __CLASS__ . ': $Id$';