Merge "MediaWiki.php: Redirect non-standard title urls to canonical"
[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 /** @var array Map of (session ID => SHA-1 of the data) */
32 protected static $hashCache = array();
33
34 const TTL_REFRESH_WINDOW = 600; // refresh if expiring in 10 minutes
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 $data = self::getCache()->get( self::getKey( $id ) );
112
113 self::$hashCache = array( $id => self::getHash( $data ) );
114
115 return ( $data === false ) ? '' : $data;
116 }
117
118 /**
119 * Callback when writing session data.
120 *
121 * @param string $id Session id
122 * @param string $data Session data
123 * @return bool Success
124 */
125 static function write( $id, $data ) {
126 global $wgObjectCacheSessionExpiry;
127
128 // Only issue a write if anything changed (PHP 5.6 already does this)
129 if ( !isset( self::$hashCache[$id] )
130 || self::getHash( $data ) !== self::$hashCache[$id]
131 ) {
132 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
133 }
134
135 return true;
136 }
137
138 /**
139 * Callback to destroy a session when calling session_destroy().
140 *
141 * @param string $id Session id
142 * @return bool Success
143 */
144 static function destroy( $id ) {
145 self::getCache()->delete( self::getKey( $id ) );
146
147 return true;
148 }
149
150 /**
151 * Callback to execute garbage collection.
152 * NOP: Object caches perform garbage collection implicitly
153 *
154 * @param int $maxlifetime Maximum session life time
155 * @return bool Success
156 */
157 static function gc( $maxlifetime ) {
158 return true;
159 }
160
161 /**
162 * Shutdown function.
163 * See the comment inside ObjectCacheSessionHandler::install for rationale.
164 */
165 static function handleShutdown() {
166 global $wgObjectCacheSessionExpiry;
167
168 $now = microtime( true );
169 // Session are only written in object stores when $_SESSION changes,
170 // which also renews the TTL ($wgObjectCacheSessionExpiry). If a user
171 // is active but not causing session data changes, it may suddenly
172 // as they view a form, blocking the first submission.
173 // Make a dummy change every so often to avoid this.
174 if ( !isset( $_SESSION['wsExpiresUnix'] )
175 || ( $now + self::TTL_REFRESH_WINDOW ) > isset( $_SESSION['wsExpiresUnix'] )
176 ) {
177 $_SESSION['wsExpiresUnix'] = $now + $wgObjectCacheSessionExpiry;
178 }
179
180 session_write_close();
181 }
182 }