MediaWiki  master
LatestPageContentHandler.php
Go to the documentation of this file.
1 <?php
2 
3 
4 namespace MediaWiki\Rest\Handler;
5 
6 use Config;
11 use RequestContext;
12 use Title;
13 use TitleFormatter;
14 use User;
16 
17 abstract class LatestPageContentHandler extends SimpleHandler {
18 
20  protected $config;
21 
23  protected $permissionManager;
24 
26  protected $revisionLookup;
27 
29  protected $titleFormatter;
30 
32  protected $user;
33 
35  private $latestRevision;
36 
38  private $titleObject;
39 
46  public function __construct(
51  ) {
52  $this->config = $config;
53  $this->permissionManager = $permissionManager;
54  $this->revisionLookup = $revisionLookup;
55  $this->titleFormatter = $titleFormatter;
56 
57  // @todo Inject this, when there is a good way to do that
58  $this->user = RequestContext::getMain()->getUser();
59  }
60 
64  protected function getTitle() {
65  if ( $this->titleObject === null ) {
66  $this->titleObject = Title::newFromText( $this->getValidatedParams()['title'] ) ?? false;
67  }
68  return $this->titleObject;
69  }
70 
74  protected function getLatestRevision() {
75  if ( $this->latestRevision === null ) {
76  $title = $this->getTitle();
77  if ( $title && $title->getArticleID() ) {
78  $this->latestRevision = $this->revisionLookup->getKnownCurrentRevision( $title );
79  } else {
80  $this->latestRevision = false;
81  }
82  }
83  return $this->latestRevision;
84  }
85 
86  protected function isAccessible( $titleObject ): bool {
87  return $this->permissionManager->userCan( 'read', $this->user, $titleObject );
88  }
89 
90  protected function constructMetadata(
92  RevisionRecord $revision
93  ): array {
94  return [
95  'id' => $titleObject->getArticleID(),
96  'key' => $this->titleFormatter->getPrefixedDbKey( $titleObject ),
97  'title' => $this->titleFormatter->getPrefixedText( $titleObject ),
98  'latest' => [
99  'id' => $revision->getId(),
100  'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() )
101  ],
102  'content_model' => $titleObject->getContentModel(),
103  'license' => [
104  'url' => $this->config->get( 'RightsUrl' ),
105  'title' => $this->config->get( 'RightsText' )
106  ],
107  ];
108  }
109 
110  public function needsWriteAccess(): bool {
111  return false;
112  }
113 
114  public function getParamSettings(): array {
115  return [
116  'title' => [
117  self::PARAM_SOURCE => 'path',
118  ParamValidator::PARAM_TYPE => 'string',
119  ParamValidator::PARAM_REQUIRED => true,
120  ],
121  ];
122  }
123 }
MediaWiki\Rest\Handler\LatestPageContentHandler\$permissionManager
PermissionManager $permissionManager
Definition: LatestPageContentHandler.php:23
MediaWiki\Rest\Handler
Definition: CompareHandler.php:3
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:335
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\Rest\Handler\LatestPageContentHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: LatestPageContentHandler.php:114
MediaWiki\Rest\Handler\LatestPageContentHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: LatestPageContentHandler.php:110
Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Definition: RevisionRecord.php:442
Title\getArticleID
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition: Title.php:3179
Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
MediaWiki\Rest\Handler\LatestPageContentHandler\$revisionLookup
RevisionLookup $revisionLookup
Definition: LatestPageContentHandler.php:26
Config
Interface for configuration instances.
Definition: Config.php:28
MediaWiki\Rest\Handler\LatestPageContentHandler
Definition: LatestPageContentHandler.php:17
wfTimestampOrNull
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Definition: GlobalFunctions.php:1888
$title
$title
Definition: testCompression.php:36
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:34
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:188
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:279
MediaWiki\Rest\Handler\LatestPageContentHandler\$user
User $user
Definition: LatestPageContentHandler.php:32
MediaWiki\Rest\Handler\LatestPageContentHandler\getLatestRevision
getLatestRevision()
Definition: LatestPageContentHandler.php:74
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:48
MediaWiki\Rest\Handler\LatestPageContentHandler\$latestRevision
RevisionRecord bool $latestRevision
Definition: LatestPageContentHandler.php:35
MediaWiki\Rest\Handler\LatestPageContentHandler\constructMetadata
constructMetadata(Title $titleObject, RevisionRecord $revision)
Definition: LatestPageContentHandler.php:90
MediaWiki\Rest\Handler\LatestPageContentHandler\$titleFormatter
TitleFormatter $titleFormatter
Definition: LatestPageContentHandler.php:29
Title\getContentModel
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition: Title.php:1065
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
MediaWiki\Rest\Handler\LatestPageContentHandler\isAccessible
isAccessible( $titleObject)
Definition: LatestPageContentHandler.php:86
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Rest\Handler\LatestPageContentHandler\$config
Config $config
Definition: LatestPageContentHandler.php:20
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
MediaWiki\Rest\Handler\LatestPageContentHandler\getTitle
getTitle()
Definition: LatestPageContentHandler.php:64
MediaWiki\Rest\Handler\LatestPageContentHandler\$titleObject
Title bool $titleObject
Definition: LatestPageContentHandler.php:38
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\Handler\LatestPageContentHandler\__construct
__construct(Config $config, PermissionManager $permissionManager, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter)
Definition: LatestPageContentHandler.php:46
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:14