Thiemo Mättig (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/176655
Change subject: Move all Hooks to Hooks folder and namespace ...................................................................... Move all Hooks to Hooks folder and namespace Change-Id: Icdc745fcfc5e8eefe9c9ea9506f6346f9664250b --- R client/includes/Hooks/BaseTemplateAfterPortletHandler.php R client/includes/Hooks/BeforePageDisplayHandler.php R client/includes/Hooks/ChangesPageWikibaseFilterHandler.php R client/includes/Hooks/DataUpdateHookHandlers.php R client/includes/Hooks/DeletePageNoticeCreator.php R client/includes/Hooks/InfoActionHookHandler.php R client/includes/Hooks/LanguageLinkBadgeDisplay.php R client/includes/Hooks/MovePageNotice.php R client/includes/Hooks/OtherProjectsSidebarGenerator.php R client/includes/Hooks/ParserFunctionRegistrant.php R client/includes/Hooks/SidebarHookHandlers.php R client/includes/Hooks/SpecialWatchlistQueryHandler.php M client/includes/Usage/Sql/SqlUsageTrackerSchemaUpdate.php M client/tests/phpunit/includes/hooks/DeletePageNoticeCreatorTest.php M client/tests/phpunit/includes/hooks/MovePageNoticeTest.php M repo/Wikibase.php A repo/includes/Hooks/LinkBeginHookHandler.php R repo/includes/Hooks/OutputPageJsConfigHookHandler.php M repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php M repo/tests/phpunit/includes/Hook/OutputPageJsConfigHookHandlerTest.php 20 files changed, 247 insertions(+), 12 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/55/176655/1 diff --git a/client/includes/hooks/BaseTemplateAfterPortletHandler.php b/client/includes/Hooks/BaseTemplateAfterPortletHandler.php similarity index 100% rename from client/includes/hooks/BaseTemplateAfterPortletHandler.php rename to client/includes/Hooks/BaseTemplateAfterPortletHandler.php diff --git a/client/includes/hooks/BeforePageDisplayHandler.php b/client/includes/Hooks/BeforePageDisplayHandler.php similarity index 100% rename from client/includes/hooks/BeforePageDisplayHandler.php rename to client/includes/Hooks/BeforePageDisplayHandler.php diff --git a/client/includes/hooks/ChangesPageWikibaseFilterHandler.php b/client/includes/Hooks/ChangesPageWikibaseFilterHandler.php similarity index 100% rename from client/includes/hooks/ChangesPageWikibaseFilterHandler.php rename to client/includes/Hooks/ChangesPageWikibaseFilterHandler.php diff --git a/client/includes/hooks/DataUpdateHookHandlers.php b/client/includes/Hooks/DataUpdateHookHandlers.php similarity index 100% rename from client/includes/hooks/DataUpdateHookHandlers.php rename to client/includes/Hooks/DataUpdateHookHandlers.php diff --git a/client/includes/hooks/DeletePageNoticeCreator.php b/client/includes/Hooks/DeletePageNoticeCreator.php similarity index 96% rename from client/includes/hooks/DeletePageNoticeCreator.php rename to client/includes/Hooks/DeletePageNoticeCreator.php index f1c4cb6..04af59f 100644 --- a/client/includes/hooks/DeletePageNoticeCreator.php +++ b/client/includes/Hooks/DeletePageNoticeCreator.php @@ -1,9 +1,10 @@ <?php -namespace Wikibase\Client; +namespace Wikibase\Client\Hooks; use Html; use Title; +use Wikibase\Client\RepoLinker; use Wikibase\DataModel\SiteLink; use Wikibase\Lib\Store\SiteLinkLookup; diff --git a/client/includes/hooks/InfoActionHookHandler.php b/client/includes/Hooks/InfoActionHookHandler.php similarity index 100% rename from client/includes/hooks/InfoActionHookHandler.php rename to client/includes/Hooks/InfoActionHookHandler.php diff --git a/client/includes/hooks/LanguageLinkBadgeDisplay.php b/client/includes/Hooks/LanguageLinkBadgeDisplay.php similarity index 100% rename from client/includes/hooks/LanguageLinkBadgeDisplay.php rename to client/includes/Hooks/LanguageLinkBadgeDisplay.php diff --git a/client/includes/hooks/MovePageNotice.php b/client/includes/Hooks/MovePageNotice.php similarity index 96% rename from client/includes/hooks/MovePageNotice.php rename to client/includes/Hooks/MovePageNotice.php index 1c0de37..004866c 100644 --- a/client/includes/hooks/MovePageNotice.php +++ b/client/includes/Hooks/MovePageNotice.php @@ -1,9 +1,10 @@ <?php -namespace Wikibase\Client; +namespace Wikibase\Client\Hooks; use Html; use Title; +use Wikibase\Client\RepoLinker; use Wikibase\DataModel\SiteLink; use Wikibase\Lib\Store\SiteLinkLookup; diff --git a/client/includes/hooks/OtherProjectsSidebarGenerator.php b/client/includes/Hooks/OtherProjectsSidebarGenerator.php similarity index 100% rename from client/includes/hooks/OtherProjectsSidebarGenerator.php rename to client/includes/Hooks/OtherProjectsSidebarGenerator.php diff --git a/client/includes/hooks/ParserFunctionRegistrant.php b/client/includes/Hooks/ParserFunctionRegistrant.php similarity index 100% rename from client/includes/hooks/ParserFunctionRegistrant.php rename to client/includes/Hooks/ParserFunctionRegistrant.php diff --git a/client/includes/hooks/SidebarHookHandlers.php b/client/includes/Hooks/SidebarHookHandlers.php similarity index 100% rename from client/includes/hooks/SidebarHookHandlers.php rename to client/includes/Hooks/SidebarHookHandlers.php diff --git a/client/includes/hooks/SpecialWatchlistQueryHandler.php b/client/includes/Hooks/SpecialWatchlistQueryHandler.php similarity index 100% rename from client/includes/hooks/SpecialWatchlistQueryHandler.php rename to client/includes/Hooks/SpecialWatchlistQueryHandler.php diff --git a/client/includes/Usage/Sql/SqlUsageTrackerSchemaUpdate.php b/client/includes/Usage/Sql/SqlUsageTrackerSchemaUpdate.php index 2c57056..1e0d4eb 100644 --- a/client/includes/Usage/Sql/SqlUsageTrackerSchemaUpdate.php +++ b/client/includes/Usage/Sql/SqlUsageTrackerSchemaUpdate.php @@ -29,6 +29,8 @@ * Static entry point for MediaWiki's LoadExtensionSchemaUpdates hook. * * @param DatabaseUpdater $dbUpdater + * + * @return bool */ public static function onSchemaUpdate( DatabaseUpdater $dbUpdater ) { if ( WikibaseClient::getDefaultInstance()->getSettings()->getSetting( 'useLegacyUsageIndex' ) ) { diff --git a/client/tests/phpunit/includes/hooks/DeletePageNoticeCreatorTest.php b/client/tests/phpunit/includes/hooks/DeletePageNoticeCreatorTest.php index 4b1aaa6..e82d96a 100644 --- a/client/tests/phpunit/includes/hooks/DeletePageNoticeCreatorTest.php +++ b/client/tests/phpunit/includes/hooks/DeletePageNoticeCreatorTest.php @@ -4,12 +4,12 @@ use Language; use Title; -use Wikibase\Client\DeletePageNoticeCreator; +use Wikibase\Client\Hooks\DeletePageNoticeCreator; use Wikibase\Client\RepoLinker; use Wikibase\DataModel\Entity\ItemId; /** - * @covers Wikibase\Client\DeletePageNoticeCreator + * @covers Wikibase\Client\Hooks\DeletePageNoticeCreator * * @group WikibaseClient * @group Wikibase diff --git a/client/tests/phpunit/includes/hooks/MovePageNoticeTest.php b/client/tests/phpunit/includes/hooks/MovePageNoticeTest.php index 61ba4d1..cbcfa2b 100644 --- a/client/tests/phpunit/includes/hooks/MovePageNoticeTest.php +++ b/client/tests/phpunit/includes/hooks/MovePageNoticeTest.php @@ -4,13 +4,13 @@ use Language; use Title; -use Wikibase\Client\MovePageNotice; +use Wikibase\Client\Hooks\MovePageNotice; use Wikibase\Client\RepoLinker; use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\SiteLink; /** - * @covers Wikibase\Client\MovePageNotice + * @covers Wikibase\Client\Hooks\MovePageNotice * * @group WikibaseClient * @group Wikibase diff --git a/repo/Wikibase.php b/repo/Wikibase.php index a5fdfaf..b6e9bad 100644 --- a/repo/Wikibase.php +++ b/repo/Wikibase.php @@ -195,7 +195,7 @@ $wgHooks['ArticleDeleteComplete'][] = 'Wikibase\RepoHooks::onArticleDeleteComplete'; $wgHooks['ArticleUndelete'][] = 'Wikibase\RepoHooks::onArticleUndelete'; $wgHooks['GetPreferences'][] = 'Wikibase\RepoHooks::onGetPreferences'; - $wgHooks['LinkBegin'][] = 'Wikibase\Repo\Hook\LinkBeginHookHandler::onLinkBegin'; + $wgHooks['LinkBegin'][] = 'Wikibase\Repo\Hooks\LinkBeginHookHandler::onLinkBegin'; $wgHooks['OutputPageBodyAttributes'][] = 'Wikibase\RepoHooks::onOutputPageBodyAttributes'; //FIXME: handle other types of entities with autocomments too! $wgHooks['FormatAutocomments'][] = array( 'Wikibase\RepoHooks::onFormat', array( CONTENT_MODEL_WIKIBASE_ITEM, "wikibase-item" ) ); diff --git a/repo/includes/Hooks/LinkBeginHookHandler.php b/repo/includes/Hooks/LinkBeginHookHandler.php new file mode 100644 index 0000000..0227115 --- /dev/null +++ b/repo/includes/Hooks/LinkBeginHookHandler.php @@ -0,0 +1,230 @@ +<?php + +namespace Wikibase\Repo\Hooks; + +use DummyLinker; +use Html; +use IContextSource; +use Language; +use MWContentSerializationException; +use RequestContext; +use Title; +use Wikibase\EntityContent; +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Repo\Content\EntityContentFactory; +use Wikibase\Repo\WikibaseRepo; +use WikiPage; + +/** + * @since 0.5 + * + * @licence GNU GPL v2+ + */ +class LinkBeginHookHandler { + + /** + * @var EntityContentFactory + */ + private $entityContentFactory; + + /** + * @var LanguageFallbackChainFactory + */ + private $languageFallbackChainFactory; + + /** + * @var IContextSource + */ + private $context; + + /** + * @return LinkBeginHookHandler + */ + public static function newFromGlobalState() { + $entityContentFactory = WikibaseRepo::getDefaultInstance()->getEntityContentFactory(); + $languageFallbackChainFactory = WikibaseRepo::getDefaultInstance()->getLanguageFallbackChainFactory(); + $context = RequestContext::getMain(); + + return new self( $entityContentFactory, $languageFallbackChainFactory, $context ); + } + + /** + * Special page handling where we want to display meaningful link labels instead of just the items ID. + * This is only handling special pages right now and gets disabled in normal pages. + * @see https://www.mediawiki.org/wiki/Manual:Hooks/LinkBegin + * + * @param DummyLinker $skin + * @param Title $target + * @param string $html + * @param array $customAttribs + * @param string $query + * @param array $options + * @param mixed $ret + * @return bool true + */ + public static function onLinkBegin( $skin, $target, &$html, array &$customAttribs, &$query, + &$options, &$ret + ) { + $handler = self::newFromGlobalState(); + return $handler->doOnLinkBegin( $target, $html, $customAttribs ); + } + + /** + * @param EntityContentFactory $entityContentFactory + * @param LanguageFallbackChainFactory $languageFallbackChainFactory + * @param IContextSource $context + */ + public function __construct( + EntityContentFactory $entityContentFactory, + LanguageFallbackChainFactory $languageFallbackChainFactory, + IContextSource $context + ) { + $this->entityContentFactory = $entityContentFactory; + $this->languageFallbackChainFactory = $languageFallbackChainFactory; + $this->context = $context; + } + + /** + * @param Title $target + * @param string &$html + * @param array &$customAttribs + * + * @return boolean + */ + public function doOnLinkBegin( Title $target, &$html, array &$customAttribs ) { + wfProfileIn( __METHOD__ ); + + if ( !$this->onSpecialPage() || !$this->isEntityContent( $target ) ) { + wfProfileOut( __METHOD__ ); + return true; + } + + // if custom link text is given, there is no point in overwriting it + // but not if it is similar to the plain title + if ( $html !== null && $target->getFullText() !== $html ) { + wfProfileOut( __METHOD__ ); + return true; + } + + $entity = $this->getEntityForTitle( $target ); + + if ( !$entity ) { + wfProfileOut( __METHOD__ ); + return true; + } + + // Try to find the most preferred available language to display data in current context. + $languageFallbackChain = $this->languageFallbackChainFactory->newFromContext( $this->context ); + + $labelData = $languageFallbackChain->extractPreferredValueOrAny( $entity->getLabels() ); + + if ( $labelData ) { + $labelText = $labelData['value']; + $labelLang = Language::factory( $labelData['language'] ); + } else { + $labelText = ''; + $labelLang = $this->context->getLanguage(); + } + + $descriptionData = $languageFallbackChain->extractPreferredValueOrAny( + $entity->getDescriptions() + ); + + $html = $this->getHtml( $target, $labelLang, $labelText ); + + $customAttribs['title'] = $this->getTitleAttribute( + $target, + $labelLang, + $labelText, + $descriptionData + ); + + // add wikibase styles in all cases, so we can format the link properly: + $this->context->getOutput()->addModuleStyles( array( 'wikibase.common' ) ); + + wfProfileOut( __METHOD__ ); + return true; + } + + private function onSpecialPage() { + // Title is temporarily set to special pages Title in case of special page inclusion! + // Therefore we can just check whether the page is a special page and + // if not, disable the behavior. + $currentTitle = $this->context->getTitle(); + + return $currentTitle !== null && $currentTitle->isSpecialPage(); + } + + private function isEntityContent( Title $title ) { + return $this->entityContentFactory->isEntityContentModel( $title->getContentModel() ); + } + + private function getEntityForTitle( Title $title ) { + $page = new WikiPage( $title ); + + try { + $content = $page->getContent(); + + // Failed, can't continue. This could happen because the content is empty + // (page doesn't exist), e.g. after item was deleted. + + // TODO: resolve redirect, show redirect info in link + if ( $content instanceof EntityContent && !$content->isRedirect() ) { + return $content->getEntity(); + } + } catch ( MWContentSerializationException $ex ) { + // if this fails, it's not horrible. + wfWarn( 'Failed to get entity object for [[' . $title->getFullText() . ']]' + . ': ' . $ex->getMessage() ); + } + } + + private function getHtml( Title $title, $labelLang, $labelText ) { + $idHtml = Html::openElement( 'span', array( 'class' => 'wb-itemlink-id' ) ) + . wfMessage( + 'wikibase-itemlink-id-wrapper', + $title->getText() + )->inContentLanguage()->escaped() + . Html::closeElement( 'span' ); + + $labelHtml = Html::openElement( 'span', array( + 'class' => 'wb-itemlink-label', + 'lang' => $labelLang->getHtmlCode(), + 'dir' => $labelLang->getDir() + ) ) + . htmlspecialchars( $labelText ) + . Html::closeElement( 'span' ); + + return Html::openElement( 'span', array( 'class' => 'wb-itemlink' ) ) + . wfMessage( 'wikibase-itemlink' )->rawParams( + $labelHtml, + $idHtml + )->inContentLanguage()->escaped() + . Html::closeElement( 'span' ); + } + + private function getTitleAttribute( Title $title, $labelLang, $labelText, $descriptionData ) { + if ( $descriptionData ) { + $descriptionText = $descriptionData['value']; + $descriptionLang = Language::factory( $descriptionData['language'] ); + } else { + $descriptionText = ''; + $descriptionLang = $this->context->getLanguage(); + } + + // Set title attribute for constructed link, and make tricks with the directionality to get it right + $titleText = ( $labelText !== '' ) + ? $labelLang->getDirMark() . $labelText . $this->context->getLanguage()->getDirMark() + : $title->getPrefixedText(); + + return ( $descriptionText !== '' ) ? + wfMessage( + 'wikibase-itemlink-title', + $titleText, + $descriptionLang->getDirMark() . $descriptionText + . $this->context->getLanguage()->getDirMark() + )->inContentLanguage()->text() : + $titleText; // no description, just display the title then + } + +} diff --git a/repo/includes/Hook/OutputPageJsConfigHookHandler.php b/repo/includes/Hooks/OutputPageJsConfigHookHandler.php similarity index 97% rename from repo/includes/Hook/OutputPageJsConfigHookHandler.php rename to repo/includes/Hooks/OutputPageJsConfigHookHandler.php index e242766..34a8f7b 100644 --- a/repo/includes/Hook/OutputPageJsConfigHookHandler.php +++ b/repo/includes/Hooks/OutputPageJsConfigHookHandler.php @@ -1,6 +1,6 @@ <?php -namespace Wikibase\Hook; +namespace Wikibase\Repo\Hooks; use OutputPage; use Wikibase\OutputPageJsConfigBuilder; diff --git a/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php b/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php index 0d48d8b..39bcba9 100644 --- a/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php +++ b/repo/tests/phpunit/includes/Hook/LinkBeginHookHandlerTest.php @@ -8,11 +8,11 @@ use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; use Wikibase\Lib\Store\EntityTitleLookup; -use Wikibase\Repo\Hook\LinkBeginHookHandler; +use Wikibase\Repo\Hooks\LinkBeginHookHandler; use Wikibase\Repo\WikibaseRepo; /** - * @covers Wikibase\Repo\Hook\LinkBeginHookHandler + * @covers Wikibase\Repo\Hooks\LinkBeginHookHandler * * @since 0.5 * @@ -185,4 +185,5 @@ ); } + } diff --git a/repo/tests/phpunit/includes/Hook/OutputPageJsConfigHookHandlerTest.php b/repo/tests/phpunit/includes/Hook/OutputPageJsConfigHookHandlerTest.php index dbec07c..bb0b88a 100644 --- a/repo/tests/phpunit/includes/Hook/OutputPageJsConfigHookHandlerTest.php +++ b/repo/tests/phpunit/includes/Hook/OutputPageJsConfigHookHandlerTest.php @@ -6,11 +6,11 @@ use Title; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\ItemId; -use Wikibase\Hook\OutputPageJsConfigHookHandler; +use Wikibase\Repo\Hooks\OutputPageJsConfigHookHandler; use Wikibase\Settings; /** - * @covers Wikibase\Hook\OutputPageJsConfigHookHandler + * @covers Wikibase\Repo\Hooks\OutputPageJsConfigHookHandler * * @since 0.5 * -- To view, visit https://gerrit.wikimedia.org/r/176655 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Icdc745fcfc5e8eefe9c9ea9506f6346f9664250b Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: Aude <aude.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits