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