Merge "Add CollationFa"
[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 execute() {
29 $params = $this->extractRequestParams();
30
31 $rev1 = $this->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
32 $rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
33
34 $revision = Revision::newFromId( $rev1 );
35
36 if ( !$revision ) {
37 $this->dieWithError( 'apierror-baddiff' );
38 }
39
40 $contentHandler = $revision->getContentHandler();
41 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
42 $rev1,
43 $rev2,
44 null, // rcid
45 true,
46 false );
47
48 $vals = [];
49 if ( isset( $params['fromtitle'] ) ) {
50 $vals['fromtitle'] = $params['fromtitle'];
51 }
52 if ( isset( $params['fromid'] ) ) {
53 $vals['fromid'] = $params['fromid'];
54 }
55 $vals['fromrevid'] = $rev1;
56 if ( isset( $params['totitle'] ) ) {
57 $vals['totitle'] = $params['totitle'];
58 }
59 if ( isset( $params['toid'] ) ) {
60 $vals['toid'] = $params['toid'];
61 }
62 $vals['torevid'] = $rev2;
63
64 $difftext = $de->getDiffBody();
65
66 if ( $difftext === false ) {
67 $this->dieWithError( 'apierror-baddiff' );
68 }
69
70 ApiResult::setContentValue( $vals, 'body', $difftext );
71
72 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
73 }
74
75 /**
76 * @param int $revision
77 * @param string $titleText
78 * @param int $titleId
79 * @return int
80 */
81 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
82 if ( $revision ) {
83 return $revision;
84 } elseif ( $titleText ) {
85 $title = Title::newFromText( $titleText );
86 if ( !$title || $title->isExternal() ) {
87 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titleText ) ] );
88 }
89
90 return $title->getLatestRevID();
91 } elseif ( $titleId ) {
92 $title = Title::newFromID( $titleId );
93 if ( !$title ) {
94 $this->dieWithError( [ 'apierror-nosuchpageid', $titleId ] );
95 }
96
97 return $title->getLatestRevID();
98 }
99 $this->dieWithError( 'apierror-compare-inputneeded', 'inputneeded' );
100 }
101
102 public function getAllowedParams() {
103 return [
104 'fromtitle' => null,
105 'fromid' => [
106 ApiBase::PARAM_TYPE => 'integer'
107 ],
108 'fromrev' => [
109 ApiBase::PARAM_TYPE => 'integer'
110 ],
111 'totitle' => null,
112 'toid' => [
113 ApiBase::PARAM_TYPE => 'integer'
114 ],
115 'torev' => [
116 ApiBase::PARAM_TYPE => 'integer'
117 ],
118 ];
119 }
120
121 protected function getExamplesMessages() {
122 return [
123 'action=compare&fromrev=1&torev=2'
124 => 'apihelp-compare-example-1',
125 ];
126 }
127 }