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.
 
 shouldDisplayMobileView ()
 Determine whether 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 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.
 
 hasMobileDomain ()
 True if the current wiki has separate mobile and desktop domains (regardless of which domain is used by the current request).
 
 getMobileUrl ( $url, $forceHttps=false)
 Take a URL and return the equivalent mobile URL (ie.
 
 usingMobileDomain ()
 Checks whether the current request is using the mobile domain.
 
 getDesktopUrl ( $url)
 Take a URL and return a copy that removes any mobile tokens.
 
 doToggling ()
 Set the toggling cookie and redirect, as requested by toggleView()
 
 checkToggleView ()
 Determine whether we need to toggle the view, and toggle it.
 
 isLocalUrl ( $url)
 Determine whether 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 STOP_MOBILE_REDIRECT_COOKIE_NAME = 'stopMobileRedirect'
 
const USEFORMAT_COOKIE_NAME = 'mf_useformat'
 

Static Public Attributes

static string $mfStopRedirectCookieHost = null
 

Protected Member Functions

 __construct (IContextSource $context, private readonly Config $config,)
 
 getUseFormatCookieExpiry ( $startTime=null, $cookieDuration=null)
 Get the expiration time for the mf_useformat cookie.
 
 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.
 

Protected Attributes

string $useFormat = null
 Save explicitly requested format.
 
array $analyticsLogItems = []
 
string $mobileAction = null
 Saves requested Mobile action.
 

Detailed Description

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

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.

This only works with URLs of the current wiki.

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

◆ getMobileUrl()

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

Take a URL and return the equivalent mobile URL (ie.

replace the domain with the mobile domain).

Typically, this is a URL for the current wiki, but it can be anything as long as $wgMobileUrlCallback can convert its domain (so e.g. interwiki links can be converted). If the domain is already a mobile domain, or not recognized by $wgMobileUrlCallback, or the wiki does not use mobile domains, and so $wgMobileUrlCallback is not set, the URL will be returned unchanged (except $forceHttps will still be applied).

Parameters
string$urlURL to convert
bool$forceHttpsForce HTTPS, even if the original URL used HTTP
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 the result of getCookieDomain()

Returns
string|null

◆ getUseFormatCookie()

MobileContext::getUseFormatCookie ( )

This cookie can determine whether 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

◆ isLocalUrl()

MobileContext::isLocalUrl ( $url)

Determine whether 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

◆ setForceMobileView()

MobileContext::setForceMobileView ( $value)

Save whether mobile view should always be enforced.

Parameters
bool$valueshould mobile view be enforced?

◆ 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 a user should see the mobile version of pages.

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

◆ shouldDisplayMobileView()

MobileContext::shouldDisplayMobileView ( )

Determine whether 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 the mobile view for page edits or page history. 'useformat' 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

◆ 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 UrlUtils::parse()

◆ 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 UrlUtils::parse()

◆ usingMobileDomain()

MobileContext::usingMobileDomain ( )

Checks whether the current request is using the mobile domain.

This assumes that some infrastructure outside MediaWiki will set a header (specified by $wgMFMobileHeader) on requests which should use the mobile format. This means that the traffic routing layer can rewrite any m-dot hostnames to be canonical, allowing non-MobileFrontend-aware code in MediaWiki to work as-is.

Returns
bool

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