Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add priority field to loaded resources for fine-grained load ordering #9

Merged
merged 12 commits into from
Apr 16, 2024
6 changes: 5 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
"resourceloaderarticles-page": "Page",
"resourceloaderarticles-wiki": "Wiki",
"resourceloaderarticles-type": "Type",
"resourceloaderarticles-priority": "Priority",
"resourceloaderarticles-id": "ID",

"resourceloaderarticles-error-page-empty": "The page parameter cannot be empty!",
"resourceloaderarticles-error-page-invalid": "The page name is invalid!",
"resourceloaderarticles-error-priority-empty": "The priority parameter cannot be empty!",
"resourceloaderarticles-error-priority-invalid": "The priority is invalid - must be an integer!",
"resourceloaderarticles-error-wiki-empty": "The wiki parameter cannot be empty!",
"resourceloaderarticles-success-add": "Page successfully added",
"resourceloaderarticles-success-edit": "Page successfully edited",
"resourceloaderarticles-success-delete": "Page successfully deleted"
}
}
6 changes: 4 additions & 2 deletions sql/resourceloaderarticles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ CREATE TABLE IF NOT EXISTS /*_*/resourceloaderarticles (
`rla_id` int(11) NOT NULL,
`rla_wiki` VARBINARY(255) NOT NULL,
`rla_page` VARBINARY(255) NOT NULL,
`rla_type` VARBINARY(255) NOT NULL
`rla_type` VARBINARY(255) NOT NULL,
`rla_priority` int(11) NOT NULL
) /*$wgDBTableOptions*/;

ALTER TABLE /*_*/resourceloaderarticles
ADD PRIMARY KEY `rla_id` (`rla_id`),
ADD KEY `rla_type` (`rla_type`),
ADD KEY `rla_wiki` (`rla_wiki`);
ADD KEY `rla_wiki` (`rla_wiki`),
ADD KEY `rla_priority` (`rla_priority`);

