These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SMW\PropertyAnnotator; |
||
4 | |||
5 | use SMW\ApplicationFactory; |
||
6 | use SMW\DIProperty; |
||
7 | use SMW\DIWikiPage; |
||
8 | use SMW\PropertyAnnotator; |
||
9 | |||
10 | /** |
||
11 | * Handling category annotation |
||
12 | * |
||
13 | * @ingroup SMW |
||
14 | * |
||
15 | * @license GNU GPL v2+ |
||
16 | * @since 1.9 |
||
17 | * |
||
18 | * @author mwjames |
||
19 | */ |
||
20 | class CategoryPropertyAnnotator extends PropertyAnnotatorDecorator { |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $categories; |
||
26 | |||
27 | /** |
||
28 | * @var array|null |
||
29 | */ |
||
30 | private $hiddenCategories = null; |
||
31 | |||
32 | /** |
||
33 | * @var boolean |
||
34 | */ |
||
35 | private $showHiddenCategoriesState = true; |
||
36 | |||
37 | /** |
||
38 | * @var boolean |
||
39 | */ |
||
40 | private $categoryInstanceUsageState = true; |
||
41 | |||
42 | /** |
||
43 | * @var boolean |
||
44 | */ |
||
45 | private $categoryHierarchyUsageState = true; |
||
46 | |||
47 | /** |
||
48 | * @since 1.9 |
||
49 | * |
||
50 | * @param PropertyAnnotator $propertyAnnotator |
||
51 | * @param array $categories |
||
52 | */ |
||
53 | 191 | public function __construct( PropertyAnnotator $propertyAnnotator, array $categories ) { |
|
54 | 191 | parent::__construct( $propertyAnnotator ); |
|
55 | 191 | $this->categories = $categories; |
|
56 | 191 | } |
|
57 | |||
58 | /** |
||
59 | * @since 2.3 |
||
60 | * |
||
61 | * @param boolean $showHiddenCategoriesState |
||
62 | */ |
||
63 | 190 | public function setShowHiddenCategoriesState( $showHiddenCategoriesState ) { |
|
64 | 190 | $this->showHiddenCategoriesState = (bool)$showHiddenCategoriesState; |
|
65 | 190 | } |
|
66 | |||
67 | /** |
||
68 | * @since 2.3 |
||
69 | * |
||
70 | * @param boolean $categoryInstanceUsageState |
||
71 | */ |
||
72 | 190 | public function setCategoryInstanceUsageState( $categoryInstanceUsageState ) { |
|
73 | 190 | $this->categoryInstanceUsageState = (bool)$categoryInstanceUsageState; |
|
74 | 190 | } |
|
75 | |||
76 | /** |
||
77 | * @since 2.3 |
||
78 | * |
||
79 | * @param boolean $categoryHierarchyUsageState |
||
80 | */ |
||
81 | 190 | public function setCategoryHierarchyUsageState( $categoryHierarchyUsageState ) { |
|
82 | 190 | $this->categoryHierarchyUsageState = (bool)$categoryHierarchyUsageState; |
|
83 | 190 | } |
|
84 | |||
85 | /** |
||
86 | * @see PropertyAnnotatorDecorator::addPropertyValues |
||
87 | */ |
||
88 | 190 | protected function addPropertyValues() { |
|
89 | |||
90 | 190 | $namespace = $this->getSemanticData()->getSubject()->getNamespace(); |
|
91 | |||
92 | 190 | foreach ( $this->categories as $catname ) { |
|
93 | |||
94 | 67 | if ( !$this->showHiddenCategoriesState && $this->isHiddenCategory( $catname ) ) { |
|
95 | 2 | continue; |
|
96 | } |
||
97 | |||
98 | 67 | if ( $this->categoryInstanceUsageState && ( $namespace !== NS_CATEGORY ) ) { |
|
99 | 61 | $this->getSemanticData()->addPropertyObjectValue( |
|
100 | 61 | new DIProperty( DIProperty::TYPE_CATEGORY ), |
|
101 | 61 | new DIWikiPage( $catname, NS_CATEGORY, '' ) |
|
102 | ); |
||
103 | } |
||
104 | |||
105 | 67 | if ( $this->categoryHierarchyUsageState && ( $namespace === NS_CATEGORY ) ) { |
|
106 | 16 | $this->getSemanticData()->addPropertyObjectValue( |
|
107 | 16 | new DIProperty( DIProperty::TYPE_SUBCATEGORY ), |
|
108 | 67 | new DIWikiPage( $catname, NS_CATEGORY, '' ) |
|
109 | ); |
||
110 | } |
||
111 | } |
||
112 | 190 | } |
|
113 | |||
114 | 2 | private function isHiddenCategory( $catName ) { |
|
115 | |||
116 | 2 | if ( $this->hiddenCategories === null ) { |
|
117 | |||
118 | 2 | $wikipage = ApplicationFactory::getInstance()->newPageCreator()->createPage( |
|
119 | 2 | $this->getSemanticData()->getSubject()->getTitle() |
|
0 ignored issues
–
show
|
|||
120 | ); |
||
121 | |||
122 | 2 | $this->hiddenCategories = $wikipage->getHiddenCategories(); |
|
123 | } |
||
124 | |||
125 | 2 | foreach ( $this->hiddenCategories as $hiddenCategory ) { |
|
126 | |||
127 | 2 | if ( $hiddenCategory->getText() === $catName ) { |
|
128 | 2 | return true; |
|
129 | }; |
||
130 | |||
131 | } |
||
132 | |||
133 | 2 | return false; |
|
134 | } |
||
135 | |||
136 | } |
||
137 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: