Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
0.00% |
0 / 45 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
onParserFirstCallInit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onFuncInsider | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
onOutputPageParserOutput | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getInsiderUrls | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
onSidebarBeforeOutput | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License version 2 as |
5 | * published by the Free Software Foundation. |
6 | * |
7 | * This program is distributed in the hope that it will be useful, |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | * GNU General Public License for more details. |
11 | * |
12 | * You should have received a copy of the GNU General Public License along |
13 | * with this program; if not, write to the Free Software Foundation, Inc., |
14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
15 | * http://www.gnu.org/copyleft/gpl.html |
16 | * |
17 | * @file |
18 | */ |
19 | |
20 | namespace MediaWiki\Extension\Insider; |
21 | |
22 | use MediaWiki\Hook\ParserFirstCallInitHook; |
23 | use MediaWiki\Hook\SidebarBeforeOutputHook; |
24 | use MediaWiki\MediaWikiServices; |
25 | use MediaWiki\Output\Hook\OutputPageParserOutputHook; |
26 | use MediaWiki\Output\OutputPage; |
27 | use MediaWiki\Parser\Parser; |
28 | use MediaWiki\Parser\ParserOutput; |
29 | use MediaWiki\Title\Title; |
30 | use Skin; |
31 | |
32 | class Hooks implements |
33 | ParserFirstCallInitHook, |
34 | OutputPageParserOutputHook, |
35 | SidebarBeforeOutputHook |
36 | { |
37 | /** |
38 | * @param Parser $parser |
39 | */ |
40 | public function onParserFirstCallInit( $parser ) { |
41 | $parser->setFunctionHook( 'insider', [ self::class, 'onFuncInsider' ] ); |
42 | } |
43 | |
44 | /** |
45 | * @param Parser $parser |
46 | * @param string ...$args |
47 | * @return string |
48 | */ |
49 | public static function onFuncInsider( Parser $parser, ...$args ) { |
50 | $parserOutput = $parser->getOutput(); |
51 | $insiders = $parserOutput->getExtensionData( 'Insider' ) ?: []; |
52 | |
53 | foreach ( $args as $insider ) { |
54 | $insiders[] = $insider; |
55 | } |
56 | |
57 | $parserOutput->setExtensionData( 'Insider', $insiders ); |
58 | |
59 | return ''; |
60 | } |
61 | |
62 | /** |
63 | * @param OutputPage $out |
64 | * @param ParserOutput $parserOutput |
65 | */ |
66 | public function onOutputPageParserOutput( $out, $parserOutput ): void { |
67 | $related = $parserOutput->getExtensionData( 'Insider' ); |
68 | |
69 | if ( $related ) { |
70 | $out->setProperty( 'Insider', $related ); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * @param array $insiders |
76 | * @return array |
77 | */ |
78 | protected static function getInsiderUrls( array $insiders ) { |
79 | $insiderUrls = []; |
80 | |
81 | foreach ( $insiders as $insider ) { |
82 | // Tribute to Evan |
83 | $insider = urldecode( $insider ); |
84 | |
85 | $userTitle = Title::newFromText( $insider, NS_USER ); |
86 | if ( $userTitle ) { |
87 | $insiderUrls[] = [ |
88 | 'href' => $userTitle->getLocalURL(), |
89 | 'text' => $userTitle->getText(), |
90 | 'class' => 'interwiki-insider' |
91 | ]; |
92 | } |
93 | } |
94 | |
95 | return $insiderUrls; |
96 | } |
97 | |
98 | /** |
99 | * Write out HTML-code. |
100 | * |
101 | * @param Skin $skin |
102 | * @param array &$bar |
103 | */ |
104 | public function onSidebarBeforeOutput( $skin, &$bar ): void { |
105 | $out = $skin->getOutput(); |
106 | $insiders = $out->getProperty( 'Insider' ); |
107 | |
108 | if ( !$insiders ) { |
109 | return; |
110 | } |
111 | |
112 | $insiderUrls = self::getInsiderUrls( $insiders ); |
113 | $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); |
114 | |
115 | // build insider <li>'s |
116 | $list = []; |
117 | foreach ( $insiders as $insider ) { |
118 | // Tribute to Evan |
119 | $insider = urldecode( $insider ); |
120 | |
121 | $userTitle = Title::newFromText( $insider, NS_USER ); |
122 | if ( $userTitle ) { |
123 | $list[] = [ |
124 | 'text' => $userTitle->getText(), |
125 | 'class' => 'interwiki-insider', |
126 | 'href' => $userTitle->getLocalUrl(), |
127 | ]; |
128 | } |
129 | } |
130 | |
131 | // add general "insiders" entry |
132 | $title = Title::newFromText( wfMessage( 'insider-about-page' )->inContentLanguage()->plain() ); |
133 | if ( $title ) { |
134 | $list[] = [ |
135 | 'class' => 'interwiki-insider', |
136 | 'text' => $skin->msg( 'insider-about' )->text(), |
137 | 'href' => $title->getLocalUrl(), |
138 | ]; |
139 | } |
140 | |
141 | // build complete html |
142 | $bar[$skin->msg( 'insider-title' )->text()] = $list; |
143 | } |
144 | } |