2011-05-01 21:56:02 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Created on May 1, 2011
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2011 Sam Reed
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class ApiComparePages extends ApiBase {
|
|
|
|
|
|
|
|
|
|
public function __construct( $main, $action ) {
|
|
|
|
|
parent::__construct( $main, $action );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
2012-04-07 23:30:54 +00:00
|
|
|
$rev1 = $this->revisionOrTitleOrId( $params['fromrev'], $params['fromtitle'], $params['fromid'] );
|
|
|
|
|
$rev2 = $this->revisionOrTitleOrId( $params['torev'], $params['totitle'], $params['toid'] );
|
2011-05-01 21:56:02 +00:00
|
|
|
|
2012-08-24 18:49:19 +00:00
|
|
|
$revision = Revision::newFromId( $rev1 );
|
|
|
|
|
|
|
|
|
|
if ( !$revision ) {
|
|
|
|
|
$this->dieUsage( 'The diff cannot be retrieved, ' .
|
|
|
|
|
'one revision does not exist or you do not have permission to view it.', 'baddiff' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contentHandler = $revision->getContentHandler();
|
2012-06-19 01:28:05 +00:00
|
|
|
$de = $contentHandler->createDifferenceEngine( $this->getContext(),
|
2011-05-01 21:56:02 +00:00
|
|
|
$rev1,
|
|
|
|
|
$rev2,
|
|
|
|
|
null, // rcid
|
|
|
|
|
true,
|
|
|
|
|
false );
|
|
|
|
|
|
|
|
|
|
$vals = array();
|
|
|
|
|
if ( isset( $params['fromtitle'] ) ) {
|
|
|
|
|
$vals['fromtitle'] = $params['fromtitle'];
|
|
|
|
|
}
|
2012-04-07 23:30:54 +00:00
|
|
|
if ( isset( $params['fromid'] ) ) {
|
|
|
|
|
$vals['fromid'] = $params['fromid'];
|
|
|
|
|
}
|
2011-05-01 21:56:02 +00:00
|
|
|
$vals['fromrevid'] = $rev1;
|
|
|
|
|
if ( isset( $params['totitle'] ) ) {
|
|
|
|
|
$vals['totitle'] = $params['totitle'];
|
|
|
|
|
}
|
2012-04-07 23:30:54 +00:00
|
|
|
if ( isset( $params['toid'] ) ) {
|
|
|
|
|
$vals['toid'] = $params['toid'];
|
|
|
|
|
}
|
2011-05-01 21:56:02 +00:00
|
|
|
$vals['torevid'] = $rev2;
|
|
|
|
|
|
|
|
|
|
$difftext = $de->getDiffBody();
|
2011-07-17 16:51:11 +00:00
|
|
|
|
2011-06-06 09:22:52 +00:00
|
|
|
if ( $difftext === false ) {
|
2011-07-17 16:51:11 +00:00
|
|
|
$this->dieUsage( 'The diff cannot be retrieved. ' .
|
2011-06-06 09:22:52 +00:00
|
|
|
'Maybe one or both revisions do not exist or you do not have permission to view them.', 'baddiff' );
|
|
|
|
|
} else {
|
|
|
|
|
ApiResult::setContent( $vals, $difftext );
|
|
|
|
|
}
|
2011-05-01 21:56:02 +00:00
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $vals );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $revision int
|
2011-05-14 09:05:15 +00:00
|
|
|
* @param $titleText string
|
2012-04-07 23:30:54 +00:00
|
|
|
* @param $titleId int
|
2011-05-01 21:56:02 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2012-04-07 23:30:54 +00:00
|
|
|
private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
|
2011-05-01 21:56:02 +00:00
|
|
|
if( $revision ){
|
|
|
|
|
return $revision;
|
2011-05-13 16:16:08 +00:00
|
|
|
} elseif( $titleText ) {
|
|
|
|
|
$title = Title::newFromText( $titleText );
|
2011-05-01 21:56:02 +00:00
|
|
|
if( !$title ){
|
2011-05-13 16:16:08 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $titleText ) );
|
2011-05-01 21:56:02 +00:00
|
|
|
}
|
|
|
|
|
return $title->getLatestRevID();
|
2012-04-07 23:30:54 +00:00
|
|
|
} elseif ( $titleId ) {
|
|
|
|
|
$title = Title::newFromID( $titleId );
|
|
|
|
|
if( !$title ) {
|
|
|
|
|
$this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
|
|
|
|
|
}
|
|
|
|
|
return $title->getLatestRevID();
|
2011-05-01 21:56:02 +00:00
|
|
|
}
|
2012-04-07 23:30:54 +00:00
|
|
|
$this->dieUsage( 'inputneeded', 'A title, a page ID, or a revision number is needed for both the from and the to parameters' );
|
2011-05-01 21:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
|
return array(
|
|
|
|
|
'fromtitle' => null,
|
2012-04-07 23:30:54 +00:00
|
|
|
'fromid' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
|
|
|
),
|
2011-05-01 21:56:02 +00:00
|
|
|
'fromrev' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
|
|
|
),
|
|
|
|
|
'totitle' => null,
|
2012-04-07 23:30:54 +00:00
|
|
|
'toid' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
|
|
|
),
|
2011-05-01 21:56:02 +00:00
|
|
|
'torev' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
|
|
|
|
return array(
|
2011-05-02 11:12:48 +00:00
|
|
|
'fromtitle' => 'First title to compare',
|
2012-04-07 23:30:54 +00:00
|
|
|
'fromid' => 'First page ID to compare',
|
2011-05-02 11:12:48 +00:00
|
|
|
'fromrev' => 'First revision to compare',
|
|
|
|
|
'totitle' => 'Second title to compare',
|
2012-04-07 23:30:54 +00:00
|
|
|
'toid' => 'Second page ID to compare',
|
2011-05-02 11:12:48 +00:00
|
|
|
'torev' => 'Second revision to compare',
|
2011-05-01 21:56:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
|
|
|
|
|
public function getResultProperties() {
|
|
|
|
|
return array(
|
|
|
|
|
'' => array(
|
|
|
|
|
'fromtitle' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => 'string',
|
|
|
|
|
ApiBase::PROP_NULLABLE => true
|
|
|
|
|
),
|
|
|
|
|
'fromrevid' => 'integer',
|
|
|
|
|
'totitle' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => 'string',
|
|
|
|
|
ApiBase::PROP_NULLABLE => true
|
|
|
|
|
),
|
|
|
|
|
'torevid' => 'integer',
|
|
|
|
|
'*' => 'string'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-01 21:56:02 +00:00
|
|
|
public function getDescription() {
|
|
|
|
|
return array(
|
|
|
|
|
'Get the difference between 2 pages',
|
2012-04-07 23:30:54 +00:00
|
|
|
'You must pass a revision number or a page title or a page ID id for each part (1 and 2)'
|
2011-05-01 21:56:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
|
array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
|
|
|
|
|
array( 'invalidtitle', 'title' ),
|
2012-04-07 23:30:54 +00:00
|
|
|
array( 'nosuchpageid', 'pageid' ),
|
2011-06-06 09:40:45 +00:00
|
|
|
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.' ),
|
2011-05-01 21:56:02 +00:00
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2011-05-01 21:56:02 +00:00
|
|
|
return array(
|
2012-01-12 17:36:06 +00:00
|
|
|
'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
|
2011-05-01 21:56:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
|
}
|
|
|
|
|
}
|