Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.28% covered (danger)
48.28%
28 / 58
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageRestHelperFactory
48.28% covered (danger)
48.28%
28 / 58
42.86% covered (danger)
42.86%
3 / 7
13.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 newRevisionContentHelper
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 newPageContentHelper
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 newHtmlOutputRendererHelper
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 newHtmlMessageOutputHelper
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newHtmlInputTransformHelper
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 newPageRedirectHelper
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Rest\Handler\Helper;
4
5use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
6use MediaWiki\Config\ServiceOptions;
7use MediaWiki\Content\IContentHandlerFactory;
8use MediaWiki\Edit\ParsoidOutputStash;
9use MediaWiki\Languages\LanguageConverterFactory;
10use MediaWiki\Languages\LanguageFactory;
11use MediaWiki\Page\PageLookup;
12use MediaWiki\Page\ParserOutputAccess;
13use MediaWiki\Page\RedirectStore;
14use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
15use MediaWiki\Parser\Parsoid\HtmlTransformFactory;
16use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
17use MediaWiki\Rest\RequestInterface;
18use MediaWiki\Rest\ResponseFactory;
19use MediaWiki\Rest\Router;
20use MediaWiki\Revision\RevisionLookup;
21use MediaWiki\Title\TitleFormatter;
22
23/**
24 * @since 1.40 Factory for helper objects designed for sharing logic between REST handlers that deal with page content.
25 */
26class PageRestHelperFactory {
27
28    /**
29     * @internal
30     */
31    public const CONSTRUCTOR_OPTIONS = PageContentHelper::CONSTRUCTOR_OPTIONS;
32
33    private ServiceOptions $options;
34    private RevisionLookup $revisionLookup;
35    private TitleFormatter $titleFormatter;
36    private PageLookup $pageLookup;
37    private ParsoidOutputStash $parsoidOutputStash;
38    private StatsdDataFactoryInterface $stats;
39    private ParserOutputAccess $parserOutputAccess;
40    private ParsoidOutputAccess $parsoidOutputAccess;
41    private ParsoidSiteConfig $parsoidSiteConfig;
42    private HtmlTransformFactory $htmlTransformFactory;
43    private IContentHandlerFactory $contentHandlerFactory;
44    private LanguageFactory $languageFactory;
45    private RedirectStore $redirectStore;
46    private LanguageConverterFactory $languageConverterFactory;
47
48    /**
49     * @param ServiceOptions $options
50     * @param RevisionLookup $revisionLookup
51     * @param TitleFormatter $titleFormatter
52     * @param PageLookup $pageLookup
53     * @param ParsoidOutputStash $parsoidOutputStash
54     * @param StatsdDataFactoryInterface $statsDataFactory
55     * @param ParserOutputAccess $parserOutputAccess
56     * @param ParsoidOutputAccess $parsoidOutputAccess
57     * @param ParsoidSiteConfig $parsoidSiteConfig
58     * @param HtmlTransformFactory $htmlTransformFactory
59     * @param IContentHandlerFactory $contentHandlerFactory
60     * @param LanguageFactory $languageFactory
61     * @param RedirectStore $redirectStore
62     * @param LanguageConverterFactory $languageConverterFactory
63     */
64    public function __construct(
65        ServiceOptions $options,
66        RevisionLookup $revisionLookup,
67        TitleFormatter $titleFormatter,
68        PageLookup $pageLookup,
69        ParsoidOutputStash $parsoidOutputStash,
70        StatsdDataFactoryInterface $statsDataFactory,
71        ParserOutputAccess $parserOutputAccess,
72        ParsoidOutputAccess $parsoidOutputAccess,
73        ParsoidSiteConfig $parsoidSiteConfig,
74        HtmlTransformFactory $htmlTransformFactory,
75        IContentHandlerFactory $contentHandlerFactory,
76        LanguageFactory $languageFactory,
77        RedirectStore $redirectStore,
78        LanguageConverterFactory $languageConverterFactory
79    ) {
80        $this->options = $options;
81        $this->revisionLookup = $revisionLookup;
82        $this->titleFormatter = $titleFormatter;
83        $this->pageLookup = $pageLookup;
84        $this->parsoidOutputStash = $parsoidOutputStash;
85        $this->stats = $statsDataFactory;
86        $this->parserOutputAccess = $parserOutputAccess;
87        $this->parsoidOutputAccess = $parsoidOutputAccess;
88        $this->parsoidSiteConfig = $parsoidSiteConfig;
89        $this->htmlTransformFactory = $htmlTransformFactory;
90        $this->contentHandlerFactory = $contentHandlerFactory;
91        $this->languageFactory = $languageFactory;
92        $this->redirectStore = $redirectStore;
93        $this->languageConverterFactory = $languageConverterFactory;
94    }
95
96    public function newRevisionContentHelper(): RevisionContentHelper {
97        return new RevisionContentHelper(
98            $this->options,
99            $this->revisionLookup,
100            $this->titleFormatter,
101            $this->pageLookup
102        );
103    }
104
105    public function newPageContentHelper(): PageContentHelper {
106        return new PageContentHelper(
107            $this->options,
108            $this->revisionLookup,
109            $this->titleFormatter,
110            $this->pageLookup
111        );
112    }
113
114    /**
115     * Should we ignore page id mismatches between page and revision objects
116     * in HTML/pagebundle requests? Mismatches arise because of page moves.
117     * This is recommended only for handling calls to internal APIs.
118     */
119    public function newHtmlOutputRendererHelper(
120        bool $lenientRevHandling = false
121    ): HtmlOutputRendererHelper {
122        return new HtmlOutputRendererHelper(
123            $this->parsoidOutputStash,
124            $this->stats,
125            $this->parsoidOutputAccess,
126            $this->parserOutputAccess,
127            $this->pageLookup,
128            $this->revisionLookup,
129            $this->parsoidSiteConfig,
130            $this->htmlTransformFactory,
131            $this->contentHandlerFactory,
132            $this->languageFactory,
133            $lenientRevHandling
134        );
135    }
136
137    public function newHtmlMessageOutputHelper(): HtmlMessageOutputHelper {
138        return new HtmlMessageOutputHelper();
139    }
140
141    public function newHtmlInputTransformHelper( $envOptions = [] ): HtmlInputTransformHelper {
142        return new HtmlInputTransformHelper(
143            $this->stats,
144            $this->htmlTransformFactory,
145            $this->parsoidOutputStash,
146            $this->parserOutputAccess,
147            $this->pageLookup,
148            $this->revisionLookup,
149            $envOptions
150        );
151    }
152
153    /**
154     * @since 1.41
155     */
156    public function newPageRedirectHelper(
157        ResponseFactory $responseFactory,
158        Router $router,
159        string $route,
160        RequestInterface $request
161    ): PageRedirectHelper {
162        return new PageRedirectHelper(
163            $this->redirectStore,
164            $this->titleFormatter,
165            $responseFactory,
166            $router,
167            $route,
168            $request,
169            $this->languageConverterFactory
170        );
171    }
172
173}