MediaWiki  1.28.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 = [];
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::setContentValue( $vals, 'body', $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( [ 'invalidtitle', $titleText ] );
93  }
94 
95  return $title->getLatestRevID();
96  } elseif ( $titleId ) {
97  $title = Title::newFromID( $titleId );
98  if ( !$title ) {
99  $this->dieUsageMsg( [ '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 [
112  'fromtitle' => null,
113  'fromid' => [
114  ApiBase::PARAM_TYPE => 'integer'
115  ],
116  'fromrev' => [
117  ApiBase::PARAM_TYPE => 'integer'
118  ],
119  'totitle' => null,
120  'toid' => [
121  ApiBase::PARAM_TYPE => 'integer'
122  ],
123  'torev' => [
124  ApiBase::PARAM_TYPE => 'integer'
125  ],
126  ];
127  }
128 
129  protected function getExamplesMessages() {
130  return [
131  'action=compare&fromrev=1&torev=2'
132  => 'apihelp-compare-example-1',
133  ];
134  }
135 }
static newFromID($id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:402
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
getResult()
Get the result object.
Definition: ApiBase.php:577
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:678
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Definition: ApiResult.php:480
getContext()
Get the base IContextSource object.
$params
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:457
static newFromId($id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:110
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
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:1574
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
revisionOrTitleOrId($revision, $titleText, $titleId)
dieUsageMsg($error)
Output the error message related to a certain array.
Definition: ApiBase.php:2192