ALTER TABLE /*_*/resourceloaderarticles
MODIFY `rla_id` int(11) NOT NULL AUTO_INCREMENT;
2 changes: 2 additions & 0 deletions sql/resourceloaderarticlesPriorityMigration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE resourceloaderarticles
iMarbot marked this conversation as resolved.
Show resolved Hide resolved
ADD COLUMN IF NOT EXISTS `rla_priority` int(11) NOT NULL;
iMarbot marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/Hooks/MainHookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function onBeforePageDisplay( $out, $skin ): void {
'*',
[ '`rla_wiki` IN(\'' . $scriptPath . '\', \'all\')' ],
__METHOD__,
[ 'ORDER BY' => 'rla_type ASC, rla_page ASC, rla_wiki ASC' ]
[ 'ORDER BY' => 'rla_priority DESC, rla_type ASC, rla_page ASC, rla_wiki ASC' ]
);
foreach ( $res as $row ) {
if ( $row->rla_type === 'script' ) {
Expand Down
4 changes: 4 additions & 0 deletions src/Hooks/SchemaHookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function onLoadExtensionSchemaUpdates( $updater ) {
$updater->output( "Creating resourceloaderarticles table resourceloaderarticles ...\n" );
$db->sourceFile( __DIR__ . '/../sql/resourceloaderarticles.sql' );
$updater->output( "done.\n" );
} elseif ( !$db->fieldExists( 'resourceloaderarticles', 'rla_priority', __METHOD__ ) ) {
$updater->output( "Adding `rla_priority` field to resourceloaderarticles table resourceloaderarticles ...\n" );
$db->sourceFile( __DIR__ . '/../sql/resourceloaderarticlesPriorityMigration.sql' );
$updater->output( "done.\n" );
} else {
$updater->output( "...resourceloaderarticles table already exists.\n" );
}
Expand Down
66 changes: 62 additions & 4 deletions src/SpecialPage/SpecialResourceLoaderArticles.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ private function listPages() {
'*',
[],
__METHOD__,
[ 'ORDER BY' => 'rla_type ASC, rla_page ASC, rla_wiki ASC' ]
[ 'ORDER BY' => 'rla_priority DESC, rla_type ASC, rla_page ASC, rla_wiki ASC' ]
iMarbot marked this conversation as resolved.
Show resolved Hide resolved
);
$output->addHTML( '<div class="table-responsive"><table class="wikitable">' );
$output->addHTML(
'<tr><th>#</th><th>' . $this->msg( 'resourceloaderarticles-page' )->text()
'<tr><th>' . $this->msg( 'resourceloaderarticles-id' )->text()
. '</th><th>' . $this->msg( 'resourceloaderarticles-priority' )->text()
. '</th><th>' . $this->msg( 'resourceloaderarticles-page' )->text()
. '</th><th>' . $this->msg( 'resourceloaderarticles-wiki' )->text()
. '</th><th>' . $this->msg( 'resourceloaderarticles-type' )->text()
. '</th><th></th></tr>'
Expand All @@ -81,6 +83,7 @@ private function listPages() {
$editTitle = Title::newFromText( 'ResourceLoaderArticles/edit/' . $row->rla_id, NS_SPECIAL );
$output->addHTML(
'<tr><td>' . $row->rla_id
. '</td><td>' . $row->rla_priority
. '</td><td>' . $row->rla_page
. '</td><td>' . $row->rla_wiki
. '</td><td>' . $row->rla_type
Expand Down Expand Up @@ -118,6 +121,12 @@ private function addPage() {
'CSS' => 'style',
],
],
'Priority' => [
'label-message' => 'resourceloaderarticles-priority',
'type' => 'number',
'required' => true,
'default' => '0',
],
];

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
Expand Down Expand Up @@ -163,14 +172,30 @@ public function addPageCB( $formData ) {
);
$store = false;
}
if ( empty( $formData[ 'Priority' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-priority-empty' )->text()
. '</div>'
);
$store = false;
} elseif ( !is_int( $formData[ 'Priority' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-priority-invalid' )->text()
. '</div>'
);
$store = false;
}
if ( $store ) {
$dbw = wfGetDB( DB_PRIMARY );
$dbw->insert(
'resourceloaderarticles',
[
'rla_page' => $formData[ 'Page' ],
'rla_wiki' => $formData[ 'Wiki' ],
'rla_type' => $formData[ 'Type' ]
'rla_type' => $formData[ 'Type' ],
'rla_priority' => $formData[ 'Priority' ]
]
);
$output->addWikiTextAsContent(
Expand Down Expand Up @@ -215,6 +240,12 @@ private function editPage( $id ) {
],
'default' => $row->rla_type,
],
'Priority' => [
'label-message' => 'resourceloaderarticles-priority',
'type' => 'number',
'required' => true,
'default' => $row->rla_priority,
],
];

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
Expand Down Expand Up @@ -260,14 +291,30 @@ public function editPageCB( $formData ) {
);
$store = false;
}
if ( empty( $formData[ 'Priority' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-priority-empty' )->text()
. '</div>'
);
$store = false;
} elseif ( !is_int( $formData[ 'Priority' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-priority-invalid' )->text()
. '</div>'
);
$store = false;
}
if ( $store ) {
$dbw = wfGetDB( DB_PRIMARY );
$dbw->update(
'resourceloaderarticles',
[
'rla_page' => $formData[ 'Page' ],
'rla_wiki' => $formData[ 'Wiki' ],
'rla_type' => $formData[ 'Type' ]
'rla_type' => $formData[ 'Type' ],
'rla_priority' => $formData[ 'Priority' ]
],
[
'rla_id' => $formData[ 'Id' ]
Expand All @@ -292,18 +339,21 @@ private function deletePage( $id ) {
'Id' => [
'type' => 'hidden',
'required' => true,
'disabled' => true,
'default' => $row->rla_id,
],
'Page' => [
'label-message' => 'resourceloaderarticles-page',
'type' => 'text',
'required' => true,
'disabled' => true,
'default' => $row->rla_page,
],
'Wiki' => [
'label-message' => 'resourceloaderarticles-wiki',
'type' => 'text',
'required' => true,
'disabled' => true,
'default' => $row->rla_wiki,
],
'Type' => [
Expand All @@ -313,8 +363,16 @@ private function deletePage( $id ) {
'JavaScript' => 'script',
'CSS' => 'style',
],
'disabled' => true,
'default' => $row->rla_type,
],
'Priority' => [
'label-message' => 'resourceloaderarticles-priority',
'type' => 'number',
'required' => true,
'disabled' => true,
'default' => $row->rla_priority,
],
];

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
Expand Down