|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types=1 ); |
| 4 | + |
| 5 | +namespace MediaWiki\Extension\AGGrid\Rest; |
| 6 | + |
| 7 | +use MediaWiki\Extension\AGGrid\DataSource\DataSource; |
| 8 | +use MediaWiki\Permissions\PermissionManager; |
| 9 | +use MediaWiki\Rest\LocalizedHttpException; |
| 10 | +use MediaWiki\Rest\SimpleHandler; |
| 11 | +use MediaWiki\Title\TitleFactory; |
| 12 | +use MediaWiki\User\UserFactory; |
| 13 | +use Wikimedia\Message\MessageValue; |
| 14 | +use Wikimedia\ParamValidator\ParamValidator; |
| 15 | + |
| 16 | +/** |
| 17 | + * GET /aggrid/v0/grid/{pageid}/{rev}/{index}/rows |
| 18 | + * |
| 19 | + * Serves a stored grid's rows. The {rev} segment pins the cache entry to the |
| 20 | + * page revision (an edit yields a new URL); the stored rows are the current |
| 21 | + * version. |
| 22 | + */ |
| 23 | +class GridRowsHandler extends SimpleHandler { |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly DataSource $dataSource, |
| 27 | + private readonly PermissionManager $permissionManager, |
| 28 | + private readonly TitleFactory $titleFactory, |
| 29 | + private readonly UserFactory $userFactory |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param int $pageid |
| 35 | + * @param int $rev |
| 36 | + * @param int $index |
| 37 | + * @return \MediaWiki\Rest\Response |
| 38 | + */ |
| 39 | + public function run( int $pageid, int $rev, int $index ) { |
| 40 | + $title = $this->titleFactory->newFromID( $pageid ); |
| 41 | + if ( !$title ) { |
| 42 | + throw new LocalizedHttpException( new MessageValue( 'rest-nonexistent-title' ), 404 ); |
| 43 | + } |
| 44 | + if ( !$this->getAuthority()->probablyCan( 'read', $title ) ) { |
| 45 | + throw new LocalizedHttpException( |
| 46 | + new MessageValue( 'rest-permission-denied-title', [ $title->getPrefixedText() ] ), |
| 47 | + 403 |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + $rows = $this->dataSource->getRows( $pageid, $index ); |
| 52 | + if ( $rows === null ) { |
| 53 | + throw new LocalizedHttpException( |
| 54 | + new MessageValue( 'rest-nonexistent-title', [ $title->getPrefixedText() ] ), |
| 55 | + 404 |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + $response = $this->getResponseFactory()->createJson( [ 'rows' => $rows ] ); |
| 60 | + |
| 61 | + // Inline rows are a pure function of (page, revision): cache hard, but only |
| 62 | + // publicly when an anonymous reader could fetch this page. |
| 63 | + $anonCanRead = $this->permissionManager->userCan( |
| 64 | + 'read', $this->userFactory->newAnonymous(), $title |
| 65 | + ); |
| 66 | + $response->setHeader( |
| 67 | + 'Cache-Control', |
| 68 | + $anonCanRead ? 'public, max-age=86400, immutable' : 'private, max-age=0' |
| 69 | + ); |
| 70 | + return $response; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritDoc |
| 75 | + */ |
| 76 | + public function getParamSettings() { |
| 77 | + return [ |
| 78 | + 'pageid' => [ |
| 79 | + self::PARAM_SOURCE => 'path', |
| 80 | + ParamValidator::PARAM_TYPE => 'integer', |
| 81 | + ParamValidator::PARAM_REQUIRED => true, |
| 82 | + ], |
| 83 | + 'rev' => [ |
| 84 | + self::PARAM_SOURCE => 'path', |
| 85 | + ParamValidator::PARAM_TYPE => 'integer', |
| 86 | + ParamValidator::PARAM_REQUIRED => true, |
| 87 | + ], |
| 88 | + 'index' => [ |
| 89 | + self::PARAM_SOURCE => 'path', |
| 90 | + ParamValidator::PARAM_TYPE => 'integer', |
| 91 | + ParamValidator::PARAM_REQUIRED => true, |
| 92 | + ], |
| 93 | + ]; |
| 94 | + } |
| 95 | +} |
0 commit comments