Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentTransformer
85.71% covered (warning)
85.71%
6 / 7
66.67% covered (warning)
66.67%
2 / 3
3.03
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
 preSaveTransform
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 preloadTransform
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace MediaWiki\Content\Transform;
3
4use Content;
5use MediaWiki\Content\IContentHandlerFactory;
6use MediaWiki\Page\PageReference;
7use MediaWiki\User\UserIdentity;
8use ParserOptions;
9
10/**
11 * A service to transform content.
12 *
13 * @since 1.37
14 */
15class ContentTransformer {
16    /** @var IContentHandlerFactory */
17    private $contentHandlerFactory;
18
19    /**
20     * @param IContentHandlerFactory $contentHandlerFactory
21     */
22    public function __construct( IContentHandlerFactory $contentHandlerFactory ) {
23        $this->contentHandlerFactory = $contentHandlerFactory;
24    }
25
26    /**
27     * Returns a Content object with pre-save transformations applied (or $content
28     * if no transformations apply).
29     *
30     * @param Content $content
31     * @param PageReference $page
32     * @param UserIdentity $user
33     * @param ParserOptions $parserOptions
34     *
35     * @return Content
36     */
37    public function preSaveTransform(
38        Content $content,
39        PageReference $page,
40        UserIdentity $user,
41        ParserOptions $parserOptions
42    ): Content {
43        $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
44        $pstParams = new PreSaveTransformParamsValue( $page, $user, $parserOptions );
45
46        return $contentHandler->preSaveTransform( $content, $pstParams );
47    }
48
49    /**
50     * Returns a Content object with preload transformations applied (or $content
51     * if no transformations apply).
52     *
53     * @param Content $content
54     * @param PageReference $page
55     * @param ParserOptions $parserOptions
56     * @param array $params
57     *
58     * @return Content
59     */
60    public function preloadTransform(
61        Content $content,
62        PageReference $page,
63        ParserOptions $parserOptions,
64        array $params = []
65    ): Content {
66        $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
67        $pltParams = new PreloadTransformParamsValue( $page, $parserOptions, $params );
68
69        return $contentHandler->preloadTransform( $content, $pltParams );
70    }
71}