raise timeout for CdbTest::testCdb()
[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 __construct( $main, $action ) {
29 parent::__construct( $main, $action );
30 }
31
32 public function execute() {
33 $params = $this->extractRequestParams();
34
35 $rev1 = $this->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
36 $rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
37
38 $revision = Revision::newFromId( $rev1 );
39
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' );
43 }
44
45 $contentHandler = $revision->getContentHandler();
46 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
47 $rev1,
48 $rev2,
49 null, // rcid
50 true,
51 false );
52
53 $vals = array();
54 if ( isset( $params['fromtitle'] ) ) {
55 $vals['fromtitle'] = $params['fromtitle'];
56 }
57 if ( isset( $params['fromid'] ) ) {
58 $vals['fromid'] = $params['fromid'];
59 }
60 $vals['fromrevid'] = $rev1;
61 if ( isset( $params['totitle'] ) ) {
62 $vals['totitle'] = $params['totitle'];
63 }
64 if ( isset( $params['toid'] ) ) {
65 $vals['toid'] = $params['toid'];
66 }
67 $vals['torevid'] = $rev2;
68
69 $difftext = $de->getDiffBody();
70
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 );
76 }
77
78 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
79 }
80
81 /**
82 * @param $revision int
83 * @param $titleText string
84 * @param $titleId int
85 * @return int
86 */
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 ) );
94 }
95 return $title->getLatestRevID();
96 } elseif ( $titleId ) {
97 $title = Title::newFromID( $titleId );
98 if( !$title ) {
99 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
100 }
101 return $title->getLatestRevID();
102 }
103 $this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
104 }
105
106 public function getAllowedParams() {
107 return array(
108 'fromtitle' => null,
109 'fromid' => array(
110 ApiBase::PARAM_TYPE => 'integer'
111 ),
112 'fromrev' => array(
113 ApiBase::PARAM_TYPE => 'integer'
114 ),
115 'totitle' => null,
116 'toid' => array(
117 ApiBase::PARAM_TYPE => 'integer'
118 ),
119 'torev' => array(
120 ApiBase::PARAM_TYPE => 'integer'
121 ),
122 );
123 }
124
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',
133 );
134 }
135
136 public function getResultProperties() {
137 return array(
138 '' => array(
139 'fromtitle' => array(
140 ApiBase::PROP_TYPE => 'string',
141 ApiBase::PROP_NULLABLE => true
142 ),
143 'fromrevid' => 'integer',
144 'totitle' => array(
145 ApiBase::PROP_TYPE => 'string',
146 ApiBase::PROP_NULLABLE => true
147 ),
148 'torevid' => 'integer',
149 '*' => 'string'
150 )
151 );
152 }
153
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)'
158 );
159 }
160
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 ) );
168 }
169
170 public function getExamples() {
171 return array(
172 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
173 );
174 }
175 }