Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ActionPageFactory
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getPage
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 getNames
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\SecurePoll;
4
5use MediaWiki\Extension\SecurePoll\Pages\ActionPage;
6use MediaWiki\Extension\SecurePoll\Pages\ArchivedPage;
7use MediaWiki\Extension\SecurePoll\Pages\ArchivePage;
8use MediaWiki\Extension\SecurePoll\Pages\CreatePage;
9use MediaWiki\Extension\SecurePoll\Pages\DetailsPage;
10use MediaWiki\Extension\SecurePoll\Pages\DumpPage;
11use MediaWiki\Extension\SecurePoll\Pages\EntryPage;
12use MediaWiki\Extension\SecurePoll\Pages\ListPage;
13use MediaWiki\Extension\SecurePoll\Pages\LoginPage;
14use MediaWiki\Extension\SecurePoll\Pages\MessageDumpPage;
15use MediaWiki\Extension\SecurePoll\Pages\TallyPage;
16use MediaWiki\Extension\SecurePoll\Pages\TranslatePage;
17use MediaWiki\Extension\SecurePoll\Pages\UnarchivePage;
18use MediaWiki\Extension\SecurePoll\Pages\VotePage;
19use MediaWiki\Extension\SecurePoll\Pages\VoterEligibilityPage;
20use MediaWiki\Languages\LanguageFallback;
21use MediaWiki\User\Options\UserOptionsLookup;
22use Wikimedia\ObjectFactory\ObjectFactory;
23
24class ActionPageFactory {
25    /**
26     * List of page names to the subclass of ActionPage which handles them.
27     */
28    private const PAGE_LIST = [
29        'archive' => [
30            'class' => ArchivePage::class,
31            'services' => [
32                'JobQueueGroup'
33            ],
34        ],
35        'archived' => [
36            'class' => ArchivedPage::class,
37            'services' => [
38                'LinkRenderer',
39                'DBLoadBalancer',
40            ],
41        ],
42        'create' => [
43            'class' => CreatePage::class,
44            'services' => [
45                'DBLoadBalancerFactory',
46                'UserGroupManager',
47                'LanguageNameUtils',
48                'WikiPageFactory',
49                'UserFactory',
50            ],
51        ],
52        'edit' => [
53            'class' => CreatePage::class,
54            'services' => [
55                'DBLoadBalancerFactory',
56                'UserGroupManager',
57                'LanguageNameUtils',
58                'WikiPageFactory',
59                'UserFactory',
60            ],
61        ],
62        'details' => [
63            'class' => DetailsPage::class,
64        ],
65        'dump' => [
66            'class' => DumpPage::class,
67        ],
68        'entry' => [
69            'class' => EntryPage::class,
70            'services' => [
71                'LinkRenderer',
72                'DBLoadBalancer',
73            ],
74        ],
75        'list' => [
76            'class' => ListPage::class,
77            'services' => [
78                'JobQueueGroup',
79            ],
80        ],
81        'login' => [
82            'class' => LoginPage::class,
83        ],
84        'msgdump' => [
85            'class' => MessageDumpPage::class,
86        ],
87        'tally' => [
88            'class' => TallyPage::class,
89            'services' => [
90                'DBLoadBalancer',
91                'JobQueueGroup',
92            ],
93        ],
94        'translate' => [
95            'class' => TranslatePage::class,
96            'services' => [
97                'LanguageNameUtils',
98                'SecurePoll.TranslationRepo'
99            ],
100        ],
101        'unarchive' => [
102            'class' => UnarchivePage::class,
103            'services' => [
104                'JobQueueGroup'
105            ],
106        ],
107        'vote' => [
108            'class' => VotePage::class,
109            'services' => [
110                'DBLoadBalancer',
111                'HookContainer',
112            ],
113        ],
114        'votereligibility' => [
115            'class' => VoterEligibilityPage::class,
116            'services' => [
117                'DBLoadBalancerFactory',
118                'LinkRenderer',
119                'TitleFactory',
120                'UserGroupManager',
121                'WikiPageFactory',
122            ]
123        ],
124    ];
125
126    /** @var ObjectFactory */
127    private $objectFactory;
128
129    /** @var UserOptionsLookup */
130    private $userOptionsLookup;
131
132    /** @var LanguageFallback */
133    private $languageFallback;
134
135    /**
136     * @param ObjectFactory $objectFactory
137     * @param UserOptionsLookup $userOptionsLookup
138     * @param LanguageFallback $languageFallback
139     */
140    public function __construct(
141        ObjectFactory $objectFactory,
142        UserOptionsLookup $userOptionsLookup,
143        LanguageFallback $languageFallback
144    ) {
145        $this->objectFactory = $objectFactory;
146        $this->userOptionsLookup = $userOptionsLookup;
147        $this->languageFallback = $languageFallback;
148    }
149
150    /**
151     * Find the object with a given name and return it (or NULL)
152     *
153     * @param string $name Action page name
154     * @param SpecialSecurePoll $specialPage
155     * @return ActionPage|null ActionPage object or null if the page doesn't exist
156     */
157    public function getPage( $name, $specialPage ) {
158        if ( empty( self::PAGE_LIST[$name] ) ) {
159            return null;
160        }
161        /** @var ActionPage $page */
162        // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
163        // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey
164        $page = $this->objectFactory->createObject(
165            self::PAGE_LIST[$name],
166            [
167                'allowClassName' => true,
168                'allowCallable' => true,
169                'extraArgs' => [ $specialPage ],
170            ]
171        );
172
173        // These are needed for standard parts of ActionPage, so we don't
174        // want every single subclass to need to declare them. (See: how
175        // SpecialPageFactory handles this.)
176        $page->setUserOptionsLookup( $this->userOptionsLookup );
177        $page->setLanguageFallback( $this->languageFallback );
178        return $page;
179    }
180
181    /**
182     * Returns a list of action page names.
183     *
184     * @return string[]
185     */
186    public function getNames() {
187        return array_keys( self::PAGE_LIST );
188    }
189}