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
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 );
37 $this->dieWithError( 'apierror-baddiff' );
40 $contentHandler = $revision->getContentHandler();
41 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
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 );
76 * @param int $revision
77 * @param string $titleText
81 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
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 );
94 $this->dieWithError( [ 'apierror-nosuchpageid', $titleId ] );
97 return $title->getLatestRevID();
99 $this->dieWithError( 'apierror-compare-inputneeded', 'inputneeded' );
102 public function getAllowedParams() {
106 ApiBase
::PARAM_TYPE
=> 'integer'
109 ApiBase
::PARAM_TYPE
=> 'integer'
113 ApiBase
::PARAM_TYPE
=> 'integer'
116 ApiBase
::PARAM_TYPE
=> 'integer'
121 protected function getExamplesMessages() {
123 'action=compare&fromrev=1&torev=2'
124 => 'apihelp-compare-example-1',