Merge "Reduced some master queries by adding flags to Revision functions."
[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 $de = new DifferenceEngine( $this->getContext(),
39 $rev1,
40 $rev2,
41 null, // rcid
42 true,
43 false );
44
45 $vals = array();
46 if ( isset( $params['fromtitle'] ) ) {
47 $vals['fromtitle'] = $params['fromtitle'];
48 }
49 if ( isset( $params['fromid'] ) ) {
50 $vals['fromid'] = $params['fromid'];
51 }
52 $vals['fromrevid'] = $rev1;
53 if ( isset( $params['totitle'] ) ) {
54 $vals['totitle'] = $params['totitle'];
55 }
56 if ( isset( $params['toid'] ) ) {
57 $vals['toid'] = $params['toid'];
58 }
59 $vals['torevid'] = $rev2;
60
61 $difftext = $de->getDiffBody();
62
63 if ( $difftext === false ) {
64 $this->dieUsage( 'The diff cannot be retrieved. ' .
65 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
66 } else {
67 ApiResult::setContent( $vals, $difftext );
68 }
69
70 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
71 }
72
73 /**
74 * @param $revision int
75 * @param $titleText string
76 * @param $titleId int
77 * @return int
78 */
79 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
80 if( $revision ){
81 return $revision;
82 } elseif( $titleText ) {
83 $title = Title::newFromText( $titleText );
84 if( !$title ){
85 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
86 }
87 return $title->getLatestRevID();
88 } elseif ( $titleId ) {
89 $title = Title::newFromID( $titleId );
90 if( !$title ) {
91 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
92 }
93 return $title->getLatestRevID();
94 }
95 $this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
96 }
97
98 public function getAllowedParams() {
99 return array(
100 'fromtitle' => null,
101 'fromid' => array(
102 ApiBase::PARAM_TYPE => 'integer'
103 ),
104 'fromrev' => array(
105 ApiBase::PARAM_TYPE => 'integer'
106 ),
107 'totitle' => null,
108 'toid' => array(
109 ApiBase::PARAM_TYPE => 'integer'
110 ),
111 'torev' => array(
112 ApiBase::PARAM_TYPE => 'integer'
113 ),
114 );
115 }
116
117 public function getParamDescription() {
118 return array(
119 'fromtitle' => 'First title to compare',
120 'fromid' => 'First page ID to compare',
121 'fromrev' => 'First revision to compare',
122 'totitle' => 'Second title to compare',
123 'toid' => 'Second page ID to compare',
124 'torev' => 'Second revision to compare',
125 );
126 }
127
128 public function getResultProperties() {
129 return array(
130 '' => array(
131 'fromtitle' => array(
132 ApiBase::PROP_TYPE => 'string',
133 ApiBase::PROP_NULLABLE => true
134 ),
135 'fromrevid' => 'integer',
136 'totitle' => array(
137 ApiBase::PROP_TYPE => 'string',
138 ApiBase::PROP_NULLABLE => true
139 ),
140 'torevid' => 'integer',
141 '*' => 'string'
142 )
143 );
144 }
145
146 public function getDescription() {
147 return array(
148 'Get the difference between 2 pages',
149 'You must pass a revision number or a page title or a page ID id for each part (1 and 2)'
150 );
151 }
152
153 public function getPossibleErrors() {
154 return array_merge( parent::getPossibleErrors(), array(
155 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
156 array( 'invalidtitle', 'title' ),
157 array( 'nosuchpageid', 'pageid' ),
158 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.' ),
159 ) );
160 }
161
162 public function getExamples() {
163 return array(
164 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }