Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LinkRendererFactory
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 createFromLegacyOptions
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
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 as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Kunal Mehta <legoktm@debian.org>
20 */
21namespace MediaWiki\Linker;
22
23use MediaWiki\Cache\LinkCache;
24use MediaWiki\Config\ServiceOptions;
25use MediaWiki\HookContainer\HookContainer;
26use MediaWiki\SpecialPage\SpecialPageFactory;
27use MediaWiki\Title\TitleFormatter;
28
29/**
30 * Factory to create LinkRender objects
31 * @since 1.28
32 */
33class LinkRendererFactory {
34
35    /**
36     * @var TitleFormatter
37     */
38    private $titleFormatter;
39
40    /**
41     * @var LinkCache
42     */
43    private $linkCache;
44
45    /**
46     * @var HookContainer
47     */
48    private $hookContainer;
49
50    /**
51     * @var SpecialPageFactory
52     */
53    private $specialPageFactory;
54
55    /**
56     * @internal For use by core ServiceWiring
57     * @param TitleFormatter $titleFormatter
58     * @param LinkCache $linkCache
59     * @param SpecialPageFactory $specialPageFactory
60     * @param HookContainer $hookContainer
61     */
62    public function __construct(
63        TitleFormatter $titleFormatter,
64        LinkCache $linkCache,
65        SpecialPageFactory $specialPageFactory,
66        HookContainer $hookContainer
67    ) {
68        $this->titleFormatter = $titleFormatter;
69        $this->linkCache = $linkCache;
70        $this->specialPageFactory = $specialPageFactory;
71        $this->hookContainer = $hookContainer;
72    }
73
74    /**
75     * @param array $options optional flags for rendering
76     *  - 'renderForComment': set to true if the created LinkRenderer will be used for
77     *    links in an edit summary or log comments. An instance with renderForComment
78     *    enabled must not be used for other links.
79     *
80     * @return LinkRenderer
81     */
82    public function create( array $options = [ 'renderForComment' => false ] ) {
83        return new LinkRenderer(
84            $this->titleFormatter, $this->linkCache, $this->specialPageFactory,
85            $this->hookContainer,
86            new ServiceOptions( LinkRenderer::CONSTRUCTOR_OPTIONS, $options )
87        );
88    }
89
90    /**
91     * @param array $options
92     * @return LinkRenderer
93     */
94    public function createFromLegacyOptions( array $options ) {
95        $linkRenderer = $this->create();
96
97        if ( in_array( 'forcearticlepath', $options, true ) ) {
98            $linkRenderer->setForceArticlePath( true );
99        }
100
101        if ( in_array( 'http', $options, true ) ) {
102            $linkRenderer->setExpandURLs( PROTO_HTTP );
103        } elseif ( in_array( 'https', $options, true ) ) {
104            $linkRenderer->setExpandURLs( PROTO_HTTPS );
105        }
106
107        return $linkRenderer;
108    }
109}