Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
8 / 11
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
TextRevision
72.73% covered (warning)
72.73%
8 / 11
57.14% covered (warning)
57.14%
4 / 7
9.30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 throwExceptionIfMissingFields
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContentFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContentModel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFields
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace FileImporter\Data;
4
5use FileImporter\Exceptions\InvalidArgumentException;
6use 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 */
16class 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    /**
31     * @throws InvalidArgumentException if incorrect fields are entered
32     */
33    public function __construct(
34        private readonly array $fields,
35    ) {
36        $this->throwExceptionIfMissingFields( $fields );
37    }
38
39    private function throwExceptionIfMissingFields( array $fields ): void {
40        $diff = array_diff_key( array_flip( self::REQUIRED_FIELDS ), $fields );
41        if ( $diff !== [] ) {
42            throw new InvalidArgumentException(
43                __CLASS__ . ': Missing ' . key( $diff ) . ' field on construction',
44                self::ERROR_TEXT_FIELD_MISSING );
45        }
46    }
47
48    /**
49     * @return mixed|null Null if the field isn't known
50     */
51    public function getField( string $name ) {
52        return $this->fields[$name] ?? null;
53    }
54
55    public function getContent(): string {
56        // Old, incomplete database entries result in slots with no content but marked as "missing"
57        // or "badcontentformat", {@see ApiQueryRevisionsBase::extractAllSlotInfo}. FileImporter's
58        // general philosophy is to be ok with missing text, but not with missing files.
59        return $this->fields['slots'][SlotRecord::MAIN]['content'] ?? '';
60    }
61
62    public function getContentFormat(): string {
63        // We know old, incomplete database entries can't be anything but wikitext
64        return $this->fields['slots'][SlotRecord::MAIN]['contentformat'] ?? CONTENT_FORMAT_WIKITEXT;
65    }
66
67    public function getContentModel(): string {
68        // We know old, incomplete database entries can't be anything but wikitext
69        return $this->fields['slots'][SlotRecord::MAIN]['contentmodel'] ?? CONTENT_MODEL_WIKITEXT;
70    }
71
72    /**
73     * @internal for debugging only
74     */
75    public function getFields(): array {
76        return $this->fields;
77    }
78
79}