Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
75.00% |
9 / 12 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
TextRevision | |
75.00% |
9 / 12 |
|
57.14% |
4 / 7 |
9.00 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
throwExceptionIfMissingFields | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getContentFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getContentModel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace FileImporter\Data; |
4 | |
5 | use FileImporter\Exceptions\InvalidArgumentException; |
6 | use MediaWiki\Revision\SlotRecord; |
7 | |
8 | /** |
9 | * This class represents a single revision of text, as recognized by MediaWiki. |
10 | * This data can all be retrieved from the API or the Database and can be used to copy |
11 | * the exact revision onto another site. |
12 | * |
13 | * @license GPL-2.0-or-later |
14 | * @author Addshore |
15 | */ |
16 | class TextRevision { |
17 | |
18 | private const ERROR_TEXT_FIELD_MISSING = 'textFieldMissing'; |
19 | |
20 | private const REQUIRED_FIELDS = [ |
21 | 'minor', |
22 | 'user', |
23 | 'timestamp', |
24 | 'comment', |
25 | 'slots', |
26 | 'title', |
27 | 'tags', |
28 | ]; |
29 | |
30 | private array $fields; |
31 | |
32 | /** |
33 | * @throws InvalidArgumentException if incorrect fields are entered |
34 | */ |
35 | public function __construct( array $fields ) { |
36 | $this->throwExceptionIfMissingFields( $fields ); |
37 | $this->fields = $fields; |
38 | } |
39 | |
40 | private function throwExceptionIfMissingFields( array $fields ): void { |
41 | $diff = array_diff_key( array_flip( self::REQUIRED_FIELDS ), $fields ); |
42 | if ( $diff !== [] ) { |
43 | throw new InvalidArgumentException( |
44 | __CLASS__ . ': Missing ' . key( $diff ) . ' field on construction', |
45 | self::ERROR_TEXT_FIELD_MISSING ); |
46 | } |
47 | } |
48 | |
49 | /** |
50 | * @return mixed|null Null if the field isn't known |
51 | */ |
52 | public function getField( string $name ) { |
53 | return $this->fields[$name] ?? null; |
54 | } |
55 | |
56 | public function getContent(): string { |
57 | // Old, incomplete database entries result in slots with no content but marked as "missing" |
58 | // or "badcontentformat", {@see ApiQueryRevisionsBase::extractAllSlotInfo}. FileImporter's |
59 | // general philosophy is to be ok with missing text, but not with missing files. |
60 | return $this->fields['slots'][SlotRecord::MAIN]['content'] ?? ''; |
61 | } |
62 | |
63 | public function getContentFormat(): string { |
64 | // We know old, incomplete database entries can't be anything but wikitext |
65 | return $this->fields['slots'][SlotRecord::MAIN]['contentformat'] ?? CONTENT_FORMAT_WIKITEXT; |
66 | } |
67 | |
68 | public function getContentModel(): string { |
69 | // We know old, incomplete database entries can't be anything but wikitext |
70 | return $this->fields['slots'][SlotRecord::MAIN]['contentmodel'] ?? CONTENT_MODEL_WIKITEXT; |
71 | } |
72 | |
73 | /** |
74 | * @internal for debugging only |
75 | */ |
76 | public function getFields(): array { |
77 | return $this->fields; |
78 | } |
79 | |
80 | } |