use factory method to get difference engine everywhere
[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->revisionOrTitle( $params['fromrev'], $params['fromtitle'] );
36 $rev2 = $this->revisionOrTitle( $params['torev'], $params['totitle'] );
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 $vals['fromrevid'] = $rev1;
51 if ( isset( $params['totitle'] ) ) {
52 $vals['totitle'] = $params['totitle'];
53 }
54 $vals['torevid'] = $rev2;
55
56 $difftext = $de->getDiffBody();
57
58 if ( $difftext === false ) {
59 $this->dieUsage( 'The diff cannot be retrieved. ' .
60 'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
61 } else {
62 ApiResult::setContent( $vals, $difftext );
63 }
64
65 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
66 }
67
68 /**
69 * @param $revision int
70 * @param $titleText string
71 * @return int
72 */
73 private function revisionOrTitle( $revision, $titleText ) {
74 if( $revision ){
75 return $revision;
76 } elseif( $titleText ) {
77 $title = Title::newFromText( $titleText );
78 if( !$title ){
79 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
80 }
81 return $title->getLatestRevID();
82 }
83 $this->dieUsage( 'inputneeded', 'A title or a revision number is needed for both the from and the to parameters' );
84 }
85
86 public function getAllowedParams() {
87 return array(
88 'fromtitle' => null,
89 'fromrev' => array(
90 ApiBase::PARAM_TYPE => 'integer'
91 ),
92 'totitle' => null,
93 'torev' => array(
94 ApiBase::PARAM_TYPE => 'integer'
95 ),
96 );
97 }
98
99 public function getParamDescription() {
100 return array(
101 'fromtitle' => 'First title to compare',
102 'fromrev' => 'First revision to compare',
103 'totitle' => 'Second title to compare',
104 'torev' => 'Second revision to compare',
105 );
106 }
107 public function getDescription() {
108 return array(
109 'Get the difference between 2 pages',
110 'You must pass a revision number or a page title for each part (1 and 2)'
111 );
112 }
113
114 public function getPossibleErrors() {
115 return array_merge( parent::getPossibleErrors(), array(
116 array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
117 array( 'invalidtitle', 'title' ),
118 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.' ),
119 ) );
120 }
121
122 public function getExamples() {
123 return array(
124 'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
125 );
126 }
127
128 public function getVersion() {
129 return __CLASS__ . ': $Id$';
130 }
131 }