MobileFrontend
Mobile optimised frontend for MediaWiki
Loading...
Searching...
No Matches
MobileContext Class Reference

Provide various request-dependant methods to use in mobile context. More...

+ Inheritance diagram for MobileContext:
+ Collaboration diagram for MobileContext:

Public Member Functions

 isMobileDevice ()
 Detects whether the UA is sending the request from a device and, if so, whether to display the mobile view to that device.
 
 setForceMobileView ( $value)
 Save whether mobile view should always be enforced.
 
 setMobileMode ( $mode)
 Sets testing group membership, both cookie and this class variables.
 
 isBetaGroupMember ()
 Whether user is Beta group member.
 
 shouldDisplayMobileView ()
 Determine whether or not we should display the mobile view.
 
 getMobileAction ()
 Get requested mobile action.
 
 setStopMobileRedirectCookie ( $expiry=null)
 Set Cookie to stop automatically redirect to mobile page.
 
 unsetStopMobileRedirectCookie ()
 Remove cookie and continue automatic redirect to mobile page.
 
 getStopMobileRedirectCookie ()
 Read cookie for stop automatic mobile redirect.
 
 getUseFormatCookie ()
 This cookie can determine whether or not a user should see the mobile version of a page.
 
 getCookieDomain ()
 Return the base level domain or IP address.
 
 getStopMobileRedirectCookieDomain ()
 Determine the correct domain to use for the stopMobileRedirect cookie.
 
 setUseFormatCookie ( $cookieFormat='true', $expiry=null)
 Set the mf_useformat cookie.
 
 unsetUseFormatCookie ()
 Remove cookie based saved useformat value.
 
 getUseFormatCookieDuration ()
 Determine the duration the cookie should last.
 
 getMobileHostToken ( $mobileUrlHostTemplate)
 Take a URL Host Template and return the mobile token portion.
 
 hasMobileDomain ()
 True if the current wiki has a mobile URL that's different from the desktop one.
 
 getMobileUrl ( $url, $forceHttps=false)
 Take a URL and return a copy that conforms to the mobile URL template.
 
 usingMobileDomain ()
 If a mobile-domain is specified by the $wgMobileUrlTemplate and there's a mobile header, then we assume the user is accessing the site from the mobile-specific domain (because why would the desktop site set the header?).
 
 getDesktopUrl ( $url)
 Take a URL and return a copy that removes any mobile tokens.
 
 parseMobileUrlTemplate ( $part=null)
 Parse mobile URL template into its host and path components.
 
 toggleView ( $view)
 Toggles view to one specified by the user.
 
 doToggling ()
 Performs view change as requested vy toggleView()
 
 checkToggleView ()
 Determine whether or not we need to toggle the view, and toggle it.
 
 isLocalUrl ( $url)
 Determine whether or not a given URL is local.
 
 addAnalyticsLogItem ( $key, $val)
 Add key/value pairs for analytics purposes to $this->analyticsLogItems.
 
 getAnalyticsLogItems ()
 Read key/value pairs for analytics purposes from $this->analyticsLogItems.
 
 getXAnalyticsHeader ()
 Get HTTP header string for X-Analytics.
 
 addAnalyticsLogItemFromXAnalytics ( $xanalytics_item)
 Take a key/val pair in string format and add it to $this->analyticsLogItems.
 
 logMobileMode ()
 Adds analytics log items depending on which modes are enabled for the user.
 
 shouldShowWikibaseDescriptions ( $feature, Config $config)
 Gets whether Wikibase descriptions should be shown in search results, and watchlists; or as taglines on article pages.
 

Static Public Member Functions

static singleton ()
 Returns the actual MobileContext Instance or create a new if no exists.
 
static resetInstanceForTesting ()
 Resets the singleton instance.
 

Public Attributes

const MODE_BETA = 'beta'
 
const MODE_STABLE = 'stable'
 
const OPTIN_COOKIE_NAME = 'optin'
 
const STOP_MOBILE_REDIRECT_COOKIE_NAME = 'stopMobileRedirect'
 
const USEFORMAT_COOKIE_NAME = 'mf_useformat'
 
const USER_MODE_PREFERENCE_NAME = 'mfMode'
 

Static Public Attributes

static $mfStopRedirectCookieHost = null
 

Protected Member Functions

 __construct (IContextSource $context, Config $config)
 
 getUseFormatCookieExpiry ( $startTime=null, $cookieDuration=null)
 Get the expiration time for the mf_useformat cookie.
 
 updateMobileUrlHost (array &$parsedUrl)
 Update host of given URL to conform to mobile URL template.
 
 updateDesktopUrlHost (array &$parsedUrl)
 Update the host of a given URL to strip out any mobile tokens.
 
 updateDesktopUrlQuery (array &$parsedUrl)
 Update the query portion of a given URL to remove any 'useformat' params.
 
 updateMobileUrlPath (array &$parsedUrl)
 Update path of given URL to conform to mobile URL template.
 

Protected Attributes

 $mobileMode = null
 
 $useFormat = null
 
 $analyticsLogItems = []
 
 $mobileAction = null
 

Detailed Description

Provide various request-dependant methods to use in mobile context.

Constructor & Destructor Documentation

◆ __construct()

MobileContext::__construct ( IContextSource $context,
Config $config )
protected
Parameters
IContextSource$context
Config$config

Member Function Documentation

◆ addAnalyticsLogItem()

MobileContext::addAnalyticsLogItem ( $key,
$val )

Add key/value pairs for analytics purposes to $this->analyticsLogItems.

Pre-existing entries are appended to as sets delimited by commas.

Parameters
string$keyfor <key> in X-Analytics: <key>=<value>
string$valfor

in X-Analytics: <key>=<value>

◆ addAnalyticsLogItemFromXAnalytics()

MobileContext::addAnalyticsLogItemFromXAnalytics ( $xanalytics_item)

Take a key/val pair in string format and add it to $this->analyticsLogItems.

Parameters
string$xanalytics_itemIn the format key=value

◆ getAnalyticsLogItems()

MobileContext::getAnalyticsLogItems ( )

Read key/value pairs for analytics purposes from $this->analyticsLogItems.

Returns
array

◆ getCookieDomain()

MobileContext::getCookieDomain ( )

Return the base level domain or IP address.

Returns
string|null

◆ getDesktopUrl()

MobileContext::getDesktopUrl ( $url)

Take a URL and return a copy that removes any mobile tokens.

Parameters
string$urlrepresenting a page on the mobile domain e.g. https://en.m.wikipedia.org/
Returns
string (absolute url)

◆ getMobileAction()

MobileContext::getMobileAction ( )

Get requested mobile action.

Returns
string

◆ getMobileHostToken()

MobileContext::getMobileHostToken ( $mobileUrlHostTemplate)

Take a URL Host Template and return the mobile token portion.

Eg if a desktop domain is en.wikipedia.org, but the mobile variant is en.m.wikipedia.org, the mobile token is 'm.'

Parameters
string$mobileUrlHostTemplateURL host
Returns
string
Deprecated

◆ getMobileUrl()

MobileContext::getMobileUrl ( $url,
$forceHttps = false )

Take a URL and return a copy that conforms to the mobile URL template.

Parameters
string$urlURL to convert
bool$forceHttpsshould force HTTPS?
Returns
string|bool

◆ getStopMobileRedirectCookie()

MobileContext::getStopMobileRedirectCookie ( )

Read cookie for stop automatic mobile redirect.

Returns
string

◆ getStopMobileRedirectCookieDomain()

MobileContext::getStopMobileRedirectCookieDomain ( )

Determine the correct domain to use for the stopMobileRedirect cookie.

Will use $wgMFStopRedirectCookieHost if it's set, otherwise will use result of getCookieDomain()

Returns
string|null

◆ getUseFormatCookie()

MobileContext::getUseFormatCookie ( )

This cookie can determine whether or not a user should see the mobile version of a page.

Returns
string|null

◆ getUseFormatCookieDuration()

MobileContext::getUseFormatCookieDuration ( )

Determine the duration the cookie should last.

If $wgMobileFrontendFormatcookieExpiry has a non-0 value, use that for the duration. Otherwise, fall back to $wgCookieExpiration.

Returns
int The number of seconds for which the cookie should last.

◆ getUseFormatCookieExpiry()

MobileContext::getUseFormatCookieExpiry ( $startTime = null,
$cookieDuration = null )
protected

Get the expiration time for the mf_useformat cookie.

Parameters
int | null$startTimeThe base time (in seconds since Epoch) from which to calculate cookie expiration. If null, time() is used.
int | null$cookieDurationThe time (in seconds) the cookie should last
Returns
int The time (in seconds since Epoch) that the cookie should expire

◆ getXAnalyticsHeader()

MobileContext::getXAnalyticsHeader ( )

Get HTTP header string for X-Analytics.

This is made up of key/value pairs and is used for analytics purposes.

Returns
string|bool

◆ hasMobileDomain()

MobileContext::hasMobileDomain ( )

True if the current wiki has a mobile URL that's different from the desktop one.

Returns
bool

◆ isBetaGroupMember()

MobileContext::isBetaGroupMember ( )

Whether user is Beta group member.

Returns
bool

◆ isLocalUrl()

MobileContext::isLocalUrl ( $url)

Determine whether or not a given URL is local.

Parameters
string$urlURL to check against
Returns
bool

◆ isMobileDevice()

MobileContext::isMobileDevice ( )

Detects whether the UA is sending the request from a device and, if so, whether to display the mobile view to that device.

The mobile view will always be displayed to mobile devices. However, it will only be displayed to tablet devices if $wgMFShowMobileViewToTablets is truthy.

@fixme This should be renamed to something more appropriate, e.g. shouldDisplayMobileViewToDevice.

See also
MobileContext::shouldDisplayMobileView
Returns
bool

◆ logMobileMode()

MobileContext::logMobileMode ( )

Adds analytics log items depending on which modes are enabled for the user.

Invoked from MobileFrontendHooks::onRequestContextCreateSkin()

Making changes to what this method logs? Make sure you update the documentation for the X-Analytics header: https://wikitech.wikimedia.org/wiki/X-Analytics

◆ parseMobileUrlTemplate()

MobileContext::parseMobileUrlTemplate ( $part = null)

Parse mobile URL template into its host and path components.

Optionally specify which portion of the template you want returned.

Parameters
string | null$partwhich part to return?
Returns
mixed
Deprecated

This if/else block exists because of an annoying aspect of substr() Even if you pass 'null' or 'false' into the 'length' param, it will return an empty string. http://www.stopgeek.com/wp-content/uploads/2007/07/sense.jpg

◆ setForceMobileView()

MobileContext::setForceMobileView ( $value)

Save whether mobile view should always be enforced.

Parameters
bool$valueshould mobile view be enforced?

◆ setMobileMode()

MobileContext::setMobileMode ( $mode)

Sets testing group membership, both cookie and this class variables.

WARNING: Does not persist the updated user preference to the database. The caller must handle this by calling User::saveSettings() after all preference updates associated with this web request are made.

Parameters
string$modeMode to set

◆ setStopMobileRedirectCookie()

MobileContext::setStopMobileRedirectCookie ( $expiry = null)

Set Cookie to stop automatically redirect to mobile page.

Parameters
int | null$expiryExpire time of cookie

◆ setUseFormatCookie()

MobileContext::setUseFormatCookie ( $cookieFormat = 'true',
$expiry = null )

Set the mf_useformat cookie.

This cookie can determine whether or not a user should see the mobile version of pages.

Parameters
string$cookieFormatshould user see mobile version of pages?
int | null$expiryExpiration of cookie

◆ shouldDisplayMobileView()

MobileContext::shouldDisplayMobileView ( )

Determine whether or not we should display the mobile view.

Step through the hierarchy of what should or should not trigger the mobile view.

Primacy is given to the page action - we will never show mobile view for page edits or page history. 'userformat' request param is then honored, followed by cookie settings, then actual device detection, finally falling back on false.

Returns
bool

◆ shouldShowWikibaseDescriptions()

MobileContext::shouldShowWikibaseDescriptions ( $feature,
Config $config )

Gets whether Wikibase descriptions should be shown in search results, and watchlists; or as taglines on article pages.

Doesn't take into account whether the wikidata descriptions feature has been enabled.

Parameters
string$featurewhich description to show?
Config$config
Returns
bool
Exceptions
DomainExceptionIf feature isn't one that shows Wikidata descriptions. See the wgMFDisplayWikibaseDescriptions configuration variable for detail

◆ singleton()

static MobileContext::singleton ( )
static

Returns the actual MobileContext Instance or create a new if no exists.

Deprecated
use MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' );
Returns
self

◆ toggleView()

MobileContext::toggleView ( $view)

Toggles view to one specified by the user.

If a user has requested a particular view (eg clicked 'Desktop' from a mobile page), set the requested view for this particular request and set a cookie to keep them on that view for subsequent requests.

Parameters
string$viewUser requested particular view

◆ updateDesktopUrlHost()

MobileContext::updateDesktopUrlHost ( array & $parsedUrl)
protected

Update the host of a given URL to strip out any mobile tokens.

Parameters
array&$parsedUrlResult of parseUrl() or wfParseUrl()

◆ updateDesktopUrlQuery()

MobileContext::updateDesktopUrlQuery ( array & $parsedUrl)
protected

Update the query portion of a given URL to remove any 'useformat' params.

Parameters
array&$parsedUrlResult of parseUrl() or wfParseUrl()

◆ updateMobileUrlHost()

MobileContext::updateMobileUrlHost ( array & $parsedUrl)
protected

Update host of given URL to conform to mobile URL template.

Parameters
array&$parsedUrlResult of parseUrl() or wfParseUrl()
Deprecated

◆ updateMobileUrlPath()

MobileContext::updateMobileUrlPath ( array & $parsedUrl)
protected

Update path of given URL to conform to mobile URL template.

NB: this is not actually being used anywhere at the moment. It will take some magic to get MW to properly handle path modifications like this is intended to provide. This will hopefully be implemented someday in the not to distant future.

Parameters
array&$parsedUrlResult of parseUrl() or wfParseUrl()
Deprecated

◆ usingMobileDomain()

MobileContext::usingMobileDomain ( )

If a mobile-domain is specified by the $wgMobileUrlTemplate and there's a mobile header, then we assume the user is accessing the site from the mobile-specific domain (because why would the desktop site set the header?).

Returns
bool

The documentation for this class was generated from the following file: