WMDE-leszek has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/339654 )
Change subject: Refactor foreign EntityId parsing in LinkBeginHookHandler
......................................................................
Refactor foreign EntityId parsing in LinkBeginHookHandler
Main reason to touch this code was the fact that two methods implemented
the string "Special:EntityPage/".
I also removed the "lookup as a local ID using the $this->entityIdLookup
service" from the method, and moved this out. What's left is a method that
exclusively deals with foreign IDs.
This should not change any behavior, this is pure refactoring.
Bug: T157004
Change-Id: I26893ee1c0bc0cc26b5c38db5c56ec271e059c56
---
M repo/includes/Hooks/LinkBeginHookHandler.php
1 file changed, 26 insertions(+), 31 deletions(-)
Approvals:
Aleksey Bekh-Ivanov (WMDE): Looks good to me, approved
jenkins-bot: Verified
Thiemo Mättig (WMDE): Looks good to me, approved
diff --git a/repo/includes/Hooks/LinkBeginHookHandler.php
b/repo/includes/Hooks/LinkBeginHookHandler.php
index 439bf26..9f45378 100644
--- a/repo/includes/Hooks/LinkBeginHookHandler.php
+++ b/repo/includes/Hooks/LinkBeginHookHandler.php
@@ -171,8 +171,8 @@
$out = $context->getOutput();
$outTitle = $out->getTitle();
- $targetIsForeignEntityPage = $this->isForeignEntityPage(
$target );
- $isLocal = !$targetIsForeignEntityPage;
+ $foreignEntityId = $this->parseForeignEntityId( $target );
+ $isLocal = !$foreignEntityId;
if ( $isLocal
&& !$this->entityNamespaceLookup->isEntityNamespace(
$target->getNamespace() )
@@ -215,7 +215,7 @@
return;
}
- $entityId = $this->getEntityIdFromTarget( $target );
+ $entityId = $foreignEntityId ?:
$this->entityIdLookup->getEntityIdForTitle( $target );
if ( !$entityId ) {
return;
@@ -252,38 +252,18 @@
/**
* @param LinkTarget $target
*
- * @return bool
+ * @return EntityId|null
*/
- private function isForeignEntityPage( LinkTarget $target ) {
+ private function parseForeignEntityId( LinkTarget $target ) {
$interwiki = $target->getInterwiki();
if ( $interwiki === '' ||
!$this->interwikiLookup->isValidInterwiki( $interwiki ) ) {
- return false;
+ return null;
}
- return $this->startsWith( $target->getText(),
'Special:EntityPage/' );
- }
+ $idPart = $this->extractForeignIdString( $target->getText() );
- /**
- * @param string $haystack
- * @param string $needle
- *
- * @return bool
- */
- private function startsWith( $haystack, $needle ) {
- return strncmp( $haystack, $needle, strlen( $needle ) ) === 0;
- }
-
- /**
- * @param Title $target
- *
- * @return null|EntityId
- */
- private function getEntityIdFromTarget( Title $target ) {
- if ( $this->isForeignEntityPage( $target ) ) {
- $interwiki = $target->getInterwiki();
- $idPart = substr( $target->getText(), strlen(
'Special:EntityPage/' ) );
-
+ if ( $idPart !== null ) {
try {
// FIXME: This assumes repository name is equal
to interwiki. This assumption might
// become invalid
@@ -292,11 +272,26 @@
);
} catch ( EntityIdParsingException $ex ) {
}
-
- return null;
}
- return $this->entityIdLookup->getEntityIdForTitle( $target );
+ return null;
+ }
+
+ /**
+ * @param string $pageName
+ *
+ * @return string|null
+ */
+ private function extractForeignIdString( $pageName ) {
+ // FIXME: This encodes knowledge from
EntityContentFactory::getTitleForId
+ $prefix = 'Special:EntityPage/';
+ $prefixLength = 19;
+
+ if ( strncmp( $pageName, $prefix, $prefixLength ) === 0 ) {
+ return substr( $pageName, $prefixLength );
+ }
+
+ return null;
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/339654
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I26893ee1c0bc0cc26b5c38db5c56ec271e059c56
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits