MediaWiki  1.29.1
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->dieWithError( 'apierror-baddiff' );
38  }
39 
40  $contentHandler = $revision->getContentHandler();
41  $de = $contentHandler->createDifferenceEngine( $this->getContext(),
42  $rev1,
43  $rev2,
44  null, // rcid
45  true,
46  false );
47 
48  $vals = [];
49  if ( isset( $params['fromtitle'] ) ) {
50  $vals['fromtitle'] = $params['fromtitle'];
51  }
52  if ( isset( $params['fromid'] ) ) {
53  $vals['fromid'] = $params['fromid'];
54  }
55  $vals['fromrevid'] = $rev1;
56  if ( isset( $params['totitle'] ) ) {
57  $vals['totitle'] = $params['totitle'];
58  }
59  if ( isset( $params['toid'] ) ) {
60  $vals['toid'] = $params['toid'];
61  }
62  $vals['torevid'] = $rev2;
63 
64  $difftext = $de->getDiffBody();
65 
66  if ( $difftext === false ) {
67  $this->dieWithError( 'apierror-baddiff' );
68  }
69 
70  ApiResult::setContentValue( $vals, 'body', $difftext );
71 
72  $this->getResult()->addValue( null, $this->getModuleName(), $vals );
73  }
74 
81  private function revisionOrTitleOrId( $revision, $titleText, $titleId ) {
82  if ( $revision ) {
83  return $revision;
84  } elseif ( $titleText ) {
85  $title = Title::newFromText( $titleText );
86  if ( !$title || $title->isExternal() ) {
87  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titleText ) ] );
88  }
89 
90  return $title->getLatestRevID();
91  } elseif ( $titleId ) {
92  $title = Title::newFromID( $titleId );
93  if ( !$title ) {
94  $this->dieWithError( [ 'apierror-nosuchpageid', $titleId ] );
95  }
96 
97  return $title->getLatestRevID();
98  }
99  $this->dieWithError( 'apierror-compare-inputneeded', 'inputneeded' );
100  }
101 
102  public function getAllowedParams() {
103  return [
104  'fromtitle' => null,
105  'fromid' => [
106  ApiBase::PARAM_TYPE => 'integer'
107  ],
108  'fromrev' => [
109  ApiBase::PARAM_TYPE => 'integer'
110  ],
111  'totitle' => null,
112  'toid' => [
113  ApiBase::PARAM_TYPE => 'integer'
114  ],
115  'torev' => [
116  ApiBase::PARAM_TYPE => 'integer'
117  ],
118  ];
119  }
120 
121  protected function getExamplesMessages() {
122  return [
123  'action=compare&fromrev=1&torev=2'
124  => 'apihelp-compare-example-1',
125  ];
126  }
127 }
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:265
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:41
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:116
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1796
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:91
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:610
$params
$params
Definition: styleTest.css.php:40
php
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
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:41
ApiResult\setContentValue
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:478
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
ApiComparePages
Definition: ApiComparePages.php:26
ApiComparePages\revisionOrTitleOrId
revisionOrTitleOrId( $revision, $titleText, $titleId)
Definition: ApiComparePages.php:81
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:718
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1657
ApiComparePages\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiComparePages.php:102
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:490
ApiComparePages\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiComparePages.php:121
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:405