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 | use User; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @private |
||
| 15 | * @ingroup SESP |
||
| 16 | * |
||
| 17 | * @license GNU GPL v2+ |
||
| 18 | * @since 2.0 |
||
| 19 | * |
||
| 20 | * @author mwjames |
||
| 21 | */ |
||
| 22 | class UserEditCountPropertyAnnotator implements PropertyAnnotator { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Predefined property ID |
||
| 26 | */ |
||
| 27 | const PROP_ID = '___USEREDITCNT'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var AppFactory |
||
| 31 | */ |
||
| 32 | private $appFactory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @since 2.0 |
||
| 36 | * |
||
| 37 | * @param AppFactory $appFactory |
||
| 38 | */ |
||
| 39 | 3 | public function __construct( AppFactory $appFactory ) { |
|
| 40 | 3 | $this->appFactory = $appFactory; |
|
| 41 | 3 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @since 2.0 |
||
| 45 | * |
||
| 46 | * {@inheritDoc} |
||
| 47 | */ |
||
| 48 | 1 | public function isAnnotatorFor( DIProperty $property ) { |
|
| 49 | 1 | return $property->getKey() === self::PROP_ID ; |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @since 2.0 |
||
| 54 | * |
||
| 55 | * {@inheritDoc} |
||
| 56 | */ |
||
| 57 | 1 | public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { |
|
| 58 | |||
| 59 | 1 | $title = $semanticData->getSubject()->getTitle(); |
|
| 60 | |||
| 61 | 1 | if ( !$title->inNamespace( NS_USER ) ) { |
|
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | 1 | $user = $this->appFactory->newUserFromTitle( $title ); |
|
|
0 ignored issues
–
show
|
|||
| 66 | 1 | $dataItem = null; |
|
| 67 | |||
| 68 | 1 | if ( $user instanceof User ) { |
|
|
0 ignored issues
–
show
|
|||
| 69 | 1 | $dataItem = new DINumber( $user->getEditCount() ); |
|
| 70 | 1 | } |
|
| 71 | |||
| 72 | 1 | if ( $dataItem instanceof DataItem ) { |
|
| 73 | 1 | $semanticData->addPropertyObjectValue( $property, $dataItem ); |
|
| 74 | 1 | } |
|
| 75 | 1 | } |
|
| 76 | |||
| 77 | } |
||
| 78 |
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: