MediaWiki  master
LatestPageContentHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest\Handler;
4 
5 use Config;
10 use RequestContext;
11 use Title;
12 use TitleFormatter;
13 use User;
15 
16 abstract class LatestPageContentHandler extends SimpleHandler {
17 
19  protected $config;
20 
22  protected $permissionManager;
23 
25  protected $revisionLookup;
26 
28  protected $titleFormatter;
29 
31  protected $user;
32 
34  private $latestRevision;
35 
37  private $titleObject;
38 
45  public function __construct(
50  ) {
51  $this->config = $config;
52  $this->permissionManager = $permissionManager;
53  $this->revisionLookup = $revisionLookup;
54  $this->titleFormatter = $titleFormatter;
55 
56  // @todo Inject this, when there is a good way to do that
57  $this->user = RequestContext::getMain()->getUser();
58  }
59 
63  protected function getTitle() {
64  if ( $this->titleObject === null ) {
65  $this->titleObject = Title::newFromText( $this->getValidatedParams()['title'] ) ?? false;
66  }
67  return $this->titleObject;
68  }
69 
73  protected function getLatestRevision() {
74  if ( $this->latestRevision === null ) {
75  $title = $this->getTitle();
76  if ( $title && $title->getArticleID() ) {
77  $this->latestRevision = $this->revisionLookup->getKnownCurrentRevision( $title );
78  } else {
79  $this->latestRevision = false;
80  }
81  }
82  return $this->latestRevision;
83  }
84 
85  protected function isAccessible( $titleObject ): bool {
86  return $this->permissionManager->userCan( 'read', $this->user, $titleObject );
87  }
88 
89  protected function constructMetadata(
91  RevisionRecord $revision
92  ): array {
93  return [
94  'id' => $titleObject->getArticleID(),
95  'key' => $this->titleFormatter->getPrefixedDBkey( $titleObject ),
96  'title' => $this->titleFormatter->getPrefixedText( $titleObject ),
97  'latest' => [
98  'id' => $revision->getId(),
99  'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() )
100  ],
101  'content_model' => $titleObject->getContentModel(),
102  'license' => [
103  'url' => $this->config->get( 'RightsUrl' ),
104  'title' => $this->config->get( 'RightsText' )
105  ],
106  ];
107  }
108 
109  public function needsWriteAccess(): bool {
110  return false;
111  }
112 
113  public function getParamSettings(): array {
114  return [
115  'title' => [
116  self::PARAM_SOURCE => 'path',
117  ParamValidator::PARAM_TYPE => 'string',
118  ParamValidator::PARAM_REQUIRED => true,
119  ],
120  ];
121  }
122 }
MediaWiki\Rest\Handler\LatestPageContentHandler\$permissionManager
PermissionManager $permissionManager
Definition: LatestPageContentHandler.php:22
MediaWiki\Rest\Handler
Definition: ActionModuleBasedHandler.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:332
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\Rest\Handler\LatestPageContentHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: LatestPageContentHandler.php:113
MediaWiki\Rest\Handler\LatestPageContentHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: LatestPageContentHandler.php:109
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:3171
Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
MediaWiki\Rest\Handler\LatestPageContentHandler\$revisionLookup
RevisionLookup $revisionLookup
Definition: LatestPageContentHandler.php:25
Config
Interface for configuration instances.
Definition: Config.php:28
MediaWiki\Rest\Handler\LatestPageContentHandler
Definition: LatestPageContentHandler.php:16
wfTimestampOrNull
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Definition: GlobalFunctions.php:1822
$title
$title
Definition: testCompression.php:38
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:189
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:279
MediaWiki\Rest\Handler\LatestPageContentHandler\$user
User $user
Definition: LatestPageContentHandler.php:31
MediaWiki\Rest\Handler\LatestPageContentHandler\getLatestRevision
getLatestRevision()
Definition: LatestPageContentHandler.php:73
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:34
MediaWiki\Rest\Handler\LatestPageContentHandler\constructMetadata
constructMetadata(Title $titleObject, RevisionRecord $revision)
Definition: LatestPageContentHandler.php:89
MediaWiki\Rest\Handler\LatestPageContentHandler\$titleFormatter
TitleFormatter $titleFormatter
Definition: LatestPageContentHandler.php:28
Title\getContentModel
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition: Title.php:1046
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:85
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Rest\Handler\LatestPageContentHandler\$config
Config $config
Definition: LatestPageContentHandler.php:19
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
MediaWiki\Rest\Handler\LatestPageContentHandler\getTitle
getTitle()
Definition: LatestPageContentHandler.php:63
MediaWiki\Rest\Handler\LatestPageContentHandler\$titleObject
Title bool $titleObject
Definition: LatestPageContentHandler.php:37
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:54
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:45
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:14