84 if ( self::$instance === null ) {
85 self::$instance =
new self();
87 return self::$instance;
107 !self::$globalSession
108 || self::$globalSessionRequest !==
$request
109 || $id !==
'' && self::$globalSession->getId() !== $id
111 self::$globalSessionRequest =
$request;
121 self::$globalSession =
$request->getSession();
126 self::$globalSession = self::singleton()->getSessionById( $id,
true,
$request )
130 return self::$globalSession;
140 if ( isset(
$options[
'config'] ) ) {
142 if ( !$this->config instanceof
Config ) {
143 throw new \InvalidArgumentException(
144 '$options[\'config\'] must be an instance of Config'
151 if ( isset(
$options[
'logger'] ) ) {
152 if ( !
$options[
'logger'] instanceof LoggerInterface ) {
153 throw new \InvalidArgumentException(
154 '$options[\'logger\'] must be an instance of LoggerInterface'
164 throw new \InvalidArgumentException(
165 '$options[\'store\'] must be an instance of BagOStuff'
174 register_shutdown_function( [ $this,
'shutdown' ] );
193 if ( !self::validateSessionId( $id ) ) {
194 throw new \InvalidArgumentException(
'Invalid session ID' );
204 if ( isset( $this->allSessionBackends[$id] ) ) {
210 if ( is_array( $this->
store->get(
$key ) ) ) {
217 if ( $create && $session === null ) {
222 $this->logger->error(
'Failed to create empty session: {exception}',
224 'method' => __METHOD__,
245 if ( $id !== null ) {
246 if ( !self::validateSessionId( $id ) ) {
247 throw new \InvalidArgumentException(
'Invalid session ID' );
251 if ( is_array( $this->
store->get(
$key ) ) ) {
252 throw new \InvalidArgumentException(
'Session ID already exists' );
261 $info = $provider->newSessionInfo( $id );
265 if ( $info->getProvider() !== $provider ) {
266 throw new \UnexpectedValueException(
267 "$provider returned an empty session info for a different provider: $info"
270 if ( $id !== null && $info->getId() !== $id ) {
271 throw new \UnexpectedValueException(
272 "$provider returned empty session info with a wrong id: " .
273 $info->getId() .
' != ' . $id
276 if ( !$info->isIdSafe() ) {
277 throw new \UnexpectedValueException(
278 "$provider returned empty session info with id flagged unsafe"
282 if ( $compare > 0 ) {
285 if ( $compare === 0 ) {
293 if ( count( $infos ) > 1 ) {
294 throw new \UnexpectedValueException(
295 'Multiple empty sessions tied for top priority: ' . implode(
', ', $infos )
297 } elseif ( count( $infos ) < 1 ) {
298 throw new \UnexpectedValueException(
'No provider could provide an empty session!' );
310 $authUser->resetAuthToken();
314 $provider->invalidateSessionsForUser( $user );
320 if ( defined(
'MW_NO_SESSION' ) &&
MW_NO_SESSION !==
'warn' ) {
324 if ( $this->varyHeaders === null ) {
327 foreach ( $provider->getVaryHeaders()
as $header =>
$options ) {
328 if ( !isset( $headers[$header] ) ) {
329 $headers[$header] = [];
332 $headers[$header] = array_unique( array_merge( $headers[$header],
$options ) );
336 $this->varyHeaders = $headers;
343 if ( defined(
'MW_NO_SESSION' ) &&
MW_NO_SESSION !==
'warn' ) {
347 if ( $this->varyCookies === null ) {
350 $cookies = array_merge( $cookies, $provider->getVaryCookies() );
352 $this->varyCookies = array_values( array_unique( $cookies ) );
363 return is_string( $id ) && preg_match(
'/^[a-zA-Z0-9_-]{32,}$/', $id );
381 return \MediaWiki\Auth\AuthManager::singleton()->autoCreateUser(
383 \
MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_SESSION,
402 $provider->preventSessionsForUser(
$username );
413 return !empty( $this->preventUsers[
$username] );
421 if ( $this->sessionProviders === null ) {
422 $this->sessionProviders = [];
423 foreach ( $this->config->get(
'SessionProviders' )
as $spec ) {
425 $provider->setLogger( $this->logger );
426 $provider->setConfig( $this->config );
427 $provider->setManager( $this );
428 if ( isset( $this->sessionProviders[(
string)$provider] ) ) {
429 throw new \UnexpectedValueException(
"Duplicate provider name \"$provider\"" );
431 $this->sessionProviders[(
string)$provider] = $provider;
449 return isset( $providers[
$name] ) ? $providers[
$name] : null;
457 if ( $this->allSessionBackends ) {
458 $this->logger->debug(
'Saving all sessions on shutdown' );
459 if ( session_id() !==
'' ) {
461 session_write_close();
464 foreach ( $this->allSessionBackends
as $backend ) {
465 $backend->shutdown();
479 $info = $provider->provideSessionInfo( $request );
483 if ( $info->getProvider() !== $provider ) {
484 throw new \UnexpectedValueException(
485 "$provider returned session info for a different provider: $info"
494 usort( $infos,
'MediaWiki\\Session\\SessionInfo::compare' );
497 $info = array_pop( $infos );
501 $info = array_pop( $infos );
512 $info->getProvider()->unpersistSession( $request );
517 $info->getProvider()->unpersistSession( $request );
521 if ( count( $retInfos ) > 1 ) {
522 $ex = new \OverflowException(
523 'Multiple sessions for this request tied for top priority: ' . implode(
', ', $retInfos )
525 $ex->sessionInfos = $retInfos;
529 return $retInfos ? $retInfos[0] : null;
547 $failHandler =
function ()
use (
$key, &$info, $request ) {
552 $failHandler =
function () {
559 if ( $blob !==
false ) {
561 if ( !is_array( $blob ) ) {
562 $this->logger->warning(
'Session "{session}": Bad data', [
566 return $failHandler();
570 if ( !isset( $blob[
'data'] ) || !is_array( $blob[
'data'] ) ||
571 !isset( $blob[
'metadata'] ) || !is_array( $blob[
'metadata'] )
573 $this->logger->warning(
'Session "{session}": Bad data structure', [
577 return $failHandler();
580 $data = $blob[
'data'];
581 $metadata = $blob[
'metadata'];
585 if ( !array_key_exists(
'userId', $metadata ) ||
586 !array_key_exists(
'userName', $metadata ) ||
587 !array_key_exists(
'userToken', $metadata ) ||
588 !array_key_exists(
'provider', $metadata )
590 $this->logger->warning(
'Session "{session}": Bad metadata', [
594 return $failHandler();
599 if ( $provider === null ) {
600 $newParams[
'provider'] = $provider = $this->
getProvider( $metadata[
'provider'] );
602 $this->logger->warning(
603 'Session "{session}": Unknown provider ' . $metadata[
'provider'],
609 return $failHandler();
611 } elseif ( $metadata[
'provider'] !== (
string)$provider ) {
612 $this->logger->warning(
'Session "{session}": Wrong provider ' .
613 $metadata[
'provider'] .
' !== ' . $provider,
617 return $failHandler();
622 if ( isset( $metadata[
'providerMetadata'] ) ) {
623 if ( $providerMetadata === null ) {
624 $newParams[
'metadata'] = $metadata[
'providerMetadata'];
627 $newProviderMetadata = $provider->mergeMetadata(
628 $metadata[
'providerMetadata'], $providerMetadata
630 if ( $newProviderMetadata !== $providerMetadata ) {
631 $newParams[
'metadata'] = $newProviderMetadata;
634 $this->logger->warning(
635 'Session "{session}": Metadata merge failed: {exception}',
641 return $failHandler();
651 if ( $metadata[
'userId'] ) {
653 } elseif ( $metadata[
'userName'] !== null ) {
659 $this->logger->error(
'Session "{session}": {exception}', [
663 return $failHandler();
665 $newParams[
'userInfo'] = $userInfo;
669 if ( $metadata[
'userId'] ) {
670 if ( $metadata[
'userId'] !== $userInfo->getId() ) {
671 $this->logger->warning(
672 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}',
675 'uid_a' => $metadata[
'userId'],
676 'uid_b' => $userInfo->
getId(),
678 return $failHandler();
682 if ( $metadata[
'userName'] !== null &&
683 $userInfo->getName() !== $metadata[
'userName']
685 $this->logger->warning(
686 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}',
689 'uname_a' => $metadata[
'userName'],
690 'uname_b' => $userInfo->getName(),
692 return $failHandler();
695 } elseif ( $metadata[
'userName'] !== null ) {
696 if ( $metadata[
'userName'] !== $userInfo->getName() ) {
697 $this->logger->warning(
698 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}',
701 'uname_a' => $metadata[
'userName'],
702 'uname_b' => $userInfo->getName(),
704 return $failHandler();
706 } elseif ( !$userInfo->isAnon() ) {
709 $this->logger->warning(
710 'Session "{session}": Metadata has an anonymous user, but a non-anon user was provided',
714 return $failHandler();
719 if ( $metadata[
'userToken'] !== null &&
720 $userInfo->getToken() !== $metadata[
'userToken']
722 $this->logger->warning(
'Session "{session}": User token mismatch', [
725 return $failHandler();
727 if ( !$userInfo->isVerified() ) {
728 $newParams[
'userInfo'] = $userInfo->verified();
731 if ( !empty( $metadata[
'remember'] ) && !$info->
wasRemembered() ) {
732 $newParams[
'remembered'] =
true;
734 if ( !empty( $metadata[
'forceHTTPS'] ) && !$info->
forceHTTPS() ) {
735 $newParams[
'forceHTTPS'] =
true;
737 if ( !empty( $metadata[
'persisted'] ) && !$info->
wasPersisted() ) {
738 $newParams[
'persisted'] =
true;
742 $newParams[
'idIsSafe'] =
true;
747 $this->logger->warning(
748 'Session "{session}": Null provider and no metadata',
752 return $failHandler();
761 'Session "{session}": No user provided and provider cannot set user',
765 return $failHandler();
768 $this->logger->warning(
769 'Session "{session}": Unverified user provided and no metadata to auth it',
773 return $failHandler();
782 $newParams[
'idIsSafe'] =
true;
788 $newParams[
'copyFrom'] = $info;
794 if ( !$info->
getProvider()->refreshSessionInfo( $info, $request, $providerMetadata ) ) {
795 return $failHandler();
799 'metadata' => $providerMetadata,
807 $reason =
'Hook aborted';
810 [ &$reason, $info, $request, $metadata, $data ]
812 $this->logger->warning(
'Session "{session}": ' . $reason, [
815 return $failHandler();
831 if ( defined(
'MW_NO_SESSION' ) ) {
834 $this->logger->error(
'Sessions are supposed to be disabled for this entry point', [
835 'exception' =>
new \BadMethodCallException(
'Sessions are disabled for this entry point' ),
838 throw new \BadMethodCallException(
'Sessions are disabled for this entry point' );
843 $id = $info->
getId();
845 if ( !isset( $this->allSessionBackends[$id] ) ) {
846 if ( !isset( $this->allSessionIds[$id] ) ) {
847 $this->allSessionIds[$id] =
new SessionId( $id );
850 $this->allSessionIds[$id],
854 $this->config->get(
'ObjectCacheSessionExpiry' )
856 $this->allSessionBackends[$id] = $backend;
859 $backend = $this->allSessionBackends[$id];
860 $delay = $backend->delaySave();
865 $backend->setRememberUser(
true );
870 $session = $backend->getSession( $request );
886 $id = $backend->
getId();
887 if ( !isset( $this->allSessionBackends[$id] ) || !isset( $this->allSessionIds[$id] ) ||
888 $this->allSessionBackends[$id] !== $backend ||
891 throw new \InvalidArgumentException(
'Backend was not registered with this SessionManager' );
894 unset( $this->allSessionBackends[$id] );
905 $oldId = (
string)$sessionId;
906 if ( !isset( $this->allSessionBackends[$oldId] ) || !isset( $this->allSessionIds[$oldId] ) ||
907 $this->allSessionBackends[$oldId] !== $backend ||
908 $this->allSessionIds[$oldId] !== $sessionId
910 throw new \InvalidArgumentException(
'Backend was not registered with this SessionManager' );
915 unset( $this->allSessionBackends[$oldId], $this->allSessionIds[$oldId] );
916 $sessionId->setId( $newId );
917 $this->allSessionBackends[$newId] = $backend;
918 $this->allSessionIds[$newId] = $sessionId;
929 }
while ( isset( $this->allSessionIds[$id] ) || is_array( $this->
store->get(
$key ) ) );
946 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
948 throw new MWException( __METHOD__ .
' may only be called from unit tests!' );
952 self::$globalSession = null;
953 self::$globalSessionRequest = null;
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
static getObjectFromSpec($spec)
Instantiate an object based on a specification array.
saveSettings()
Save this user's settings into the database.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
processing should stop and the error should be shown to the user * false
static getInstance($id)
Get a cached instance of the specified type of cache object.
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
setToken($token=false)
Set the random token (used for persistent authentication) Called from loadDefaults() among other plac...
MediaWiki s SiteStore can be cached and stored in a flat in a json format If the SiteStore is frequently the file cache may provide a performance benefit over a database store
static getMain()
Static methods.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
setSessionId(SessionId $sessionId)
Set the session for this request.
static getDefaultInstance()
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
this hook is for auditing only or null if authentication failed before getting that far $username
wfBaseConvert($input, $sourceBase, $destBase, $pad=1, $lowercase=true, $engine= 'auto')
Convert an arbitrarily-long digit string from one numeric base to another, optionally zero-padding to...
error also a ContextSource you ll probably need to make sure the header is varied on $request
static generateHex($chars, $forceStrong=false)
Generate a run of (ideally) cryptographically random data and return it in hexadecimal string format...
static consume(ScopedCallback &$sc=null)
Trigger a scoped callback and destroy it.
wfMemcKey()
Make a cache key for the local wiki.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Allows to change the fields on the form that will be generated $name