MediaWiki  1.23.0
ApiComparePages.php
Go to the documentation of this file.
1 <?php
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->dieUsage( 'The diff cannot be retrieved, ' .
38  'one revision does not exist or you do not have permission to view it.', 'baddiff' );
39  }
40 
41  $contentHandler = $revision->getContentHandler();
42  $de = $contentHandler->createDifferenceEngine( $this->getContext(),
43  $rev1,
44  $rev2,
45  null, // rcid
46  true,
47  false );
48 
49  $vals = array();
50  if ( isset( $params['fromtitle'] ) ) {
51  $vals['fromtitle'] = $params['fromtitle'];
52  }
53  if ( isset( $params['fromid'] ) ) {
54  $vals['fromid'] = $params['fromid'];
55  }
56  $vals['fromrevid'] = $rev1;
57  if ( isset( $params['totitle'] ) ) {
58  $vals['totitle'] = $params['totitle'];
59  }
60  if ( isset( $params['toid'] ) ) {
61  $vals['toid'] = $params['toid'];
62  }
63  $vals['torevid'] = $rev2;
64 
65  $difftext = $de->getDiffBody();
66 
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'
72  );
73  }
74 
75  ApiResult::setContent( $vals, $difftext );
76 
77  $this->getResult()->addValue( null, $this->getModuleName(), $vals );
78  }
79 
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 ) );
93  }
94 
95  return $title->getLatestRevID();
96  } elseif ( $titleId ) {
97  $title = Title::newFromID( $titleId );
98  if ( !$title ) {
99  $this->dieUsageMsg( array( 'nosuchpageid', $titleId ) );
100  }
101 
102  return $title->getLatestRevID();
103  }
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'
107  );
108  }
109 
110  public function getAllowedParams() {
111  return array(
112  'fromtitle' => null,
113  'fromid' => array(
114  ApiBase::PARAM_TYPE => 'integer'
115  ),
116  'fromrev' => array(
117  ApiBase::PARAM_TYPE => 'integer'
118  ),
119  'totitle' => null,
120  'toid' => array(
121  ApiBase::PARAM_TYPE => 'integer'
122  ),
123  'torev' => array(
124  ApiBase::PARAM_TYPE => 'integer'
125  ),
126  );
127  }
128 
129  public function getParamDescription() {
130  return array(
131  'fromtitle' => 'First title to compare',
132  'fromid' => 'First page ID to compare',
133  'fromrev' => 'First revision to compare',
134  'totitle' => 'Second title to compare',
135  'toid' => 'Second page ID to compare',
136  'torev' => 'Second revision to compare',
137  );
138  }
139 
140  public function getResultProperties() {
141  return array(
142  '' => array(
143  'fromtitle' => array(
144  ApiBase::PROP_TYPE => 'string',
145  ApiBase::PROP_NULLABLE => true
146  ),
147  'fromrevid' => 'integer',
148  'totitle' => array(
149  ApiBase::PROP_TYPE => 'string',
150  ApiBase::PROP_NULLABLE => true
151  ),
152  'torevid' => 'integer',
153  '*' => 'string'
154  )
155  );
156  }
157 
158  public function getDescription() {
159  return array(
160  'Get the difference between 2 pages.',
161  'You must pass a revision number or a page title or a page ID id for each part (1 and 2).'
162  );
163  }
164 
165  public function getPossibleErrors() {
166  return array_merge( parent::getPossibleErrors(), array(
167  array( 'code' => 'inputneeded', 'info' => 'A title or a revision is needed' ),
168  array( 'invalidtitle', 'title' ),
169  array( 'nosuchpageid', 'pageid' ),
170  array(
171  'code' => 'baddiff',
172  'info' => 'The diff cannot be retrieved. Maybe one or both ' .
173  'revisions do not exist or you do not have permission to view them.'
174  ),
175  ) );
176  }
177 
178  public function getExamples() {
179  return array(
180  'api.php?action=compare&fromrev=1&torev=2' => 'Create a diff between revision 1 and 2',
181  );
182  }
183 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
ContextSource\getContext
getContext()
Get the RequestContext object.
Definition: ContextSource.php:40
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:88
ApiResult\setContent
static setContent(&$arr, $value, $subElemName=null)
Adds a content element to an array.
Definition: ApiResult.php:201
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1929
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
$params
$params
Definition: styleTest.css.php:40
ApiComparePages\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiComparePages.php:165
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiComparePages\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiComparePages.php:158
ApiComparePages\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiComparePages.php:129
ApiComparePages
Definition: ApiComparePages.php:26
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\PROP_TYPE
const PROP_TYPE
Definition: ApiBase.php:74
ApiComparePages\revisionOrTitleOrId
revisionOrTitleOrId( $revision, $titleText, $titleId)
Definition: ApiComparePages.php:86
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiBase\dieUsage
dieUsage( $description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1363
ApiBase\PROP_NULLABLE
const PROP_NULLABLE
Definition: ApiBase.php:76
ApiComparePages\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiComparePages.php:110
ApiComparePages\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiComparePages.php:28
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:148
ApiComparePages\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiComparePages.php:140
ApiComparePages\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiComparePages.php:178
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:297