Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 199
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3namespace FileImporter;
4
5use FileImporter\Remote\MediaWiki\AnyMediaWikiFileUrlChecker;
6use FileImporter\Remote\MediaWiki\ApiDetailRetriever;
7use FileImporter\Remote\MediaWiki\HttpApiLookup;
8use FileImporter\Remote\MediaWiki\InterwikiTablePrefixLookup;
9use FileImporter\Remote\MediaWiki\RemoteApiActionExecutor;
10use FileImporter\Remote\MediaWiki\RemoteApiImportTitleChecker;
11use FileImporter\Remote\MediaWiki\RemoteSourceFileEditDeleteAction;
12use FileImporter\Remote\MediaWiki\SiteTableSiteLookup;
13use FileImporter\Remote\MediaWiki\SiteTableSourceUrlChecker;
14use FileImporter\Remote\MediaWiki\SuggestManualTemplateAction;
15use FileImporter\Remote\NullPrefixLookup;
16use FileImporter\Services\CategoryExtractor;
17use FileImporter\Services\DuplicateFileRevisionChecker;
18use FileImporter\Services\FileTextRevisionValidator;
19use FileImporter\Services\Http\HttpRequestExecutor;
20use FileImporter\Services\Importer;
21use FileImporter\Services\ImportPlanFactory;
22use FileImporter\Services\NullRevisionCreator;
23use FileImporter\Services\SourceSite;
24use FileImporter\Services\SourceSiteLocator;
25use FileImporter\Services\SuccessCache;
26use FileImporter\Services\UploadBase\UploadBaseFactory;
27use FileImporter\Services\WikidataTemplateLookup;
28use FileImporter\Services\WikimediaSourceUrlNormalizer;
29use FileImporter\Services\WikiRevisionFactory;
30use FileImporter\Services\Wikitext\WikiLinkParserFactory;
31use ImportableUploadRevisionImporter;
32use MediaWiki\Context\RequestContext;
33use MediaWiki\Logger\LoggerFactory;
34use MediaWiki\MainConfigNames;
35use MediaWiki\MediaWikiServices;
36use MediaWiki\Registration\ExtensionRegistry;
37use UploadBase;
38
39/** @phpcs-require-sorted-array */
40return [
41
42    // Sites
43
44    /**
45     * This configuration example can be used for development and is very plain and lenient!
46     * It will allow importing files form ANY mediawiki site.
47     */
48    'FileImporter-Site-DefaultMediaWiki' => static function ( MediaWikiServices $services ): SourceSite {
49        /** @var HttpApiLookup $httpApiLookup */
50        $httpApiLookup = $services->getService( 'FileImporterMediaWikiHttpApiLookup' );
51        /** @var HttpRequestExecutor $httpRequestExecutor */
52        $httpRequestExecutor = $services->getService( 'FileImporterHttpRequestExecutor' );
53        $logger = LoggerFactory::getInstance( 'FileImporter' );
54        $maxFileSize = UploadBase::getMaxUploadSize( 'import' );
55
56        /** @var RemoteApiActionExecutor $remoteApiActionExecutor */
57        $remoteApiActionExecutor = $services->getService(
58            'FileImporterMediaWikiRemoteApiActionExecutor'
59        );
60
61        /** @var WikidataTemplateLookup $templateLookup */
62        $templateLookup = $services->getService( 'FileImporterTemplateLookup' );
63
64        $postImportHandler = new SuggestManualTemplateAction( $templateLookup );
65        $config = $services->getMainConfig();
66        if ( ExtensionRegistry::getInstance()->isLoaded( 'CentralAuth' ) &&
67            ( $config->get( 'FileImporterSourceWikiTemplating' ) ||
68                $config->get( 'FileImporterSourceWikiDeletion' ) )
69        ) {
70            $postImportHandler = new RemoteSourceFileEditDeleteAction(
71                $postImportHandler,
72                $templateLookup,
73                $remoteApiActionExecutor,
74                $services->getUrlUtils(),
75                $logger,
76                $services->getStatsdDataFactory()
77            );
78        }
79
80        return new SourceSite(
81            new AnyMediaWikiFileUrlChecker(),
82            new ApiDetailRetriever(
83                $httpApiLookup,
84                $httpRequestExecutor,
85                $maxFileSize,
86                $logger
87            ),
88            new RemoteApiImportTitleChecker(
89                $httpApiLookup,
90                $httpRequestExecutor,
91                $logger
92            ),
93            new WikimediaSourceUrlNormalizer(),
94            new NullPrefixLookup(),
95            $postImportHandler
96        );
97    },
98
99    /**
100     * This configuration example is setup to handle the wikimedia style setup.
101     * This only allows importing files from sites in the sites table.
102     */
103    'FileImporter-WikimediaSitesTableSite' => static function ( MediaWikiServices $services ): SourceSite {
104        /** @var SiteTableSiteLookup $siteTableLookup */
105        $siteTableLookup = $services->getService( 'FileImporterMediaWikiSiteTableSiteLookup' );
106        /** @var HttpApiLookup $httpApiLookup */
107        $httpApiLookup = $services->getService( 'FileImporterMediaWikiHttpApiLookup' );
108        /** @var HttpRequestExecutor $httpRequestExecutor */
109        $httpRequestExecutor = $services->getService( 'FileImporterHttpRequestExecutor' );
110        $logger = LoggerFactory::getInstance( 'FileImporter' );
111        $maxFileSize = UploadBase::getMaxUploadSize( 'import' );
112
113        /** @var RemoteApiActionExecutor $remoteApiActionExecutor */
114        $remoteApiActionExecutor = $services->getService(
115            'FileImporterMediaWikiRemoteApiActionExecutor'
116        );
117
118        /** @var WikidataTemplateLookup $templateLookup */
119        $templateLookup = $services->getService( 'FileImporterTemplateLookup' );
120
121        $postImportHandler = new SuggestManualTemplateAction( $templateLookup );
122        $config = $services->getMainConfig();
123        if ( ExtensionRegistry::getInstance()->isLoaded( 'CentralAuth' ) &&
124            ( $config->get( 'FileImporterSourceWikiTemplating' ) ||
125                $config->get( 'FileImporterSourceWikiDeletion' ) )
126        ) {
127            $postImportHandler = new RemoteSourceFileEditDeleteAction(
128                $postImportHandler,
129                $templateLookup,
130                $remoteApiActionExecutor,
131                $services->getUrlUtils(),
132                $logger,
133                $services->getStatsdDataFactory()
134            );
135        }
136
137        return new SourceSite(
138            new SiteTableSourceUrlChecker(
139                $siteTableLookup,
140                $logger
141            ),
142            new ApiDetailRetriever(
143                $httpApiLookup,
144                $httpRequestExecutor,
145                $maxFileSize,
146                $logger
147            ),
148            new RemoteApiImportTitleChecker(
149                $httpApiLookup,
150                $httpRequestExecutor,
151                $logger
152            ),
153            new WikimediaSourceUrlNormalizer(),
154            new InterwikiTablePrefixLookup(
155                $services->getInterwikiLookup(),
156                $httpApiLookup,
157                $httpRequestExecutor,
158                $services->getMainConfig()->get( 'FileImporterInterWikiMap' ),
159                $logger
160            ),
161            $postImportHandler
162        );
163    },
164
165    'FileImporterCategoryExtractor' => static function ( MediaWikiServices $services ): CategoryExtractor {
166        return new CategoryExtractor(
167            $services->getParserFactory(),
168            $services->getConnectionProvider(),
169            $services->getLinkBatchFactory()
170        );
171    },
172
173    'FileImporterDuplicateFileRevisionChecker' => static function (
174        MediaWikiServices $services
175    ): DuplicateFileRevisionChecker {
176        $localRepo = $services->getRepoGroup()->getLocalRepo();
177        return new DuplicateFileRevisionChecker( $localRepo );
178    },
179
180    'FileImporterHttpRequestExecutor' => static function ( MediaWikiServices $services ): HttpRequestExecutor {
181        $config = $services->getMainConfig();
182        $maxFileSize = UploadBase::getMaxUploadSize( 'import' );
183        $service = new HttpRequestExecutor(
184            $services->getHttpRequestFactory(),
185            [
186                'originalRequest' => RequestContext::getMain()->getRequest(),
187                'proxy' => $config->get( MainConfigNames::CopyUploadProxy ),
188                'timeout' => $config->get( MainConfigNames::CopyUploadTimeout ),
189            ],
190            $maxFileSize
191        );
192        $service->setLogger( LoggerFactory::getInstance( 'FileImporter' ) );
193        return $service;
194    },
195
196    'FileImporterImporter' => static function ( MediaWikiServices $services ): Importer {
197        /** @var WikiRevisionFactory $wikiRevisionFactory */
198        $wikiRevisionFactory = $services->getService( 'FileImporterWikiRevisionFactory' );
199        /** @var NullRevisionCreator $nullRevisionCreator */
200        $nullRevisionCreator = $services->getService( 'FileImporterNullRevisionCreator' );
201        /** @var HttpRequestExecutor $httpRequestExecutor */
202        $httpRequestExecutor = $services->getService( 'FileImporterHttpRequestExecutor' );
203        /** @var UploadBaseFactory $uploadBaseFactory */
204        $uploadBaseFactory = $services->getService( 'FileImporterUploadBaseFactory' );
205
206        $uploadRevisionImporter = $services->getUploadRevisionImporter();
207        // FIXME: Should be part of the UploadRevisionImporter interface or the import() method
208        if ( $uploadRevisionImporter instanceof ImportableUploadRevisionImporter ) {
209            $uploadRevisionImporter->setNullRevisionCreation( false );
210        }
211
212        return new Importer(
213            $services->getWikiPageFactory(),
214            $wikiRevisionFactory,
215            $nullRevisionCreator,
216            $services->getUserIdentityLookup(),
217            $httpRequestExecutor,
218            $uploadBaseFactory,
219            $services->getOldRevisionImporter(),
220            $uploadRevisionImporter,
221            new FileTextRevisionValidator(),
222            $services->getRestrictionStore(),
223            LoggerFactory::getInstance( 'FileImporter' ),
224            $services->getStatsdDataFactory()
225        );
226    },
227
228    'FileImporterImportPlanFactory' => static function ( MediaWikiServices $services ): ImportPlanFactory {
229        /** @var HttpRequestExecutor $httpRequestExecutor */
230        $httpRequestExecutor = $services->getService( 'FileImporterHttpRequestExecutor' );
231        /** @var SourceSiteLocator $sourceSiteLocator */
232        $sourceSiteLocator = $services->getService( 'FileImporterSourceSiteLocator' );
233        /** @var DuplicateFileRevisionChecker $duplicateFileChecker */
234        $duplicateFileChecker = $services->getService( 'FileImporterDuplicateFileRevisionChecker' );
235        /** @var UploadBaseFactory $uploadBaseFactory */
236        $uploadBaseFactory = $services->getService( 'FileImporterUploadBaseFactory' );
237
238        return new ImportPlanFactory(
239            $services->getMainConfig(),
240            new WikiLinkParserFactory(
241                $services->getTitleParser(),
242                $services->getNamespaceInfo(),
243                $services->getLanguageFactory()
244            ),
245            $services->getRestrictionStore(),
246            $httpRequestExecutor,
247            $sourceSiteLocator,
248            $duplicateFileChecker,
249            $uploadBaseFactory
250        );
251    },
252
253    'FileImporterNullRevisionCreator' => static function ( MediaWikiServices $services ): NullRevisionCreator {
254        return new NullRevisionCreator(
255            $services->getRevisionStore(),
256            $services->getConnectionProvider()
257        );
258    },
259
260    'FileImporterSourceSiteLocator' => static function ( MediaWikiServices $services ): SourceSiteLocator {
261        $sourceSiteServices = $services->getMainConfig()->get( 'FileImporterSourceSiteServices' );
262        $sourceSites = [];
263
264        foreach ( $sourceSiteServices as $serviceName ) {
265            $sourceSites[] = $services->getService( $serviceName );
266        }
267
268        if ( $sourceSites === [] ) {
269            $sourceSites[] = $services->getService( 'FileImporter-Site-DefaultMediaWiki' );
270        }
271
272        return new SourceSiteLocator( $sourceSites );
273    },
274
275    'FileImporterSuccessCache' => static function ( MediaWikiServices $services ): SuccessCache {
276        return new SuccessCache(
277            $services->getMainObjectStash(),
278            LoggerFactory::getInstance( 'FileImporter' )
279        );
280    },
281
282    'FileImporterTemplateLookup' => static function ( MediaWikiServices $services ): WikidataTemplateLookup {
283        return new WikidataTemplateLookup(
284            $services->getMainConfig(),
285            $services->getService( 'FileImporterMediaWikiSiteTableSiteLookup' ),
286            $services->getService( 'FileImporterHttpRequestExecutor' ),
287            LoggerFactory::getInstance( 'FileImporter' )
288        );
289    },
290
291    'FileImporterUploadBaseFactory' => static function ( MediaWikiServices $services ): UploadBaseFactory {
292        return new UploadBaseFactory( LoggerFactory::getInstance( 'FileImporter' ) );
293    },
294
295    'FileImporterWikiRevisionFactory' => static function ( MediaWikiServices $services ): WikiRevisionFactory {
296        return new WikiRevisionFactory(
297            $services->getContentHandlerFactory()
298        );
299    },
300
301];