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 TitleFactory;
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 
41  private $titleFactory;
42 
50  public function __construct(
56  ) {
57  $this->config = $config;
58  $this->permissionManager = $permissionManager;
59  $this->revisionLookup = $revisionLookup;
60  $this->titleFormatter = $titleFormatter;
61 
62  // @todo Inject this, when there is a good way to do that
63  $this->user = RequestContext::getMain()->getUser();
64  $this->titleFactory = $titleFactory;
65  }
66 
70  protected function getTitle() {
71  if ( $this->titleObject === null ) {
72  $this->titleObject =
73  $this->titleFactory->newFromText( $this->getValidatedParams()['title'] ) ?? false;
74  }
75  return $this->titleObject;
76  }
77 
81  protected function getLatestRevision() {
82  if ( $this->latestRevision === null ) {
83  $title = $this->getTitle();
84  if ( $title && $title->getArticleID() ) {
85  $this->latestRevision = $this->revisionLookup->getKnownCurrentRevision( $title );
86  } else {
87  $this->latestRevision = false;
88  }
89  }
90  return $this->latestRevision;
91  }
92 
93  protected function isAccessible( $titleObject ): bool {
94  return $this->permissionManager->userCan( 'read', $this->user, $titleObject );
95  }
96 
97  protected function constructMetadata(
99  RevisionRecord $revision
100  ): array {
101  return [
102  'id' => $titleObject->getArticleID(),
103  'key' => $this->titleFormatter->getPrefixedDBkey( $titleObject ),
104  'title' => $this->titleFormatter->getPrefixedText( $titleObject ),
105  'latest' => [
106  'id' => $revision->getId(),
107  'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() )
108  ],
109  'content_model' => $titleObject->getContentModel(),
110  'license' => [
111  'url' => $this->config->get( 'RightsUrl' ),
112  'title' => $this->config->get( 'RightsText' )
113  ],
114  ];
115  }
116 
117  public function needsWriteAccess(): bool {
118  return false;
119  }
120 
121  public function getParamSettings(): array {
122  return [
123  'title' => [
124  self::PARAM_SOURCE => 'path',
125  ParamValidator::PARAM_TYPE => 'string',
126  ParamValidator::PARAM_REQUIRED => true,
127  ],
128  ];
129  }
130 }
MediaWiki\Rest\Handler\LatestPageContentHandler\$permissionManager
PermissionManager $permissionManager
Definition: LatestPageContentHandler.php:23
MediaWiki\Rest\Handler
Definition: AbstractContributionHandler.php:3
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\Rest\Handler\LatestPageContentHandler\$titleFactory
TitleFactory $titleFactory
Definition: LatestPageContentHandler.php:41
MediaWiki\Rest\Handler\LatestPageContentHandler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: LatestPageContentHandler.php:121
MediaWiki\Rest\Handler\LatestPageContentHandler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: LatestPageContentHandler.php:117
Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Definition: RevisionRecord.php:434
Title\getArticleID
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition: Title.php:3118
MediaWiki\Rest\Handler\LatestPageContentHandler\__construct
__construct(Config $config, PermissionManager $permissionManager, RevisionLookup $revisionLookup, TitleFormatter $titleFormatter, TitleFactory $titleFactory)
Definition: LatestPageContentHandler.php:50
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:30
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:1831
$title
$title
Definition: testCompression.php:38
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:39
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:257
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:271
MediaWiki\Rest\Handler\LatestPageContentHandler\$user
User $user
Definition: LatestPageContentHandler.php:32
MediaWiki\Rest\Handler\LatestPageContentHandler\getLatestRevision
getLatestRevision()
Definition: LatestPageContentHandler.php:81
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:50
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:97
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:1046
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:454
MediaWiki\Rest\Handler\LatestPageContentHandler\isAccessible
isAccessible( $titleObject)
Definition: LatestPageContentHandler.php:93
Title
Represents a title within MediaWiki.
Definition: Title.php:41
MediaWiki\Rest\Handler\LatestPageContentHandler\$config
Config $config
Definition: LatestPageContentHandler.php:20
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:33
MediaWiki\Rest\Handler\LatestPageContentHandler\getTitle
getTitle()
Definition: LatestPageContentHandler.php:70
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:56
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\SimpleHandler
Definition: SimpleHandler.php:15