Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
FileDescriptionPageValidator
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validateTemplates
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 validateCategories
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 hasRequiredTemplate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace FileImporter\Services;
4
5use FileImporter\Data\WikitextConversions;
6use FileImporter\Exceptions\CommunityPolicyException;
7
8/**
9 * This class checks a file description page for required and forbidden categories and templates. It
10 * does not have any knowledge about the wikitext syntax.
11 *
12 * @license GPL-2.0-or-later
13 */
14class FileDescriptionPageValidator {
15
16    private WikitextConversions $wikitextConversions;
17
18    public function __construct( WikitextConversions $conversions ) {
19        $this->wikitextConversions = $conversions;
20    }
21
22    /**
23     * @param string[] $templates List of case-insensitive page names without namespace prefix
24     *
25     * @throws CommunityPolicyException
26     */
27    public function validateTemplates( array $templates ) {
28        foreach ( $templates as $template ) {
29            if ( $this->wikitextConversions->isTemplateBad( $template ) ) {
30                throw new CommunityPolicyException( [
31                    'fileimporter-file-contains-blocked-category-template',
32                    $template
33                ] );
34            }
35        }
36    }
37
38    /**
39     * @param string[] $categories List of case-insensitive page names without namespace prefix
40     *
41     * @throws CommunityPolicyException
42     */
43    public function validateCategories( array $categories ): void {
44        foreach ( $categories as $category ) {
45            if ( $this->wikitextConversions->isCategoryBad( $category ) ) {
46                throw new CommunityPolicyException( [
47                    'fileimporter-file-contains-blocked-category-template',
48                    $category
49                ] );
50            }
51        }
52    }
53
54    /**
55     * @param string[] $templates List of case-insensitive page names without namespace prefix
56     *
57     * @throws CommunityPolicyException
58     */
59    public function hasRequiredTemplate( array $templates ) {
60        if ( !$this->wikitextConversions->hasGoodTemplates() ) {
61            return;
62        }
63
64        foreach ( $templates as $template ) {
65            if ( $this->wikitextConversions->isTemplateGood( $template ) ) {
66                return;
67            }
68        }
69
70        throw new CommunityPolicyException( [ 'fileimporter-file-missing-required-template' ] );
71    }
72
73}