SpecialSetDescription::factory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\Repo\Specials;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\DataModel\Term\DescriptionsProvider;
8
use Wikibase\Lib\Store\EntityTitleLookup;
9
use Wikibase\Lib\Summary;
10
use Wikibase\Repo\ChangeOp\ChangeOps;
11
use Wikibase\Repo\CopyrightMessageBuilder;
12
use Wikibase\Repo\EditEntity\MediawikiEditEntityFactory;
13
use Wikibase\Repo\Store\EntityPermissionChecker;
14
use Wikibase\Repo\SummaryFormatter;
15
use Wikibase\Repo\WikibaseRepo;
16
17
/**
18
 * Special page for setting the description of a Wikibase entity.
19
 *
20
 * @license GPL-2.0-or-later
21
 * @author Bene* < [email protected] >
22
 */
23
class SpecialSetDescription extends SpecialModifyTerm {
0 ignored issues
show
Bug introduced by
There is one abstract method getName in this class; you could implement it, or declare this class as abstract.
Loading history...
24
25
	public function __construct(
26
		SpecialPageCopyrightView $copyrightView,
27
		SummaryFormatter $summaryFormatter,
28
		EntityTitleLookup $entityTitleLookup,
29
		MediawikiEditEntityFactory $editEntityFactory,
30
		EntityPermissionChecker $entityPermissionChecker
31
	) {
32
		parent::__construct(
33
			'SetDescription',
34
			$copyrightView,
35
			$summaryFormatter,
36
			$entityTitleLookup,
37
			$editEntityFactory,
38
			$entityPermissionChecker
39
		);
40
	}
41
42
	public static function factory(): self {
43
		$wikibaseRepo = WikibaseRepo::getDefaultInstance();
44
45
		$settings = $wikibaseRepo->getSettings();
46
		$copyrightView = new SpecialPageCopyrightView(
47
			new CopyrightMessageBuilder(),
48
			$settings->getSetting( 'dataRightsUrl' ),
49
			$settings->getSetting( 'dataRightsText' )
50
		);
51
52
		return new self(
53
			$copyrightView,
54
			$wikibaseRepo->getSummaryFormatter(),
55
			$wikibaseRepo->getEntityTitleLookup(),
56
			$wikibaseRepo->newEditEntityFactory(),
57
			$wikibaseRepo->getEntityPermissionChecker()
58
		);
59
	}
60
61
	public function doesWrites() {
62
		return true;
63
	}
64
65
	/**
66
	 * @see SpecialModifyTerm::validateInput
67
	 *
68
	 * @return bool
69
	 */
70
	protected function validateInput() {
71
		if ( !parent::validateInput() ) {
72
			return false;
73
		}
74
75
		return $this->getBaseRevision()->getEntity() instanceof DescriptionsProvider;
76
	}
77
78
	/**
79
	 * @see SpecialModifyTerm::getPostedValue()
80
	 *
81
	 * @return string|null
82
	 */
83
	protected function getPostedValue() {
84
		return $this->getRequest()->getVal( 'description' );
85
	}
86
87
	/**
88
	 * @see SpecialModifyTerm::getValue()
89
	 *
90
	 * @param EntityDocument $entity
91
	 * @param string $languageCode
92
	 *
93
	 * @throws InvalidArgumentException
94
	 * @return string
95
	 */
96
	protected function getValue( EntityDocument $entity, $languageCode ) {
97
		if ( !( $entity instanceof DescriptionsProvider ) ) {
98
			throw new InvalidArgumentException( '$entity must be a DescriptionsProvider' );
99
		}
100
101
		$descriptions = $entity->getDescriptions();
102
103
		if ( $descriptions->hasTermForLanguage( $languageCode ) ) {
104
			return $descriptions->getByLanguage( $languageCode )->getText();
105
		}
106
107
		return '';
108
	}
109
110
	/**
111
	 * @see SpecialModifyTerm::setValue()
112
	 *
113
	 * @param EntityDocument $entity
114
	 * @param string $languageCode
115
	 * @param string $value
116
	 *
117
	 * @return Summary
118
	 */
119
	protected function setValue( EntityDocument $entity, $languageCode, $value ) {
120
		$value = $value === '' ? null : $value;
121
		$summary = new Summary( 'wbsetdescription' );
122
123
		if ( $value === null ) {
124
			$changeOp = $this->termChangeOpFactory->newRemoveDescriptionOp( $languageCode );
125
		} else {
126
			$changeOp = $this->termChangeOpFactory->newSetDescriptionOp( $languageCode, $value );
127
		}
128
129
		$fingerprintChangeOp = $this->termChangeOpFactory->newFingerprintChangeOp( new ChangeOps( [ $changeOp ] ) );
130
131
		$this->applyChangeOp( $fingerprintChangeOp, $entity, $summary );
132
133
		return $summary;
134
	}
135
136
}
137