SpecialSetAliases   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 12
dl 0
loc 146
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A doesWrites() 0 3 1
A validateInput() 0 7 2
A getPostedValue() 0 3 1
A getValue() 0 10 3
A setValue() 0 20 3
A assertNoPipeCharacterInAliases() 0 19 4
A factory() 0 18 1
1
<?php
2
3
namespace Wikibase\Repo\Specials;
4
5
use InvalidArgumentException;
6
use MediaWiki\Logger\LoggerFactory;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Term\AliasesProvider;
9
use Wikibase\Lib\Store\EntityTitleLookup;
10
use Wikibase\Lib\Summary;
11
use Wikibase\Lib\UserInputException;
12
use Wikibase\Repo\ChangeOp\ChangeOps;
13
use Wikibase\Repo\CopyrightMessageBuilder;
14
use Wikibase\Repo\EditEntity\MediawikiEditEntityFactory;
15
use Wikibase\Repo\Store\EntityPermissionChecker;
16
use Wikibase\Repo\SummaryFormatter;
17
use Wikibase\Repo\WikibaseRepo;
18
19
/**
20
 * Special page for setting the aliases of a Wikibase entity.
21
 *
22
 * @license GPL-2.0-or-later
23
 * @author Bene* < [email protected] >
24
 */
25
class SpecialSetAliases 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...
26
27
	public function __construct(
28
		SpecialPageCopyrightView $copyrightView,
29
		SummaryFormatter $summaryFormatter,
30
		EntityTitleLookup $entityTitleLookup,
31
		MediawikiEditEntityFactory $editEntityFactory,
32
		EntityPermissionChecker $entityPermissionChecker
33
	) {
34
		parent::__construct(
35
			'SetAliases',
36
			$copyrightView,
37
			$summaryFormatter,
38
			$entityTitleLookup,
39
			$editEntityFactory,
40
			$entityPermissionChecker
41
		);
42
	}
43
44
	public static function factory(): self {
45
		$wikibaseRepo = WikibaseRepo::getDefaultInstance();
46
47
		$settings = $wikibaseRepo->getSettings();
48
		$copyrightView = new SpecialPageCopyrightView(
49
			new CopyrightMessageBuilder(),
50
			$settings->getSetting( 'dataRightsUrl' ),
51
			$settings->getSetting( 'dataRightsText' )
52
		);
53
54
		return new self(
55
			$copyrightView,
56
			$wikibaseRepo->getSummaryFormatter(),
57
			$wikibaseRepo->getEntityTitleLookup(),
58
			$wikibaseRepo->newEditEntityFactory(),
59
			$wikibaseRepo->getEntityPermissionChecker()
60
		);
61
	}
62
63
	public function doesWrites() {
64
		return true;
65
	}
66
67
	/**
68
	 * @see SpecialModifyTerm::validateInput
69
	 *
70
	 * @return bool
71
	 */
72
	protected function validateInput() {
73
		if ( !parent::validateInput() ) {
74
			return false;
75
		}
76
77
		return $this->getBaseRevision()->getEntity() instanceof AliasesProvider;
78
	}
79
80
	/**
81
	 * @see SpecialModifyTerm::getPostedValue()
82
	 *
83
	 * @return string|null
84
	 */
85
	protected function getPostedValue() {
86
		return $this->getRequest()->getVal( 'aliases' );
87
	}
88
89
	/**
90
	 * @see SpecialModifyTerm::getValue()
91
	 *
92
	 * @param EntityDocument $entity
93
	 * @param string $languageCode
94
	 *
95
	 * @throws InvalidArgumentException
96
	 * @return string
97
	 */
98
	protected function getValue( EntityDocument $entity, $languageCode ) {
99
		if ( !( $entity instanceof AliasesProvider ) ) {
100
			throw new InvalidArgumentException( '$entity must be an AliasesProvider' );
101
		}
102
		$aliases = $entity->getAliasGroups();
103
		if ( $aliases->hasGroupForLanguage( $languageCode ) ) {
104
			return implode( '|', $aliases->getByLanguage( $languageCode )->getAliases() );
105
		}
106
		return '';
107
	}
108
109
	/**
110
	 * @see SpecialModifyTerm::setValue()
111
	 *
112
	 * @param EntityDocument $entity
113
	 * @param string $languageCode
114
	 * @param string $value
115
	 *
116
	 * @throws UserInputException|InvalidArgumentException
117
	 * @return Summary
118
	 * @suppress PhanTypeMismatchArgument
119
	 */
120
	protected function setValue( EntityDocument $entity, $languageCode, $value ) {
121
		if ( !( $entity instanceof AliasesProvider ) ) {
122
			throw new InvalidArgumentException( '$entity must be an AliasesProvider' );
123
		}
124
125
		$summary = new Summary( 'wbsetaliases' );
126
		if ( $value === '' ) {
127
			$aliases = $entity->getAliasGroups()->getByLanguage( $languageCode )->getAliases();
128
			$changeOp = $this->termChangeOpFactory->newRemoveAliasesOp( $languageCode, $aliases );
129
		} else {
130
			$this->assertNoPipeCharacterInAliases( $entity, $languageCode );
131
			$changeOp = $this->termChangeOpFactory->newSetAliasesOp( $languageCode, explode( '|', $value ) );
132
		}
133
134
		$fingerprintChangeOp = $this->termChangeOpFactory->newFingerprintChangeOp( new ChangeOps( [ $changeOp ] ) );
135
136
		$this->applyChangeOp( $fingerprintChangeOp, $entity, $summary );
137
138
		return $summary;
139
	}
140
141
	/**
142
	 * Screams and throws an error if any of existing aliases has pipe character
143
	 *
144
	 * @param EntityDocument $entity
145
	 * @param string $languageCode
146
	 *
147
	 * @throws UserInputException
148
	 * @suppress PhanTypeMismatchDeclaredParam Intersection type
149
	 */
150
	private function assertNoPipeCharacterInAliases( AliasesProvider $entity, $languageCode ) {
151
		$aliases = $entity->getAliasGroups();
152
		if ( !$aliases->hasGroupForLanguage( $languageCode ) ) {
153
			return;
154
		}
155
		$aliasesInLang = $entity->getAliasGroups()->getByLanguage( $languageCode )->getAliases();
156
157
		foreach ( $aliasesInLang as $alias ) {
158
			if ( strpos( $alias, '|' ) !== false ) {
159
				$logger = LoggerFactory::getInstance( 'Wikibase' );
160
				$logger->error( 'Special:SetAliases attempt to save pipes in aliases' );
161
				throw new UserInputException(
162
					'wikibase-wikibaserepopage-pipe-in-alias',
163
					[],
164
					$this->msg( 'wikibase-wikibaserepopage-pipe-in-alias' )->text()
165
				);
166
			}
167
		}
168
	}
169
170
}
171