PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / api / ApiComparePages.php
blob23009123a5527de47180e543ec41ae1940d127a4
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->dieUsage( 'The diff cannot be retrieved, ' .
38 'one revision does not exist or you do not have permission to view it.', 'baddiff' );
41 $contentHandler = $revision->getContentHandler();
42 $de = $contentHandler->createDifferenceEngine( $this->getContext(),
43 $rev1,
44 $rev2,
45 null, // rcid
46 true,
47 false );
49 $vals = array();
50 if ( isset( $params['fromtitle'] ) ) {
51 $vals['fromtitle'] = $params['fromtitle'];
53 if ( isset( $params['fromid'] ) ) {
54 $vals['fromid'] = $params['fromid'];
56 $vals['fromrevid'] = $rev1;
57 if ( isset( $params['totitle'] ) ) {
58 $vals['totitle'] = $params['totitle'];
60 if ( isset( $params['toid'] ) ) {
61 $vals['toid'] = $params['toid'];
63 $vals['torevid'] = $rev2;
65 $difftext = $de->getDiffBody();
67 if ( $difftext === false ) {
68 $this->dieUsage(
69 'The diff cannot be retrieved. Maybe one or both revisions do ' .
70 'not exist or you do not have permission to view them.',
71 'baddiff'
75 ApiResult::setContentValue( $vals, 'body', $difftext );
77 $this->getResult()->addValue( null, $this->getModuleName(), $vals );
80 /**
81 * @param int $revision
82 * @param string $titleText
83 * @param int $titleId
84 * @return int
86 private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
87 if ( $revision ) {
88 return $revision;
89 } elseif ( $titleText ) {
90 $title = Title::newFromText( $titleText );
91 if ( !$title || $title->isExternal() ) {
92 $this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
95 return $title->getLatestRevID();
96 } elseif ( $titleId ) {
97 $title = Title::newFromID( $titleId );
98 if ( !$title ) {
99 $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
102 return $title->getLatestRevID();
104 $this->dieUsage(
105 'A title, a page ID, or a revision number is needed for both the from and the to parameters',
106 'inputneeded'
110 public function getAllowedParams() {
111 return array(
112 'fromtitle' => null,
113 'fromid' => array(
114 ApiBase::PARAM_TYPE => 'integer'
116 'fromrev' => array(
117 ApiBase::PARAM_TYPE => 'integer'
119 'totitle' => null,
120 'toid' => array(
121 ApiBase::PARAM_TYPE => 'integer'
123 'torev' => array(
124 ApiBase::PARAM_TYPE => 'integer'
129 protected function getExamplesMessages() {
130 return array(
131 'action=compare&fromrev=1&torev=2'
132 => 'apihelp-compare-example-1',