WBOJsonOutput.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is Weave Minimal Server
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Mozilla Labs.
  19. # Portions created by the Initial Developer are Copyright (C) 2008
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # Toby Elliott (telliott@mozilla.com)
  24. # Luca Tettamanti
  25. #
  26. # Alternatively, the contents of this file may be used under the terms of
  27. # either the GNU General Public License Version 2 or later (the "GPL"), or
  28. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. # in which case the provisions of the GPL or the LGPL are applicable instead
  30. # of those above. If you wish to allow use of your version of this file only
  31. # under the terms of either the GPL or the LGPL, and not to allow others to
  32. # use your version of this file under the terms of the MPL, indicate your
  33. # decision by deleting the provisions above and replace them with the notice
  34. # and other provisions required by the GPL or the LGPL. If you do not delete
  35. # the provisions above, a recipient may use your version of this file under
  36. # the terms of any one of the MPL, the GPL or the LGPL.
  37. #
  38. # ***** END LICENSE BLOCK *****
  39. #The datasets we might be dealing with here are too large for sticking it all into an array, so
  40. #we need to define a direct-output method for the storage class to use. If we start producing multiples
  41. #(unlikely), we can put them in their own class.
  42. class WBOJsonOutput
  43. {
  44. private $_full = null;
  45. private $_comma_flag = 0;
  46. private $_output_format = 'json';
  47. function __construct ($full = false)
  48. {
  49. $this->_full = $full;
  50. if (array_key_exists('HTTP_ACCEPT', $_SERVER)
  51. && !preg_match('/\*\/\*/', $_SERVER['HTTP_ACCEPT'])
  52. && !preg_match('/application\/json/', $_SERVER['HTTP_ACCEPT']))
  53. {
  54. if (preg_match('/application\/whoisi/', $_SERVER['HTTP_ACCEPT']))
  55. {
  56. header("Content-type: application/whoisi");
  57. $this->_output_format = 'whoisi';
  58. }
  59. elseif (preg_match('/application\/newlines/', $_SERVER['HTTP_ACCEPT']))
  60. {
  61. header("Content-type: application/newlines");
  62. $this->_output_format = 'newlines';
  63. }
  64. }
  65. }
  66. function set_format($format)
  67. {
  68. $this->_output_format = $format;
  69. }
  70. function output($sth)
  71. {
  72. if (($rowcount = $sth->rowCount()) > 0)
  73. {
  74. header('X-Weave-Records: ' . $rowcount);
  75. }
  76. if ($this->_output_format == 'newlines')
  77. {
  78. return $this->output_newlines($sth);
  79. }
  80. elseif ($this->_output_format == 'whoisi')
  81. {
  82. return $this->output_whoisi($sth);
  83. }
  84. else
  85. {
  86. return $this->output_json($sth);
  87. }
  88. }
  89. function output_json($sth)
  90. {
  91. echo '[';
  92. while ($result = $sth->fetch(PDO::FETCH_ASSOC))
  93. {
  94. if ($this->_comma_flag) { echo ','; } else { $this->_comma_flag = 1; }
  95. if ($this->_full)
  96. {
  97. $wbo = new wbo();
  98. $wbo->populate($result);
  99. echo $wbo->json();
  100. }
  101. else
  102. echo json_encode($result{'id'});
  103. }
  104. echo ']';
  105. return 1;
  106. }
  107. function output_whoisi($sth)
  108. {
  109. while ($result = $sth->fetch(PDO::FETCH_ASSOC))
  110. {
  111. if ($this->_full)
  112. {
  113. $wbo = new wbo();
  114. $wbo->populate($result);
  115. $output = $wbo->json();
  116. }
  117. else
  118. $output = json_encode($result{'id'});
  119. echo pack('N', mb_strlen($output, '8bit')) . $output;
  120. }
  121. return 1;
  122. }
  123. function output_newlines($sth)
  124. {
  125. while ($result = $sth->fetch(PDO::FETCH_ASSOC))
  126. {
  127. if ($this->_full)
  128. {
  129. $wbo = new wbo();
  130. $wbo->populate($result);
  131. echo preg_replace('/\n/', '\u000a', $wbo->json());
  132. }
  133. else
  134. echo json_encode($result{'id'});
  135. echo "\n";
  136. }
  137. return 1;
  138. }
  139. }
  140. ?>