Don't hit the DB unnecessarily

If $wgRelatedArticlesShowInFooter is false, then we'll still test
whether or not the page is a disambiguation page with the Disambiguator
extension. Unfortunately, DisambiguatorHooks::isDisambiguationPage hits
the DB.

Order the tests in RelatedArticles\FooterHooks::onBeforePageDisplay from
least expensive to most expensive so that the DB is queried only when it
must be.

This version of the patch takes into account namespace.

Bug: T127068
Bug: T144409
Change-Id: Ie6ddaa4e9bd8326c8b84dc400b78e9a4f4d0d78d
This commit is contained in:
Jdlrobson 2016-08-31 19:02:46 +00:00 committed by Sam Smith
parent 9e67ef6590
commit 4b53afc983

View file

@ -9,6 +9,7 @@ use Skin;
use ConfigFactory;
use User;
use DisambiguatorHooks;
use Title;
class FooterHooks {
@ -34,6 +35,19 @@ class FooterHooks {
return true;
}
/**
* Uses the Disambiguator extension to test whether the page is a disambiguation page.
*
* If the Disambiguator extension isn't installed, then the test always fails, i.e. the page is
* never a disambiguation page.
*
* @return boolean
*/
private static function isDisambiguationPage( Title $title ) {
return class_exists( 'DisambiguatorHooks' ) &&
DisambiguatorHooks::isDisambiguationPage( $title );
}
/**
* Handler for the <code>BeforePageDisplay</code> hook.
*
@ -41,12 +55,12 @@ class FooterHooks {
* to the output when:
*
* <ol>
* <li>The page is not a disambiguation page</li>
* <li><code>$wgRelatedArticlesShowInFooter</code> is truthy</li>
* <li>On mobile, the output is being rendered with
* <code>SkinMinervaBeta<code></li>
* <li>On desktop, the beta feature has been enabled</li>
* <li>The page is in mainspace</li>
* <li>The page is not a disambiguation page</li>
* </ol>
*
* @param OutputPage $out
@ -54,10 +68,6 @@ class FooterHooks {
* @return boolean Always <code>true</code>
*/
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
if ( class_exists( 'DisambiguatorHooks' ) &&
DisambiguatorHooks::isDisambiguationPage( $out->getTitle() ) ) {
return true;
}
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'RelatedArticles' );
$showReadMore = $config->get( 'RelatedArticlesShowInFooter' );
@ -66,7 +76,8 @@ class FooterHooks {
if (
$showReadMore &&
$title->inNamespace( NS_MAIN ) &&
!$title->isMainPage()
!$title->isMainPage() &&
!self::isDisambiguationPage( $title )
) {
if (
get_class( $skin ) === 'SkinMinervaBeta' ||