Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.97% covered (warning)
72.97%
54 / 74
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SourceWikiCleanupSnippet
72.97% covered (warning)
72.97%
54 / 74
80.00% covered (warning)
80.00%
4 / 5
16.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getHtml
68.75% covered (warning)
68.75%
44 / 64
0.00% covered (danger)
0.00%
0 / 1
9.95
 isFreshImport
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSourceEditAllowed
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isSourceDeleteAllowed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace FileImporter\Html;
4
5use FileImporter\Data\ImportPlan;
6use FileImporter\Data\ImportRequest;
7use FileImporter\Data\SourceUrl;
8use FileImporter\Remote\MediaWiki\RemoteApiActionExecutor;
9use FileImporter\Services\WikidataTemplateLookup;
10use MediaWiki\Config\Config;
11use MediaWiki\Html\Html;
12use MediaWiki\Language\MessageLocalizer;
13use MediaWiki\MediaWikiServices;
14use MediaWiki\User\User;
15use OOUI\CheckboxInputWidget;
16use OOUI\FieldLayout;
17
18/**
19 * @license GPL-2.0-or-later
20 */
21class SourceWikiCleanupSnippet {
22
23    public const ACTION_OFFERED_SOURCE_DELETE = 'offeredSourceDelete';
24    public const ACTION_OFFERED_SOURCE_EDIT = 'offeredSourceEdit';
25
26    private WikidataTemplateLookup $lookup;
27    private RemoteApiActionExecutor $remoteActionApi;
28
29    public function __construct(
30        private readonly MessageLocalizer $messageLocalizer,
31    ) {
32        // TODO: Inject
33        $this->lookup = MediaWikiServices::getInstance()->getService(
34            'FileImporterTemplateLookup' );
35        $this->remoteActionApi = MediaWikiServices::getInstance()->getService(
36            'FileImporterMediaWikiRemoteApiActionExecutor' );
37    }
38
39    public function getHtml(
40        ImportPlan $importPlan,
41        User $user,
42        Config $config,
43    ): string {
44        $context = $this->messageLocalizer;
45        $sourceUrl = $importPlan->getRequest()->getUrl();
46
47        $canAutomateEdit = $config->get( 'FileImporterSourceWikiTemplating' ) &&
48            $this->isSourceEditAllowed(
49                $sourceUrl,
50                $user,
51                $importPlan->getOriginalTitle()->getPrefixedText()
52            );
53        $canAutomateDelete = $config->get( 'FileImporterSourceWikiDeletion' ) &&
54            $this->isSourceDeleteAllowed( $sourceUrl, $user );
55
56        if ( !$canAutomateEdit && !$canAutomateDelete ) {
57            return '';
58        }
59
60        $html = Html::element(
61            'h2',
62            [],
63            $context->msg( 'fileimporter-heading-cleanup' )->plain()
64        );
65
66        if ( $canAutomateDelete ) {
67            $automateDeleteSelected = $importPlan->getAutomateSourceWikiDelete();
68            $importPlan->setActionIsPerformed( self::ACTION_OFFERED_SOURCE_DELETE );
69
70            $html .= Html::rawElement(
71                    'p',
72                    [],
73                    $context->msg( 'fileimporter-delete-text' )->parse()
74                ) .
75                new FieldLayout(
76                    new CheckboxInputWidget(
77                        [
78                            'name' => 'automateSourceWikiDelete',
79                            'selected' => $automateDeleteSelected,
80                            'value' => true
81                        ]
82                    ),
83                    [
84                        'label' => $context->msg( 'fileimporter-delete-checkboxlabel' )->parse(),
85                        'align' => 'inline'
86                    ]
87                );
88        } elseif ( $canAutomateEdit ) {
89            $automateEditSelected = $importPlan->getAutomateSourceWikiCleanUp() ||
90                $this->isFreshImport( $importPlan->getRequest() );
91            $importPlan->setActionIsPerformed( self::ACTION_OFFERED_SOURCE_EDIT );
92
93            $html .= Html::rawElement(
94                    'p',
95                    [],
96                    $context->msg(
97                        'fileimporter-cleanup-text',
98                        $this->lookup->fetchNowCommonsLocalTitle( $sourceUrl ) ?? ''
99                    )->parse()
100                ) .
101                new FieldLayout(
102                    new CheckboxInputWidget(
103                        [
104                            'name' => 'automateSourceWikiCleanup',
105                            'selected' => $automateEditSelected,
106                            'value' => true
107                        ]
108                    ),
109                    [
110                        'label' => $context->msg( 'fileimporter-cleanup-checkboxlabel' )->parse(),
111                        'align' => 'inline'
112                    ]
113                );
114        }
115
116        return $html;
117    }
118
119    /**
120     * @return bool
121     */
122    private function isFreshImport( ImportRequest $importRequest ) {
123        return $importRequest->getImportDetailsHash() === '';
124    }
125
126    /**
127     * Warning, contrary to the method name this currently doesn't check if the user is allowed to
128     * edit the page!
129     *
130     * @return bool True if source wiki editing is enabled and a localized {{Now Commons}} template
131     *  can be found.
132     */
133    private function isSourceEditAllowed( SourceUrl $sourceUrl, User $user, string $title ) {
134        if (
135            // Note: This intentionally doesn't allow a template with the name "0".
136            !$this->lookup->fetchNowCommonsLocalTitle( $sourceUrl )
137        ) {
138            return false;
139        }
140
141        return $this->remoteActionApi->executeTestEditActionQuery( $sourceUrl, $user, $title )
142            ->isGood();
143    }
144
145    /**
146     * @return bool True if source wiki deletions are enabled and the user does have the right to
147     *  delete pages. Also returns false if querying the user rights failed.
148     */
149    private function isSourceDeleteAllowed( SourceUrl $sourceUrl, User $user ) {
150        return $this->remoteActionApi->executeUserRightsQuery( $sourceUrl, $user )->isGood();
151    }
152
153}