Merge "Fixes for DatabaseMysql::open()"
[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 * Install a session handler for the current web request
33 */
34 static function install() {
35 session_set_save_handler(
36 array( __CLASS__, 'open' ),
37 array( __CLASS__, 'close' ),
38 array( __CLASS__, 'read' ),
39 array( __CLASS__, 'write' ),
40 array( __CLASS__, 'destroy' ),
41 array( __CLASS__, 'gc' ) );
42
43 // It's necessary to register a shutdown function to call session_write_close(),
44 // because by the time the request shutdown function for the session module is
45 // called, $wgMemc has already been destroyed. Shutdown functions registered
46 // this way are called before object destruction.
47 register_shutdown_function( array( __CLASS__, 'handleShutdown' ) );
48 }
49
50 /**
51 * Get the cache storage object to use for session storage
52 */
53 static function getCache() {
54 global $wgSessionCacheType;
55 return ObjectCache::getInstance( $wgSessionCacheType );
56 }
57
58 /**
59 * Get a cache key for the given session id.
60 *
61 * @param $id String: session id
62 * @return String: cache key
63 */
64 static function getKey( $id ) {
65 return wfMemcKey( 'session', $id );
66 }
67
68 /**
69 * Callback when opening a session.
70 *
71 * @param $save_path String: path used to store session files, unused
72 * @param $session_name String: session name
73 * @return Boolean: success
74 */
75 static function open( $save_path, $session_name ) {
76 return true;
77 }
78
79 /**
80 * Callback when closing a session.
81 * NOP.
82 *
83 * @return Boolean: success
84 */
85 static function close() {
86 return true;
87 }
88
89 /**
90 * Callback when reading session data.
91 *
92 * @param $id String: session id
93 * @return Mixed: session data
94 */
95 static function read( $id ) {
96 $data = self::getCache()->get( self::getKey( $id ) );
97 if( $data === false ) {
98 return '';
99 }
100 return $data;
101 }
102
103 /**
104 * Callback when writing session data.
105 *
106 * @param $id String: session id
107 * @param $data Mixed: session data
108 * @return Boolean: success
109 */
110 static function write( $id, $data ) {
111 global $wgObjectCacheSessionExpiry;
112 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
113 return true;
114 }
115
116 /**
117 * Callback to destroy a session when calling session_destroy().
118 *
119 * @param $id String: session id
120 * @return Boolean: success
121 */
122 static function destroy( $id ) {
123 self::getCache()->delete( self::getKey( $id ) );
124 return true;
125 }
126
127 /**
128 * Callback to execute garbage collection.
129 * NOP: Object caches perform garbage collection implicitly
130 *
131 * @param $maxlifetime Integer: maximum session life time
132 * @return Boolean: success
133 */
134 static function gc( $maxlifetime ) {
135 return true;
136 }
137
138 /**
139 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
140 * for rationale.
141 */
142 static function handleShutdown() {
143 session_write_close();
144 }
145 }