Thiemo Mättig (WMDE) has uploaded a new change for review.
https://gerrit.wikimedia.org/r/155006
Change subject: Improve code readability in EntityContent and related
......................................................................
Improve code readability in EntityContent and related
This clean-up is partly split from I9523656 (without the new status).
I hope this makes the code more readable.
Change-Id: If488df0d010df4da343b55ca351300b337dd0a10
---
M repo/includes/content/EntityContent.php
M repo/includes/content/ItemContent.php
M repo/tests/phpunit/includes/content/EntityContentTest.php
3 files changed, 32 insertions(+), 65 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/06/155006/1
diff --git a/repo/includes/content/EntityContent.php
b/repo/includes/content/EntityContent.php
index 54ecce8..ececba7 100644
--- a/repo/includes/content/EntityContent.php
+++ b/repo/includes/content/EntityContent.php
@@ -64,6 +64,7 @@
/**
* For use in the wb-status page property to indicate that the entity
is empty.
+ *
* @see getEntityStatus()
*/
const STATUS_EMPTY = 200;
@@ -76,26 +77,15 @@
* @see Content::isValid()
*/
public function isValid() {
-
if ( $this->isRedirect() ) {
-
// Under some circumstances, the handler will not
support redirects,
// but it's still possible to construct Content objects
that represent
// redirects. In such a case, make sure such Content
objects are considered
// invalid and do not get saved.
-
- if ( !$this->getContentHandler()->supportsRedirects() )
{
- return false;
- }
-
- return true;
+ return $this->getContentHandler()->supportsRedirects();
}
- if ( is_null( $this->getEntity()->getId() ) ) {
- return false;
- }
-
- return true;
+ return $this->getEntity()->getId() !== null;
}
/**
@@ -302,25 +292,22 @@
$context = RequestContext::getMain();
}
- // determine output language ----
- $langCode = $context->getLanguage()->getCode();
+ $languageCode = $context->getLanguage()->getCode();
if ( $options !== null ) {
// NOTE: Parser Options language overrides context
language!
- $langCode = $options->getUserLang();
+ $languageCode = $options->getUserLang();
}
- // make formatter options ----
$formatterOptions = new FormatterOptions();
- $formatterOptions->setOption( ValueFormatter::OPT_LANG,
$langCode );
+ $formatterOptions->setOption( ValueFormatter::OPT_LANG,
$languageCode );
// Force the context's language to be the one specified by the
parser options.
- if ( $context && $context->getLanguage()->getCode() !==
$langCode ) {
+ if ( $context && $context->getLanguage()->getCode() !==
$languageCode ) {
$context = clone $context;
- $context->setLanguage( $langCode );
+ $context->setLanguage( $languageCode );
}
- // apply language fallback chain ----
if ( !$uiLanguageFallbackChain ) {
$factory =
WikibaseRepo::getDefaultInstance()->getLanguageFallbackChainFactory();
$uiLanguageFallbackChain =
$factory->newFromContextForPageView( $context );
@@ -337,9 +324,8 @@
$entityContentFactory =
WikibaseRepo::getDefaultInstance()->getEntityContentFactory();
$idParser = new BasicEntityIdParser();
- $options = $this->makeSerializationOptions( $langCode,
$uiLanguageFallbackChain );
+ $options = $this->makeSerializationOptions( $languageCode,
$uiLanguageFallbackChain );
- // construct the instance ----
$entityView = $this->newEntityView(
$context,
$snakFormatter,
@@ -389,10 +375,8 @@
wfProfileIn( __METHOD__ );
- $entity = $this->getEntity();
-
$searchTextGenerator = new EntitySearchTextGenerator();
- $text = $searchTextGenerator->generate( $entity );
+ $text = $searchTextGenerator->generate( $this->getEntity() );
wfProfileOut( __METHOD__ );
return $text;
@@ -485,12 +469,12 @@
public function getTextForSummary( $maxLength = 250 ) {
if ( $this->isRedirect() ) {
return $this->getRedirectText();
- } else {
- /** @var Language $language */
- $language = $GLOBALS['wgLang'];
- $text = $this->getEntity()->getDescription(
$language->getCode() );
- return substr( $text, 0, $maxLength );
}
+
+ /* @var Language $language */
+ $language = $GLOBALS['wgLang'];
+ $description = $this->getEntity()->getDescription(
$language->getCode() );
+ return substr( $description, 0, $maxLength );
}
/**
@@ -548,19 +532,11 @@
* @see Content::equals
*/
public function equals( Content $that = null ) {
- if ( is_null( $that ) ) {
- return false;
- }
-
if ( $that === $this ) {
return true;
}
- if ( $that->getModel() !== $this->getModel() ) {
- return false;
- }
-
- if ( !( $that instanceof EntityContent ) ) {
+ if ( !( $that instanceof EntityContent ) || $that->getModel()
!== $this->getModel() ) {
return false;
}
@@ -734,8 +710,7 @@
if ( $this->isRedirect() ) {
return $handler->makeEntityRedirectContent(
$this->getEntityRedirect() );
} else {
- $entity = $this->getEntity()->copy();
- return $handler->makeEntityContent( $entity );
+ return $handler->makeEntityContent(
$this->getEntity()->copy() );
}
}
@@ -767,16 +742,16 @@
}
/**
- * @param string $langCode
+ * @param string $languageCode
* @param LanguageFallbackChain $fallbackChain
*
* @return SerializationOptions
*/
- private function makeSerializationOptions( $langCode,
LanguageFallbackChain $fallbackChain ) {
- $langCodes = Utils::getLanguageCodes() + array( $langCode =>
$fallbackChain );
+ private function makeSerializationOptions( $languageCode,
LanguageFallbackChain $fallbackChain ) {
+ $languageCodes = Utils::getLanguageCodes() + array(
$languageCode => $fallbackChain );
$options = new SerializationOptions();
- $options->setLanguages( $langCodes );
+ $options->setLanguages( $languageCodes );
return $options;
}
@@ -813,10 +788,8 @@
return array();
}
- $entity = $this->getEntity();
-
$properties = array(
- 'wb-claims' => count( $entity->getClaims() ),
+ 'wb-claims' => count( $this->getEntity()->getClaims() ),
);
$status = $this->getEntityStatus();
@@ -836,9 +809,9 @@
* @note Will fail if this ItemContent is a redirect.
*
* @see getEntityPageProperties()
- * @see STATUS_NONE
- * @see STATUS_EMPTY
- * @see STATUS_STUB
+ * @see EntityContent::STATUS_NONE
+ * @see EntityContent::STATUS_STUB
+ * @see EntityContent::STATUS_EMPTY
*
* @return int
*/
diff --git a/repo/includes/content/ItemContent.php
b/repo/includes/content/ItemContent.php
index 48960f6..e064496 100644
--- a/repo/includes/content/ItemContent.php
+++ b/repo/includes/content/ItemContent.php
@@ -185,10 +185,9 @@
}
wfProfileIn( __METHOD__ );
- $item = $this->getItem();
$searchTextGenerator = new ItemSearchTextGenerator();
- $text = $searchTextGenerator->generate( $item );
+ $text = $searchTextGenerator->generate( $this->getItem() );
wfProfileOut( __METHOD__ );
return $text;
@@ -250,14 +249,10 @@
return array();
}
- $item = $this->getItem();
+ $properties = parent::getEntityPageProperties();
+ $properties['wb-sitelinks'] =
$this->getItem()->getSiteLinkList()->count();
- return array_merge(
- parent::getEntityPageProperties(),
- array(
- 'wb-sitelinks' =>
$item->getSiteLinkList()->count(),
- )
- );
+ return $properties;
}
/**
diff --git a/repo/tests/phpunit/includes/content/EntityContentTest.php
b/repo/tests/phpunit/includes/content/EntityContentTest.php
index 90ed889..f05156d 100644
--- a/repo/tests/phpunit/includes/content/EntityContentTest.php
+++ b/repo/tests/phpunit/includes/content/EntityContentTest.php
@@ -8,16 +8,14 @@
use ParserOptions;
use RequestContext;
use Title;
-use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Entity\Entity;
-use Wikibase\DataModel\Term\Term;
-use Wikibase\Lib\Store\EntityRedirect;
use Wikibase\DataModel\Entity\EntityDiff;
-use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Term\Term;
use Wikibase\EntityContent;
use Wikibase\LanguageFallbackChain;
use Wikibase\LanguageWithConversion;
+use Wikibase\Lib\Store\EntityRedirect;
use Wikibase\Lib\Store\EntityStore;
use Wikibase\Repo\Content\EntityContentDiff;
use Wikibase\Repo\WikibaseRepo;
@@ -509,4 +507,5 @@
$this->assertNotNull( $content->getRedirectTarget() );
}
}
+
}
--
To view, visit https://gerrit.wikimedia.org/r/155006
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If488df0d010df4da343b55ca351300b337dd0a10
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits