Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / objectcache / ObjectCacheSessionHandler.php
1 <?php
2 /**
3 * Session storage in object cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 use MediaWiki\Logger\LoggerFactory;
25
26 /**
27 * Session storage in object cache.
28 * Used if $wgSessionsInObjectCache is true.
29 *
30 * @ingroup Cache
31 */
32 class ObjectCacheSessionHandler {
33 /** @var array Map of (session ID => SHA-1 of the data) */
34 protected static $hashCache = array();
35
36 /**
37 * Install a session handler for the current web request
38 */
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' ) );
47
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, $wgMemc has already been destroyed. Shutdown functions registered
51 // this way are called before object destruction.
52 register_shutdown_function( array( __CLASS__, 'handleShutdown' ) );
53 }
54
55 /**
56 * Get the cache storage object to use for session storage
57 * @return BagOStuff
58 */
59 protected static function getCache() {
60 global $wgSessionCacheType;
61
62 return ObjectCache::getInstance( $wgSessionCacheType );
63 }
64
65 /**
66 * Get a cache key for the given session id.
67 *
68 * @param string $id Session id
69 * @return string Cache key
70 */
71 protected static function getKey( $id ) {
72 return wfMemcKey( 'session', $id );
73 }
74
75 /**
76 * @param mixed $data
77 * @return string
78 */
79 protected static function getHash( $data ) {
80 return sha1( serialize( $data ) );
81 }
82
83 /**
84 * Callback when opening a session.
85 *
86 * @param string $save_path Path used to store session files, unused
87 * @param string $session_name Session name
88 * @return bool Success
89 */
90 static function open( $save_path, $session_name ) {
91 return true;
92 }
93
94 /**
95 * Callback when closing a session.
96 * NOP.
97 *
98 * @return bool Success
99 */
100 static function close() {
101 return true;
102 }
103
104 /**
105 * Callback when reading session data.
106 *
107 * @param string $id Session id
108 * @return mixed Session data
109 */
110 static function read( $id ) {
111 $stime = microtime( true );
112 $data = self::getCache()->get( self::getKey( $id ) );
113 $real = microtime( true ) - $stime;
114
115 RequestContext::getMain()->getStats()->timing( "session.read", 1000 * $real );
116
117 self::$hashCache = array( $id => self::getHash( $data ) );
118
119 return ( $data === false ) ? '' : $data;
120 }
121
122 /**
123 * Callback when writing session data.
124 *
125 * @param string $id Session id
126 * @param string $data Session data
127 * @return bool Success
128 */
129 static function write( $id, $data ) {
130 global $wgObjectCacheSessionExpiry;
131
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]
135 ) {
136 $stime = microtime( true );
137 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
138 $real = microtime( true ) - $stime;
139
140 RequestContext::getMain()->getStats()->timing( "session.write", 1000 * $real );
141 }
142
143 return true;
144 }
145
146 /**
147 * Callback to destroy a session when calling session_destroy().
148 *
149 * @param string $id Session id
150 * @return bool Success
151 */
152 static function destroy( $id ) {
153 $stime = microtime( true );
154 self::getCache()->delete( self::getKey( $id ) );
155 $real = microtime( true ) - $stime;
156
157 RequestContext::getMain()->getStats()->timing( "session.destroy", 1000 * $real );
158
159 return true;
160 }
161
162 /**
163 * Callback to execute garbage collection.
164 * NOP: Object caches perform garbage collection implicitly
165 *
166 * @param int $maxlifetime Maximum session life time
167 * @return bool Success
168 */
169 static function gc( $maxlifetime ) {
170 return true;
171 }
172
173 /**
174 * Shutdown function.
175 * See the comment inside ObjectCacheSessionHandler::install for rationale.
176 */
177 static function handleShutdown() {
178 session_write_close();
179 }
180
181 /**
182 * Pre-emptive session renewal function
183 */
184 static function renewCurrentSession() {
185 global $wgObjectCacheSessionExpiry;
186
187 // Once a session is at half TTL, renew it
188 $window = $wgObjectCacheSessionExpiry / 2;
189 $logger = LoggerFactory::getInstance( 'SessionHandler' );
190
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;
199
200 $logger->info( "Set expiry for session " . session_id(), array() );
201 } elseif ( ( $now + $window ) > $_SESSION['wsExpiresUnix'] ) {
202 $_SESSION['wsExpiresUnix'] = $now + $wgObjectCacheSessionExpiry;
203
204 $logger->info( "Renewed session " . session_id(), array() );
205 }
206 }
207 }