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