]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/wikimedia/cdb/src/Reader.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / wikimedia / cdb / src / Reader.php
1 <?php
2
3 namespace Cdb;
4
5 /**
6  * Native CDB file reader and writer.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @file
24  */
25
26 /**
27  * Read from a CDB file.
28  * Native and pure PHP implementations are provided.
29  * http://cr.yp.to/cdb.html
30  */
31 abstract class Reader {
32         /**
33          * The file handle
34          */
35         protected $handle;
36
37         /**
38          * Open a file and return a subclass instance
39          *
40          * @param string $fileName
41          *
42          * @return Reader
43          */
44         public static function open( $fileName ) {
45                 return self::haveExtension() ?
46                         new Reader\DBA( $fileName ) :
47                         new Reader\PHP( $fileName );
48         }
49
50         /**
51          * Returns true if the native extension is available
52          *
53          * @return bool
54          */
55         public static function haveExtension() {
56                 if ( !function_exists( 'dba_handlers' ) ) {
57                         return false;
58                 }
59                 $handlers = dba_handlers();
60                 if ( !in_array( 'cdb', $handlers ) || !in_array( 'cdb_make', $handlers ) ) {
61                         return false;
62                 }
63
64                 return true;
65         }
66
67         /**
68          * Create the object and open the file
69          *
70          * @param string $fileName
71          */
72         abstract public function __construct( $fileName );
73
74         /**
75          * Close the file. Optional, you can just let the variable go out of scope.
76          */
77         abstract public function close();
78
79         /**
80          * Get a value with a given key. Only string values are supported.
81          *
82          * @param string $key
83          */
84         abstract public function get( $key );
85
86         /**
87          * Check whether key exists
88          *
89          * @param string $key
90          */
91         abstract public function exists( $key );
92
93         /**
94          * Fetch first key
95          */
96         abstract public function firstkey();
97
98         /**
99          * Fetch next key
100          */
101         abstract public function nextkey();
102 }