Merge "[Hygiene] Reorganise hooks to reflect placements" into dev

This commit is contained in:
jenkins-bot 2015-12-02 22:42:50 +00:00 committed by Gerrit Code Review
commit 1ffd313910
4 changed files with 203 additions and 191 deletions

View file

@ -13,15 +13,20 @@
"type": "other",
"AutoloadClasses": {
"RelatedArticles\\Hooks": "includes/Hooks.php",
"RelatedArticles\\ReadMoreHooks": "includes/ReadMoreHooks.php"
"RelatedArticles\\SidebarHooks": "includes/SidebarHooks.php",
"RelatedArticles\\FooterHooks": "includes/FooterHooks.php"
},
"ExtensionMessagesFiles": {
"RelatedArticlesMagic": "RelatedArticles.i18n.magic.php"
},
"Hooks": {
"ResourceLoaderTestModules": [
"RelatedArticles\\ReadMoreHooks::onResourceLoaderTestModules"
"UnitTestsList": [
"RelatedArticles\\Hooks::onUnitTestsList"
],
"ResourceLoaderTestModules": [
"RelatedArticles\\Hooks::onResourceLoaderTestModules"
],
"ParserFirstCallInit": [
"RelatedArticles\\Hooks::onParserFirstCallInit"
],
@ -31,32 +36,31 @@
"OutputPageParserOutput": [
"RelatedArticles\\Hooks::onOutputPageParserOutput"
],
"@SkinBuildSidebar": [
"RelatedArticles\\Hooks::onSkinBuildSidebar"
"RelatedArticles\\SidebarHooks::onSkinBuildSidebar"
],
"SkinTemplateToolboxEnd": [
"RelatedArticles\\Hooks::onSkinTemplateToolboxEnd"
],
"UnitTestsList": [
"RelatedArticles\\Hooks::onUnitTestsList"
"RelatedArticles\\SidebarHooks::onSkinTemplateToolboxEnd"
],
"GetBetaFeaturePreferences": [
"RelatedArticles\\ReadMoreHooks::onGetBetaFeaturePreferences"
"RelatedArticles\\FooterHooks::onGetBetaFeaturePreferences"
],
"MakeGlobalVariablesScript": [
"RelatedArticles\\ReadMoreHooks::onMakeGlobalVariablesScript"
"RelatedArticles\\FooterHooks::onMakeGlobalVariablesScript"
],
"BeforePageDisplay": [
"RelatedArticles\\ReadMoreHooks::onBeforePageDisplay"
"RelatedArticles\\FooterHooks::onBeforePageDisplay"
],
"EventLoggingRegisterSchemas": [
"RelatedArticles\\ReadMoreHooks::onEventLoggingRegisterSchemas"
"RelatedArticles\\FooterHooks::onEventLoggingRegisterSchemas"
],
"ResourceLoaderGetConfigVars": [
"RelatedArticles\\ReadMoreHooks::onResourceLoaderGetConfigVars"
"RelatedArticles\\FooterHooks::onResourceLoaderGetConfigVars"
],
"ResourceLoaderRegisterModules": [
"RelatedArticles\\ReadMoreHooks::onResourceLoaderRegisterModules"
"RelatedArticles\\FooterHooks::onResourceLoaderRegisterModules"
]
},
"MessagesDirs": {

View file

@ -9,32 +9,7 @@ use Skin;
use ConfigFactory;
use User;
class ReadMoreHooks {
/**
* Register QUnit tests.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules
*
* @param array $modules
* @param ResourceLoader $rl
* @return bool
*/
public static function onResourceLoaderTestModules( &$modules, &$rl ) {
$boilerplate = array(
'localBasePath' => __DIR__ . '/../tests/qunit/',
'remoteExtPath' => 'RelatedArticles/tests/qunit',
'targets' => array( 'desktop', 'mobile' ),
);
$modules['qunit']['ext.relatedArticles.readMore.gateway.tests'] = $boilerplate + array(
'scripts' => array(
'ext.relatedArticles.readMore.gateway/test_RelatedPagesGateway.js',
),
'dependencies' => array(
'ext.relatedArticles.readMore.gateway',
),
);
return true;
}
class FooterHooks {
/**
* Handler for the <code>MakeGlobalVariablesScript</code> hook.

View file

@ -2,16 +2,9 @@
namespace RelatedArticles;
use ConfigFactory;
use Parser;
use Title;
use SkinTemplate;
use BaseTemplate;
use Skin;
use Html;
use OutputPage;
use ParserOutput;
use User;
class Hooks {
@ -104,138 +97,6 @@ class Hooks {
return true;
}
/**
* Generates anchor element attributes for each entry in list of pages.
*
* The attributes that are generated are: <code>href</code>,
* <code>text</code>, and <code>class</code>, with the latter always
* set to <code>"interwiki-relart"</code>.
*
* If the the page is of the form <code>"Foo && Bar"</code>, then
* the <code>text</code> attribute will be set to "Bar", otherwise the
* page's {@see Title::getPrefixedText prefixed text} will be used.
*
* @param array[string] $relatedPages
* @return array An array of maps, each with <code>href</code>,
* <code>text</code>, and <code>class</code> entries.
*/
private static function getRelatedPagesUrls( array $relatedPages ) {
$relatedPagesUrls = array();
foreach ( $relatedPages as $page ) {
// Tribute to Evan
$page = urldecode( $page );
$altText = '';
if ( preg_match( '/\&\&/', $page ) ) {
$parts = array_map( 'trim', explode( '&&', $page, 2 ) );
$page = $parts[0];
$altText = $parts[1];
}
$title = Title::newFromText( $page );
if ( $title ) {
$relatedPagesUrls[] = array(
'href' => $title->getLocalURL(),
'text' => $altText ?: $title->getPrefixedText(),
'class' => 'interwiki-relart'
);
}
};
return $relatedPagesUrls;
}
/**
* Handler for the <code>SkinBuildSidebar</code> hook.
*
* Retrieves the list of related pages
* and adds its HTML representation to the sidebar if the ReadMore feature
* is disabled and the beta feature is enabled by the user.
*
* @param Skin $skin
* @param array $bar
* @return boolean Always <code>true</code>
*/
public static function onSkinBuildSidebar( Skin $skin, &$bar ) {
$out = $skin->getOutput();
$relatedPages = $out->getProperty( 'RelatedArticles' );
if ( !Hooks::isInSidebar( $relatedPages, $out->getUser() ) ) {
return true;
}
$relatedPagesUrls = self::getRelatedPagesUrls( $relatedPages );
// build relatedarticles <li>'s
$relatedPages = array();
foreach ( (array) $relatedPagesUrls as $url ) {
$relatedPages[] =
Html::rawElement( 'li', array( 'class' => htmlspecialchars( $url['class'] ) ),
Html::element( 'a', array( 'href' => htmlspecialchars( $url['href'] ) ),
$url['text']
)
);
}
// build complete html
$bar[$skin->msg( 'relatedarticles-title' )->text()] =
Html::rawElement( 'ul', array(),
implode( '', $relatedPages )
);
return true;
}
/**
* Handler for the <code>SkinTemplateToolboxEnd</code> hook.
*
* Retrieves the list of related pages from the template and
* <code>echo</code>s its HTML representation to the sidebar if the
* ReadMore feature is disabled and the beta feature is enabled by the user.
*
* @param SkinTemplate $skinTpl
* @return boolean Always <code>true</code>
*/
public static function onSkinTemplateToolboxEnd( BaseTemplate &$skinTpl ) {
$out = $skinTpl->getSkin()->getOutput();
$relatedPages = $out->getProperty( 'RelatedArticles' );
if ( !Hooks::isInSidebar( $relatedPages, $out->getUser() ) ) {
return true;
}
$relatedPagesUrls = self::getRelatedPagesUrls( $relatedPages );
// build relatedarticles <li>'s
$relatedPages = array();
foreach ( (array) $relatedPagesUrls as $url ) {
$relatedPages[] =
Html::rawElement( 'li', array( 'class' => htmlspecialchars( $url['class'] ) ),
Html::element( 'a', array( 'href' => htmlspecialchars( $url['href'] ) ),
$url['text']
)
);
}
// build complete html
echo
Html::closeElement( 'ul' ) .
Html::closeElement( 'div' ) .
Html::closeElement( 'div' ) .
Html::openElement( 'div', array(
'class' => 'portal',
'role' => 'navigation',
'id' => 'p-relatedarticles',
) ) .
Html::element( 'h3', array(), wfMessage( 'relatedarticles-title' )->text() ) .
Html::openElement( 'div', array( 'class' => 'body' ) ) .
Html::openElement( 'ul' ) .
implode( '', $relatedPages );
return true;
}
/**
* Handler for the <code>UnitTestsList</code> hook.
*
@ -252,22 +113,28 @@ class Hooks {
}
/**
* Check whether there are related articles that can be displayed, or
* the ReadMore feature is disabled. The beta feature is used only
* for enabling ReadMore, so do not take it into account.
* Register QUnit tests.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules
*
* @param mixed|null $relatedPages
* @param User $user
* @param array $modules
* @param ResourceLoader $rl
* @return bool
* @throws \ConfigException
*/
private static function isInSidebar( $relatedPages, User $user ) {
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'RelatedArticles' );
if ( !$relatedPages || $config->get( 'RelatedArticlesShowReadMore' ) ) {
return false;
}
public static function onResourceLoaderTestModules( &$modules, &$rl ) {
$boilerplate = array(
'localBasePath' => __DIR__ . '/../tests/qunit/',
'remoteExtPath' => 'RelatedArticles/tests/qunit',
'targets' => array( 'desktop', 'mobile' ),
);
$modules['qunit']['ext.relatedArticles.readMore.gateway.tests'] = $boilerplate + array(
'scripts' => array(
'ext.relatedArticles.readMore.gateway/test_RelatedPagesGateway.js',
),
'dependencies' => array(
'ext.relatedArticles.readMore.gateway',
),
);
return true;
}
}

166
includes/SidebarHooks.php Normal file
View file

@ -0,0 +1,166 @@
<?php
namespace RelatedArticles;
use ConfigFactory;
use Title;
use SkinTemplate;
use BaseTemplate;
use Skin;
use Html;
use User;
class SidebarHooks {
/**
* Handler for the <code>SkinBuildSidebar</code> hook.
*
* Retrieves the list of related pages
* and adds its HTML representation to the sidebar if the ReadMore feature
* is disabled and the beta feature is enabled by the user.
*
* @param Skin $skin
* @param array $bar
* @return boolean Always <code>true</code>
*/
public static function onSkinBuildSidebar( Skin $skin, &$bar ) {
$out = $skin->getOutput();
$relatedPages = $out->getProperty( 'RelatedArticles' );
if ( !self::isInSidebar( $relatedPages, $out->getUser() ) ) {
return true;
}
$relatedPagesUrls = self::getRelatedPagesUrls( $relatedPages );
// build relatedarticles <li>'s
$relatedPages = array();
foreach ( (array) $relatedPagesUrls as $url ) {
$relatedPages[] =
Html::rawElement( 'li', array( 'class' => htmlspecialchars( $url['class'] ) ),
Html::element( 'a', array( 'href' => htmlspecialchars( $url['href'] ) ),
$url['text']
)
);
}
// build complete html
$bar[$skin->msg( 'relatedarticles-title' )->text()] =
Html::rawElement( 'ul', array(),
implode( '', $relatedPages )
);
return true;
}
/**
* Handler for the <code>SkinTemplateToolboxEnd</code> hook.
*
* Retrieves the list of related pages from the template and
* <code>echo</code>s its HTML representation to the sidebar if the
* ReadMore feature is disabled and the beta feature is enabled by the user.
*
* @param SkinTemplate $skinTpl
* @return boolean Always <code>true</code>
*/
public static function onSkinTemplateToolboxEnd( BaseTemplate &$skinTpl ) {
$out = $skinTpl->getSkin()->getOutput();
$relatedPages = $out->getProperty( 'RelatedArticles' );
if ( !self::isInSidebar( $relatedPages, $out->getUser() ) ) {
return true;
}
$relatedPagesUrls = self::getRelatedPagesUrls( $relatedPages );
// build relatedarticles <li>'s
$relatedPages = array();
foreach ( (array) $relatedPagesUrls as $url ) {
$relatedPages[] =
Html::rawElement( 'li', array( 'class' => htmlspecialchars( $url['class'] ) ),
Html::element( 'a', array( 'href' => htmlspecialchars( $url['href'] ) ),
$url['text']
)
);
}
// build complete html
echo
Html::closeElement( 'ul' ) .
Html::closeElement( 'div' ) .
Html::closeElement( 'div' ) .
Html::openElement( 'div', array(
'class' => 'portal',
'role' => 'navigation',
'id' => 'p-relatedarticles',
) ) .
Html::element( 'h3', array(), wfMessage( 'relatedarticles-title' )->text() ) .
Html::openElement( 'div', array( 'class' => 'body' ) ) .
Html::openElement( 'ul' ) .
implode( '', $relatedPages );
return true;
}
/**
* Generates anchor element attributes for each entry in list of pages.
*
* The attributes that are generated are: <code>href</code>,
* <code>text</code>, and <code>class</code>, with the latter always
* set to <code>"interwiki-relart"</code>.
*
* If the the page is of the form <code>"Foo && Bar"</code>, then
* the <code>text</code> attribute will be set to "Bar", otherwise the
* page's {@see Title::getPrefixedText prefixed text} will be used.
*
* @param array[string] $relatedPages
* @return array An array of maps, each with <code>href</code>,
* <code>text</code>, and <code>class</code> entries.
*/
private static function getRelatedPagesUrls( array $relatedPages ) {
$relatedPagesUrls = array();
foreach ( $relatedPages as $page ) {
// Tribute to Evan
$page = urldecode( $page );
$altText = '';
if ( preg_match( '/\&\&/', $page ) ) {
$parts = array_map( 'trim', explode( '&&', $page, 2 ) );
$page = $parts[0];
$altText = $parts[1];
}
$title = Title::newFromText( $page );
if ( $title ) {
$relatedPagesUrls[] = array(
'href' => $title->getLocalURL(),
'text' => $altText ?: $title->getPrefixedText(),
'class' => 'interwiki-relart'
);
}
};
return $relatedPagesUrls;
}
/**
* Check whether there are related articles that can be displayed, or
* the ReadMore feature is disabled. The beta feature is used only
* for enabling ReadMore, so do not take it into account.
*
* @param mixed $relatedPages
* @param User $user
* @return bool
* @throws \ConfigException
*/
private static function isInSidebar( $relatedPages, User $user ) {
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'RelatedArticles' );
if ( !$relatedPages || $config->get( 'RelatedArticlesShowReadMore' ) ) {
return false;
}
return true;
}
}