|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SWL\MediaWiki\Hooks; |
|
4
|
|
|
|
|
5
|
|
|
use Title; |
|
6
|
|
|
use User; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Called after the personal URLs have been set up, before they are shown. |
|
10
|
|
|
* https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Manual:Hooks/PersonalUrls |
|
11
|
|
|
* |
|
12
|
|
|
* @ingroup SWL |
|
13
|
|
|
* |
|
14
|
|
|
* @licence GNU GPL v2+ |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
* |
|
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
18
|
|
|
* @author mwjames |
|
19
|
|
|
*/ |
|
20
|
|
|
class PersonalUrls { |
|
21
|
|
|
|
|
22
|
|
|
protected $personalUrls; |
|
23
|
|
|
protected $title; |
|
24
|
|
|
protected $user; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @param array &$personalUrls |
|
30
|
|
|
* @param Title &$title |
|
31
|
|
|
* @param User $user |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function __construct( array &$personalUrls, Title $title, User $user ) { |
|
34
|
4 |
|
$this->personalUrls =& $personalUrls; |
|
35
|
4 |
|
$this->title = $title; |
|
36
|
4 |
|
$this->user = $user; |
|
37
|
4 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @since 1.0 |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $configuration |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function setConfiguration( array $configuration ) { |
|
45
|
3 |
|
$this->configuration = $configuration; |
|
|
|
|
|
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @since 1.0 |
|
50
|
|
|
* |
|
51
|
|
|
* @return boolean |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function execute() { |
|
54
|
3 |
|
return $this->isEnabledForTopLink() && $this->isEnabledForUser() ? $this->addSwlTopLinkUrl(): true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
protected function isEnabledForTopLink() { |
|
58
|
3 |
|
return isset( $this->configuration['egSWLEnableTopLink'] ) && $this->configuration['egSWLEnableTopLink']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
protected function isEnabledForUser() { |
|
62
|
2 |
|
return $this->user->isLoggedIn() && $this->user->getOption( 'swl_watchlisttoplink' ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
protected function addSwlTopLinkUrl() { |
|
66
|
|
|
|
|
67
|
1 |
|
$url = \SpecialPage::getTitleFor( 'SemanticWatchlist' )->getLinkUrl(); |
|
68
|
|
|
|
|
69
|
|
|
$semanticWatchlist = array( |
|
70
|
1 |
|
'text' => wfMessage( 'prefs-swl' )->inLanguage( $this->title->getPageLanguage() )->text(), |
|
71
|
1 |
|
'href' => $url, |
|
72
|
1 |
|
'active' => ( $url == $this->title->getLinkUrl() ) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
1 |
|
$keys = array_keys( $this->personalUrls ); |
|
76
|
|
|
|
|
77
|
1 |
|
array_splice( |
|
78
|
1 |
|
$this->personalUrls, |
|
79
|
1 |
|
$this->getWatchListLocation( $keys ), |
|
80
|
1 |
|
1, |
|
81
|
1 |
|
array( $this->getWatchListItem( $keys ), $semanticWatchlist ) |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
1 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
protected function getWatchListLocation( $keys ) { |
|
88
|
1 |
|
return array_search( 'watchlist', $keys ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
protected function getWatchListItem( $keys ) { |
|
92
|
1 |
|
return $this->personalUrls[ $keys [ $this->getWatchListLocation( $keys ) ] ]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: