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

fix: priority always rejected + cleanup form validation #11

Merged
merged 17 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"right-adminresourceloaderarticles":"Administrate ResourceLoader modules",

"resourceloaderarticles-cacheversion": "1",
"resourceloaderarticles-show-list": "Show all pages",
"resourceloaderarticles-add-page": "Add new page",
"resourceloaderarticles-delete-page": "Delete page",
"resourceloaderarticles-show-list": "Show all resources",
"resourceloaderarticles-add-page": "Add new resource",
"resourceloaderarticles-edit-page": "Save resource changes",
"resourceloaderarticles-delete-page": "Delete resource",
"resourceloaderarticles-edit": "Edit",
"resourceloaderarticles-delete": "Delete",
"resourceloaderarticles-page": "Page",
"resourceloaderarticles-page": "Resource Page Name",
"resourceloaderarticles-wiki": "Wiki",
"resourceloaderarticles-type": "Type",
"resourceloaderarticles-priority": "Priority",
Expand All @@ -24,14 +25,12 @@
"resourceloaderarticles-resourcetype-script": "JavaScript",
"resourceloaderarticles-resourcetype-style": "Style Sheet",

"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",
"resourceloaderarticles-error-page-invalid": "The resource page name is invalid.",

"resourceloaderarticles-success-add": "Resource successfully added",
"resourceloaderarticles-success-edit": "Resource successfully edited",
"resourceloaderarticles-success-delete": "Resource successfully deleted",

