Skip to content

Commit 6c43ce4

Browse files
alistair3149claude
andauthored
feat: fetch grid row data on demand via a cacheable REST API (#7)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c1f587d commit 6c43ce4

30 files changed

Lines changed: 1120 additions & 101 deletions

‎extension.json‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,58 @@
2626
"includes/ServiceWiring.php"
2727
],
2828
"Hooks": {
29-
"ScribuntoExternalLibraries": "scribunto"
29+
"ScribuntoExternalLibraries": "scribunto",
30+
"LoadExtensionSchemaUpdates": "schema",
31+
"LinksUpdateComplete": "linksupdate",
32+
"PageDeleteComplete": "pagedelete"
3033
},
3134
"HookHandlers": {
3235
"scribunto": {
33-
"class": "MediaWiki\\Extension\\AGGrid\\Hooks"
36+
"class": "MediaWiki\\Extension\\AGGrid\\Hooks\\ScribuntoHandler"
37+
},
38+
"schema": {
39+
"class": "MediaWiki\\Extension\\AGGrid\\Hooks\\SchemaHandler"
40+
},
41+
"linksupdate": {
42+
"class": "MediaWiki\\Extension\\AGGrid\\Hooks\\LinksUpdateHandler",
43+
"services": [ "AGGrid.InlineDataStore" ]
44+
},
45+
"pagedelete": {
46+
"class": "MediaWiki\\Extension\\AGGrid\\Hooks\\PageDeleteHandler",
47+
"services": [ "AGGrid.InlineDataStore" ]
3448
}
3549
},
50+
"RestRoutes": [
51+
{
52+
"path": "/aggrid/v0/grid/{pageid}/{rev}/{index}/rows",
53+
"method": "GET",
54+
"class": "MediaWiki\\Extension\\AGGrid\\Rest\\GridRowsHandler",
55+
"services": [
56+
"AGGrid.InlineDataStore",
57+
"PermissionManager",
58+
"TitleFactory",
59+
"UserFactory"
60+
]
61+
}
62+
],
3663
"ResourceModules": {
64+
"ext.aggrid.styles": {
65+
"styles": [
66+
"ext.aggrid/ext.aggrid.less"
67+
]
68+
},
3769
"ext.aggrid": {
3870
"packageFiles": [
3971
"ext.aggrid/init.js",
4072
"ext.aggrid/mountGrid.js",
4173
"ext.aggrid/theme.js",
4274
"ext.aggrid/lazyMount.js"
4375
],
44-
"styles": [
45-
"ext.aggrid/ext.aggrid.less"
76+
"dependencies": [
77+
"mediawiki.api"
78+
],
79+
"messages": [
80+
"aggrid-error-load"
4681
]
4782
}
4883
},

‎i18n/en.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"alistair3149"
55
]
66
},
7-
"aggrid-desc": "Renders AG Grid data grids"
7+
"aggrid-desc": "Renders AG Grid data grids",
8+
"aggrid-error-load": "Could not load the grid data."
89
}

‎i18n/qqq.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"alistair3149"
55
]
66
},
7-
"aggrid-desc": "{{desc|name=AGGrid|url=https://www.mediawiki.org/wiki/Extension:AGGrid}}"
7+
"aggrid-desc": "{{desc|name=AGGrid|url=https://www.mediawiki.org/wiki/Extension:AGGrid}}",
8+
"aggrid-error-load": "Error message shown inside an AG Grid (as its overlay) when the grid's row data fails to load from the server."
89
}

