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