wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / objectcache / ObjectCacheSessionHandler.php
blobcc8507417549d2033ed06c2e98be6d152581b6ea
1 <?php
2 /**
3 * Session storage in object cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Cache
24 use MediaWiki\Logger\LoggerFactory;
26 /**
27 * Session storage in object cache.
28 * Used if $wgSessionsInObjectCache is true.
30 * @ingroup Cache
32 class ObjectCacheSessionHandler {
33 /** @var array Map of (session ID => SHA-1 of the data) */
34 protected static $hashCache = array();
36 /**
37 * Install a session handler for the current web request
39 static function install() {
40 session_set_save_handler(
41 array( __CLASS__, 'open' ),
42 array( __CLASS__, 'close' ),
43 array( __CLASS__, 'read' ),
44 array( __CLASS__, 'write' ),
45 array( __CLASS__, 'destroy' ),
46 array( __CLASS__, 'gc' ) );
48 // It's necessary to register a shutdown function to call session_write_close(),
49 // because by the time the request shutdown function for the session module is
50 // called, the BagOStuff has already been destroyed. Shutdown functions registered
51 // this way are called before object destruction.
52 register_shutdown_function( array( __CLASS__, 'handleShutdown' ) );
55 /**
56 * Get the cache storage object to use for session storage
57 * @return BagOStuff
59 protected static function getCache() {
60 global $wgSessionCacheType;
62 return ObjectCache::getInstance( $wgSessionCacheType );
65 /**
66 * Get a cache key for the given session id.
68 * @param string $id Session id
69 * @return string Cache key
71 protected static function getKey( $id ) {
72 return wfMemcKey( 'session', $id );
75 /**
76 * @param mixed $data
77 * @return string
79 protected static function getHash( $data ) {
80 return sha1( serialize( $data ) );
83 /**
84 * Callback when opening a session.
86 * @param string $save_path Path used to store session files, unused
87 * @param string $session_name Session name
88 * @return bool Success
90 static function open( $save_path, $session_name ) {
91 return true;
94 /**
95 * Callback when closing a session.
96 * NOP.
98 * @return bool Success
100 static function close() {
101 return true;
105 * Callback when reading session data.
107 * @param string $id Session id
108 * @return mixed Session data
110 static function read( $id ) {
111 $stime = microtime( true );
112 $data = self::getCache()->get( self::getKey( $id ) );
113 $real = microtime( true ) - $stime;
115 RequestContext::getMain()->getStats()->timing( "session.read", 1000 * $real );
117 self::$hashCache = array( $id => self::getHash( $data ) );
119 return ( $data === false ) ? '' : $data;
123 * Callback when writing session data.
125 * @param string $id Session id
126 * @param string $data Session data
127 * @return bool Success
129 static function write( $id, $data ) {
130 global $wgObjectCacheSessionExpiry;
132 // Only issue a write if anything changed (PHP 5.6 already does this)
133 if ( !isset( self::$hashCache[$id] )
134 || self::getHash( $data ) !== self::$hashCache[$id]
136 $stime = microtime( true );
137 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
138 $real = microtime( true ) - $stime;
140 RequestContext::getMain()->getStats()->timing( "session.write", 1000 * $real );
143 return true;
147 * Callback to destroy a session when calling session_destroy().
149 * @param string $id Session id
150 * @return bool Success
152 static function destroy( $id ) {
153 $stime = microtime( true );
154 self::getCache()->delete( self::getKey( $id ) );
155 $real = microtime( true ) - $stime;
157 RequestContext::getMain()->getStats()->timing( "session.destroy", 1000 * $real );
159 return true;
163 * Callback to execute garbage collection.
164 * NOP: Object caches perform garbage collection implicitly
166 * @param int $maxlifetime Maximum session life time
167 * @return bool Success
169 static function gc( $maxlifetime ) {
170 return true;
174 * Shutdown function.
175 * See the comment inside ObjectCacheSessionHandler::install for rationale.
177 static function handleShutdown() {
178 session_write_close();
182 * Pre-emptive session renewal function
184 static function renewCurrentSession() {
185 global $wgObjectCacheSessionExpiry;
187 // Once a session is at half TTL, renew it
188 $window = $wgObjectCacheSessionExpiry / 2;
189 $logger = LoggerFactory::getInstance( 'SessionHandler' );
191 $now = microtime( true );
192 // Session are only written in object stores when $_SESSION changes,
193 // which also renews the TTL ($wgObjectCacheSessionExpiry). If a user
194 // is active but not causing session data changes, it may suddenly
195 // expire as they view a form, blocking the first submission.
196 // Make a dummy change every so often to avoid this.
197 if ( !isset( $_SESSION['wsExpiresUnix'] ) ) {
198 $_SESSION['wsExpiresUnix'] = $now + $wgObjectCacheSessionExpiry;
200 $logger->info( "Set expiry for session " . session_id(), array() );
201 } elseif ( ( $now + $window ) > $_SESSION['wsExpiresUnix'] ) {
202 $_SESSION['wsExpiresUnix'] = $now + $wgObjectCacheSessionExpiry;
204 $logger->info( "Renewed session " . session_id(), array() );