Completed
Push — master ( 4c3769...c73606 )
by
unknown
09:05
created

SpecialExternalDatabases   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 164
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A newFromGlobalState() 0 10 1
A __construct() 0 12 1
A getGroupName() 0 3 1
A getDescription() 0 3 1
B execute() 0 48 4
B getRowGroup() 0 37 2
1
<?php
2
3
namespace WikibaseQuality\ExternalValidation\Specials;
4
5
use Html;
6
use Language;
7
use Linker;
8
use SpecialPage;
9
use Wikibase\DataModel\Services\Lookup\LanguageLabelDescriptionLookup;
10
use Wikibase\DataModel\Services\Lookup\TermLookup;
11
use Wikibase\Repo\EntityIdHtmlLinkFormatterFactory;
12
use Wikibase\Repo\WikibaseRepo;
13
use WikibaseQuality\ExternalValidation\DumpMetaInformation\DumpMetaInformation;
14
use WikibaseQuality\ExternalValidation\DumpMetaInformation\DumpMetaInformationLookup;
15
use WikibaseQuality\ExternalValidation\DumpMetaInformation\SqlDumpMetaInformationRepo;
16
use WikibaseQuality\ExternalValidation\ExternalValidationServices;
17
use WikibaseQuality\Html\HtmlTableBuilder;
18
use WikibaseQuality\Html\HtmlTableCellBuilder;
19
20
class SpecialExternalDatabases extends SpecialPage {
21
22
	/**
23
	 * @var EntityIdHtmlLinkFormatterFactory
24
	 */
25
	private $entityIdLinkFormatter;
26
27
	/**
28
	 * @var SqlDumpMetaInformationRepo
29
	 */
30
	private $dumpMetaInformationRepo;
31
32
	/**
33
	 * Creates new instance from global state.
34
	 *
35
	 * @return self
36
	 */
37
	public static function newFromGlobalState() {
38
		$repo = WikibaseRepo::getDefaultInstance();
39
		$externalValidationServices = ExternalValidationServices::getDefaultInstance();
40
41
		return new self(
42
			$repo->getTermLookup(),
43
			$repo->getEntityIdHtmlLinkFormatterFactory(),
44
			$externalValidationServices->getDumpMetaInformationLookup()
45
		);
46
	}
47
48
	/**
49
	 * @param TermLookup $termLookup
50
	 * @param EntityIdHtmlLinkFormatterFactory $entityIdHtmlLinkFormatterFactory
51
	 * @param DumpMetaInformationLookup $dumpMetaInformationRepo
52
	 */
53
	public function __construct(
54
		TermLookup $termLookup,
55
		EntityIdHtmlLinkFormatterFactory $entityIdHtmlLinkFormatterFactory,
56
		DumpMetaInformationLookup $dumpMetaInformationRepo ) {
57
		parent::__construct( 'ExternalDatabases' );
58
59
		$this->entityIdLinkFormatter = $entityIdHtmlLinkFormatterFactory->getEntityIdFormatter(
60
			new LanguageLabelDescriptionLookup( $termLookup, $this->getLanguage()->getCode() )
61
		);
62
63
		$this->dumpMetaInformationRepo = $dumpMetaInformationRepo;
0 ignored issues
show
Documentation Bug introduced by
$dumpMetaInformationRepo is of type object<WikibaseQuality\E...pMetaInformationLookup>, but the property $dumpMetaInformationRepo was declared to be of type object<WikibaseQuality\E...umpMetaInformationRepo>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
64
	}
65
66
	/**
67
	 * @see SpecialPage::getGroupName
68
	 *
69
	 * @return string
70
	 */
71
	function getGroupName() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
72
		return 'wikibasequality';
73
	}
74
75
	/**
76
	 * @see SpecialPage::getDescription
77
	 *
78
	 * @return string
79
	 */
80
	public function getDescription() {
81
		return $this->msg( 'wbqev-externaldbs' )->text();
82
	}
83
84
	/**
85
	 * @see SpecialPage::execute
86
	 *
87
	 * @param string|null $subPage
88
	 */
