Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| StoryTrackingCategories | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllTextForms | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getTextForm | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| hasDiff | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\Wikistories; |
| 4 | |
| 5 | use MediaWiki\Context\RequestContext; |
| 6 | use MediaWiki\Title\Title; |
| 7 | |
| 8 | class StoryTrackingCategories { |
| 9 | |
| 10 | public const TC_NO_IMAGE = 'wikistories-no-image-category'; |
| 11 | |
| 12 | public const TC_NO_ARTICLE = 'wikistories-no-related-article'; |
| 13 | |
| 14 | public const TC_OUTDATED_TEXT = 'wikistories-outdated-text-category'; |
| 15 | |
| 16 | /** |
| 17 | * @var string[] ALL_TEXT_FORMS |
| 18 | */ |
| 19 | private static $ALL_TEXT_FORMS = []; |
| 20 | |
| 21 | private readonly RequestContext $context; |
| 22 | |
| 23 | public function __construct() { |
| 24 | $this->context = RequestContext::getMain(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * get text form from all the tracking categories in WikiStory |
| 29 | */ |
| 30 | private function getAllTextForms(): array { |
| 31 | if ( self::$ALL_TEXT_FORMS !== [] ) { |
| 32 | return self::$ALL_TEXT_FORMS; |
| 33 | } |
| 34 | |
| 35 | $context = $this->context; |
| 36 | return array_map( static function ( $key ) use ( $context ) { |
| 37 | $textForm = $context->msg( $key )->inContentLanguage()->text(); |
| 38 | self::$ALL_TEXT_FORMS[ $key ] = $textForm; |
| 39 | return $textForm; |
| 40 | }, [ self::TC_NO_IMAGE, self::TC_NO_ARTICLE, self::TC_OUTDATED_TEXT ] ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * get text form from the category page |
| 45 | */ |
| 46 | private function getTextForm( string $key ): string { |
| 47 | if ( !isset( self::$ALL_TEXT_FORMS[ $key ] ) ) { |
| 48 | $this->getAllTextForms(); |
| 49 | } |
| 50 | |
| 51 | return self::$ALL_TEXT_FORMS[ $key ]; |
| 52 | } |
| 53 | |
| 54 | public function hasDiff( array $categories, Title $title ): bool { |
| 55 | // find the existing wiki story tracking categories in this story title |
| 56 | $parentCategories = array_intersect( |
| 57 | array_map( static function ( $key ) { |
| 58 | $categoryName = explode( ':', $key, 2 )[ 1 ]; |
| 59 | return str_replace( '_', ' ', $categoryName ); |
| 60 | }, array_keys( $title->getParentCategories() ) ), |
| 61 | $this->getAllTextForms() |
| 62 | ); |
| 63 | |
| 64 | // in most cases, the diff is either addtional or missing one category |
| 65 | if ( count( $parentCategories ) !== count( $categories ) ) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | // get text form from the category page |
| 70 | $trackingCategoriesTexts = array_map( function ( $trackingCategory ) { |
| 71 | return self::getTextForm( $trackingCategory ); |
| 72 | }, $categories ); |
| 73 | |
| 74 | // check diff |
| 75 | return array_diff( $trackingCategoriesTexts, $parentCategories ) || |
| 76 | array_diff( $parentCategories, $trackingCategoriesTexts ); |
| 77 | } |
| 78 | } |