|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * RMongo for compatibility between Mongo and MongoClient |
| 4 | + * |
| 5 | + |
| 6 | + */ |
| 7 | +class RMongo { |
| 8 | + private static $_lastId; |
| 9 | + |
| 10 | + private $_mongo; |
| 11 | + |
| 12 | + /** |
| 13 | + * Contruct a new object |
| 14 | + * |
| 15 | + * @param string $server Server definition |
| 16 | + * @param array $options Options |
| 17 | + */ |
| 18 | + public function __construct($server, array $options = array()) { |
| 19 | + if (class_exists("MongoClient")) { |
| 20 | + $this->_mongo = new MongoClient($server, $options); |
| 21 | + } |
| 22 | + else { |
| 23 | + $this->_mongo = new Mongo($server, $options); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Closes this connection |
| 29 | + * |
| 30 | + * @param boolean|string $connection Connection |
| 31 | + * @return boolean |
| 32 | + */ |
| 33 | + public function close($connection) { |
| 34 | + return $this->_mongo->close($connection); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Connects to a database server |
| 39 | + */ |
| 40 | + public function connect() { |
| 41 | + return $this->_mongo->connect(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Drops a database |
| 46 | + * |
| 47 | + * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + public function dropDB($db) { |
| 51 | + if (!is_object($db)) { |
| 52 | + $db = $this->selectDB($db); |
| 53 | + } |
| 54 | + if (method_exists($db, "drop")) { |
| 55 | + return $db->drop(); |
| 56 | + } |
| 57 | + if (method_exists($this->_mongo, "dropDB")) { |
| 58 | + $this->_mongo->dropDB($db); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Force server to response error |
| 64 | + */ |
| 65 | + public function forceError() { |
| 66 | + if (method_exists($this->_mongo, "forceError")) { |
| 67 | + return $this->_mongo->forceError(); |
| 68 | + } |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Gets a database |
| 74 | + * |
| 75 | + * @param string $dbname The database name |
| 76 | + * @return MongoDB |
| 77 | + */ |
| 78 | + public function __get($dbname) { |
| 79 | + return $this->_mongo->$dbname; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Updates status for all associated hosts |
| 84 | + * |
| 85 | + * @return array |
| 86 | + * @todo implement it under different versions |
| 87 | + */ |
| 88 | + public function getHosts() { |
| 89 | + if (method_exists($this->_mongo, "getHosts")) { |
| 90 | + return $this->_mongo->getHosts(); |
| 91 | + } |
| 92 | + return array(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get the read preference for this connection |
| 97 | + * |
| 98 | + * @return array |
| 99 | + * @todo implement it under different versions |
| 100 | + */ |
| 101 | + public function getReadPreference() { |
| 102 | + if (method_exists($this->_mongo, "getReadPreference")) { |
| 103 | + return $this->_mongo->getReadPreference(); |
| 104 | + } |
| 105 | + return array(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get last erro |
| 110 | + * |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + public function lastError() { |
| 114 | + if (method_exists($this->_mongo, "lastError")) { |
| 115 | + return $this->_mongo->lastError(); |
| 116 | + } |
| 117 | + return array(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Lists all of the databases available |
| 122 | + * |
| 123 | + * @return array |
| 124 | + */ |
| 125 | + public function listDBs() { |
| 126 | + return $this->_mongo->listDBs(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Connect pair servers |
| 131 | + * |
| 132 | + * @return boolean |
| 133 | + */ |
| 134 | + public function pairConnect() { |
| 135 | + if (method_exists($this->_mongo, "pairConnect")) { |
| 136 | + return $this->_mongo->pairConnect(); |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Create pair persist connection |
| 143 | + * |
| 144 | + * @param string $username |
| 145 | + * @param string $password |
| 146 | + * @return boolean |
| 147 | + */ |
| 148 | + public function pairPersistConnect($username = "" , $password = "") { |
| 149 | + if (method_exists($this->_mongo, "pairPersistConnect")) { |
| 150 | + return $this->_mongo->pairPersistConnect($username, $password); |
| 151 | + } |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Create persist connection |
| 157 | + * |
| 158 | + * @param string $username Username |
| 159 | + * @param string $password Password |
| 160 | + * @return boolean |
| 161 | + */ |
| 162 | + public function persistConnect($username = "" , $password = "" ) { |
| 163 | + if (method_exists($this->_mongo, "persistConnect")) { |
| 164 | + return $this->_mongo->persistConnect($username, $password); |
| 165 | + } |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Get previous error |
| 171 | + * |
| 172 | + * @return array |
| 173 | + */ |
| 174 | + public function prevError() { |
| 175 | + if (method_exists($this->_mongo, "prevError")) { |
| 176 | + return $this->_mongo->prevError(); |
| 177 | + } |
| 178 | + return array(); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Reset error |
| 183 | + * |
| 184 | + * @return array |
| 185 | + */ |
| 186 | + public function resetError() { |
| 187 | + if (method_exists($this->_mongo, "resetError")) { |
| 188 | + return $this->_mongo->resetError(); |
| 189 | + } |
| 190 | + return array(); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Gets a database collection |
| 195 | + * |
| 196 | + * @param string $db The database name |
| 197 | + * @param string $collection The collection name |
| 198 | + * @return MongoCollection |
| 199 | + */ |
| 200 | + public function selectCollection($db, $collection) { |
| 201 | + return $this->_mongo->selectCollection($db, $collection); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Gets a database |
| 206 | + * |
| 207 | + * @param string $db The database name |
| 208 | + * @return MongoDB |
| 209 | + */ |
| 210 | + public function selectDB($db) { |
| 211 | + return $this->_mongo->selectDB($db); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Set the read preference for this connection |
| 216 | + * |
| 217 | + * @param int $readPreference The read preference mode: Mongo::RP_PRIMARY, Mongo::RP_PRIMARY_PREFERRED, Mongo::RP_SECONDARY, Mongo::RP_SECONDARY_PREFERRED, or Mongo::RP_NEAREST |
| 218 | + * @param array $tags An array of zero or more tag sets, where each tag set is itself an array of criteria used to match tags on replica set members |
| 219 | + * @return boolean |
| 220 | + */ |
| 221 | + public function setReadPreference($readPreference, array $tags = array()) { |
| 222 | + if (method_exists($this->_mongo, "setReadPreference")) { |
| 223 | + return $this->_mongo->setReadPreference($readPreference, $tags); |
| 224 | + } |
| 225 | + return false; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Change slaveOkay setting for this connection |
| 230 | + * |
| 231 | + * @param boolean $ok If reads should be sent to secondary members of a replica set for all possible queries using this Mongo instance |
| 232 | + * @return boolean |
| 233 | + */ |
| 234 | + public function setSlaveOkay($ok) { |
| 235 | + if (method_exists($this->_mongo, "setSlaveOkay")) { |
| 236 | + return $this->_mongo->setSlaveOkay($ok); |
| 237 | + } |
| 238 | + elseif (method_exists($this->_mongo, 'setReadPreference')) { |
| 239 | + $result = $this->_mongo->setReadPreference(MongoClient::RP_PRIMARY_PREFERRED); |
| 240 | + } |
| 241 | + else { |
| 242 | + $result = false; |
| 243 | + } |
| 244 | + return $result; |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * String representation of this connection |
| 249 | + * |
| 250 | + * @return string |
| 251 | + */ |
| 252 | + public function __toString() { |
| 253 | + return $this->_mongo->__toString(); |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Get mongo driver version |
| 258 | + * |
| 259 | + * @return string |
| 260 | + * @since 1.1.4 |
| 261 | + */ |
| 262 | + public static function getVersion() { |
| 263 | + if (class_exists("MongoClient")) { |
| 264 | + return MongoClient::VERSION; |
| 265 | + } |
| 266 | + if (class_exists("Mongo")) { |
| 267 | + return Mongo::VERSION; |
| 268 | + } |
| 269 | + return "0"; |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Compare another version with current version |
| 274 | + * |
| 275 | + * @param string $version Version to compare |
| 276 | + * @return integer -1,0,1 |
| 277 | + * @since 1.1.4 |
| 278 | + */ |
| 279 | + public static function compareVersion($version) { |
| 280 | + $currentVersion = self::getVersion(); |
| 281 | + preg_match("/^[\\.\\d]+/", $currentVersion, $match); |
| 282 | + $number = $match[0]; |
| 283 | + return version_compare($number, $version); |
| 284 | + } |
| 285 | + |
| 286 | + static function setLastInsertId($lastId) { |
| 287 | + self::$_lastId = $lastId; |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Enter description here... |
| 292 | + * |
| 293 | + * @return string |
| 294 | + */ |
| 295 | + static function lastInsertId() { |
| 296 | + return self::$_lastId; |
| 297 | + } |
| 298 | +} |
| 299 | + |
| 300 | +?> |
0 commit comments