Merge "A clearer wording for apihelp-query+info-paramvalue-prop-displaytitle"
[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 /**
25 * Session storage in object cache.
26 * Used if $wgSessionsInObjectCache is true.
27 *
28 * @ingroup Cache
29 */
30 class ObjectCacheSessionHandler {
31
32 const TTL_REFRESH_WINDOW = 600; // refresh if expiring in 10 minutes
33
34 /**
35 * Install a session handler for the current web request
36 */
37 static function install() {
38 session_set_save_handler(
39 array( __CLASS__, 'open' ),
40 array( __CLASS__, 'close' ),
41 array( __CLASS__, 'read' ),
42 array( __CLASS__, 'write' ),
43 array( __CLASS__, 'destroy' ),
44 array( __CLASS__, 'gc' ) );
45
46 // It's necessary to register a shutdown function to call session_write_close(),
47 // because by the time the request shutdown function for the session module is
48 // called, $wgMemc has already been destroyed. Shutdown functions registered
49 // this way are called before object destruction.
50 register_shutdown_function( array( __CLASS__, 'handleShutdown' ) );
51 }
52
53 /**
54 * Get the cache storage object to use for session storage
55 * @return BagOStuff
56 */
57 protected static function getCache() {
58 global $wgSessionCacheType;
59 return ObjectCache::getInstance( $wgSessionCacheType );
60 }
61
62 /**
63 * Get a cache key for the given session id.
64 *
65 * @param string $id Session id
66 * @return string Cache key
67 */
68 protected static function getKey( $id ) {
69 return wfMemcKey( 'session', $id );
70 }
71
72 /**
73 * Callback when opening a session.
74 *
75 * @param string $save_path Path used to store session files, unused
76 * @param string $session_name Session name
77 * @return bool Success
78 */
79 static function open( $save_path, $session_name ) {
80 return true;
81 }
82
83 /**
84 * Callback when closing a session.
85 * NOP.
86 *
87 * @return bool Success
88 */
89 static function close() {
90 return true;
91 }
92
93 /**
94 * Callback when reading session data.
95 *
96 * @param string $id Session id
97 * @return mixed Session data
98 */
99 static function read( $id ) {
100 $data = self::getCache()->get( self::getKey( $id ) );
101 if ( $data === false ) {
102 return '';
103 }
104 return $data;
105 }
106
107 /**
108 * Callback when writing session data.
109 *
110 * @param string $id Session id
111 * @param string $data Session data
112 * @return bool Success
113 */
114 static function write( $id, $data ) {
115 global $wgObjectCacheSessionExpiry;
116 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
117 return true;
118 }
119
120 /**
121 * Callback to destroy a session when calling session_destroy().
122 *
123 * @param string $id Session id
124 * @return bool Success
125 */
126 static function destroy( $id ) {
127 self::getCache()->delete( self::getKey( $id ) );
128
129 return true;
130 }
131
132 /**
133 * Callback to execute garbage collection.
134 * NOP: Object caches perform garbage collection implicitly
135 *
136 * @param int $maxlifetime Maximum session life time
137 * @return bool Success
138 */
139 static function gc( $maxlifetime ) {
140 return true;
141 }
142
143 /**
144 * Shutdown function.
145 * See the comment inside ObjectCacheSessionHandler::install for rationale.
146 */
147 static function handleShutdown() {
148 global $wgObjectCacheSessionExpiry;
149
150 $now = microtime( true );
151 // Session are only written in object stores when $_SESSION changes,
152 // which also renews the TTL ($wgObjectCacheSessionExpiry). If a user
153 // is active but not causing session data changes, it may suddenly
154 // as they view a form, blocking the first submission.
155 // Make a dummy change every so often to avoid this.
156 if ( !isset( $_SESSION['wsExpiresUnix'] )
157 || ( $now + self::TTL_REFRESH_WINDOW ) > isset( $_SESSION['wsExpiresUnix'] )
158 ) {
159 $_SESSION['wsExpiresUnix'] = $now + $wgObjectCacheSessionExpiry;
160 }
161
162 session_write_close();
163 }
164 }