SemanticMediaWiki /
SemanticExtraSpecialProperties
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 SESP\PropertyAnnotators; |
||
| 4 | |||
| 5 | use SMW\DIProperty; |
||
| 6 | use SMW\SemanticData; |
||
| 7 | use SMWDataItem as DataItem; |
||
| 8 | use SMWDINumber as DINumber; |
||
| 9 | use SESP\PropertyAnnotator; |
||
| 10 | use SESP\AppFactory; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @private |
||
| 14 | * @ingroup SESP |
||
| 15 | * |
||
| 16 | * @license GNU GPL v2+ |
||
| 17 | * @since 2.0 |
||
| 18 | * |
||
| 19 | * @author mwjames |
||
| 20 | */ |
||
| 21 | class PageViewsPropertyAnnotator implements PropertyAnnotator { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Predefined property ID |
||
| 25 | */ |
||
| 26 | const PROP_ID = '___VIEWS'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var AppFactory |
||
| 30 | */ |
||
| 31 | private $appFactory; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @since 2.0 |
||
| 35 | * |
||
| 36 | * @param AppFactory $appFactory |
||
| 37 | */ |
||
| 38 | 4 | public function __construct( AppFactory $appFactory ) { |
|
| 39 | 4 | $this->appFactory = $appFactory; |
|
| 40 | 4 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @since 2.0 |
||
| 44 | * |
||
| 45 | * {@inheritDoc} |
||
| 46 | */ |
||
| 47 | 1 | public function isAnnotatorFor( DIProperty $property ) { |
|
| 48 | 1 | return $property->getKey() === self::PROP_ID; |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @since 2.0 |
||
| 53 | * |
||
| 54 | * {@inheritDoc} |
||
| 55 | */ |
||
| 56 | 2 | public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { |
|
| 57 | |||
| 58 | 2 | if ( $this->appFactory->getOption( 'wgDisableCounters' ) ) { |
|
| 59 | 1 | return null; |
|
| 60 | } |
||
| 61 | |||
| 62 | 1 | $page = $this->appFactory->newWikiPage( $semanticData->getSubject()->getTitle() ); |
|
|
0 ignored issues
–
show
|
|||
| 63 | 1 | $count = $this->getPageViewCount( $page ); |
|
| 64 | |||
| 65 | 1 | if ( is_numeric( $count ) ) { |
|
| 66 | 1 | $semanticData->addPropertyObjectValue( $property, new DINumber( $count ) ); |
|
| 67 | 1 | } |
|
| 68 | 1 | } |
|
| 69 | |||
| 70 | 1 | private function getPageViewCount( $page ) { |
|
| 71 | |||
| 72 | 1 | if ( class_exists( '\HitCounters\HitCounters' ) ) { |
|
| 73 | return \HitCounters\HitCounters::getCount( $page->getTitle() ); |
||
| 74 | } |
||
| 75 | |||
| 76 | 1 | if ( method_exists( $page, 'getCount' ) ) { |
|
| 77 | 1 | return $page->getCount(); |
|
| 78 | } |
||
| 79 | |||
| 80 | return null; |
||
| 81 | } |
||
| 82 | |||
| 83 | } |
||
| 84 |
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: