Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiComparePages.php
blobd6867eb52dffb88a6cd8359e5af63839d530d261
1 <?php
2 /**
4 * Created on May 1, 2011
6 * Copyright © 2011 Sam Reed
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.
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.
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
23 * @file
26 class ApiComparePages extends ApiBase {
28 public function execute() {
29 $params = $this->extractRequestParams();
31 $rev1 = $this->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
32 $rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
34 $revision = Revision::newFromId( $rev1 );
36 if ( !$revision ) {
37 $this->dieWithError( 'apierror-baddiff' );
40 $contentHandler = $revision->getContentHandler();
41 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
42 $rev1,
43 $rev2,
44 null, // rcid
45 true,
46 false );
48 $vals = [];
49 if ( isset( $params['fromtitle'] ) ) {
50 $vals['fromtitle'] = $params['fromtitle'];
52 if ( isset( $params['fromid'] ) ) {
53 $vals['fromid'] = $params['fromid'];
55 $vals['fromrevid'] = $rev1;
56 if ( isset( $params['totitle'] ) ) {
57 $vals['totitle'] = $params['totitle'];
59 if ( isset( $params['toid'] ) ) {
60 $vals['toid'] = $params['toid'];
62 $vals['torevid'] = $rev2;
64 $difftext = $de->getDiffBody();
66 if ( $difftext === false ) {
67 $this->dieWithError( 'apierror-baddiff' );
70 ApiResult::setContentValue( $vals, 'body', $difftext );
72 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
75 /**
76 * @param int $revision
77 * @param string $titleText
78 * @param int $titleId
79 * @return int
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 ) ] );
90 return $title->getLatestRevID();
91 } elseif ( $titleId ) {
92 $title = Title::newFromID( $titleId );
93 if ( !$title ) {
94 $this->dieWithError( [ 'apierror-nosuchpageid', $titleId ] );
97 return $title->getLatestRevID();
99 $this->dieWithError( 'apierror-compare-inputneeded', 'inputneeded' );
102 public function getAllowedParams() {
103 return [
104 'fromtitle' => null,
105 'fromid' => [
106 ApiBase::PARAM_TYPE => 'integer'
108 'fromrev' => [
109 ApiBase::PARAM_TYPE => 'integer'
111 'totitle' => null,
112 'toid' => [
113 ApiBase::PARAM_TYPE => 'integer'
115 'torev' => [
116 ApiBase::PARAM_TYPE => 'integer'
121 protected function getExamplesMessages() {
122 return [
123 'action=compare&fromrev=1&torev=2'
124 => 'apihelp-compare-example-1',