From c042f244703090b3e0af81fb7aedfa7c7466a3d1 Mon Sep 17 00:00:00 2001 From: alex4401 Date: Mon, 19 Feb 2024 07:23:14 +0100 Subject: [PATCH] feat: check most required files Tiles and overlays are still pending. Issue: #246 --- .../RequiredFilesConstraint.php | 91 +++++++++++++++++++ includes/Content/MapDataConstraintChecker.php | 2 + 2 files changed, 93 insertions(+) create mode 100644 includes/Content/DataConstraints/RequiredFilesConstraint.php diff --git a/includes/Content/DataConstraints/RequiredFilesConstraint.php b/includes/Content/DataConstraints/RequiredFilesConstraint.php new file mode 100644 index 00000000..bb14cd70 --- /dev/null +++ b/includes/Content/DataConstraints/RequiredFilesConstraint.php @@ -0,0 +1,91 @@ +groups ) ) { + foreach ( (array)$data->groups as $groupId => $group ) { + if ( isset( $group->icon ) && !$this->checkFile( $group->icon ) ) { + $status->error( self::MESSAGE, "/groups/$groupId/icon" ); + $result = false; + } + } + } + + if ( isset( $data->categories ) ) { + foreach ( (array)$data->categories as $categoryId => $category ) { + if ( isset( $category->overrideIcon ) && !$this->checkFile( $category->overrideIcon ) ) { + $status->error( self::MESSAGE, "/categories/$categoryId/overrideIcon" ); + $result = false; + } + } + } + + if ( isset( $data->background ) ) { + if ( is_string( $data->background ) ) { + if ( !$this->checkFile( $data->background ) ) { + $status->error( self::MESSAGE, '/background' ); + $result = false; + } + } else { + $result = $result && $this->checkBackground( $status, $version, $data->background, '/background' ); + } + } + + if ( isset( $data->backgrounds ) ) { + foreach ( (array)$data->backgrounds as $index => $background ) { + $result = $result && $this->checkBackground( $status, $version, $background, "/backgrounds/$index" ); + } + } + + if ( isset( $data->markers ) ) { + foreach ( (array)$data->markers as $assocStr => $markers ) { + foreach ( $markers as $index => $marker ) { + if ( isset( $marker->icon ) && !$this->checkFile( $marker->icon ) ) { + $status->error( self::MESSAGE, "/markers/$assocStr/$index/icon" ); + $result = false; + } + + if ( isset( $marker->image ) && !$this->checkFile( $marker->image ) ) { + $status->error( self::MESSAGE, "/markers/$assocStr/$index/image" ); + $result = false; + } + } + } + } + + return $result; + } + + private function checkFile( $fileName ): bool { + $file = DataMapFileUtils::getFile( $fileName ); + return $file && $file->exists(); + } + + private function checkBackground( Status $status, MapVersionInfo $version, stdClass $data, string $ptr ): bool { + $result = true; + + if ( isset( $data->image ) && !$this->checkFile( $data->image ) ) { + $status->error( self::MESSAGE, "$ptr/image" ); + $result = false; + } + + // TODO: tiles + // TODO: overlays + + return $result; + } +} diff --git a/includes/Content/MapDataConstraintChecker.php b/includes/Content/MapDataConstraintChecker.php index 739606ca..c5919771 100644 --- a/includes/Content/MapDataConstraintChecker.php +++ b/includes/Content/MapDataConstraintChecker.php @@ -8,6 +8,7 @@ use MediaWiki\Extension\DataMaps\Content\DataConstraints\DataConstraint; use MediaWiki\Extension\DataMaps\Content\DataConstraints\LayerIdNoOverlapConstraint; use MediaWiki\Extension\DataMaps\Content\DataConstraints\MarkerUidNoOverlapConstraint; +use MediaWiki\Extension\DataMaps\Content\DataConstraints\RequiredFilesConstraint; use MediaWiki\MainConfigNames; use MediaWiki\Utils\UrlUtils; use stdClass; @@ -40,6 +41,7 @@ private function getConstraints(): array { new AssociationStringGroupExistsConstraint(), new LayerIdNoOverlapConstraint(), new MarkerUidNoOverlapConstraint(), + new RequiredFilesConstraint(), ]; }