Merge "Remove meaningless default action name"
[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 * @return BagOStuff
53 */
54 static function getCache() {
55 global $wgSessionCacheType;
56 return ObjectCache::getInstance( $wgSessionCacheType );
57 }
58
59 /**
60 * Get a cache key for the given session id.
61 *
62 * @param string $id Session id
63 * @return string Cache key
64 */
65 static function getKey( $id ) {
66 return wfMemcKey( 'session', $id );
67 }
68
69 /**
70 * Callback when opening a session.
71 *
72 * @param string $save_path Path used to store session files, unused
73 * @param string $session_name Session name
74 * @return bool Success
75 */
76 static function open( $save_path, $session_name ) {
77 return true;
78 }
79
80 /**
81 * Callback when closing a session.
82 * NOP.
83 *
84 * @return bool Success
85 */
86 static function close() {
87 return true;
88 }
89
90 /**
91 * Callback when reading session data.
92 *
93 * @param string $id Session id
94 * @return mixed Session data
95 */
96 static function read( $id ) {
97 $data = self::getCache()->get( self::getKey( $id ) );
98 if ( $data === false ) {
99 return '';
100 }
101 return $data;
102 }
103
104 /**
105 * Callback when writing session data.
106 *
107 * @param string $id Session id
108 * @param mixed $data Session data
109 * @return bool Success
110 */
111 static function write( $id, $data ) {
112 global $wgObjectCacheSessionExpiry;
113 self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
114 return true;
115 }
116
117 /**
118 * Callback to destroy a session when calling session_destroy().
119 *
120 * @param string $id Session id
121 * @return bool Success
122 */
123 static function destroy( $id ) {
124 self::getCache()->delete( self::getKey( $id ) );
125 return true;
126 }
127
128 /**
129 * Callback to execute garbage collection.
130 * NOP: Object caches perform garbage collection implicitly
131 *
132 * @param int $maxlifetime Maximum session life time
133 * @return bool Success
134 */
135 static function gc( $maxlifetime ) {
136 return true;
137 }
138
139 /**
140 * Shutdown function. See the comment inside ObjectCacheSessionHandler::install
141 * for rationale.
142 */
143 static function handleShutdown() {
144 session_write_close();
145 }
146 }