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 | * @var AppFactory |
||
| 25 | */ |
||
| 26 | private $appFactory; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @since 2.0 |
||
| 30 | * |
||
| 31 | * @param AppFactory $appFactory |
||
| 32 | */ |
||
| 33 | public function __construct( AppFactory $appFactory ) { |
||
| 34 | $this->appFactory = $appFactory; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @since 2.0 |
||
| 39 | * |
||
| 40 | * {@inheritDoc} |
||
| 41 | */ |
||
| 42 | public function isAnnotatorFor( DIProperty $property ) { |
||
| 43 | return $property->getKey() === '___VIEWS' ; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @since 2.0 |
||
| 48 | * |
||
| 49 | * {@inheritDoc} |
||
| 50 | */ |
||
| 51 | public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { |
||
| 52 | |||
| 53 | if ( $this->appFactory->getOption( 'wgDisableCounters' ) ) { |
||
| 54 | return null; |
||
| 55 | } |
||
| 56 | |||
| 57 | $page = $this->appFactory->newWikiPage( $semanticData->getSubject()->getTitle() ); |
||
|
0 ignored issues
–
show
|
|||
| 58 | $count = $this->getPageViewCount( $page ); |
||
| 59 | |||
| 60 | if ( is_numeric( $count ) ) { |
||
| 61 | $semanticData->addPropertyObjectValue( $property, new DINumber( $count ) ); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | private function getPageViewCount( $page ) { |
||
| 66 | |||
| 67 | if ( class_exists( '\HitCounters\HitCounters' ) ) { |
||
| 68 | return \HitCounters\HitCounters::getCount( $page->getTitle() ); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ( method_exists( $page, 'getCount' ) ) { |
||
| 72 | return $page->getCount(); |
||
| 73 | } |
||
| 74 | |||
| 75 | return null; |
||
| 76 | } |
||
| 77 | |||
| 78 | } |
||
| 79 |
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: