Skip to content

Commit eb66561

Browse files
manickithiemowmde
authored andcommitted
Sync with 7.3: Optimize EntityId::getRepositoryName and ::getLocalPart (#769)
Cherry-picked from 7.x branch
1 parent 85cf116 commit eb66561

5 files changed

Lines changed: 70 additions & 8 deletions

File tree

RELEASE-NOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ This release removes integers as acceptable entity IDs.
1818
* Un-deprecated several sitelink related shortcuts from `Item`: `addSiteLink`, `getSiteLink`,
1919
`hasLinkToSite`, and `removeSiteLink`.
2020

21+
## Version 7.3.0 (2017-11-13)
22+
23+
* Performance optimizations on `EntityId`:
24+
* Added protected `$repositoryName` and `$localPart` properties
25+
* Added protected `extractRepositoryNameAndLocalPart`
26+
2127
## Version 7.2.0 (2017-10-23)
2228

2329
* Performance optimizations on methods critical for dump generation:

src/Entity/EntityId.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,34 @@ abstract class EntityId implements Comparable, Serializable {
1616

1717
protected $serialization;
1818

19+
/**
20+
* @since 7.3
21+
*
22+
* @var string
23+
*/
24+
protected $repositoryName;
25+
26+
/**
27+
* @since 7.3
28+
*
29+
* @var string
30+
*/
31+
protected $localPart;
32+
1933
const PATTERN = '/^:?(\w+:)*[^:]+\z/';
2034

2135
/**
2236
* @since 6.2
2337
*
2438
* @param string $serialization
39+
*
40+
* @throws InvalidArgumentException
2541
*/
2642
public function __construct( $serialization ) {
2743
self::assertValidSerialization( $serialization );
2844
$this->serialization = self::normalizeIdSerialization( $serialization );
45+
46+
list ( $this->repositoryName, $this->localPart ) = self::extractRepositoryNameAndLocalPart( $serialization );
2947
}
3048

3149
private static function assertValidSerialization( $serialization ) {
@@ -63,12 +81,31 @@ public function getSerialization() {
6381
* @since 6.2
6482
*
6583
* @param string $serialization
84+
*
85+
* @throws InvalidArgumentException
6686
* @return string[] Array containing the serialization split into 3 parts.
6787
*/
6888
public static function splitSerialization( $serialization ) {
6989
self::assertValidSerialization( $serialization );
7090

71-
$parts = explode( ':', self::normalizeIdSerialization( $serialization ) );
91+
return self::extractSerializationParts( self::normalizeIdSerialization( $serialization ) );
92+
}
93+
94+
/**
95+
* Splits the given ID serialization into an array with the following three elements:
96+
* - the repository name as the first element (empty string for local repository)
97+
* - any parts of the ID serialization but the repository name and the local ID (if any, empty string
98+
* if nothing else present)
99+
* - the local ID
100+
* Note: this method does not perform any validation of the given input. Calling code should take
101+
* care of this!
102+
*
103+
* @param string $serialization
104+
*
105+
* @return string[]
106+
*/
107+
private static function extractSerializationParts( $serialization ) {
108+
$parts = explode( ':', $serialization );
72109
$localPart = array_pop( $parts );
73110
$repoName = array_shift( $parts );
74111
$prefixRemainder = implode( ':', $parts );
@@ -86,9 +123,9 @@ public static function splitSerialization( $serialization ) {
86123
* @since 6.2
87124
*
88125
* @param string[] $parts
89-
* @return string
90126
*
91127
* @throws InvalidArgumentException
128+
* @return string
92129
*/
93130
public static function joinSerialization( array $parts ) {
94131
if ( end( $parts ) === '' ) {
@@ -112,9 +149,7 @@ public static function joinSerialization( array $parts ) {
112149
* @return string
113150
*/
114151
public function getRepositoryName() {
115-
$parts = self::splitSerialization( $this->serialization );
116-
117-
return $parts[0];
152+
return $this->repositoryName;
118153
}
119154

120155
/**
@@ -125,9 +160,7 @@ public function getRepositoryName() {
125160
* @return string
126161
*/
127162
public function getLocalPart() {
128-
$parts = self::splitSerialization( $this->serialization );
129-
130-
return self::joinSerialization( [ '', $parts[1], $parts[2] ] );
163+
return $this->localPart;
131164
}
132165

133166
/**
@@ -144,6 +177,7 @@ public function isForeign() {
144177

145178
/**
146179
* @param string $id
180+
*
147181
* @return string
148182
*/
149183
private static function normalizeIdSerialization( $id ) {
@@ -179,4 +213,20 @@ public function equals( $target ) {
179213
&& $target->serialization === $this->serialization;
180214
}
181215

216+
/**
217+
* Returns a pair (repository name, local part of ID) from the given ID serialization.
218+
* Note: this does not perform any validation of the given input. Calling code should take
219+
* care of this!
220+
*
221+
* @since 7.3
222+
*
223+
* @param string $serialization
224+
*
225+
* @return string[] Array of form [ string $repositoryName, string $localPart ]
226+
*/
227+
protected static function extractRepositoryNameAndLocalPart( $serialization ) {
228+
$parts = explode( ':', $serialization, 2 );
229+
return isset( $parts[1] ) ? $parts : [ '', $parts[0] ];
230+
}
231+
182232
}

src/Entity/ItemId.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function serialize() {
8383
public function unserialize( $serialized ) {
8484
$array = json_decode( $serialized );
8585
$this->serialization = is_array( $array ) ? $array[1] : $serialized;
86+
list ( $this->repositoryName, $this->localPart ) =
87+
self::extractRepositoryNameAndLocalPart( $this->serialization );
8688
}
8789

8890
/**

src/Entity/PropertyId.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function serialize() {
8383
public function unserialize( $serialized ) {
8484
$array = json_decode( $serialized );
8585
$this->serialization = is_array( $array ) ? $array[1] : $serialized;
86+
list ( $this->repositoryName, $this->localPart ) =
87+
self::extractRepositoryNameAndLocalPart( $this->serialization );
8688
}
8789

8890
/**

tests/fixtures/CustomEntityId.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function serialize() {
2828
*/
2929
public function unserialize( $serialized ) {
3030
$this->serialization = $serialized;
31+
$this->repositoryName = '';
32+
$this->localPart = $serialized;
3133
}
3234

3335
/**

0 commit comments

Comments
 (0)