Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
16 / 18 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
PageRevisionTagger | |
88.89% |
16 / 18 |
|
50.00% |
1 / 2 |
7.07 | |
0.00% |
0 / 1 |
getTagsForChange | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getTagsForIds | |
86.67% |
13 / 15 |
|
0.00% |
0 / 1 |
6.09 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Page; |
4 | |
5 | use ProofreadPage\Tags; |
6 | use RecentChange; |
7 | |
8 | class PageRevisionTagger { |
9 | |
10 | /** |
11 | * Get the tags for a given recent change |
12 | * @param RecentChange $rc the recent change |
13 | * @return array array of tags |
14 | */ |
15 | public function getTagsForChange( RecentChange $rc ): array { |
16 | $newId = $rc->getAttribute( 'rc_this_oldid' ); |
17 | $oldId = $rc->getAttribute( 'rc_last_oldid' ); |
18 | |
19 | return $this->getTagsForIds( $oldId, $newId ); |
20 | } |
21 | |
22 | /** |
23 | * Get the tags for a given revision ID. |
24 | * @param int $oldId parent rev ID of the change (0 if new page or don't need |
25 | * to compare with the previous rev) |
26 | * @param int $newId rev ID of the change |
27 | * @return array array of tags |
28 | */ |
29 | public function getTagsForIds( int $oldId, int $newId ): array { |
30 | $tags = []; |
31 | $newPageContent = PageContent::getContentForRevId( $newId ); |
32 | |
33 | // this is not ProofreadPage content |
34 | if ( !( $newPageContent instanceof PageContent ) ) { |
35 | return []; |
36 | } |
37 | |
38 | $newLevel = $newPageContent->getLevel(); |
39 | $newLevelTag = Tags::getTagForPageLevel( $newLevel ); |
40 | |
41 | // couldn't find a suitable new tag for the page |
42 | if ( $newLevelTag === null ) { |
43 | return []; |
44 | } |
45 | |
46 | if ( $oldId !== 0 ) { |
47 | $oldPageContent = PageContent::getContentForRevId( $oldId ); |
48 | |
49 | # if the page has had its content model changed to PRP page |
50 | # or it was already a PRP page, but the status changed |
51 | if ( !( $oldPageContent instanceof PageContent ) |
52 | || !$oldPageContent->getLevel()->equals( $newLevel ) ) { |
53 | $tags[] = $newLevelTag; |
54 | } |
55 | } else { |
56 | // new page - always tag with the initial level |
57 | $tags[] = $newLevelTag; |
58 | } |
59 | return $tags; |
60 | } |
61 | } |