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