diff --git a/i18n/en.json b/i18n/en.json
index fc7a918..4f8542d 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -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",
@@ -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."
}
diff --git a/src/SpecialPage/SpecialResourceLoaderArticles.php b/src/SpecialPage/SpecialResourceLoaderArticles.php
index baeb486..407fef8 100644
--- a/src/SpecialPage/SpecialResourceLoaderArticles.php
+++ b/src/SpecialPage/SpecialResourceLoaderArticles.php
@@ -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',
@@ -136,6 +140,8 @@ private function addPage() {
'type' => 'int',
'required' => true,
'default' => '0',
+ 'min' => -1000,
+ 'max' => 1000,
],
];
@@ -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(
- '
'
- . $this->msg( 'resourceloaderarticles-error-page-empty' )->text()
- . '
'
- );
- $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(
- ''
- . $this->msg( 'resourceloaderarticles-error-page-invalid' )->text()
- . '
'
- );
- $store = false;
- }
- if ( empty( $formData[ 'Wiki' ] ) ) {
- $output->addWikiTextAsContent(
- ''
- . $this->msg( 'resourceloaderarticles-error-wiki-empty' )->text()
- . '
'
- );
- $store = false;
- }
- if ( empty( $formData[ 'Priority' ] ) ) {
- $output->addWikiTextAsContent(
- ''
- . $this->msg( 'resourceloaderarticles-error-priority-empty' )->text()
- . '
'
- );
- $store = false;
- } elseif ( !is_int( $formData[ 'Priority' ] ) ) {
- $output->addWikiTextAsContent(
- ''
- . $this->msg( 'resourceloaderarticles-error-priority-invalid' )->text()
- . '
'
- );
- $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(
- ''
- . $this->msg( 'resourceloaderarticles-success-add' )->text()
- . '
'
- );
- }
+ $output->addWikiTextAsContent(
+ ''
+ . $this->msg( 'resourceloaderarticles-success-add' )->text()
+ . '
'
+ );
}
/**
@@ -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',
@@ -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' ] );
@@ -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(
- ''
- . $this->msg( 'resourceloaderarticles-error-page-empty' )->text()
- . '
'
- );
- $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(
- ''
- . $this->msg( 'resourceloaderarticles-error-page-invalid' )->text()
- . '
'
- );
- $store = false;
- }
- if ( empty( $formData[ 'Wiki' ] ) ) {
- $output->addWikiTextAsContent(
- ''
- . $this->msg( 'resourceloaderarticles-error-wiki-empty' )->text()
- . '
'
- );
- $store = false;
- }
- if ( empty( $formData[ 'Priority' ] ) ) {
- $output->addWikiTextAsContent(
- ''
- . $this->msg( 'resourceloaderarticles-error-priority-empty' )->text()
- . '
'
- );
- $store = false;
- } elseif ( !is_int( $formData[ 'Priority' ] ) ) {
- $output->addWikiTextAsContent(
- ''
- . $this->msg( 'resourceloaderarticles-error-priority-invalid' )->text()
- . '
'
- );
- $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(
- ''
- . $this->msg( 'resourceloaderarticles-success-edit' )->text()
- . '
'
- );
- }
+ $output->addWikiTextAsContent(
+ ''
+ . $this->msg( 'resourceloaderarticles-success-edit' )->text()
+ . '
'
+ );
}
/**
@@ -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,
@@ -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;
+ }
+
}