89
	public function execute( $subPage ) {
90
		$out = $this->getOutput();
91
92
		$this->setHeaders();
93
94
		$out->addHTML(
95
			Html::openElement( 'p' )
96
			. $this->msg( 'wbqev-externaldbs-instructions' )->parse()
97
			. Html::closeElement( 'p' )
98
			. Html::openElement( 'h3' )
99
			. $this->msg( 'wbqev-externaldbs-overview-headline' )->parse()
100
			. Html::closeElement( 'h3' )
101
		);
102
103
		$dumps = $this->dumpMetaInformationRepo->getAll();
104
		if ( count( $dumps ) > 0 ) {
105
			$groupedDumpMetaInformation = array();
106
			foreach ( $dumps as $dump ) {
0 ignored issues
show
Bug introduced by
The expression $dumps of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
107
				$sourceItemId = $dump->getSourceItemId()->getSerialization();
108
				$groupedDumpMetaInformation[$sourceItemId][] = $dump;
109
			}
110
111
			$table = new HtmlTableBuilder(
112
				array(
113
					$this->msg( 'wbqev-externaldbs-name' )->escaped(),
114
					$this->msg( 'wbqev-externaldbs-id' )->escaped(),
115
					$this->msg( 'wbqev-externaldbs-import-date' )->escaped(),
116
					$this->msg( 'wbqev-externaldbs-language' )->escaped(),
117
					$this->msg( 'wbqev-externaldbs-source-urls' )->escaped(),
118
					$this->msg( 'wbqev-externaldbs-size' )->escaped(),
119
					$this->msg( 'wbqev-externaldbs-license' )->escaped()
120
				),
121
				true
122
			);
123
124
			foreach ( $groupedDumpMetaInformation as $dumpMetaInformation ) {
125
				$table->appendRows( $this->getRowGroup( $dumpMetaInformation ) );
126
			}
127
128
			$out->addHTML( $table->toHtml() );
129
		} else {
130
			$out->addHTML(
131
				Html::openElement( 'p' )
132
				. $this->msg( 'wbqev-externaldbs-no-databases' )->escaped()
133
				. Html::closeElement( 'p' )
134
			);
135
		}
136
	}
137
138
	/**
139
	 * Build grouped rows for dump meta information with same source item id
140
	 *
141
	 * @param DumpMetaInformation[] $dumpMetaInformationGroup
142
	 *
143
	 * @return array
144
	 */
145
	private function getRowGroup( array $dumpMetaInformationGroup ) {
146
		$rows = array();
147
148
		foreach ( $dumpMetaInformationGroup as $dumpMetaInformation ) {
149
			$dumpId = $dumpMetaInformation->getDumpId();
150
			$importDate = $this->getLanguage()->timeanddate( $dumpMetaInformation->getImportDate() );
151
			$language = Language::fetchLanguageName(
152
				$dumpMetaInformation->getLanguageCode(),
153
				$this->getLanguage()->getCode()
154
			);
155
			$sourceUrl = Linker::makeExternalLink(
156
				$dumpMetaInformation->getSourceUrl(),
157
				$dumpMetaInformation->getSourceUrl()
158
			);
159
			$size = $this->getLanguage()->formatSize( $dumpMetaInformation->getSize() );
160
			$license = $this->entityIdLinkFormatter->formatEntityId( $dumpMetaInformation->getLicenseItemId() );
161
			$rows[] = array(
162
				new HtmlTableCellBuilder( $dumpId ),
163
				new HtmlTableCellBuilder( $importDate ),
164
				new HtmlTableCellBuilder( $language ),
165
				new HtmlTableCellBuilder( $sourceUrl, array(), true ),
166
				new HtmlTableCellBuilder( $size ),
167
				new HtmlTableCellBuilder( $license, array(), true )
168
			);
169
		}
170
171
		array_unshift(
172
			$rows[0],
173
			new HtmlTableCellBuilder(
174
				$this->entityIdLinkFormatter->formatEntityId( $dumpMetaInformationGroup[0]->getSourceItemId() ),
175
				array( 'rowspan' => (string)count( $dumpMetaInformationGroup ) ),
176
				true
177
			)
178
		);
179
180
		return $rows;
181
	}
182
183
}
184