PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Xinc/Plugin/Repos/Api/LogMessages.php

http://xinc.googlecode.com/
PHP | 241 lines | 147 code | 14 blank | 80 comment | 23 complexity | 3bd0e4d2f96dcc7fd0b0bbd7aa0d37d1 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. declare(encoding = 'utf-8');
  3. /**
  4. * Xinc - Continuous Integration.
  5. * Api to get log messages for a build
  6. *
  7. * PHP version 5
  8. *
  9. * @category Development
  10. * @package Xinc.Plugin.Repos.Api
  11. * @author Arno Schneider <username@example.org>
  12. * @copyright 2007 Arno Schneider, Barcelona
  13. * @license http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php
  14. * This file is part of Xinc.
  15. * Xinc is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as
  17. * published by the Free Software Foundation; either version 2.1 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * Xinc is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with Xinc, write to the Free Software Foundation,
  27. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. * @link http://xincplus.sourceforge.net
  29. */
  30. require_once 'Xinc/Api/Module/Interface.php';
  31. require_once 'Xinc/Plugin/Repos/Gui/Dashboard/Detail/Extension.php';
  32. class Xinc_Plugin_Repos_Api_LogMessages implements Xinc_Api_Module_Interface
  33. {
  34. /**
  35. * Enter description here...
  36. *
  37. * @var Xinc_Plugin_Interface
  38. */
  39. protected $_plugin;
  40. /**
  41. *
  42. * @param Xinc_Plugin_Interface $plugin
  43. */
  44. public function __construct(Xinc_Plugin_Interface $plugin)
  45. {
  46. $this->_plugin = $plugin;
  47. }
  48. /**
  49. *
  50. * @return string
  51. */
  52. public function getName()
  53. {
  54. return 'logmessages';
  55. }
  56. /**
  57. *
  58. * @return array
  59. */
  60. public function getMethods()
  61. {
  62. return array('get');
  63. }
  64. /**
  65. *
  66. * @param string $methodName
  67. * @param array $params
  68. * @return Xinc_Api_Response_Object
  69. */
  70. public function processCall($methodName, $params = array())
  71. {
  72. switch ($methodName){
  73. case 'get':
  74. return $this->_getLogMessages($params);
  75. break;
  76. }
  77. }
  78. /**
  79. * get logmessages and return them
  80. *
  81. * @param array $params
  82. *
  83. * @return Xinc_Api_Response_Object
  84. */
  85. private function _getLogMessages($params)
  86. {
  87. $project = isset($params['p']) ? $params['p'] : null;
  88. $buildtime = isset($params['buildtime']) ? $params['buildtime'] : null;
  89. $start = isset($params['start']) ? (int)$params['start'] : 0;
  90. $limit = isset($params['limit']) ? (int)$params['limit'] : null;
  91. $builds = $this->_getLogMessagesArr($project, $buildtime, $start, $limit);
  92. $responseObject = new Xinc_Api_Response_Object();
  93. $responseObject->set($builds);
  94. return $responseObject;
  95. }
  96. private function _getNextMessage($fh)
  97. {
  98. $message = false;
  99. $started = false;
  100. $found=false;
  101. while (!$found) {
  102. if(feof($fh)) break;
  103. $line = fgets($fh);
  104. $line = trim($line);
  105. if (empty($line) && !$started) continue;
  106. if (strstr($line, '<message ') && !$started) {
  107. $started = true;
  108. $message = '';
  109. //$message .= $line;
  110. }
  111. if ($started) {
  112. $message .= $line;
  113. if (strstr($line, '</message>')) {
  114. $found = true;
  115. } else {
  116. $message .= "<br/>";
  117. }
  118. }
  119. }
  120. //echo $message; echo "\n<br>";
  121. return $message;
  122. }
  123. /**
  124. * Get a list of all builds of a project
  125. *
  126. * @param string $projectName
  127. * @param integer $start
  128. * @param integer $limit
  129. *
  130. * @return stdClass
  131. */
  132. private function _getLogMessagesArr($projectName, $buildTime, $start, $limit=null)
  133. {
  134. $statusDir = Xinc_Gui_Handler::getInstance()->getStatusDir();
  135. $historyFile = $statusDir . DIRECTORY_SEPARATOR . $projectName . '.history';
  136. $project = new Xinc_Project();
  137. $project->setName($projectName);
  138. $totalCount = 0;
  139. try {
  140. $build = Xinc_Build::unserialize($project, $buildTime, $statusDir);
  141. $timezone = $build->getConfigDirective('timezone');
  142. if ($timezone !== null) {
  143. Xinc_Timezone::set($timezone);
  144. }
  145. $detailDir = Xinc_Build_History::getBuildDir($project, $buildTime);
  146. $logXmlFile = $detailDir.DIRECTORY_SEPARATOR.'buildlog.xml';
  147. if (file_exists($logXmlFile)) {
  148. /**
  149. * Add fopen() to the function to just get the loglines
  150. * that we need.
  151. * the bigger the logfiles get, the more this gets a
  152. * performance problem
  153. */
  154. $xmlStr = '';
  155. $pos = 0;
  156. $fh = fopen($logXmlFile, 'r');
  157. $xmlStr = fgets($fh);
  158. $xmlStr .= fgets($fh);
  159. $tagOpen = false;
  160. while ($pos < $start && ($message = $this->_getNextMessage($fh)) !== false) {
  161. $pos++;
  162. $totalCount++;
  163. }
  164. if ($limit!=null) {
  165. $addClosingTag = true;
  166. while ($pos<$start+$limit && ($message = $this->_getNextMessage($fh))!== false) {
  167. $xmlStr.= $message;
  168. $pos++;
  169. $totalCount++;
  170. }
  171. $xmlStr .='</build>';
  172. } else {
  173. while (($message = $this->_getNextMessage($fh))!== false) {
  174. $xmlStr.= $message;
  175. $totalCount++;
  176. $pos++;
  177. }
  178. $xmlStr .='</build>';
  179. }
  180. $tagOpen = false;
  181. $tagClosed = false;
  182. while (($message = $this->_getNextMessage($fh))!== false) {
  183. $totalCount++;
  184. $pos++;
  185. }
  186. fclose($fh);
  187. //echo($xmlStr);
  188. $logXml = new SimpleXMLElement($xmlStr);
  189. } else {
  190. $logXml = new SimpleXmlElement('<log/>');
  191. }
  192. $totalCount = $pos; //count($logXml->children());
  193. $i = $pos;
  194. $logmessages = array();
  195. $id = $totalCount-$start;
  196. foreach ($logXml->children() as $logEntry) {
  197. $attributes = $logEntry->attributes();
  198. $logmessages[] = array(
  199. 'id'=>$id--,
  200. 'date'=> (string)$attributes->timestamp,
  201. 'stringdate'=> date('Y-m-d H:i:s', (int)$attributes->timestamp),
  202. 'timezone' => Xinc_Timezone::get(),
  203. 'priority'=>(string)$attributes->priority,
  204. 'message'=>str_replace("\n", '\\n', addcslashes($logEntry, '"\''))
  205. );
  206. }
  207. /**
  208. * restore to system timezone
  209. */
  210. $xincTimezone = Xinc_Gui_Handler::getInstance()->getConfigDirective('timezone');
  211. if ($xincTimezone !== null) {
  212. Xinc_Timezone::set($xincTimezone);
  213. } else {
  214. Xinc_Timezone::reset();
  215. }
  216. //$logmessages = array_slice($logmessages, $start, $limit, false);
  217. } catch (Exception $e1) {
  218. $totalCount = 0;
  219. $logmessages = array();
  220. }
  221. $object = new stdClass();
  222. $object->totalmessages = $totalCount;
  223. $object->logmessages = $logmessages;
  224. //return new Xinc_Build_Iterator($builds);
  225. return $object;
  226. }
  227. }