Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 184
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListPage
0.00% covered (danger)
0.00%
0 / 184
0.00% covered (danger)
0.00%
0 / 3
1560
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 182
0.00% covered (danger)
0.00%
0 / 1
1406
 makeAnchor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Gadgets\Special;
4
5use MediaWiki\Content\ContentHandler;
6use MediaWiki\Extension\Gadgets\GadgetRepo;
7use MediaWiki\Html\Html;
8use MediaWiki\Language\Language;
9use MediaWiki\Parser\Sanitizer;
10use MediaWiki\Skin\SkinFactory;
11use MediaWiki\Title\Title;
12
13class ListPage extends ActionPage {
14
15    public function __construct(
16        SpecialGadgets $specialPage,
17        private readonly GadgetRepo $gadgetRepo,
18        private readonly Language $contentLanguage,
19        private readonly SkinFactory $skinFactory,
20    ) {
21        parent::__construct( $specialPage );
22    }
23
24    /**
25     * Displays form showing the list of installed gadgets
26     */
27    public function execute( array $params ) {
28        $output = $this->specialPage->getOutput();
29        $output->setPageTitleMsg( $this->msg( 'gadgets-title' ) );
30        $output->addWikiMsg( 'gadgets-pagetext' );
31
32        $gadgets = $this->gadgetRepo->getStructuredList();
33        if ( !$gadgets ) {
34            return;
35        }
36
37        $output->disallowUserJs();
38        $lang = $this->specialPage->getLanguage();
39        $langSuffix = "";
40        if ( !$lang->equals( $this->contentLanguage ) ) {
41            $langSuffix = "/" . $lang->getCode();
42        }
43
44        $listOpen = false;
45
46        $editDefinitionMessage = $this->specialPage->getUser()->isAllowed( 'editsitejs' )
47            ? 'edit'
48            : 'viewsource';
49        $editInterfaceMessage = $this->specialPage->getUser()->isAllowed( 'editinterface' )
50            ? 'gadgets-editdescription'
51            : 'gadgets-viewdescription';
52        $editInterfaceMessageSection = $this->specialPage->getUser()->isAllowed( 'editinterface' )
53            ? 'gadgets-editsectiontitle'
54            : 'gadgets-viewsectiontitle';
55
56        $linkRenderer = $this->specialPage->getLinkRenderer();
57        foreach ( $gadgets as $section => $entries ) {
58            if ( $section !== '' ) {
59                if ( $listOpen ) {
60                    $output->addHTML( Html::closeElement( 'ul' ) . "\n" );
61                    $listOpen = false;
62                }
63
64                // H2 section heading
65                $headingText = $this->msg( "gadget-section-$section" )->parse();
66                $output->addHTML( Html::rawElement( 'h2', [], $headingText ) . "\n" );
67
68                // Edit link for the section heading
69                $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-section-$section$langSuffix" );
70                $linkTarget = $title
71                    ? $linkRenderer->makeLink( $title, $this->msg( $editInterfaceMessageSection )->text(),
72                        [], [ 'action' => 'edit' ] )
73                    : htmlspecialchars( $section );
74                $output->addHTML( Html::rawElement( 'p', [],
75                        $this->msg( 'parentheses' )->rawParams( $linkTarget )->escaped() ) . "\n" );
76            }
77
78            foreach ( $entries as $gadget ) {
79                $name = $gadget->getName();
80                $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-{$name}$langSuffix" );
81                if ( !$title ) {
82                    continue;
83                }
84
85                $links = [];
86                $definitionTitle = $this->gadgetRepo->getGadgetDefinitionTitle( $name );
87                if ( $definitionTitle ) {
88                    $links[] = $linkRenderer->makeLink(
89                        $definitionTitle,
90                        $this->msg( $editDefinitionMessage )->text(),
91                        [],
92                        [ 'action' => 'edit' ]
93                    );
94                }
95                $links[] = $linkRenderer->makeLink(
96                    $title,
97                    $this->msg( $editInterfaceMessage )->text(),
98                    [],
99                    [ 'action' => 'edit' ]
100                );
101                $links[] = $linkRenderer->makeLink(
102                    $this->specialPage->getPageTitle( "export/{$name}" ),
103                    $this->msg( 'gadgets-export' )->text()
104                );
105
106                $nameHtml = $this->msg( "gadget-{$name}" )->parse();
107
108                if ( !$listOpen ) {
109                    $listOpen = true;
110                    $output->addHTML( Html::openElement( 'ul' ) );
111                }
112
113                $actionsHtml = '&#160;&#160;' .
114                    $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $links ) )->escaped();
115                $output->addHTML(
116                    Html::openElement( 'li', [ 'id' => $this->makeAnchor( $name ) ] ) .
117                    $nameHtml . $actionsHtml
118                );
119
120                // Portion: Show files, dependencies, peers
121                $output->addHTML(
122                    '<br>' .
123                    $this->msg( 'gadgets-uses' )->escaped() .
124                    $this->msg( 'colon-separator' )->escaped()
125                );
126                $links = [];
127                foreach ( $gadget->getPeers() as $peer ) {
128                    $links[] = Html::element(
129                        'a',
130                        [ 'href' => '#' . $this->makeAnchor( $peer ) ],
131                        $peer
132                    );
133                }
134                foreach ( $gadget->getScriptsAndStyles() as $codePage ) {
135                    $title = Title::newFromText( $codePage );
136                    if ( !$title ) {
137                        continue;
138                    }
139                    $links[] = $linkRenderer->makeLink( $title, $title->getText() );
140                }
141                $output->addHTML( $lang->commaList( $links ) );
142
143                if ( $gadget->isPackaged() ) {
144                    $output->addHTML( '<br>' . $this->msg( 'gadgets-packaged',
145                        $this->gadgetRepo->titleWithoutPrefix( $gadget->getScripts()[0], $gadget->getName() )
146                    )->parse() );
147                }
148
149                // Portion: Show required rights (optional)
150                $rights = [];
151                foreach ( $gadget->getRequiredRights() as $right ) {
152                    $rights[] = Html::element(
153                        'code',
154                        [ 'title' => $this->msg( "right-$right" )->plain() ],
155                        $right
156                    );
157                }
158                if ( $rights ) {
159                    $output->addHTML(
160                        '<br>' .
161                        $this->msg( 'gadgets-required-rights', $lang->commaList( $rights ), count( $rights ) )->parse()
162                    );
163                }
164
165                // Portion: Show required skins (optional)
166                $requiredSkins = $gadget->getRequiredSkins();
167                $skins = [];
168                $validSkins = $this->skinFactory->getInstalledSkins();
169                foreach ( $requiredSkins as $skinId ) {
170                    if ( isset( $validSkins[$skinId] ) ) {
171                        $skins[] = $this->msg( "skinname-$skinId" )->plain();
172                    } else {
173                        $skins[] = $skinId;
174                    }
175                }
176                if ( $skins ) {
177                    $output->addHTML(
178                        '<br>' .
179                        $this->msg( 'gadgets-required-skins', $lang->commaList( $skins ) )
180                            ->numParams( count( $skins ) )->parse()
181                    );
182                }
183
184                // Portion: Show required actions (optional)
185                $actions = [];
186                foreach ( $gadget->getRequiredActions() as $action ) {
187                    $actions[] = Html::element( 'code', [], $action );
188                }
189                if ( $actions ) {
190                    $output->addHTML(
191                        '<br>' .
192                        $this->msg( 'gadgets-required-actions', $lang->commaList( $actions ) )
193                            ->numParams( count( $actions ) )->parse()
194                    );
195                }
196
197                // Portion: Show required namespaces (optional)
198                $namespaces = $gadget->getRequiredNamespaces();
199                if ( $namespaces ) {
200                    $output->addHTML(
201                        '<br>' .
202                        $this->msg(
203                            'gadgets-required-namespaces',
204                            $lang->commaList( array_map( function ( int $ns ) use ( $lang ) {
205                                return $ns == NS_MAIN
206                                    ? $this->msg( 'blanknamespace' )->text()
207                                    : $lang->getFormattedNsText( $ns );
208                            }, $namespaces ) )
209                        )->numParams( count( $namespaces ) )->parse()
210                    );
211                }
212
213                // Portion: Show required content models (optional)
214                $contentModels = [];
215                foreach ( $gadget->getRequiredContentModels() as $model ) {
216                    $contentModels[] = Html::element(
217                        'code',
218                        [ 'title' => ContentHandler::getLocalizedName( $model, $lang ) ],
219                        $model
220                    );
221                }
222                if ( $contentModels ) {
223                    $output->addHTML(
224                        '<br>' .
225                        $this->msg( 'gadgets-required-contentmodels',
226                            $lang->commaList( $contentModels ),
227                            count( $contentModels )
228                        )->parse()
229                    );
230                }
231
232                // Portion: Show required categories (optional)
233                $categories = [];
234                foreach ( $gadget->getRequiredCategories() as $category ) {
235                    $title = Title::makeTitleSafe( NS_CATEGORY, $category );
236                    $categories[] = $title
237                        ? $linkRenderer->makeLink( $title, $category )
238                        : htmlspecialchars( $category );
239                }
240                if ( $categories ) {
241                    $output->addHTML(
242                        '<br>' .
243                        $this->msg( 'gadgets-required-categories' )
244                            ->rawParams( $lang->commaList( $categories ) )
245                            ->numParams( count( $categories ) )->parse()
246                    );
247                }
248                // Show if hidden
249                if ( $gadget->isHidden() ) {
250                    $output->addHTML( '<br>' . $this->msg( 'gadgets-hidden' )->parse() );
251                }
252
253                // Show if supports URL load
254                if ( $gadget->supportsUrlLoad() ) {
255                    $output->addHTML( '<br>' . $this->msg( 'gadgets-supports-urlload' )->parse() );
256                }
257
258                // Portion: Show on by default (optional)
259                if ( $gadget->isOnByDefault() ) {
260                    $output->addHTML( '<br>' . $this->msg( 'gadgets-default' )->parse() );
261                }
262
263                // Show warnings
264                $warnings = $this->gadgetRepo->validationWarnings( $gadget );
265
266                if ( $warnings ) {
267                    $output->addModuleStyles( 'mediawiki.codex.messagebox.styles' );
268                    $output->addHTML( Html::warningBox( implode( '<br>', array_map( static function ( $msg ) {
269                        return $msg->parse();
270                    }, $warnings ) ) ) );
271                }
272
273                $output->addHTML( Html::closeElement( 'li' ) . "\n" );
274            }
275        }
276
277        if ( $listOpen ) {
278            $output->addHTML( Html::closeElement( 'ul' ) . "\n" );
279        }
280    }
281
282    /**
283     * @param string $gadgetName
284     * @return string
285     */
286    private function makeAnchor( $gadgetName ) {
287        return 'gadget-' . Sanitizer::escapeIdForAttribute( $gadgetName );
288    }
289
290}