Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.78% covered (success)
98.78%
81 / 82
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialEntitiesWithoutPage
98.78% covered (success)
98.78%
81 / 82
85.71% covered (warning)
85.71%
6 / 7
19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 prepareArguments
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
7
 getLanguageOptions
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setForm
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
1 / 1
4
 getResult
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getTitleForNavigation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Wikibase\Search\Elastic;
4
5use MediaWiki\HTMLForm\HTMLForm;
6use MediaWiki\Title\Title;
7use Wikibase\Lib\ContentLanguages;
8use Wikibase\Lib\LanguageNameLookup;
9use Wikibase\Lib\Store\EntityNamespaceLookup;
10use Wikibase\Repo\Specials\SpecialWikibaseQueryPage;
11
12/**
13 * Base page for pages listing entities without a specific value.
14 *
15 * @license GPL-2.0-or-later
16 * @author Thomas Pellissier Tanon
17 * @author Bene* < benestar.wikimedia@gmail.com >
18 * @author Thiemo Kreuz
19 */
20class SpecialEntitiesWithoutPage extends SpecialWikibaseQueryPage {
21
22    /**
23     * @var string Language code as requested by the user.
24     */
25    private $requestedLanguageCode = '';
26
27    /**
28     * @var string|null Entity type identifier as requested by the user.
29     */
30    private $requestedEntityType = '';
31
32    /**
33     * @param string $name
34     * @param string $termType One of the TermIndexEntry::TYPE_... constants.
35     * @param string $legendMsg
36     * @param string[] $acceptableEntityTypes List of entity type identifiers the special page
37     *  should accept
38     * @param ContentLanguages $termsLanguages
39     * @param LanguageNameLookup $languageNameLookup
40     * @param EntityNamespaceLookup $entityNamespaceLookup
41     */
42    public function __construct(
43        string $name,
44        private readonly string $termType,
45        private readonly string $legendMsg,
46        private readonly array $acceptableEntityTypes,
47        private readonly ContentLanguages $termsLanguages,
48        private readonly LanguageNameLookup $languageNameLookup,
49        private readonly EntityNamespaceLookup $entityNamespaceLookup,
50    ) {
51        parent::__construct( $name );
52    }
53
54    /**
55     * @see \Wikibase\Repo\Specials\SpecialWikibasePage::execute
56     *
57     * @param string|null $subPage
58     */
59    public function execute( $subPage ) {
60        parent::execute( $subPage );
61
62        $this->prepareArguments( $subPage );
63        $this->setForm();
64
65        if ( $this->requestedLanguageCode !== '' && $this->requestedEntityType !== '' ) {
66            $this->showQuery(
67                [],
68                $this->requestedLanguageCode . '/' . $this->requestedEntityType
69            );
70        }
71    }
72
73    /**
74     * Prepare the arguments
75     *
76     * @param string|null $subPage
77     */
78    private function prepareArguments( $subPage ) {
79        $this->requestedLanguageCode = '';
80        $this->requestedEntityType = '';
81
82        if ( $subPage !== null ) {
83            $parts = explode( '/', $subPage, 2 );
84            $this->requestedLanguageCode = $parts[0];
85            if ( isset( $parts[1] ) ) {
86                $this->requestedEntityType = $parts[1];
87            }
88        }
89
90        $request = $this->getRequest();
91        $this->requestedLanguageCode = $request->getText( 'language', $this->requestedLanguageCode );
92        $this->requestedEntityType = $request->getText( 'type', $this->requestedEntityType );
93
94        if ( $this->requestedLanguageCode !== ''
95            && !$this->termsLanguages->hasLanguage( $this->requestedLanguageCode )
96        ) {
97            $this->showErrorHTML( $this->msg(
98                'wikibasecirrus-entitieswithoutlabel-invalid-language',
99                wfEscapeWikiText( $this->requestedLanguageCode )
100            )->parse() );
101            $this->requestedLanguageCode = '';
102        }
103
104        if ( $this->requestedEntityType !== ''
105            && !in_array( $this->requestedEntityType, $this->acceptableEntityTypes )
106        ) {
107            $this->showErrorHTML( $this->msg(
108                'wikibasecirrus-entitieswithoutlabel-invalid-type',
109                wfEscapeWikiText( $this->requestedEntityType )
110            )->parse() );
111            $this->requestedEntityType = '';
112        }
113    }
114
115    /**
116     * Return options for the language input field.
117     *
118     * @return string[]
119     */
120    private function getLanguageOptions() {
121        $options = [];
122
123        foreach ( $this->termsLanguages->getLanguages() as $languageCode ) {
124            $languageName = $this->languageNameLookup->getNameForTerms( $languageCode );
125            $options["$languageName ($languageCode)"] = $languageCode;
126        }
127
128        return $options;
129    }
130
131    /**
132     * Build the HTML form
133     */
134    private function setForm() {
135        $entityTypeOptions = [];
136        foreach ( $this->acceptableEntityTypes as $type ) {
137            // Messages:
138            // wikibasecirrus-entity-item
139            // wikibasecirrus-entity-property
140            $msg = $this->msg( 'wikibasecirrus-entity-' . $type );
141            $text = $msg->isDisabled() ? $type : $msg->text();
142            $entityTypeOptions[$text] = $type;
143        }
144
145        $formDescriptor = [
146            'language' => [
147                'name' => 'language',
148                'default' => $this->requestedLanguageCode,
149                'type' => 'combobox',
150                'options' => $this->getLanguageOptions(),
151                'cssclass' => 'wb-language-suggester',
152                'id' => 'wb-entitieswithoutpage-language',
153                'label-message' => 'wikibasecirrus-entitieswithoutlabel-label-language'
154            ],
155            'type' => [
156                'name' => 'type',
157                'options' => $entityTypeOptions,
158                'default' => $this->requestedEntityType ?: array_first( $this->acceptableEntityTypes ),
159                'type' => 'select',
160                'id' => 'wb-entitieswithoutpage-type',
161                'label-message' => 'wikibasecirrus-entitieswithoutlabel-label-type'
162            ],
163            'submit' => [
164                'name' => '',
165                'default' => $this->msg( 'wikibasecirrus-entitieswithoutlabel-submit' )->text(),
166                'type' => 'submit',
167                'id' => 'wikibasecirrus-entitieswithoutpage-submit',
168            ]
169        ];
170
171        HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
172            ->setId( 'wb-entitieswithoutpage-form' )
173            ->setMethod( 'get' )
174            ->setWrapperLegendMsg( $this->legendMsg )
175            ->suppressDefaultSubmit()
176            ->setSubmitCallback( static function () {
177                // no-op
178            } )
179            ->show();
180    }
181
182    /**
183     * @see SpecialWikibaseQueryPage::getResult
184     *
185     * @param int $offset
186     * @param int $limit
187     * @return array empty array
188     */
189    protected function getResult( $offset = 0, $limit = 0 ) {
190        $title = Title::makeTitle( NS_SPECIAL, 'Search' );
191        $ns = $this->entityNamespaceLookup->getEntityNamespace( $this->requestedEntityType );
192        $url = $title->getFullUrlForRedirect( "sort=relevance&search=-has{$this->termType}:{$this->requestedLanguageCode}&ns$ns=1" );
193        $this->getOutput()->redirect( $url );
194
195        return [];
196    }
197
198    /**
199     * @return Title
200     */
201    protected function getTitleForNavigation() {
202        return $this->getPageTitle( $this->requestedLanguageCode . '/' . $this->requestedEntityType );
203    }
204
205}