MongoDB.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. use MongoDB\Model\CollectionInfo;
  16. /**
  17. * Instances of this class are used to interact with a database.
  18. * @link http://www.php.net/manual/en/class.mongodb.php
  19. */
  20. class MongoDB
  21. {
  22. const PROFILING_OFF = 0;
  23. const PROFILING_SLOW = 1;
  24. const PROFILING_ON = 2;
  25. /**
  26. * @var int
  27. */
  28. public $w = 1;
  29. /**
  30. * @var int
  31. */
  32. public $wtimeout = 10000;
  33. /**
  34. * @var \MongoDB\Database
  35. */
  36. protected $db;
  37. /**
  38. * Creates a new database
  39. *
  40. * This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {@see Mongo::__get()} or {@see Mongo::selectDB()}.
  41. * @link http://www.php.net/manual/en/mongodb.construct.php
  42. * @param MongoClient $conn Database connection.
  43. * @param string $name Database name.
  44. * @throws Exception
  45. * @return MongoDB Returns the database.
  46. */
  47. public function __construct($conn, $name)
  48. {
  49. $this->connection = $conn;
  50. $this->name = $name;
  51. $this->db = $this->connection->getClient()->selectDatabase($name);
  52. }
  53. /**
  54. * @return \MongoDB\Database
  55. * @internal
  56. */
  57. public function getDb()
  58. {
  59. return $this->db;
  60. }
  61. /**
  62. * The name of this database
  63. * @link http://www.php.net/manual/en/mongodb.--tostring.php
  64. * @return string Returns this database's name.
  65. */
  66. public function __toString()
  67. {
  68. return $this->name;
  69. }
  70. /**
  71. * (PECL mongo &gt;= 1.0.2)<br/>
  72. * Gets a collection
  73. * @link http://www.php.net/manual/en/mongodb.get.php
  74. * @param string $name The name of the collection.
  75. * @return MongoCollection
  76. */
  77. public function __get($name)
  78. {
  79. return $this->selectCollection($name);
  80. }
  81. /**
  82. * (PECL mongo &gt;= 1.3.0)<br/>
  83. * @link http://www.php.net/manual/en/mongodb.getcollectionnames.php
  84. * Get all collections from this database
  85. * @return array Returns the names of the all the collections in the database as an
  86. * {@link http://www.php.net/manual/en/language.types.array.php array}.
  87. */
  88. public function getCollectionNames(array $options = [])
  89. {
  90. if (is_bool($options)) {
  91. $options = ['includeSystemCollections' => $options];
  92. }
  93. $collections = $this->db->listCollections($options);
  94. $getCollectionName = function (CollectionInfo $collectionInfo) {
  95. return $collectionInfo->getName();
  96. };
  97. return array_map($getCollectionName, (array) $collections);
  98. }
  99. /**
  100. * (PECL mongo &gt;= 0.9.0)<br/>
  101. * Fetches toolkit for dealing with files stored in this database
  102. * @link http://www.php.net/manual/en/mongodb.getgridfs.php
  103. * @param string $prefix [optional] The prefix for the files and chunks collections.
  104. * @return MongoGridFS Returns a new gridfs object for this database.
  105. */
  106. public function getGridFS($prefix = "fs")
  107. {
  108. return new \MongoGridFS($this, $prefix, $prefix);
  109. }
  110. /**
  111. * (PECL mongo &gt;= 0.9.0)<br/>
  112. * Gets this database's profiling level
  113. * @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php
  114. * @return int Returns the profiling level.
  115. */
  116. public function getProfilingLevel()
  117. {
  118. return static::PROFILING_OFF;
  119. }
  120. /**
  121. * (PECL mongo &gt;= 1.1.0)<br/>
  122. * Get slaveOkay setting for this database
  123. * @link http://www.php.net/manual/en/mongodb.getslaveokay.php
  124. * @return bool Returns the value of slaveOkay for this instance.
  125. */
  126. public function getSlaveOkay()
  127. {
  128. return false;
  129. }
  130. /**
  131. * (PECL mongo &gt;= 0.9.0)<br/>
  132. * Sets this database's profiling level
  133. * @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php
  134. * @param int $level Profiling level.
  135. * @return int Returns the previous profiling level.
  136. */
  137. public function setProfilingLevel($level)
  138. {
  139. return static::PROFILING_OFF;
  140. }
  141. /**
  142. * (PECL mongo &gt;= 0.9.0)<br/>
  143. * Drops this database
  144. * @link http://www.php.net/manual/en/mongodb.drop.php
  145. * @return array Returns the database response.
  146. */
  147. public function drop()
  148. {
  149. return $this->db->drop();
  150. }
  151. /**
  152. * Repairs and compacts this database
  153. * @link http://www.php.net/manual/en/mongodb.repair.php
  154. * @param bool $preserve_cloned_files [optional] <p>If cloned files should be kept if the repair fails.</p>
  155. * @param bool $backup_original_files [optional] <p>If original files should be backed up.</p>
  156. * @return array <p>Returns db response.</p>
  157. */
  158. public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE)
  159. {
  160. return [];
  161. }
  162. /**
  163. * (PECL mongo &gt;= 0.9.0)<br/>
  164. * Gets a collection
  165. * @link http://www.php.net/manual/en/mongodb.selectcollection.php
  166. * @param string $name <b>The collection name.</b>
  167. * @throws Exception if the collection name is invalid.
  168. * @return MongoCollection <p>
  169. * Returns a new collection object.
  170. * </p>
  171. */
  172. public function selectCollection($name)
  173. {
  174. return new MongoCollection($this, $name);
  175. }
  176. /**
  177. * (PECL mongo &gt;= 1.1.0)<br/>
  178. * Change slaveOkay setting for this database
  179. * @link http://php.net/manual/en/mongodb.setslaveokay.php
  180. * @param bool $ok [optional] <p>
  181. * If reads should be sent to secondary members of a replica set for all
  182. * possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance.
  183. * </p>
  184. * @return bool Returns the former value of slaveOkay for this instance.
  185. */
  186. public function setSlaveOkay ($ok = true)
  187. {
  188. return false;
  189. }
  190. /**
  191. * Creates a collection
  192. * @link http://www.php.net/manual/en/mongodb.createcollection.php
  193. * @param string $name The name of the collection.
  194. * @param array $options [optional] <p>
  195. * <p>
  196. * An array containing options for the collections. Each option is its own
  197. * element in the options array, with the option name listed below being
  198. * the key of the element. The supported options depend on the MongoDB
  199. * server version. At the moment, the following options are supported:
  200. * </p>
  201. * <p>
  202. * <b>capped</b>
  203. * <p>
  204. * If the collection should be a fixed size.
  205. * </p>
  206. * </p>
  207. * <p>
  208. * <b>size</b>
  209. * <p>
  210. * If the collection is fixed size, its size in bytes.</p></p>
  211. * <p><b>max</b>
  212. * <p>If the collection is fixed size, the maximum number of elements to store in the collection.</p></p>
  213. * <i>autoIndexId</i>
  214. *
  215. * <p>
  216. * If capped is <b>TRUE</b> you can specify <b>FALSE</b> to disable the
  217. * automatic index created on the <em>_id</em> field.
  218. * Before MongoDB 2.2, the default value for
  219. * <em>autoIndexId</em> was <b>FALSE</b>.
  220. * </p>
  221. * </p>
  222. * @return MongoCollection <p>Returns a collection object representing the new collection.</p>
  223. */
  224. public function createCollection($name, $options)
  225. {
  226. $this->db->createCollection($name, $options);
  227. return $this->selectCollection($name);
  228. }
  229. /**
  230. * (PECL mongo &gt;= 0.9.0)<br/>
  231. * @deprecated Use MongoCollection::drop() instead.
  232. * Drops a collection
  233. * @link http://www.php.net/manual/en/mongodb.dropcollection.php
  234. * @param MongoCollection|string $coll MongoCollection or name of collection to drop.
  235. * @return array Returns the database response.
  236. */
  237. public function dropCollection($coll)
  238. {
  239. return $this->db->dropCollection((string) $coll);
  240. }
  241. /**
  242. * (PECL mongo &gt;= 0.9.0)<br/>
  243. * Get a list of collections in this database
  244. * @link http://www.php.net/manual/en/mongodb.listcollections.php
  245. * @param bool $includeSystemCollections [optional] <p>Include system collections.</p>
  246. * @return array Returns a list of MongoCollections.
  247. */
  248. public function listCollections(array $options = [])
  249. {
  250. return array_map([$this, 'selectCollection'], $this->getCollectionNames($options));
  251. }
  252. /**
  253. * (PECL mongo &gt;= 0.9.0)<br/>
  254. * Creates a database reference
  255. * @link http://www.php.net/manual/en/mongodb.createdbref.php
  256. * @param string $collection The collection to which the database reference will point.
  257. * @param mixed $document_or_id <p>
  258. * If an array or object is given, its <em>_id</em> field will be
  259. * used as the reference ID. If a {@see MongoId} or scalar
  260. * is given, it will be used as the reference ID.
  261. * </p>
  262. * @return array <p>Returns a database reference array.</p>
  263. * <p>
  264. * If an array without an <em>_id</em> field was provided as the
  265. * <em>document_or_id</em> parameter, <b>NULL</b> will be returned.
  266. * </p>
  267. */
  268. public function createDBRef($collection, $document_or_id)
  269. {
  270. if (is_object($document_or_id)) {
  271. $id = isset($document_or_id->_id) ? $document_or_id->_id : null;
  272. // $id = $document_or_id->_id ?? null;
  273. } elseif (is_array($document_or_id)) {
  274. if (! isset($document_or_id['_id'])) {
  275. return null;
  276. }
  277. $id = $document_or_id['_id'];
  278. } else {
  279. $id = $document_or_id;
  280. }
  281. return [
  282. '$ref' => $collection,
  283. '$id' => $id,
  284. '$db' => $this->name,
  285. ];
  286. }
  287. /**
  288. * (PECL mongo &gt;= 0.9.0)<br/>
  289. * Fetches the document pointed to by a database reference
  290. * @link http://www.php.net/manual/en/mongodb.getdbref.php
  291. * @param array $ref A database reference.
  292. * @return array Returns the document pointed to by the reference.
  293. */
  294. public function getDBRef(array $ref)
  295. {
  296. $this->notImplemented();
  297. }
  298. /**
  299. * (PECL mongo &gt;= 1.5.0)<br/>
  300. * Get the write concern for this database
  301. * @link http://php.net/manual/en/mongodb.getwriteconcern.php
  302. * @return array <p>This function returns an array describing the write concern.
  303. * The array contains the values w for an integer acknowledgement level or string mode,
  304. * and wtimeout denoting the maximum number of milliseconds to wait for the server to satisfy the write concern.</p>
  305. */
  306. public function getWriteConcern()
  307. {
  308. $this->notImplemented();
  309. }
  310. /**
  311. * (PECL mongo &gt;= 0.9.3)<br/>
  312. * Runs JavaScript code on the database server.
  313. * @link http://www.php.net/manual/en/mongodb.execute.php
  314. * @param MongoCode|string $code Code to execute.
  315. * @param array $args [optional] Arguments to be passed to code.
  316. * @return array Returns the result of the evaluation.
  317. */
  318. public function execute($code, array $args = array())
  319. {
  320. $this->notImplemented();
  321. }
  322. /**
  323. * Execute a database command
  324. * @link http://www.php.net/manual/en/mongodb.command.php
  325. * @param array $data The query to send.
  326. * @param array() $options [optional] <p>
  327. * This parameter is an associative array of the form
  328. * <em>array("optionname" =&gt; &lt;boolean&gt;, ...)</em>. Currently
  329. * supported options are:
  330. * </p><ul>
  331. * <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li>
  332. * </ul>
  333. * @return array Returns database response.
  334. * Every database response is always maximum one document,
  335. * which means that the result of a database command can never exceed 16MB.
  336. * The resulting document's structure depends on the command,
  337. * but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents.
  338. */
  339. public function command(array $data, $options)
  340. {
  341. $this->notImplemented();
  342. }
  343. /**
  344. * (PECL mongo &gt;= 0.9.5)<br/>
  345. * Check if there was an error on the most recent db operation performed
  346. * @link http://www.php.net/manual/en/mongodb.lasterror.php
  347. * @return array Returns the error, if there was one.
  348. */
  349. public function lastError()
  350. {
  351. $this->notImplemented();
  352. }
  353. /**
  354. * (PECL mongo &gt;= 0.9.5)<br/>
  355. * Checks for the last error thrown during a database operation
  356. * @link http://www.php.net/manual/en/mongodb.preverror.php
  357. * @return array Returns the error and the number of operations ago it occurred.
  358. */
  359. public function prevError()
  360. {
  361. $this->notImplemented();
  362. }
  363. /**
  364. * (PECL mongo &gt;= 0.9.5)<br/>
  365. * Clears any flagged errors on the database
  366. * @link http://www.php.net/manual/en/mongodb.reseterror.php
  367. * @return array Returns the database response.
  368. */
  369. public function resetError()
  370. {
  371. $this->notImplemented();
  372. }
  373. /**
  374. * (PECL mongo &gt;= 0.9.5)<br/>
  375. * Creates a database error
  376. * @link http://www.php.net/manual/en/mongodb.forceerror.php
  377. * @return boolean Returns the database response.
  378. */
  379. public function forceError()
  380. {
  381. $this->notImplemented();
  382. }
  383. /**
  384. * (PECL mongo &gt;= 1.0.1)<br/>
  385. * Log in to this database
  386. * @link http://www.php.net/manual/en/mongodb.authenticate.php
  387. * @param string $username The username.
  388. * @param string $password The password (in plaintext).
  389. * @return array <p>Returns database response. If the login was successful, it will return 1.</p>
  390. * <p>
  391. * <span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?&gt;</span>
  392. * </span>
  393. * </code></div>
  394. * </div>
  395. * </p>
  396. * <p> If something went wrong, it will return </p>
  397. * <p>
  398. * <div class="example-contents">
  399. * <div class="phpcode"><code><span style="color: #000000">
  400. * <span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"errmsg"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"auth&nbsp;fails"</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?&gt;</span></p>
  401. * <p>("auth fails" could be another message, depending on database version and
  402. * what went wrong)</p>
  403. */
  404. public function authenticate($username, $password)
  405. {
  406. $this->notImplemented();
  407. }
  408. /**
  409. * (PECL mongo &gt;= 1.3.0)<br/>
  410. * Get the read preference for this database
  411. * @link http://www.php.net/manual/en/mongodb.getreadpreference.php
  412. * @return array This function returns an array describing the read preference. The array contains the values type for the string read preference mode (corresponding to the MongoClient constants), and tagsets containing a list of all tag set criteria. If no tag sets were specified, tagsets will not be present in the array.
  413. */
  414. public function getReadPreference()
  415. {
  416. $this->notImplemented();
  417. }
  418. /**
  419. * (PECL mongo &gt;= 1.3.0)<br/>
  420. * Set the read preference for this database
  421. * @link http://www.php.net/manual/en/mongodb.setreadpreference.php
  422. * @param string $read_preference <p>The read preference mode: <b>MongoClient::RP_PRIMARY</b>, <b>MongoClient::RP_PRIMARY_PREFERRED</b>, <b>MongoClient::RP_SECONDARY</b>, <b>MongoClient::RP_SECONDARY_PREFERRED</b>, or <b>MongoClient::RP_NEAREST</b>.</p>
  423. * @param array $tags [optional] <p>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.</p>
  424. * @return boolean Returns <b>TRUE</b> on success, or <b>FALSE</b> otherwise.
  425. */
  426. public function setReadPreference($read_preference, array $tags)
  427. {
  428. $this->notImplemented();
  429. }
  430. /**
  431. * (PECL mongo &gt;= 1.5.0)<br/>
  432. * @link http://php.net/manual/en/mongodb.setwriteconcern.php
  433. * Set the write concern for this database
  434. * @param mixed $w <p>The write concern. This may be an integer denoting the number of servers required to acknowledge the write, or a string mode (e.g. "majority").</p>
  435. * @param int $wtimeout[optional] <p>The maximum number of milliseconds to wait for the server to satisfy the write concern.</p>
  436. * @return boolean Returns <b>TRUE</b> on success, or <b>FALSE</b> otherwise.
  437. */
  438. public function setWriteConcern($w, $wtimeout)
  439. {
  440. $this->notImplemented();
  441. }
  442. protected function notImplemented()
  443. {
  444. throw new \Exception('Not implemented');
  445. }
  446. }