mw.ui: button: Update focus state
[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 /**
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
60 return ObjectCache::getInstance( $wgSessionCacheType );
61 }
62
63 /**
64 * Get a cache key for the given session id.
65 *
66 * @param string $id Session id
67 * @return string Cache key
68 */
69 protected static function getKey( $id ) {
70 return wfMemcKey( 'session', $id );
71 }
72
73 /**
74 * @param mixed $data
75 * @return string
76 */
77 protected static function getHash( $data ) {
78 return sha1( serialize( $data ) );
79 }
80
81 /**
82 * Callback when opening a session.
83 *
84 * @param string $save_path Path used to store session files, unused
85 * @param string $session_name Session name
86 * @return bool Success
87 */
88 static function open( $save_path, $session_name ) {
89 return true;
90 }
91
92 /**
93 * Callback when closing a session.
94 * NOP.
95 *
96 * @return bool Success
97 */
98 static function close() {
99 return true;
100 }
101
102 /**
103 * Callback when reading session data.
104 *
105 * @param string $id Session id
106 * @return mixed Session data
107 */
108 static function read( $id ) {
109 $data = self::getCache()->get( self::getKey( $id ) );
110
111 self::$hashCache = array( $id => self::getHash( $data ) );
112
113 return ( $data === false ) ? '' : $data;
114 }
115
116 /**
117 * Callback when writing session data.
118 *
119 * @param string $id Session id
120 * @param mixed $data Session data
121 * @return bool Success
122 */
123 static function write( $id, $data ) {
124 global $wgObjectCacheSessionExpiry;
125
126 // Only issue a write if anything changed (PHP 5.6 already does this)
127 if ( !isset( self::$hashCache[$id] )
128 || self::getHash( $data ) !== self::$hashCache[$id]
129 ) {
130 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
131 }
132
133 return true;
134 }
135
136 /**
137 * Callback to destroy a session when calling session_destroy().
138 *
139 * @param string $id Session id
140 * @return bool Success
141 */
142 static function destroy( $id ) {
143 self::getCache()->delete( self::getKey( $id ) );
144
145 return true;
146 }
147
148 /**
149 * Callback to execute garbage collection.
150 * NOP: Object caches perform garbage collection implicitly
151 *
152 * @param int $maxlifetime Maximum session life time
153 * @return bool Success
154 */
155 static function gc( $maxlifetime ) {
156 return true;
157 }
158
159 /**
160 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
161 * for rationale.
162 */
163 static function handleShutdown() {
164 session_write_close();
165 }
166 }