Merge "Fix history and add section tabs being collapsed on RTL wikis"
[lhc/web/wiklou.git] / includes / api / ApiComparePages.php
1 <?php
2 /**
3 *
4 * Created on May 1, 2011
5 *
6 * Copyright © 2011 Sam Reed
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 class ApiComparePages extends ApiBase {
27
28 public function execute() {
29 $params = $this->extractRequestParams();
30
31 $rev1 = $this->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
32 $rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
33
34 $revision = Revision::newFromId( $rev1 );
35
36 if ( !$revision ) {
37 $this->dieUsage( 'The diff cannot be retrieved, ' .
38 'one revision does not exist or you do not have permission to view it.', 'baddiff' );
39 }
40
41 $contentHandler = $revision->getContentHandler();
42 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
43 $rev1,
44 $rev2,
45 null, // rcid
46 true,
47 false );
48
49 $vals = array();
50 if ( isset( $params['fromtitle'] ) ) {
51 $vals['fromtitle'] = $params['fromtitle'];
52 }
53 if ( isset( $params['fromid'] ) ) {
54 $vals['fromid'] = $params['fromid'];
55 }
56 $vals['fromrevid'] = $rev1;
57 if ( isset( $params['totitle'] ) ) {
58 $vals['totitle'] = $params['totitle'];
59 }
60 if ( isset( $params['toid'] ) ) {
61 $vals['toid'] = $params['toid'];
62 }
63 $vals['torevid'] = $rev2;
64
65 $difftext = $de->getDiffBody();
66
67 if ( $difftext === false ) {
68 $this->dieUsage( 'The diff cannot be retrieved. ' .
69 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
70 } else {
71 ApiResult::setContent( $vals, $difftext );
72 }
73
74 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
75 }
76
77 /**
78 * @param $revision int
79 * @param $titleText string
80 * @param $titleId int
81 * @return int
82 */
83 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
84 if ( $revision ) {
85 return $revision;
86 } elseif ( $titleText ) {
87 $title = Title::newFromText( $titleText );
88 if ( !$title || $title->isExternal() ) {
89 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
90 }
91 return $title->getLatestRevID();
92 } elseif ( $titleId ) {
93 $title = Title::newFromID( $titleId );
94 if ( !$title ) {
95 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
96 }
97 return $title->getLatestRevID();
98 }
99 $this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
100 }
101
102 public function getAllowedParams() {
103 return array(
104 'fromtitle' => null,
105 'fromid' => array(
106 ApiBase::PARAM_TYPE => 'integer'
107 ),
108 'fromrev' => array(
109 ApiBase::PARAM_TYPE => 'integer'
110 ),
111 'totitle' => null,
112 'toid' => array(
113 ApiBase::PARAM_TYPE => 'integer'
114 ),
115 'torev' => array(
116 ApiBase::PARAM_TYPE => 'integer'
117 ),
118 );
119 }
120
121 public function getParamDescription() {
122 return array(
123 'fromtitle' => 'First title to compare',
124 'fromid' => 'First page ID to compare',
125 'fromrev' => 'First revision to compare',
126 'totitle' => 'Second title to compare',
127 'toid' => 'Second page ID to compare',
128 'torev' => 'Second revision to compare',
129 );
130 }
131
132 public function getResultProperties() {
133 return array(
134 '' => array(
135 'fromtitle' => array(
136 ApiBase::PROP_TYPE => 'string',
137 ApiBase::PROP_NULLABLE => true
138 ),
139 'fromrevid' => 'integer',
140 'totitle' => array(
141 ApiBase::PROP_TYPE => 'string',
142 ApiBase::PROP_NULLABLE => true
143 ),
144 'torevid' => 'integer',
145 '*' => 'string'
146 )
147 );
148 }
149
150 public function getDescription() {
151 return array(
152 'Get the difference between 2 pages',
153 'You must pass a revision number or a page title or a page ID id for each part (1 and 2)'
154 );
155 }
156
157 public function getPossibleErrors() {
158 return array_merge( parent::getPossibleErrors(), array(
159 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
160 array( 'invalidtitle', 'title' ),
161 array( 'nosuchpageid', 'pageid' ),
162 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.' ),
163 ) );
164 }
165
166 public function getExamples() {
167 return array(
168 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
169 );
170 }
171 }