Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Couch often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Couch, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | final class Couch extends Surfer { |
||
28 | |||
29 | //! The user agent name. |
||
30 | const USER_AGENT_NAME = "EoC Client"; |
||
31 | |||
32 | //! Default CouchDB revisions limit number. |
||
33 | const REVS_LIMIT = 1000; |
||
34 | |||
35 | /** @name Document Paths */ |
||
36 | //!@{ |
||
37 | |||
38 | const STD_DOC_PATH = ""; //!< Path for standard documents. |
||
39 | const LOCAL_DOC_PATH = "_local/"; //!< Path for local documents. |
||
40 | const DESIGN_DOC_PATH = "_design/"; //!< Path for design documents. |
||
41 | |||
42 | //!@} |
||
43 | |||
44 | // Socket or cURL HTTP client adapter. |
||
45 | private $client; |
||
46 | |||
47 | // A database's name prefix. |
||
48 | private $prefix = ''; |
||
49 | |||
50 | |||
51 | /** |
||
52 | * @brief Creates a Couch class instance. |
||
53 | * @param[in] IClientAdapter $adapter An instance of a class that implements the IClientAdapter interface. |
||
54 | */ |
||
55 | public function __construct(IClientAdapter $adapter) { |
||
58 | |||
59 | |||
60 | /** |
||
61 | * @brief Returns a CouchDB wild card. |
||
62 | * @details A standard object is translated to JSON as `{}` same of a JavaScript empty object. |
||
63 | * @return [stdClass](http://php.net/manual/en/language.types.object.php) |
||
64 | */ |
||
65 | public static function WildCard() { |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @brief Sets the document class and type in case the document hasn't one. |
||
72 | * @param[in] Doc\IDoc $doc A document. |
||
73 | */ |
||
74 | private function setDocInfo(Doc\IDoc $doc) { |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @brief Overrides the rows, adding a new row for every key hasn't been matched. |
||
90 | * @details CouchDB doesn't return rows for the keys a match is not found. To make joins having a row for each key is |
||
91 | * essential. The algorithm below overrides the rows, adding a new row for every key hasn't been matched. The JSON |
||
92 | * encoding is necessary because we might have complex keys. A complex key is no more than an array. |
||
93 | * @param array $keys An array containing the keys. |
||
94 | * @param[out] array $rows An associative array containing the rows. |
||
95 | */ |
||
96 | private function addMissingRows($keys, &$rows) { |
||
122 | |||
123 | |||
124 | /** |
||
125 | * @brief Sets a prefix which is used to compose the database's name. |
||
126 | * @param string $prefix A string prefix. |
||
127 | */ |
||
128 | public function setDbPrefix($prefix) { |
||
131 | |||
132 | |||
133 | /** |
||
134 | * @brief Gets the prefix used to compose the database's name if any. |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getDbPrefix() { |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @brief This method is used to send a Request to CouchDB. |
||
144 | * @details If you want call a not supported CouchDB API, you can use this function to send your request.\n |
||
145 | * You can also provide an instance of a class that implements the IChunkHook interface, to deal with a chunked |
||
146 | * response. |
||
147 | * @param Request $request The Request object. |
||
148 | * @param IChunkHook $chunkHook (optional) A class instance that implements the IChunkHook interface. |
||
149 | * @return Response |
||
150 | */ |
||
151 | public function send(Request $request, IChunkHook $chunkHook = NULL) { |
||
164 | |||
165 | |||
166 | /** @name Validation and Encoding Methods */ |
||
167 | //!@{ |
||
168 | |||
169 | /** |
||
170 | * @brief This method raise an exception when a user provide an invalid document path. |
||
171 | * @details This method is called by any other methods that interacts with CouchDB. You don't need to call, unless |
||
172 | * you are making a not supported call to CouchDB. |
||
173 | * @param string $path Document path. |
||
174 | * @param bool $excludeLocal Document path. |
||
175 | */ |
||
176 | public function validateDocPath($path, $excludeLocal = FALSE) { |
||
187 | |||
188 | |||
189 | /** |
||
190 | * @brief Encodes the document id. |
||
191 | * @details This method is called by any other methods that interacts with CouchDB. You don't need to call, unless |
||
192 | * you are making a not supported call to CouchDB. |
||
193 | * @param string $docId Document id. |
||
194 | */ |
||
195 | public function validateAndEncodeDocId(&$docId) { |
||
201 | |||
202 | //!@} |
||
203 | |||
204 | |||
205 | /** @name Miscellaneous Methods */ |
||
206 | //!@{ |
||
207 | |||
208 | /** |
||
209 | * @brief Creates the admin user. |
||
210 | * @todo Implement the method. |
||
211 | */ |
||
212 | public function createAdminUser() { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @brief Restarts the server. |
||
219 | * @attention Requires admin privileges. |
||
220 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#restart |
||
221 | * @bug [COUCHDB-947](https://issues.apache.org/jira/browse/COUCHDB-947) |
||
222 | */ |
||
223 | public function restartServer() { |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @brief Returns an object that contains MOTD, server and client and PHP versions. |
||
241 | * @details The MOTD can be specified in CouchDB configuration files. This function returns more information |
||
242 | * compared to the CouchDB standard REST call. |
||
243 | * @return Info::ServerInfo |
||
244 | */ |
||
245 | public function getServerInfo() { |
||
250 | |||
251 | |||
252 | /** |
||
253 | * @brief Returns information about the Elephant on Couch client. |
||
254 | * @return Info::ClientInfo |
||
255 | */ |
||
256 | public function getClientInfo() { |
||
259 | |||
260 | |||
261 | /** |
||
262 | * @brief Returns the favicon.ico file. |
||
263 | * @details The favicon is a part of the admin interface, but the handler for it is special as CouchDB tries to make |
||
264 | * sure that the favicon is cached for one year. Returns a string that represents the icon. |
||
265 | * @return string |
||
266 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#favicon-ico |
||
267 | */ |
||
268 | public function getFavicon() { |
||
276 | |||
277 | |||
278 | /** |
||
279 | * @brief Returns server statistics. |
||
280 | * @return array An associative array |
||
281 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#stats |
||
282 | */ |
||
283 | public function getStats() { |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @brief Returns a list of all databases on this server. |
||
290 | * @return array of string |
||
291 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#all-dbs |
||
292 | */ |
||
293 | public function getAllDbs() { |
||
296 | |||
297 | |||
298 | /** |
||
299 | * @brief Returns a list of all database events in the CouchDB instance. |
||
300 | * @param DbUpdatesFeedOpts $opts Additional options. |
||
301 | * @return Response |
||
302 | * @attention Requires admin privileges. |
||
303 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#db-updates |
||
304 | */ |
||
305 | public function getDbUpdates(Opt\DbUpdatesFeedOpts $opts = NULL) { |
||
313 | |||
314 | |||
315 | /** |
||
316 | * @brief Returns a list of running tasks. |
||
317 | * @attention Requires admin privileges. |
||
318 | * @return array An associative array |
||
319 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#active-tasks |
||
320 | */ |
||
321 | public function getActiveTasks() { |
||
324 | |||
325 | |||
326 | /** |
||
327 | * @brief Returns the tail of the server's log file. |
||
328 | * @attention Requires admin privileges. |
||
329 | * @param integer $bytes How many bytes to return from the end of the log file. |
||
330 | * @return string |
||
331 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#log |
||
332 | */ |
||
333 | public function getLogTail($bytes = 1000) { |
||
342 | |||
343 | |||
344 | /** |
||
345 | * @brief Returns a list of generated UUIDs. |
||
346 | * @param integer $count Requested UUIDs number. |
||
347 | * @return string|array If `$count == 1` (default) returns a string else returns an array of strings. |
||
348 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#uuids |
||
349 | */ |
||
350 | public function getUuids($count = 1) { |
||
365 | |||
366 | //!@} |
||
367 | |||
368 | |||
369 | /** @name Server Configuration Methods */ |
||
370 | //!@{ |
||
371 | |||
372 | /** |
||
373 | * @brief Returns the entire server configuration or a single section or a single configuration value of a section. |
||
374 | * @param string $section Requested section. |
||
375 | * @param string $key Requested key. |
||
376 | * @return string|array An array with the configuration keys or a simple string in case of a single key. |
||
377 | * @see http://docs.couchdb.org/en/latest/api/server/configuration.html#get--_config |
||
378 | * @see http://docs.couchdb.org/en/latest/api/server/configuration.html#get--_config-section |
||
379 | * @see http://docs.couchdb.org/en/latest/api/server/configuration.html#config-section-key |
||
380 | */ |
||
381 | public function getConfig($section = "", $key = "") { |
||
393 | |||
394 | |||
395 | /** |
||
396 | * @brief Sets a single configuration value in a given section to server configuration. |
||
397 | * @param string $section The configuration section. |
||
398 | * @param string $key The key. |
||
399 | * @param string $value The value for the key. |
||
400 | * @see http://docs.couchdb.org/en/latest/api/server/configuration.html#put--_config-section-key |
||
401 | */ |
||
402 | public function setConfigKey($section, $key, $value) { |
||
417 | |||
418 | |||
419 | /** |
||
420 | * @brief Deletes a single configuration value from a given section in server configuration. |
||
421 | * @param string $section The configuration section. |
||
422 | * @param string $key The key. |
||
423 | * @see http://docs.couchdb.org/en/latest/api/configuration.html#delete-config-section-key |
||
424 | */ |
||
425 | public function deleteConfigKey($section, $key) { |
||
434 | |||
435 | //!@} |
||
436 | |||
437 | |||
438 | /** @name Cookie Authentication */ |
||
439 | //!@{ |
||
440 | |||
441 | /** |
||
442 | * @brief Returns cookie based login user information. |
||
443 | * @return Response |
||
444 | * @see http://docs.couchdb.org/en/latest/api/server/authn.html#get--_session |
||
445 | */ |
||
446 | public function getSession() { |
||
449 | |||
450 | |||
451 | /** |
||
452 | * @brief Makes cookie based user login. |
||
453 | * @return Response |
||
454 | * @see http://docs.couchdb.org/en/latest/api/server/authn.html#post--_session |
||
455 | */ |
||
456 | public function setSession($userName, $password) { |
||
473 | |||
474 | |||
475 | /** |
||
476 | * @brief Makes user logout. |
||
477 | * @return Response |
||
478 | * @see http://docs.couchdb.org/en/latest/api/server/authn.html#delete--_session |
||
479 | */ |
||
480 | public function deleteSession() { |
||
483 | |||
484 | //!@} |
||
485 | |||
486 | |||
487 | /** @name OAuth Authentication */ |
||
488 | //! @see http://docs.couchdb.org/en/latest/api/server/authn.html#oauth-authentication |
||
489 | //!@{ |
||
490 | |||
491 | /** |
||
492 | * @brief |
||
493 | * @todo To be implemented and documented. |
||
494 | */ |
||
495 | public function getAccessToken() { |
||
498 | |||
499 | |||
500 | /** |
||
501 | * @brief |
||
502 | * @todo To be implemented and documented. |
||
503 | */ |
||
504 | public function getAuthorize() { |
||
507 | |||
508 | |||
509 | /** |
||
510 | * @brief |
||
511 | * @todo To be implemented and documented. |
||
512 | */ |
||
513 | public function setAuthorize() { |
||
516 | |||
517 | |||
518 | /** |
||
519 | * @brief |
||
520 | * @todo To be implemented and documented. |
||
521 | */ |
||
522 | public function requestToken() { |
||
525 | |||
526 | //!@} |
||
527 | |||
528 | |||
529 | /** |
||
530 | * @brief Creates a new database and selects it. |
||
531 | * @param string $name The database name. A database must be named with all lowercase letters (a-z), |
||
532 | * digits (0-9), or any of the _$()+-/ characters and must end with a slash in the URL. The name has to start with a |
||
533 | * lowercase letter (a-z). |
||
534 | * @see http://docs.couchdb.org/en/latest/api/database/common.html#put--db |
||
535 | */ |
||
536 | public function createDb($name) { |
||
553 | |||
554 | |||
555 | /** |
||
556 | * @brief Deletes an existing database. |
||
557 | * @param string $name The database name. |
||
558 | * @see http://docs.couchdb.org/en/latest/api/database/common.html#delete--db |
||
559 | */ |
||
560 | public function deleteDb($name) { |
||
563 | |||
564 | |||
565 | /** |
||
566 | * @brief Returns information about the selected database. |
||
567 | * @param string $name The database name. |
||
568 | * @return Info::DbInfo |
||
569 | * @see http://docs.couchdb.org/en/latest/api/database/common.html#get--db |
||
570 | */ |
||
571 | public function getDbInfo($name) { |
||
574 | |||
575 | |||
576 | /** |
||
577 | * @brief Obtains a list of the changes made to the database. This can be used to monitor for update and modifications |
||
578 | * to the database for post processing or synchronization. |
||
579 | * @param string $name The database name. |
||
580 | * @param ChangesFeedOpts $opts Additional options. |
||
581 | * @return Response |
||
582 | * @see http://docs.couchdb.org/en/latest/api/database/changes.html |
||
583 | */ |
||
584 | public function getDbChanges($name, Opt\ChangesFeedOpts $opts = NULL) { |
||
592 | |||
593 | |||
594 | /** |
||
595 | * @brief Starts a compaction for the current selected database. |
||
596 | * @details Writes a new version of the database file, removing any unused sections from the new version during write. |
||
597 | * Because a new file is temporary created for this purpose, you will need twice the current storage space of the |
||
598 | * specified database in order for the compaction routine to complete.\n |
||
599 | * Removes old revisions of documents from the database, up to the per-database limit specified by the `_revs_limit` |
||
600 | * database setting.\n |
||
601 | * Compaction can only be requested on an individual database; you cannot compact all the databases for a CouchDB |
||
602 | * instance.\n |
||
603 | * The compaction process runs as a background process. You can determine if the compaction process is operating on a |
||
604 | * database by obtaining the database meta information, the `compact_running` value of the returned database |
||
605 | * structure will be set to true. You can also obtain a list of running processes to determine whether compaction is |
||
606 | * currently running, using getActiveTasks(). |
||
607 | * @param string $name The database name. |
||
608 | * @attention Requires admin privileges. |
||
609 | * @see http://docs.couchdb.org/en/latest/api/database/compact.html |
||
610 | */ |
||
611 | View Code Duplication | public function compactDb($name) { |
|
619 | |||
620 | |||
621 | /** |
||
622 | * @brief Compacts the specified view. |
||
623 | * @details If you have very large views or are tight on space, you might consider compaction as well. To run compact |
||
624 | * for a particular view on a particular database, use this method. |
||
625 | * @param string $dbName The database name. |
||
626 | * @param string $designDocName Name of the design document where is stored the view. |
||
627 | * @see http://docs.couchdb.org/en/latest/api/database/compact.html#db-compact-design-doc |
||
628 | */ |
||
629 | public function compactView($dbName, $designDocName) { |
||
639 | |||
640 | |||
641 | /** |
||
642 | * @brief Removes all outdated view indexes. |
||
643 | * @details Old views files remain on disk until you explicitly run cleanup. |
||
644 | * @param string $dbName The database name. |
||
645 | * @attention Requires admin privileges. |
||
646 | * @see http://docs.couchdb.org/en/latest/api/database/compact.html#db-view-cleanup |
||
647 | */ |
||
648 | View Code Duplication | public function cleanupViews($dbName) { |
|
656 | |||
657 | |||
658 | /** |
||
659 | * @brief Makes sure all uncommited database changes are written and synchronized to the disk. |
||
660 | * @details Default CouchDB configuration use delayed commit to improve performances. So CouchDB allows operations to |
||
661 | * be run against the disk without an explicit fsync after each operation. Synchronization takes time (the disk may |
||
662 | * have to seek, on some platforms the hard disk cache buffer is flushed, etc.), so requiring an fsync for each update |
||
663 | * deeply limits CouchDB's performance for non-bulk writers.\n |
||
664 | * Delayed commit should be left set to true in the configuration settings. Anyway, you can still tell CouchDB to make |
||
665 | * an fsync, calling the this method. |
||
666 | * @param string $dbName The database name. |
||
667 | * @return string The timestamp of the last time the database file was opened. |
||
668 | * @see http://docs.couchdb.org/en/latest/api/database/compact.html#db-ensure-full-commit |
||
669 | */ |
||
670 | View Code Duplication | public function ensureFullCommit($dbName) { |
|
678 | |||
679 | //!@} |
||
680 | |||
681 | |||
682 | /** |
||
683 | * @name Security Methods |
||
684 | * @details The security object consists of two compulsory elements, admins and members, which are used to specify |
||
685 | * the list of users and/or roles that have admin and members rights to the database respectively: |
||
686 | * - members: they can read all types of documents from the DB, and they can write (and edit) documents to the |
||
687 | * database except for design documents. |
||
688 | * - admins: they have all the privileges of members plus the privileges: write (and edit) design documents, add/remove |
||
689 | * database admins and members, set the database revisions limit and execute temporary views against the database. |
||
690 | * They can not create a database nor delete a database. |
||
691 | * |
||
692 | * Both members and admins objects are contains two array-typed fields: |
||
693 | * - users: List of CouchDB user names |
||
694 | * - roles: List of users roles |
||
695 | * |
||
696 | * Any other additional fields in the security object are optional. The entire security object is made available to |
||
697 | * validation and other internal functions so that the database can control and limit functionality. |
||
698 | * If both the names and roles fields of either the admins or members properties are empty arrays, it means the |
||
699 | * database has no admins or members.\n |
||
700 | * Having no admins, only server admins (with the reserved _admin role) are able to update design document and make |
||
701 | * other admin level changes.\n |
||
702 | * Having no members, any user can write regular documents (any non-design document) and read documents from the database. |
||
703 | * If there are any member names or roles defined for a database, then only authenticated users having a matching name |
||
704 | * or role are allowed to read documents from the database. |
||
705 | */ |
||
706 | //!@{ |
||
707 | |||
708 | /** |
||
709 | * @brief Returns the special security object for the database. |
||
710 | * @param string $dbName The database name. |
||
711 | * @return string A JSON object. |
||
712 | * @see http://docs.couchdb.org/en/latest/api/database/security.html#get--db-_security |
||
713 | */ |
||
714 | public function getSecurityObj($dbName) { |
||
717 | |||
718 | |||
719 | /** |
||
720 | * @brief Sets the special security object for the database. |
||
721 | * @param string $dbName The database name. |
||
722 | * @see http://docs.couchdb.org/en/latest/api/database/security.html#put--db-_security |
||
723 | */ |
||
724 | public function setSecurityObj($dbName) { |
||
727 | |||
728 | //!@} |
||
729 | |||
730 | |||
731 | /** |
||
732 | * @name Database Replication Methods |
||
733 | * @details The replication is an incremental one way process involving two databases (a source and a destination). |
||
734 | * The aim of the replication is that at the end of the process, all active documents on the source database are also |
||
735 | * in the destination database and all documents that were deleted in the source databases are also deleted (if |
||
736 | * exists) on the destination database.\n |
||
737 | * The replication process only copies the last revision of a document, so all previous revisions that were only on |
||
738 | * the source database are not copied to the destination database.\n |
||
739 | * Changes on the master will not automatically replicate to the slaves. To make replication continuous, you must set |
||
740 | * `$continuous = true`. At this time, CouchDB does not remember continuous replications over a server restart. |
||
741 | * Specifying a local source database and a remote target database is called push replication and a remote source and |
||
742 | * local target is called pull replication. As of CouchDB 0.9, pull replication is a lot more efficient and resistant |
||
743 | * to errors, and it is suggested that you use pull replication in most cases, especially if your documents are large |
||
744 | * or you have large attachments. |
||
745 | */ |
||
746 | //!@{ |
||
747 | |||
748 | /** |
||
749 | * @brief Starts replication. |
||
750 | @code |
||
751 | startReplication("sourcedbname", "http://example.org/targetdbname", TRUE, TRUE); |
||
752 | @endcode |
||
753 | * @param string $sourceDbUrl Source database URL. |
||
754 | * @param string $targetDbUrl Destination database URL. |
||
755 | * @param string $proxy (optional) Specify the optional proxy used to perform the replication. |
||
756 | * @param bool $createTargetDb (optional) The target database has to exist and is not implicitly created. You |
||
757 | * can force the creation setting `$createTargetDb = true`. |
||
758 | * @param bool $continuous (optional) When `true` CouchDB will not stop after replicating all missing documents |
||
759 | * from the source to the target.\n |
||
760 | * At the time of writing, CouchDB doesn't remember continuous replications over a server restart. For the time being, |
||
761 | * you are required to trigger them again when you restart CouchDB. In the future, CouchDB will allow you to define |
||
762 | * permanent continuous replications that survive a server restart without you having to do anything. |
||
763 | * @param string|array $filter (optional) Name of a filter function that can choose which revisions get replicated. |
||
764 | * You can also provide an array of document identifiers; if given, only these documents will be replicated. |
||
765 | * @param ViewQueryOpts $opts (optional) Query options to get additional information, grouping results, include |
||
766 | * docs, etc. |
||
767 | * @return Response |
||
768 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#post--_replicate |
||
769 | */ |
||
770 | public function startReplication($sourceDbUrl, $targetDbUrl, $proxy = NULL, $createTargetDb = TRUE, |
||
818 | |||
819 | |||
820 | /** |
||
821 | * @brief Cancels replication. |
||
822 | * @param string $replicationId The replication ID. |
||
823 | * @return Response |
||
824 | * @see http://docs.couchdb.org/en/latest/api/server/common.html#canceling-continuous-replication |
||
825 | */ |
||
826 | public function stopReplication($replicationId) { |
||
838 | |||
839 | |||
840 | /** |
||
841 | * @brief |
||
842 | * @details |
||
843 | * @see http://wiki.apache.org/couchdb/Replication#Replicator_database |
||
844 | * @see http://docs.couchbase.org/couchdb-release-1.1/index.html#couchb-release-1.1-replicatordb |
||
845 | * @see https://gist.github.com/832610 |
||
846 | * @todo To be implemented and documented. |
||
847 | */ |
||
848 | public function getReplicator() { |
||
851 | |||
852 | //!@} |
||
853 | |||
854 | |||
855 | /** @name Query Documents Methods |
||
856 | * @details Queries are the primary mechanism for retrieving a result set from a view. The result of a query is an |
||
857 | * instance of `QueryResult`, a class that implements the [IteratorAggregate](http://php.net/manual/en/class.iteratoraggregate.php), |
||
858 | * [Countable](http://php.net/manual/en/class.countable.php) and [ArrayAccess](http://php.net/manual/en/class.arrayaccess.php) |
||
859 | * interfaces, so you can use the result set as an array. |
||
860 | */ |
||
861 | //!@{ |
||
862 | |||
863 | /** |
||
864 | * @brief Returns a built-in view of all documents in this database. If keys are specified returns only certain rows. |
||
865 | * @param string $dbName The database name. |
||
866 | * @param array $keys (optional) Used to retrieve just the view rows matching that set of keys. Rows are returned |
||
867 | * in the order of the specified keys. Combining this feature with Opt\ViewQueryOpts.includeDocs() results in the so-called |
||
868 | * multi-document-fetch feature. |
||
869 | * @param ViewQueryOpts $opts (optional) Query options to get additional information, grouping results, include |
||
870 | * docs, etc. |
||
871 | * @param ChunkHook $chunkHook (optional) A class instance that implements the IChunkHook interface. |
||
872 | * @return Result::QueryResult The result of the query. |
||
873 | * @see http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs |
||
874 | * @see http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs |
||
875 | */ |
||
876 | public function queryAllDocs($dbName, array $keys = NULL, Opt\ViewQueryOpts $opts = NULL, Hook\IChunkHook $chunkHook = NULL) { |
||
891 | |||
892 | |||
893 | /** |
||
894 | * @brief Executes the given view and returns the result. |
||
895 | * @param string $dbName The database name. |
||
896 | * @param string $designDocName The design document's name. |
||
897 | * @param string $viewName The view's name. |
||
898 | * @param array $keys (optional) Used to retrieve just the view rows matching that set of keys. Rows are returned |
||
899 | * in the order of the specified keys. Combining this feature with Opt\ViewQueryOpts.includeDocs() results in the so-called |
||
900 | * multi-document-fetch feature. |
||
901 | * @param ViewQueryOpts $opts (optional) Query options to get additional information, grouping results, include |
||
902 | * docs, etc. |
||
903 | * @param IChunkHook $chunkHook (optional) A class instance that implements the IChunkHook interface. |
||
904 | * @return Result::QueryResult The result of the query. |
||
905 | * @attention Multiple keys request to a reduce function only supports `group=true` (identical to `group-level=exact`, |
||
906 | * but it doesn't support `group_level` > 0. |
||
907 | * CouchDB raises "Multi-key fetchs for reduce view must include `group=true`" error, in case you use `group_level`. |
||
908 | * @see http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view |
||
909 | * @see http://docs.couchdb.org/en/latest/api/ddoc/views.html#post--db-_design-ddoc-_view-view |
||
910 | */ |
||
911 | public function queryView($dbName, $designDocName, $viewName, array $keys = NULL, Opt\ViewQueryOpts $opts = NULL, Hook\IChunkHook $chunkHook = NULL) { |
||
941 | |||
942 | |||
943 | /** |
||
944 | * @brief Executes the given view, both map and reduce functions, for all documents and returns the result. |
||
945 | * @details Map and Reduce functions are provided by the programmer. |
||
946 | * @attention Requires admin privileges. |
||
947 | * @param string $dbName The database name. |
||
948 | * @param string $mapFn The map function. |
||
949 | * @param string $reduceFn The reduce function. |
||
950 | * @param array $keys (optional) Used to retrieve just the view rows matching that set of keys. Rows are returned |
||
951 | * in the order of the specified keys. Combining this feature with Opt\ViewQueryOpts.includeDocs() results in the so-called |
||
952 | * multi-document-fetch feature. |
||
953 | * @param ViewQueryOpts $opts (optional) Query options to get additional information, grouping results, include |
||
954 | * docs, etc. |
||
955 | * @param string $language The language used to implement the map and reduce functions. |
||
956 | * @param IChunkHook $chunkHook (optional) A class instance that implements the IChunkHook interface. |
||
957 | * @return Result::QueryResult The result of the query. |
||
958 | * @see http://docs.couchdb.org/en/latest/api/database/temp-views.html#post--db-_temp_view |
||
959 | */ |
||
960 | public function queryTempView($dbName, $mapFn, $reduceFn = "", array $keys = NULL, Opt\ViewQueryOpts $opts = NULL, $language = 'php', Hook\IChunkHook $chunkHook = NULL) { |
||
991 | |||
992 | //!@} |
||
993 | |||
994 | |||
995 | /** @name Revisions Management Methods */ |
||
996 | //!@{ |
||
997 | |||
998 | /** |
||
999 | * @brief Given a list of document revisions, returns the document revisions that do not exist in the database. |
||
1000 | * @param string $dbName The database name. |
||
1001 | * @return Response |
||
1002 | * @see http://docs.couchdb.org/en/latest/api/database/misc.html#db-missing-revs |
||
1003 | */ |
||
1004 | public function getMissingRevs($dbName) { |
||
1009 | |||
1010 | |||
1011 | /** |
||
1012 | * @brief Given a list of document revisions, returns differences between the given revisions and ones that are in |
||
1013 | * the database. |
||
1014 | * @param string $dbName The database name. |
||
1015 | * @return Response |
||
1016 | * @see http://docs.couchdb.org/en/latest/api/database/misc.html#db-revs-diff |
||
1017 | */ |
||
1018 | public function getRevsDiff($dbName) { |
||
1023 | |||
1024 | |||
1025 | /** |
||
1026 | * @brief Gets the limit of historical revisions to store for a single document in the database. |
||
1027 | * @param string $dbName The database name. |
||
1028 | * @return integer The maximum number of document revisions that will be tracked by CouchDB. |
||
1029 | * @see http://docs.couchdb.org/en/latest/api/database/misc.html#get--db-_revs_limit |
||
1030 | */ |
||
1031 | public function getRevsLimit($dbName) { |
||
1036 | |||
1037 | |||
1038 | /** |
||
1039 | * @brief Sets the limit of historical revisions for a single document in the database. |
||
1040 | * @param string $dbName The database name. |
||
1041 | * @param integer $revsLimit (optional) Maximum number historical revisions for a single document in the database. |
||
1042 | * Must be a positive integer. |
||
1043 | * @see http://docs.couchdb.org/en/latest/api/database/misc.html#put--db-_revs_limit |
||
1044 | */ |
||
1045 | public function setRevsLimit($dbName, $revsLimit = self::REVS_LIMIT) { |
||
1055 | |||
1056 | //!@} |
||
1057 | |||
1058 | |||
1059 | /** @name Documents Management Methods */ |
||
1060 | //!@{ |
||
1061 | |||
1062 | /** |
||
1063 | * @brief Returns the document's entity tag, that can be used for caching or optimistic concurrency control purposes. |
||
1064 | * The ETag Header is simply the document's revision in quotes. |
||
1065 | * @details This function is not available for special documents. To get information about a design document, use |
||
1066 | * the special function getDesignDocInfo(). |
||
1067 | * @param string $dbName The database name. |
||
1068 | * @param string $docId The document's identifier. |
||
1069 | * @return string The document's revision. |
||
1070 | * @see http://docs.couchdb.org/en/latest/api/document/common.html#db-doc |
||
1071 | * @bug CouchDB ETag is |
||
1072 | */ |
||
1073 | View Code Duplication | public function getDocEtag($dbName, $docId) { |
|
1083 | |||
1084 | |||
1085 | /** |
||
1086 | * @brief Returns the latest revision of the document. |
||
1087 | * @details Since CouchDB uses different paths to store special documents, you must provide the document type for |
||
1088 | * design and local documents. |
||
1089 | * @param string $dbName The database name. |
||
1090 | * @param string $docId The document's identifier. |
||
1091 | * @param string $path The document's path. |
||
1092 | * @param string $rev (optional) The document's revision. |
||
1093 | * @param DocOpts $opts Query options to get additional document information, like conflicts, attachments, etc. |
||
1094 | * @return object|Response An instance of Doc, LocalDoc, DesignDoc or any subclass of Doc. |
||
1095 | * @see http://docs.couchdb.org/en/latest/api/document/common.html#get--db-docid |
||
1096 | */ |
||
1097 | public function getDoc($dbName, $path, $docId, $rev = NULL, Opt\DocOpts $opts = NULL) { |
||
1140 | |||
1141 | |||
1142 | /** |
||
1143 | * @brief Inserts or updates a document into the selected database. |
||
1144 | * @details Whether the `$doc` has an id we use a different HTTP method. Using POST CouchDB generates an id for the doc, |
||
1145 | * using PUT instead we need to specify one. We can still use the function getUuids() to ask CouchDB for some ids. |
||
1146 | * This is an internal detail. You have only to know that CouchDB can generate the document id for you. |
||
1147 | * @param string $dbName The database name. |
||
1148 | * @param Doc $doc The document you want insert or update. |
||
1149 | * @param bool $batchMode (optional) You can write documents to the database at a higher rate by using the batch |
||
1150 | * option. This collects document writes together in memory (on a user-by-user basis) before they are committed to |
||
1151 | * disk. This increases the risk of the documents not being stored in the event of a failure, since the documents are |
||
1152 | * not written to disk immediately. |
||
1153 | * @see http://docs.couchdb.org/en/latest/api/document/common.html#put--db-docid |
||
1154 | */ |
||
1155 | public function saveDoc($dbName, Doc\IDoc $doc, $batchMode = FALSE) { |
||
1178 | |||
1179 | |||
1180 | /** |
||
1181 | * @brief Deletes the specified document. |
||
1182 | * @details To delete a document you must provide the document identifier and the revision number. |
||
1183 | * @param string $dbName The database name. |
||
1184 | * @param string $docId The document's identifier you want delete. |
||
1185 | * @param string $rev The document's revision number you want delete. |
||
1186 | * @param string $path The document path. |
||
1187 | * @see http://docs.couchdb.org/en/latest/api/document/common.html#delete--db-docid |
||
1188 | */ |
||
1189 | public function deleteDoc($dbName, $path, $docId, $rev) { |
||
1203 | |||
1204 | |||
1205 | /** |
||
1206 | * @brief Makes a duplicate of the specified document. If you want to overwrite an existing document, you need to |
||
1207 | * specify the target document's revision with a `$rev` parameter. |
||
1208 | * @details If you want copy a special document you must specify his type. |
||
1209 | * @param string $dbName The database name. |
||
1210 | * @param string $sourceDocId The source document id. |
||
1211 | * @param string $targetDocId The destination document id. |
||
1212 | * @param string $rev Needed when you want override an existent document. |
||
1213 | * @param string $path The document path. |
||
1214 | * @see http://docs.couchdb.org/en/latest/api/document/common.html#copy--db-docid |
||
1215 | */ |
||
1216 | public function copyDoc($dbName, $path, $sourceDocId, $targetDocId, $rev = NULL) { |
||
1234 | |||
1235 | |||
1236 | /** |
||
1237 | * @brief The purge operation removes the references to the deleted documents from the database. |
||
1238 | * @details A database purge permanently removes the references to deleted documents from the database. Deleting a |
||
1239 | * document within CouchDB does not actually remove the document from the database, instead, the document is marked |
||
1240 | * as deleted (and a new revision is created). This is to ensure that deleted documents are replicated to other |
||
1241 | * databases as having been deleted. This also means that you can check the status of a document and identify that |
||
1242 | * the document has been deleted.\n |
||
1243 | * The purging of old documents is not replicated to other databases. If you are replicating between databases and |
||
1244 | * have deleted a large number of documents you should run purge on each database.\n |
||
1245 | * Purging documents does not remove the space used by them on disk. To reclaim disk space, you should run compactDb().\n |
||
1246 | * @param string $dbName The database name. |
||
1247 | * @param array $refs An array of references used to identify documents and revisions to delete. The array must |
||
1248 | * contains instances of the DocRef class. |
||
1249 | * @return Response |
||
1250 | * @see http://docs.couchdb.org/en/latest/api/database/misc.html#post--db-_purge |
||
1251 | * @see http://wiki.apache.org/couchdb/Purge_Documents |
||
1252 | */ |
||
1253 | public function purgeDocs($dbName, array $refs) { |
||
1266 | |||
1267 | |||
1268 | /** |
||
1269 | * @brief Inserts, updates and deletes documents in a bulk. |
||
1270 | * @details Documents that are updated or deleted must contain the `rev` number. To delete a document, you should |
||
1271 | * call delete() method on your document. When creating new documents the document ID is optional. For updating existing |
||
1272 | * documents, you must provide the document ID and revision. |
||
1273 | * @param string $dbName The database name. |
||
1274 | * @param array $docs An array of documents you want insert, delete or update. |
||
1275 | * @param bool $fullCommit (optional) Makes sure all uncommited database changes are written and synchronized |
||
1276 | * to the disk immediately. |
||
1277 | * @param bool $allOrNothing (optional) In all-or-nothing mode, either all documents are written to the database, |
||
1278 | * or no documents are written to the database, in the event of a system failure during commit.\n |
||
1279 | * You can ask CouchDB to check that all the documents pass your validation functions. If even one fails, none of the |
||
1280 | * documents are written. If all documents pass validation, then all documents will be updated, even if that introduces |
||
1281 | * a conflict for some or all of the documents. |
||
1282 | * @param bool $newEdits (optional) When `false` CouchDB pushes existing revisions instead of creating |
||
1283 | * new ones. The response will not include entries for any of the successful revisions (since their rev IDs are |
||
1284 | * already known to the sender), only for the ones that had errors. Also, the conflict error will never appear, |
||
1285 | * since in this mode conflicts are allowed. |
||
1286 | * @return Response |
||
1287 | * @see http://docs.couchdb.org/en/latest/api/database/bulk-api.html#db-bulk-docs |
||
1288 | * @see http://docs.couchdb.org/en/latest/json-structure.html#bulk-documents |
||
1289 | * @see http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API |
||
1290 | */ |
||
1291 | public function performBulkOperations($dbName, array $docs, $fullCommit = FALSE, $allOrNothing = FALSE, $newEdits = TRUE) { |
||
1322 | |||
1323 | //!@} |
||
1324 | |||
1325 | |||
1326 | /** @name Attachments Management Methods */ |
||
1327 | //!@{ |
||
1328 | |||
1329 | |||
1330 | /** |
||
1331 | * @brief Returns the minimal amount of information about the specified attachment. |
||
1332 | * @param string $dbName The database name. |
||
1333 | * @param string $fileName The attachment's name. |
||
1334 | * @param string $docId The document's identifier. |
||
1335 | * @param string $path The document's path. |
||
1336 | * @param string $rev (optional) The document's revision. |
||
1337 | * @return string The document's revision. |
||
1338 | * @see http://docs.couchdb.org/en/latest/api/document/attachments.html#db-doc-attachment |
||
1339 | */ |
||
1340 | View Code Duplication | public function getAttachmentInfo($dbName, $fileName, $path, $docId, $rev = NULL) { |
|
1354 | |||
1355 | |||
1356 | /** |
||
1357 | * @brief Retrieves the attachment from the specified document. |
||
1358 | * @param string $dbName The database name. |
||
1359 | * @param string $fileName The attachment's name. |
||
1360 | * @param string $docId The document's identifier. |
||
1361 | * @param string $path The document's path. |
||
1362 | * @param string $rev (optional) The document's revision. |
||
1363 | * @see http://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname |
||
1364 | * @see http://docs.couchdb.org/en/latest/api/document/attachments.html#http-range-requests |
||
1365 | * @todo Add support for Range request, using header "Range: bytes=0-12". |
||
1366 | */ |
||
1367 | View Code Duplication | public function getAttachment($dbName, $fileName, $path, $docId, $rev = NULL) { |
|
1381 | |||
1382 | |||
1383 | /** |
||
1384 | * @brief Inserts or updates an attachment to the specified document. |
||
1385 | * @param string $dbName The database name. |
||
1386 | * @param string $fileName The attachment's name. |
||
1387 | * @param string $docId The document's identifier. |
||
1388 | * @param string $path The document's path. |
||
1389 | * @param string $rev (optional) The document's revision. |
||
1390 | * @see http://docs.couchdb.org/en/latest/api/document/attachments.html#put--db-docid-attname |
||
1391 | */ |
||
1392 | public function putAttachment($dbName, $fileName, $path, $docId, $rev = NULL) { |
||
1411 | |||
1412 | |||
1413 | /** |
||
1414 | * @brief Deletes an attachment from the document. |
||
1415 | * @param string $dbName The database name. |
||
1416 | * @param string $fileName The attachment's name. |
||
1417 | * @param string $docId The document's identifier. |
||
1418 | * @param string $path The document's path. |
||
1419 | * @param string $rev The document's revision. |
||
1420 | * @see http://docs.couchdb.org/en/latest/api/document/attachments.html#delete--db-docid-attname |
||
1421 | */ |
||
1422 | View Code Duplication | public function deleteAttachment($dbName, $fileName, $path, $docId, $rev) { |
|
1433 | |||
1434 | //!@} |
||
1435 | |||
1436 | |||
1437 | /** @name Special Design Documents Management Methods */ |
||
1438 | //!@{ |
||
1439 | |||
1440 | |||
1441 | /** |
||
1442 | * @brief Returns basic information about the design document and his views. |
||
1443 | * @param string $dbName The database name. |
||
1444 | * @param string $docName The design document's name. |
||
1445 | * @return array An associative array |
||
1446 | * @see http://docs.couchdb.org/en/latest/api/ddoc/common.html#get--db-_design-ddoc-_info |
||
1447 | */ |
||
1448 | View Code Duplication | public function getDesignDocInfo($dbName, $docName) { |
|
1457 | |||
1458 | //!@} |
||
1459 | |||
1460 | } |