DumpRdf::setServices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Wikibase\Repo\Maintenance;
4
5
use MediaWiki\MediaWikiServices;
6
use Wikibase\DataModel\Services\Entity\EntityPrefetcher;
7
use Wikibase\DataModel\Services\EntityId\EntityIdPager;
8
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup;
9
use Wikibase\Lib\Store\EntityRevisionLookup;
10
use Wikibase\Lib\Store\EntityTitleLookup;
11
use Wikibase\Repo\Dumpers\DumpGenerator;
12
use Wikibase\Repo\Dumpers\RdfDumpGenerator;
13
use Wikibase\Repo\Rdf\EntityRdfBuilderFactory;
14
use Wikibase\Repo\Rdf\RdfVocabulary;
15
use Wikibase\Repo\Rdf\ValueSnakRdfBuilderFactory;
16
use Wikibase\Repo\Store\Sql\SqlEntityIdPagerFactory;
17
use Wikibase\Repo\WikibaseRepo;
18
use Wikimedia\Purtle\BNodeLabeler;
19
20
require_once __DIR__ . '/DumpEntities.php';
21
22
/**
23
 * @license GPL-2.0-or-later
24
 * @author Stas Malyshev
25
 * @author Addshore
26
 */
27
class DumpRdf extends DumpEntities {
28
29
	/**
30
	 * @var EntityRevisionLookup
31
	 */
32
	private $revisionLookup;
33
34
	/**
35
	 * @var EntityPrefetcher
36
	 */
37
	private $entityPrefetcher;
38
39
	/**
40
	 * @var PropertyDataTypeLookup
41
	 */
42
	private $propertyDatatypeLookup;
43
44
	/**
45
	 * @var ValueSnakRdfBuilderFactory
46
	 */
47
	private $valueSnakRdfBuilderFactory;
48
49
	/**
50
	 * @var EntityRdfBuilderFactory
51
	 */
52
	private $entityRdfBuilderFactory;
53
54
	/**
55
	 * @var RdfVocabulary
56
	 */
57
	private $rdfVocabulary;
58
59
	/**
60
	 * @var bool
61
	 */
62
	private $hasHadServicesSet = false;
63
64
	/**
65
	 * @var EntityTitleLookup
66
	 */
67
	private $titleLookup;
68
69
	public function __construct() {
70
		parent::__construct();
71
		$this->addOption( 'format', 'Set the dump format, such as "nt" or "ttl". Defaults to "ttl".', false, true );
72
		$this->addOption(
73
			'flavor',
74
			'Set the flavor to produce. Can be either "full-dump" or "truthy-dump". Defaults to "full-dump".',
75
			false,
76
			true
77
		);
78
		$this->addOption( 'redirect-only', 'Whether to only dump information about redirects.', false, false );
79
		$this->addOption( 'part-id', 'Unique identifier for this part of multi-part dump, to be used for marking bnodes.', false, true );
80
	}
81
82
	public function setServices(
83
		SqlEntityIdPagerFactory $sqlEntityIdPagerFactory,
84
		array $existingEntityTypes,
85
		array $entityTypesWithoutRdfOutput,
86
		EntityPrefetcher $entityPrefetcher,
87
		PropertyDataTypeLookup $propertyDataTypeLookup,
88
		ValueSnakRdfBuilderFactory $valueSnakRdfBuilderFactory,
89
		EntityRdfBuilderFactory $entityRdfBuilderFactory,
90
		EntityRevisionLookup $entityRevisionLookup,
91
		RdfVocabulary $rdfVocabulary,
92
		$titleLookup
93
	) {
94
		parent::setDumpEntitiesServices(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setDumpEntitiesServices() instead of setServices()). Are you sure this is correct? If so, you might want to change this to $this->setDumpEntitiesServices().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
95
			$sqlEntityIdPagerFactory,
96
			$existingEntityTypes,
97
			$entityTypesWithoutRdfOutput
98
		);
99
		$this->entityPrefetcher = $entityPrefetcher;
100
		$this->propertyDatatypeLookup = $propertyDataTypeLookup;
101
		$this->valueSnakRdfBuilderFactory = $valueSnakRdfBuilderFactory;
102
		$this->entityRdfBuilderFactory = $entityRdfBuilderFactory;
103
		$this->revisionLookup = $entityRevisionLookup;
104
		$this->rdfVocabulary = $rdfVocabulary;
105
		$this->titleLookup = $titleLookup;
106
		$this->hasHadServicesSet = true;
107
	}
