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