Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LogFormatterFactory
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 newFromEntry
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 newFromRow
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\CommentFormatter\CommentFormatter;
4use MediaWiki\Config\ServiceOptions;
5use MediaWiki\HookContainer\HookContainer;
6use MediaWiki\Linker\LinkRenderer;
7use MediaWiki\MainConfigNames;
8use MediaWiki\User\UserEditTracker;
9use Wikimedia\ObjectFactory\ObjectFactory;
10
11class LogFormatterFactory {
12
13    public const SERVICE_OPTIONS = [
14        MainConfigNames::LogActionsHandlers,
15    ];
16
17    private ServiceOptions $serviceOptions;
18    private ObjectFactory $objectFactory;
19    private HookContainer $hookContainer;
20    private LinkRenderer $linkRenderer;
21    private Language $contentLanguage;
22    private CommentFormatter $commentFormatter;
23    private UserEditTracker $userEditTracker;
24
25    public function __construct(
26        ServiceOptions $options,
27        ObjectFactory $objectFactory,
28        HookContainer $hookContainer,
29        LinkRenderer $linkRenderer,
30        Language $contentLanguage,
31        CommentFormatter $commentFormatter,
32        UserEditTracker $userEditTracker
33    ) {
34        $options->assertRequiredOptions( self::SERVICE_OPTIONS );
35        $this->serviceOptions = $options;
36        $this->objectFactory = $objectFactory;
37        $this->hookContainer = $hookContainer;
38        $this->linkRenderer = $linkRenderer;
39        $this->contentLanguage = $contentLanguage;
40        $this->commentFormatter = $commentFormatter;
41        $this->userEditTracker = $userEditTracker;
42    }
43
44    /**
45     * @param LogEntry $entry
46     * @return LogFormatter
47     */
48    public function newFromEntry( LogEntry $entry ): LogFormatter {
49        $logActionsHandlers = $this->serviceOptions->get( MainConfigNames::LogActionsHandlers );
50        $fulltype = $entry->getFullType();
51        $wildcard = $entry->getType() . '/*';
52        $handler = $logActionsHandlers[$fulltype] ?? $logActionsHandlers[$wildcard] ?? '';
53
54        if ( $handler !== '' ) {
55            $formatter = $this->objectFactory->createObject( $handler, [
56                'extraArgs' => [ $entry ],
57                'allowClassName' => true,
58                'assertClass' => LogFormatter::class,
59            ] );
60        } else {
61            $formatter = new LegacyLogFormatter( $entry, $this->hookContainer );
62        }
63
64        $formatter->setLinkRenderer( $this->linkRenderer );
65        $formatter->setContentLanguage( $this->contentLanguage );
66        $formatter->setCommentFormatter( $this->commentFormatter );
67        $formatter->setUserEditTracker( $this->userEditTracker );
68        return $formatter;
69    }
70
71    /**
72     * @param stdClass|array $row
73     * @return LogFormatter
74     * @see DatabaseLogEntry::getSelectQueryData
75     */
76    public function newFromRow( $row ): LogFormatter {
77        return $this->newFromEntry( DatabaseLogEntry::newFromRow( $row ) );
78    }
79
80}