108
109
	public function execute() {
110
		if ( !$this->hasHadServicesSet ) {
111
			$wikibaseRepo = WikibaseRepo::getDefaultInstance();
112
			$sqlEntityIdPagerFactory = new SqlEntityIdPagerFactory(
113
				$wikibaseRepo->getEntityNamespaceLookup(),
114
				$wikibaseRepo->getEntityIdLookup(),
115
				MediaWikiServices::getInstance()->getLinkCache()
116
			);
117
118
			$this->setServices(
119
				$sqlEntityIdPagerFactory,
120
				$wikibaseRepo->getEnabledEntityTypes(),
121
				$wikibaseRepo->getSettings()->getSetting( 'entityTypesWithoutRdfOutput' ),
122
				$wikibaseRepo->getStore()->getEntityPrefetcher(),
123
				$wikibaseRepo->getPropertyDataTypeLookup(),
124
				$wikibaseRepo->getValueSnakRdfBuilderFactory(),
125
				$wikibaseRepo->getEntityRdfBuilderFactory(),
126
				$wikibaseRepo->getEntityRevisionLookup( $this->getEntityRevisionLookupCacheMode() ),
127
				$wikibaseRepo->getRdfVocabulary(),
128
				$wikibaseRepo->getEntityContentFactory()
129
			);
130
		}
131
		parent::execute();
132
	}
133
134
	/**
135
	 * Returns one of the EntityIdPager::XXX_REDIRECTS constants.
136
	 *
137
	 * @return mixed a EntityIdPager::XXX_REDIRECTS constant
138
	 */
139
	protected function getRedirectMode() {
140
		$redirectOnly = $this->getOption( 'redirect-only', false );
141
142
		if ( $redirectOnly ) {
143
			return EntityIdPager::ONLY_REDIRECTS;
144
		} else {
145
			return EntityIdPager::INCLUDE_REDIRECTS;
146
		}
147
	}
148
149
	/**
150
	 * Create concrete dumper instance
151
	 *
152
	 * @param resource $output
153
	 *
154
	 * @return DumpGenerator
155
	 */
156
	protected function createDumper( $output ) {
157
		$flavor = $this->getOption( 'flavor', 'full-dump' );
158
		if ( !in_array( $flavor, [ 'full-dump', 'truthy-dump' ] ) ) {
159
			$this->fatalError( 'Invalid flavor: ' . $flavor );
160
		}
161
162
		$labeler = null;
163
		$partId = $this->getOption( 'part-id' );
164
		if ( $partId ) {
165
			$labeler = new BNodeLabeler( "genid{$partId}-" );
166
		}
167
168
		if ( $this->getOption( 'sharding-factor', 1 ) !== 1 ) {
169
			$shard = $this->getOption( 'shard', 0 );
170
			if ( !$labeler ) {
171
				// Mark this shard's bnodes with shard ID if not told otherwise
172
				$labeler = new BNodeLabeler( "genid{$shard}-" );
173
			}
174
		}
175
176
		return RdfDumpGenerator::createDumpGenerator(
177
			$this->getOption( 'format', 'ttl' ),
178
			$output,
179
			$flavor,
180
			$this->revisionLookup,
181
			$this->propertyDatatypeLookup,
182
			$this->valueSnakRdfBuilderFactory,
183
			$this->entityRdfBuilderFactory,
184
			$this->entityPrefetcher,
185
			$this->rdfVocabulary,
186
			$this->titleLookup,
187
			$labeler
188
		);
189
	}
190
191
}
192
193
$maintClass = DumpRdf::class;
194
require_once RUN_MAINTENANCE_IF_MAIN;
195