Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportDetails
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
15 / 15
17
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
 setPageLanguage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTemplates
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCategories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceLinkTarget
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceFileExtension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceFileName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageLanguage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getImageDisplayUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTextRevisions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileRevisions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTemplates
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOriginalHash
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace FileImporter\Data;
4
5use MediaWiki\Linker\LinkTarget;
6
7/**
8 * Contains the details from the source site for the import.
9 *
10 * @license GPL-2.0-or-later
11 * @author Addshore
12 */
13class ImportDetails {
14
15    /** @var string|null */
16    private $pageLanguage;
17    /** @var string[] */
18    private array $templates = [];
19    /** @var string[] */
20    private array $categories = [];
21
22    public function __construct(
23        private readonly SourceUrl $sourceUrl,
24        private readonly LinkTarget $sourceLinkTarget,
25        private readonly TextRevisions $textRevisions,
26        private readonly FileRevisions $fileRevisions,
27    ) {
28    }
29
30    public function setPageLanguage( ?string $languageCode ): void {
31        $this->pageLanguage = $languageCode;
32    }
33
34    /**
35     * @param string[] $templates
36     */
37    public function setTemplates( array $templates ): void {
38        $this->templates = $templates;
39    }
40
41    /**
42     * @param string[] $categories
43     */
44    public function setCategories( array $categories ): void {
45        $this->categories = $categories;
46    }
47
48    public function getSourceLinkTarget(): LinkTarget {
49        return $this->sourceLinkTarget;
50    }
51
52    /**
53     * @return string File extension. Example: 'png'
54     */
55    public function getSourceFileExtension(): string {
56        return pathinfo( $this->sourceLinkTarget->getText(), PATHINFO_EXTENSION );
57    }
58
59    /**
60     * @return string Filename with no namespace prefix or file extension. Example: 'Berlin'
61     */
62    public function getSourceFileName(): string {
63        return pathinfo( $this->sourceLinkTarget->getText(), PATHINFO_FILENAME );
64    }
65
66    /**
67     * @return string|null
68     */
69    public function getPageLanguage() {
70        return $this->pageLanguage;
71    }
72
73    public function getImageDisplayUrl(): string {
74        return $this->fileRevisions->getLatest()->getField( 'thumburl' );
75    }
76
77    public function getSourceUrl(): SourceUrl {
78        return $this->sourceUrl;
79    }
80
81    public function getTextRevisions(): TextRevisions {
82        return $this->textRevisions;
83    }
84
85    public function getFileRevisions(): FileRevisions {
86        return $this->fileRevisions;
87    }
88
89    /**
90     * List of templates directly or indirectly transcluded in the latest revision of the file
91     * description page.
92     *
93     * @return string[]
94     */
95    public function getTemplates(): array {
96        return $this->templates;
97    }
98
99    /**
100     * List of categories present in the latest revision of the file description page.
101     *
102     * @return string[]
103     */
104    public function getCategories(): array {
105        return $this->categories;
106    }
107
108    /**
109     * Returns a string hash based on the initial value of the object. The string must not exceed
110     * 255 bytes (255 ASCII characters or less when it contains Unicode characters that
111     * need to be UTF-8 encoded) to allow using indexes on all database systems.
112     *
113     * Can be used to see if two ImportDetails objects appear to be for the same URL and have
114     * the same number of text and file revisions.
115     * Used to detect changes to a file between the start and end of an import.
116     */
117    public function getOriginalHash(): string {
118        $hashes = [
119            sha1( $this->sourceLinkTarget->getText() ),
120            sha1( $this->sourceUrl->getUrl() ),
121            sha1( (string)count( $this->getTextRevisions()->toArray() ) ),
122            sha1( (string)count( $this->getFileRevisions()->toArray() ) ),
123        ];
124
125        foreach ( $this->getTextRevisions()->toArray() as $textRevision ) {
126            $hashes[] = $textRevision->getField( 'sha1' ) ?? '';
127        }
128
129        foreach ( $this->getFileRevisions()->toArray() as $fileRevision ) {
130            $hashes[] = $fileRevision->getField( 'sha1' ) ?? '';
131        }
132
133        return sha1( implode( '|', $hashes ) );
134    }
135
136}