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