1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SESP\PropertyAnnotators; |
4
|
|
|
|
5
|
|
|
use SMW\DIProperty; |
6
|
|
|
use SMW\SemanticData; |
7
|
|
|
use SMWDataItem as DataItem; |
8
|
|
|
use SMWDIUri as DIUri; |
9
|
|
|
use SESP\PropertyAnnotator; |
10
|
|
|
use SESP\AppFactory; |
11
|
|
|
use Title; |
12
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @private |
16
|
|
|
* @ingroup SESP |
17
|
|
|
* |
18
|
|
|
* @license GNU GPL v2+ |
19
|
|
|
* @since 2.0 |
20
|
|
|
* |
21
|
|
|
* @author mwjames |
22
|
|
|
* @author rotsee |
23
|
|
|
*/ |
24
|
|
|
class ShortUrlPropertyAnnotator implements PropertyAnnotator { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Predefined property ID |
28
|
|
|
*/ |
29
|
|
|
const PROP_ID = '___SHORTURL'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AppFactory |
33
|
|
|
*/ |
34
|
|
|
private $appFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @since 2.0 |
38
|
|
|
* |
39
|
|
|
* @param AppFactory $appFactory |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct( AppFactory $appFactory ) { |
42
|
2 |
|
$this->appFactory = $appFactory; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @since 2.0 |
47
|
|
|
* |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function isAnnotatorFor( DIProperty $property ) { |
51
|
1 |
|
return $property->getKey() === self::PROP_ID; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @since 2.0 |
56
|
|
|
* |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
2 |
|
public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { |
60
|
|
|
|
61
|
2 |
|
if ( !$this->hasShortUrlUtils() ) { |
62
|
1 |
|
throw new RuntimeException( 'Class ShortUrlUtils is not available' ); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$dataItem = null; |
66
|
1 |
|
$shortUrl = $this->getShortUrl( $semanticData->getSubject()->getTitle() ); |
|
|
|
|
67
|
|
|
|
68
|
1 |
|
if ( $shortUrl !== null ) { |
69
|
1 |
|
$dataItem = new DIUri( 'http', $shortUrl, '', '' ); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
if ( $dataItem instanceof DataItem ) { |
73
|
1 |
|
$semanticData->addPropertyObjectValue( $property, $dataItem ); |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
protected function getShortUrl( Title $title ) { |
78
|
|
|
|
79
|
|
|
//FIXME handle internal and external links |
80
|
|
|
$shortUrl = null; |
81
|
|
|
|
82
|
|
|
if ( \ShortUrlUtils::needsShortUrl( $title ) ) { |
83
|
|
|
$shortUrl = $this->getUrlPrefix() . \ShortUrlUtils::encodeTitle( $title ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $shortUrl; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getUrlPrefix() { |
90
|
|
|
|
91
|
|
|
$shortUrlPrefix = $this->appFactory->getOption( 'wgShortUrlPrefix', '' ); |
|
|
|
|
92
|
|
|
|
93
|
|
|
if ( $shortUrlPrefix === '' ) { |
94
|
|
|
return SpecialPage::getTitleFor( 'ShortUrl' )->getFullUrl() . '/'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $shortUrlPrefix; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function hasShortUrlUtils() { |
101
|
|
|
return class_exists( 'ShortUrlUtils' ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
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: