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