PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/class/SectionServer.php

https://code.google.com/p/yapeal/
PHP | 139 lines | 81 code | 0 blank | 58 comment | 18 complexity | a869438599ec261e366cb36c9536c44a MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains Section Server class.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: This file is part of Yet Another Php Eve Api library also know
  8. * as Yapeal which will be used to refer to it in the rest of this license.
  9. *
  10. * Yapeal is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Yapeal is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with Yapeal. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @author Michael Cummings <mgcummings@yahoo.com>
  24. * @copyright Copyright (c) 2008-2012, Michael Cummings
  25. * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
  26. * @package Yapeal
  27. * @link http://code.google.com/p/yapeal/
  28. * @link http://www.eveonline.com/
  29. */
  30. /**
  31. * @internal Allow viewing of the source code in web browser.
  32. */
  33. if (isset($_REQUEST['viewSource'])) {
  34. highlight_file(__FILE__);
  35. exit();
  36. };
  37. /**
  38. * @internal Only let this code be included.
  39. */
  40. if (count(get_included_files()) < 2) {
  41. $mess = basename(__FILE__)
  42. . ' must be included it can not be ran directly.' . PHP_EOL;
  43. if (PHP_SAPI != 'cli') {
  44. header('HTTP/1.0 403 Forbidden', TRUE, 403);
  45. die($mess);
  46. };
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. };
  50. /**
  51. * Class used to pull Eve APIs for server section.
  52. *
  53. * @package Yapeal
  54. * @subpackage Api_sections
  55. */
  56. class SectionServer extends ASection {
  57. /**
  58. * Constructor
  59. */
  60. public function __construct() {
  61. $this->section = strtolower(str_replace('Section', '', __CLASS__));
  62. parent::__construct();
  63. }
  64. /**
  65. * Function called by Yapeal.php to start section pulling XML from servers.
  66. *
  67. * @return bool Returns TRUE if all APIs were pulled cleanly else FALSE.
  68. */
  69. public function pullXML() {
  70. if ($this->abort === TRUE) {
  71. return FALSE;
  72. };
  73. $apiCount = 0;
  74. $apiSuccess = 0;
  75. $apis = $this->am->maskToAPIs($this->mask, $this->section);
  76. if (count($apis) == 0) {
  77. return FALSE;
  78. };
  79. // Randomize order in which APIs are tried if there is a list.
  80. if (count($apis) > 1) {
  81. shuffle($apis);
  82. };
  83. try {
  84. foreach ($apis as $api) {
  85. // If the cache for this API has expire try to get update.
  86. if (CachedUntil::cacheExpired($api) === TRUE) {
  87. ++$apiCount;
  88. $class = $this->section . $api;
  89. $hash = hash('sha1', $class);
  90. // These are passed on to the API class instance and used as part of
  91. // hash for lock.
  92. $params = array();
  93. // Use lock to keep from wasting time trying to do API that another
  94. // Yapeal is already working on.
  95. try {
  96. $con = YapealDBConnection::connect(YAPEAL_DSN);
  97. $sql = 'select get_lock(' . $con->qstr($hash) . ',5)';
  98. if ($con->GetOne($sql) != 1) {
  99. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  100. $mess = 'Failed to get lock for ' . $class . $hash;
  101. Logger::getLogger('yapeal')->info($mess);
  102. };
  103. continue;
  104. };// if $con->GetOne($sql) ...
  105. }
  106. catch(ADODB_Exception $e) {
  107. continue;
  108. }
  109. // Give each API 60 seconds to finish. This should never happen but is
  110. // here to catch runaways.
  111. set_time_limit(60);
  112. $instance = new $class($params);
  113. if ($instance->apiStore()) {
  114. ++$apiSuccess;
  115. };
  116. $instance = null;
  117. };// if CachedUntil::cacheExpired...
  118. // See if Yapeal has been running for longer than 'soft' limit.
  119. if (YAPEAL_MAX_EXECUTE < time()) {
  120. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  121. $mess = 'Yapeal has been working very hard and needs a break';
  122. Logger::getLogger('yapeal')->info($mess);
  123. };
  124. exit;
  125. };// if YAPEAL_MAX_EXECUTE < time() ...
  126. };// foreach $apis ...
  127. }
  128. catch (ADODB_Exception $e) {
  129. Logger::getLogger('yapeal')->warn($e);
  130. }
  131. // Only truly successful if API was fetched and stored.
  132. if ($apiCount == $apiSuccess) {
  133. return TRUE;
  134. } else {
  135. return FALSE;
  136. }// else $apiCount == $apiSuccess ...
  137. }// function pullXML
  138. }
  139. ?>