"resourceloaderarticles-help-page": "Page name of resource (excl. `MediaWiki:Common.[js|css]/`). Valid JS ends with `.js` and valid styling ends with `.css` or `.less`.",
"resourceloaderarticles-help-priority": "Priority for loading order of the resource (higher first), falls back to alphabetic order within a single priority class."
}
208 changes: 78 additions & 130 deletions src/SpecialPage/SpecialResourceLoaderArticles.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ private function addPage() {
$formDescriptor = [
'Page' => [
'label-message' => 'resourceloaderarticles-page',
'help-message' => 'resourceloaderarticles-help-page',
'type' => 'text',
'required' => true,
'filter-callback' => [ $this, 'trimValueCB' ],
'validation-callback' => [ $this, 'validatePageCB' ],
],
'Wiki' => [
'label-message' => 'resourceloaderarticles-wiki',
'type' => 'text',
'required' => true,
'default' => 'all',
'filter-callback' => [ $this, 'trimValueCB' ],
],
'Type' => [
'class' => 'HTMLSelectField',
Expand All @@ -136,6 +140,8 @@ private function addPage() {
'type' => 'int',
'required' => true,
'default' => '0',
'min' => -1000,
'max' => 1000,
],
];

Expand All @@ -151,69 +157,22 @@ private function addPage() {
* @param array $formData
*/
public function addPageCB( $formData ) {
$dbw = wfGetDB( DB_PRIMARY );
$dbw->insert(
'resourceloaderarticles',
[
'rla_page' => $formData[ 'Page' ],
'rla_wiki' => $formData[ 'Wiki' ],
'rla_type' => $formData[ 'Type' ],
'rla_priority' => intval( $formData[ 'Priority' ] )
]
);
$output = $this->getOutput();
$store = true;
if ( empty( $formData[ 'Page' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-page-empty' )->text()
. '</div>'
);
$store = false;
} elseif (
(
!( substr( $formData[ 'Page' ], -4 ) === '.css' || substr( $formData[ 'Page' ], -5 ) === '.less' )
&& $formData[ 'Type' ] === 'style'
)
|| ( substr( $formData[ 'Page' ], -3 ) !== '.js' && $formData[ 'Type' ] === 'script' )
) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-page-invalid' )->text()
. '</div>'
);
$store = false;
}
if ( empty( $formData[ 'Wiki' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-wiki-empty' )->text()
. '</div>'
);
$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_priority' => $formData[ 'Priority' ]
]
);
$output->addWikiTextAsContent(
'<div class="success">'
. $this->msg( 'resourceloaderarticles-success-add' )->text()
. '</div>'
);
}
$output->addWikiTextAsContent(
'<div class="success">'
. $this->msg( 'resourceloaderarticles-success-add' )->text()
. '</div>'
);
}

/**
Expand All @@ -225,21 +184,27 @@ private function editPage( $id ) {
$row = $res->fetchObject();
$formDescriptor = [
'Id' => [
'type' => 'hidden',
'label-message' => 'resourceloaderarticles-id',
'type' => 'int',
'required' => true,
'disabled' => true,
'default' => $row->rla_id,
],
'Page' => [
'label-message' => 'resourceloaderarticles-page',
'help-message' => 'resourceloaderarticles-help-page',
'type' => 'text',
'required' => true,
'default' => $row->rla_page,
'filter-callback' => [ $this, 'trimValueCB' ],
'validation-callback' => [ $this, 'validatePageCB' ],
],
'Wiki' => [
'label-message' => 'resourceloaderarticles-wiki',
'type' => 'text',
'required' => true,
'default' => $row->rla_wiki,
'filter-callback' => [ $this, 'trimValueCB' ],
],
'Type' => [
'class' => 'HTMLSelectField',
Expand All @@ -256,11 +221,13 @@ private function editPage( $id ) {
'type' => 'int',
'required' => true,
'default' => $row->rla_priority,
'min' => -1000,
'max' => 1000,
],
];

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
$htmlForm->setSubmitText( $this->msg( 'resourceloaderarticles-add-page' )->text() );
$htmlForm->setSubmitText( $this->msg( 'resourceloaderarticles-edit-page' )->text() );
$htmlForm->setFormIdentifier( 'editPageCB' );
$htmlForm->setSubmitCallback( [ $this, 'editPageCB' ] );

Expand All @@ -271,72 +238,25 @@ private function editPage( $id ) {
* @param array $formData
*/
public function editPageCB( $formData ) {
$dbw = wfGetDB( DB_PRIMARY );
$dbw->update(
'resourceloaderarticles',
[
'rla_page' => $formData[ 'Page' ],
'rla_wiki' => $formData[ 'Wiki' ],
'rla_type' => $formData[ 'Type' ],
'rla_priority' => intval( $formData[ 'Priority' ] )
],
[
'rla_id' => $formData[ 'Id' ]
]
);
$output = $this->getOutput();
$store = true;
if ( empty( $formData[ 'Page' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-page-empty' )->text()
. '</div>'
);
$store = false;
} elseif (
(
!( substr( $formData[ 'Page' ], -4 ) === '.css' || substr( $formData[ 'Page' ], -5 ) === '.less' )
&& $formData[ 'Type' ] === 'style'
)
|| ( substr( $formData[ 'Page' ], -3 ) !== '.js' && $formData[ 'Type' ] === 'script' )
) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-page-invalid' )->text()
. '</div>'
);
$store = false;
}
if ( empty( $formData[ 'Wiki' ] ) ) {
$output->addWikiTextAsContent(
'<div class="error">'
. $this->msg( 'resourceloaderarticles-error-wiki-empty' )->text()
. '</div>'
);
$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_priority' => $formData[ 'Priority' ]
],
[
'rla_id' => $formData[ 'Id' ]
]
);
$output->addWikiTextAsContent(
'<div class="success">'
. $this->msg( 'resourceloaderarticles-success-edit' )->text()
. '</div>'
);
}
$output->addWikiTextAsContent(
'<div class="success">'
. $this->msg( 'resourceloaderarticles-success-edit' )->text()
. '</div>'
);
}

/**
Expand All @@ -348,13 +268,15 @@ private function deletePage( $id ) {
$row = $res->fetchObject();
$formDescriptor = [
'Id' => [
'type' => 'hidden',
'label-message' => 'resourceloaderarticles-id',
'type' => 'int',
'required' => true,
'disabled' => true,
'default' => $row->rla_id,
],
'Page' => [
'label-message' => 'resourceloaderarticles-page',
'help-message' => 'resourceloaderarticles-help-page',
'type' => 'text',
'required' => true,
'disabled' => true,
Expand Down Expand Up @@ -409,4 +331,30 @@ public function deletePageCB( $formData ) {
);
}

/**
* @param string $value
* @return string
*/
public function trimValueCB( $value ) {
return trim( $value );
}

/**
* @param string $value
* @param array $alldata
* @return bool|string
*/
public function validatePageCB( $value, $alldata ) {
if (
( $alldata[ 'Type' ] === 'style'
&& !( ( strlen( $value ) > 4 && substr( $value, -4 ) === '.css' )
|| ( strlen( $value ) > 5 && substr( $value, -5 ) === '.less' )
)
) || ( $alldata[ 'Type' ] === 'script' && !( strlen( $value ) > 3 && substr( $value, -3 ) === '.js' ) )
) {
return $this->msg( 'resourceloaderarticles-error-page-invalid' )->text();
}
return true;
}

}