‎includes/DataSource/DataSource.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace MediaWiki\Extension\AGGrid\DataSource;
6+
7+
/**
8+
* A source of grid rows for a stored or queryable grid handle. The inline source
9+
* is the only implementation today; backend sources (Bucket/Cargo/SMW) will
10+
* implement the same contract.
11+
*/
12+
interface DataSource {
13+
14+
/**
15+
* @param int $pageId
16+
* @param int $gridIndex
17+
* @return array|null Decoded rows, or null if the handle is unknown.
18+
*/
19+
public function getRows( int $pageId, int $gridIndex ): ?array;
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace MediaWiki\Extension\AGGrid\Hooks;
6+
7+
use MediaWiki\Extension\AGGrid\Service\GridDataStore;
8+
use MediaWiki\Extension\AGGrid\Service\GridRenderer;
9+
use MediaWiki\Hook\LinksUpdateCompleteHook;
10+
11+
/**
12+
* Flushes grids queued during the parse into the aggrid_data table.
13+
*/
14+
final class LinksUpdateHandler implements LinksUpdateCompleteHook {
15+
16+
public function __construct(
17+
private readonly GridDataStore $store
18+
) {
19+
}
20+
21+
/**
22+
* @inheritDoc
23+
*/
24+
public function onLinksUpdateComplete( $linksUpdate, $ticket ) {
25+
$pageId = $linksUpdate->getPageId();
26+
if ( !$pageId ) {
27+
return;
28+
}
29+
30+
// Grids are stored one-per-key (GridRenderer::EXT_DATA_KEY . $index),
31+
// contiguous from 0; probe until the first gap.
32+
$parserOutput = $linksUpdate->getParserOutput();
33+
$grids = [];
34+
for ( $index = 0; ; $index++ ) {
35+
$grid = $parserOutput->getExtensionData( GridRenderer::EXT_DATA_KEY . $index );
36+
if ( $grid === null ) {
37+
break;
38+
}
39+
$grids[$index] = $grid;
40+
}
41+
42+
// Always call (even with no grids) so removing a grid clears stale rows.
43+
$this->store->replaceForPage( $pageId, $grids );
44+
}
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace MediaWiki\Extension\AGGrid\Hooks;
6+
7+
use MediaWiki\Extension\AGGrid\Service\GridDataStore;
8+
use MediaWiki\Page\Hook\PageDeleteCompleteHook;
9+
10+
/**
11+
* Drops a page's stored grid rows when the page is deleted.
12+
*/
13+
final class PageDeleteHandler implements PageDeleteCompleteHook {
14+
15+
public function __construct(
16+
private readonly GridDataStore $store
17+
) {
18+
}
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function onPageDeleteComplete(
24+
$page, $deleter, $reason, $pageID, $deletedRev, $logEntry, $archivedRevisionCount
25+
) {
26+
$this->store->deleteForPage( $pageID );
27+
}
28+
}

‎includes/Hooks/SchemaHandler.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace MediaWiki\Extension\AGGrid\Hooks;
6+
7+
use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook;
8+
9+
/**
10+
* Installs the aggrid_data table. Runs in the installer context, so it must not
11+
* depend on services.
12+
*/
13+
final class SchemaHandler implements LoadExtensionSchemaUpdatesHook {
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
public function onLoadExtensionSchemaUpdates( $updater ) {
19+
$type = $updater->getDB()->getType();
20+
$updater->addExtensionTable(
21+
'aggrid_data',
22+
dirname( __DIR__, 2 ) . "/sql/$type/tables-generated.sql"
23+
);
24+
}
25+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
declare( strict_types=1 );
44

5-
namespace MediaWiki\Extension\AGGrid;
5+
namespace MediaWiki\Extension\AGGrid\Hooks;
66

77
use MediaWiki\Extension\AGGrid\Scribunto\LuaLibrary;
88
use MediaWiki\Extension\Scribunto\Hooks\ScribuntoExternalLibrariesHook;
99

10-
final class Hooks implements ScribuntoExternalLibrariesHook {
10+
/**
11+
* Registers the mw.ext.aggrid Lua library with Scribunto.
12+
*/
13+
final class ScribuntoHandler implements ScribuntoExternalLibrariesHook {
1114

1215
/**
1316
* @inheritDoc

‎includes/LuaSequence.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace MediaWiki\Extension\AGGrid;
6+
7+
/**
8+
* Converts Lua's 1-indexed tables into 0-indexed PHP lists so json_encode emits
9+
* JSON arrays (which AG Grid requires for columnDefs/rowData), not JSON objects.
10+
*/
11+
final class LuaSequence {
12+
13+
/**
14+
* Re-index any array whose keys are exactly 1..n to a 0-based list; recurse
15+
* into nested values. Associative arrays are left untouched (objects).
16+
*
17+
* @param mixed $value
18+
* @return mixed
19+
*/
20+
public static function normalize( $value ) {
21+
if ( !is_array( $value ) ) {
22+
return $value;
23+
}
24+
25+
$normalized = [];
26+
foreach ( $value as $key => $item ) {
27+
$normalized[$key] = self::normalize( $item );
28+
}
29+
30+
if ( array_keys( $normalized ) === range( 1, count( $normalized ) ) ) {
31+
return array_values( $normalized );
32+
}
33+
34+
return $normalized;
35+
}
36+
}

‎includes/Rest/GridRowsHandler.php‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)