Yurik has uploaded a new change for review.
https://gerrit.wikimedia.org/r/65164
Change subject: Consolidated config, fixed bug in addWarning()
......................................................................
Consolidated config, fixed bug in addWarning()
* One method to determine if config is applicable to the current request
* Incorrect placement of the return statement in the addWarning()
Change-Id: I05a099db2f23f17f2aab27389e89e7bf1ab503ff
---
M includes/PageRenderingHooks.php
1 file changed, 76 insertions(+), 70 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroRatedMobileAccess
refs/changes/64/65164/1
diff --git a/includes/PageRenderingHooks.php b/includes/PageRenderingHooks.php
index d538878..10b647b 100644
--- a/includes/PageRenderingHooks.php
+++ b/includes/PageRenderingHooks.php
@@ -43,37 +43,38 @@
global $wgRequest;
$config = self::getConfig();
+ if ( $config === null ) {
+ return true;
+ }
- if ( $config !== null ) {
- $skin = new SkinTemplate();
- $redirectWarning =
'&renderZeroRatedRedirect=true&returnto=';
- $pattern = '/href=[\'"](.*?)[\'"]/';
+ $skin = new SkinTemplate();
+ $redirectWarning = '&renderZeroRatedRedirect=true&returnto=';
+ $pattern = '/href=[\'"](.*?)[\'"]/';
- $licenseText = wfMessage(
'mobile-frontend-footer-license' )->parse();
- $privacyText = $skin->footerLink(
'mobile-frontend-privacy-link-text', 'privacypage' );
- $termsText = wfMessage(
'mobile-frontend-terms-use-text' )->parse();
+ $licenseText = wfMessage( 'mobile-frontend-footer-license'
)->parse();
+ $privacyText = $skin->footerLink(
'mobile-frontend-privacy-link-text', 'privacypage' );
+ $termsText = wfMessage( 'mobile-frontend-terms-use-text'
)->parse();
- $licenseText = self::addWarning( $wgRequest, $pattern,
$licenseText, $redirectWarning ) ;
- $privacyText = self::addWarning( $wgRequest, $pattern,
$privacyText, $redirectWarning );
- $termsText = self::addWarning( $wgRequest, $pattern,
$termsText, $redirectWarning );
+ $licenseText = self::addWarning( $wgRequest, $pattern,
$licenseText, $redirectWarning ) ;
+ $privacyText = self::addWarning( $wgRequest, $pattern,
$privacyText, $redirectWarning );
+ $termsText = self::addWarning( $wgRequest, $pattern,
$termsText, $redirectWarning );
- $template->set( 'mobile-license', $licenseText );
- $template->set( 'privacy', $privacyText );
- $template->set( 'terms-use', $termsText );
+ $template->set( 'mobile-license', $licenseText );
+ $template->set( 'privacy', $privacyText );
+ $template->set( 'terms-use', $termsText );
- $redirectWarningQPS =
'?renderZeroRatedRedirect=true&returnto=';
+ $redirectWarningQPS = '?renderZeroRatedRedirect=true&returnto=';
- if (isset( $template->data['language_urls'] )
- && 0 < count( $template->data['language_urls'] )
- ) {
- $languageUrls =
$template->data['language_urls'];
+ if (isset( $template->data['language_urls'] )
+ && 0 < count( $template->data['language_urls'] )
+ ) {
+ $languageUrls = $template->data['language_urls'];
- foreach ($languageUrls as &$lang ) {
- $lang['href'] = $redirectWarningQPS .
urlencode( $lang['href'] );
- }
-
- $template->set( 'language_urls', $languageUrls
);
+ foreach ( $languageUrls as &$lang ) {
+ $lang['href'] = $redirectWarningQPS .
urlencode( $lang['href'] );
}
+
+ $template->set( 'language_urls', $languageUrls );
}
return true;
@@ -88,32 +89,18 @@
* @return bool
*/
public static function onBeforePageDisplay( &$out, &$options ) {
- global $wgRequest, $wgConf,
$wgEnableZeroRatedMobileAccessTesting;
- wfProfileIn( __METHOD__ );
+ global $wgRequest;
- // FIXME BUG -- why do we need master on every requests?!
- $DB = wfGetDB( DB_MASTER );
- $DBName = $DB->getDBname();
- /** @noinspection PhpUnusedLocalVariableInspection */
- list( $site, $lang ) = $wgConf->siteFromDB( $DBName );
-
- // Determine if this is a Zero-related site shown with a mobile
device
- $isZero = ( $site === 'wikipedia' ||
$wgEnableZeroRatedMobileAccessTesting );
- if ( $isZero ) {
- $useFormat = $wgRequest->getText( 'useformat' );
- $isZero = !empty( $_SERVER['HTTP_X_DEVICE'] ) ||
$useFormat === 'mobile' || $useFormat === 'mobile-wap';
- }
- if ( !$isZero ) {
- wfProfileOut( __METHOD__ );
+ $config = self::getConfig();
+ if ( $config === null ) {
return true;
}
+ wfProfileIn( __METHOD__ );
$out->addModuleStyles( 'mobile.zero.styles' );
$out->addModules( 'mobile.zero.scripts' );
$isFilePage = $out->getTitle()->inNamespace( NS_FILE );
- $config = self::getConfig();
-
$showBanner = $config !== null || $wgRequest->getFuzzyBool(
'renderZeroRatedBanner' );
if ( !$showBanner && self::isZeroRequest() ) {
$out->clearHTML();
@@ -462,44 +449,63 @@
}
wfProfileIn( __METHOD__ );
- global $wgRequest;
+ global $wgRequest, $wgConf, $wgDBname,
$wgEnableZeroRatedMobileAccessTesting;
$config = null;
+ list( $site, $langCode ) = $wgConf->siteFromDB( $wgDBname );
- // Allow URL override of the X-CS parameter for testing purposes
- $id = $wgRequest->getVal( 'X-CS' );
- if ( $id === null ) {
- $id = $wgRequest->getHeader( 'X-CS' );
+ // Determine if this is a Zero-related site shown with a mobile
device
+ $isZero = ( $site === 'wikipedia' ||
$wgEnableZeroRatedMobileAccessTesting );
+ if ( $isZero ) {
+ $useFormat = $wgRequest->getText( 'useformat' );
+ $isZero = !empty( $_SERVER['HTTP_X_DEVICE'] ) ||
$useFormat === 'mobile' || $useFormat === 'mobile-wap';
}
- if ( $id === '(null)' || !$id ) {
- $id = null;
- }
- if ( $id !== null ) {
- $store = new CarrierConfigStore( $id );
- $text = $store->get();
- if ( $text !== false ) {
- $conf = new CarrierConfig( $text );
- if ( !$conf->isError() ) {
- $config = $conf->getData();
- if ( !$config['enabled'] ) {
- // If the configuration is
disabled, pretend like it doesn't exist
- $config = null;
- } else {
- // Check if the wiki is in the
allowed list
- // @TODO: expand to allow
non-wikipedia sites
- $site = strtolower(
$wgRequest->getHeader( 'X-SUBDOMAIN' ) ) . '.wiki';
- if ( !in_array( $site,
$config['sites'] ) ) {
+ if ( $isZero ) {
+ // Allow URL override of the X-CS parameter for testing
purposes
+ $id = $wgRequest->getVal( 'X-CS' );
+ if ( $id === null ) {
+ $id = $wgRequest->getHeader( 'X-CS' );
+ }
+ if ( $id === '(null)' || !$id ) {
+ $id = null;
+ }
+ if ( $id !== null ) {
+ $store = new CarrierConfigStore( $id );
+ $text = $store->get();
+ if ( $text !== false ) {
+ $conf = new CarrierConfig( $text );
+ if ( !$conf->isError() ) {
+ $config = $conf->getData();
+ $subdomain =
$wgRequest->getHeader( 'X-SUBDOMAIN' );
+ if ( !self::isApplicable(
$config, $subdomain, $langCode ) ) {
$config = null;
}
}
}
}
}
-
wfProfileOut( __METHOD__ );
return $config;
}
+
+ static function isApplicable( $config, $subdomain, $langCode ) {
+ if ( !$config['enabled'] ) {
+ // If the configuration is disabled, pretend like it
doesn't exist
+ return false;
+ }
+ // Check if the wiki is in the allowed list
+ // @TODO: expand to allow non-wikipedia sites
+ $site = strtolower( $subdomain ) . '.wiki';
+ if ( !in_array( $site, $config['sites'] ) ) {
+ return false;
+ }
+ $freeLangs = $config['whitelistedLangs'];
+ if ( 0 < count( $freeLangs ) && !in_array( $langCode,
$freeLangs ) ) {
+ return false;
+ }
+ return true;
+ }
/**
* Returns the Html of a page with the various links appended with zero
partner parameter
@@ -635,20 +641,20 @@
/**
* Adds parameters to URLs. Helper for onMinervaPreRender(
\BaseTemplate &$template )
* @param \WebRequest $request The request object that will contain the
request path.
- * @param $pattern The regular expression string capturing the URI.
- * @param $link The full link, usually parsed wikitext.
- * @param $redirectQuery The name-value query param string to add to
the URI.
+ * @param string $pattern The regular expression string capturing the
URI.
+ * @param string $link The full link, usually parsed wikitext.
+ * @param string $redirectQuery The name-value query param string to
add to the URI.
* @return mixed The link with URIs rewritten.
*/
private static function addWarning( \WebRequest $request, $pattern,
$link, $redirectQuery ) {
if ( preg_match( $pattern, $link, $match ) ) {
$link = str_replace(
$match[1],
- $request->appendQuery( $redirectQuery ) .
urlencode($match[1]),
+ $request->appendQuery( $redirectQuery ) .
urlencode( $match[1] ),
$link
);
- return $link;
}
+ return $link;
}
--
To view, visit https://gerrit.wikimedia.org/r/65164
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I05a099db2f23f17f2aab27389e89e7bf1ab503ff
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ZeroRatedMobileAccess
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits