PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/Core/API/DocumentationGenerator.php

https://github.com/ptarcher/exercise_mvc
PHP | 202 lines | 138 code | 15 blank | 49 comment | 7 complexity | 97ec7b24bac62b1491bf0af79b98e122 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Core - Open source web analytics
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
  7. * @version $Id: DocumentationGenerator.php 1466 2009-09-11 00:58:22Z vipsoft $
  8. *
  9. * @category Core
  10. * @package Core
  11. */
  12. /**
  13. * @package Core
  14. * @subpackage Core_API
  15. */
  16. class Core_API_DocumentationGenerator
  17. {
  18. protected $countPluginsLoaded = 0;
  19. /**
  20. * trigger loading all plugins with an API.php file in the Proxy
  21. */
  22. public function __construct()
  23. {
  24. $plugins = Core_PluginsManager::getInstance()->getLoadedPluginsName();
  25. foreach( $plugins as $plugin )
  26. {
  27. $plugin = Core::unprefixClass($plugin);
  28. try {
  29. Core_API_Proxy::getInstance()->registerClass('Core_'.$plugin.'_API');
  30. }
  31. catch(Exception $e){
  32. }
  33. }
  34. }
  35. /**
  36. * Returns a HTML page containing help for all the successfully loaded APIs.
  37. * For each module it will return a mini help with the method names, parameters to give,
  38. * links to get the result in Xml/Csv/etc
  39. *
  40. * @return string
  41. */
  42. public function getAllInterfaceString( $outputExampleUrls = true, $prefixUrls = '' )
  43. {
  44. $str = '';
  45. $token_auth = "&token_auth=" . Core::getCurrentUserTokenAuth();
  46. $parametersToSet = array(
  47. 'idSite' => Core_Common::getRequestVar('idSite', 1, 'int'),
  48. 'period' => Core_Common::getRequestVar('period', 'day', 'string'),
  49. 'date' => Core_Common::getRequestVar('date', 'today', 'string')
  50. );
  51. foreach(Core_API_Proxy::getInstance()->getMetadata() as $class => $info)
  52. {
  53. $moduleName = Core_API_Proxy::getInstance()->getModuleNameFromClassName($class);
  54. $str .= "\n<h2 id='$moduleName'>Module ".$moduleName."</h2>";
  55. foreach($info as $methodName => $infoMethod)
  56. {
  57. $params = $this->getStrListParameters($class, $methodName);
  58. $str .= "\n" . "- <b>$moduleName.$methodName " . $params . "</b>";
  59. $str .= '<small>';
  60. if($outputExampleUrls)
  61. {
  62. // we prefix all URLs with $prefixUrls
  63. // used when we include this output in the Core official documentation for example
  64. $str .= "<span class=\"example\">";
  65. $exampleUrl = $this->getExampleUrl($class, $methodName, $parametersToSet);
  66. if($exampleUrl !== false)
  67. {
  68. $lastNUrls = '';
  69. if( preg_match('/(&period)|(&date)/',$exampleUrl))
  70. {
  71. $exampleUrlRss1 = $prefixUrls . $this->getExampleUrl($class, $methodName, array('date' => 'last10') + $parametersToSet) ;
  72. $exampleUrlRss2 = $prefixUrls . $this->getExampleUrl($class, $methodName, array('date' => 'last5','period' => 'week',) + $parametersToSet );
  73. $lastNUrls = ", RSS of the last <a target=_blank href='$exampleUrlRss1&format=rss$token_auth'>10 days</a>, <a target=_blank href='$exampleUrlRss2&format=Rss'>5 weeks</a>,
  74. XML of the <a target=_blank href='$exampleUrlRss1&format=xml$token_auth'>last 10 days</a>";
  75. }
  76. $exampleUrl = $prefixUrls . $exampleUrl ;
  77. $str .= " [ Example in
  78. <a target=_blank href='$exampleUrl&format=xml$token_auth'>XML</a>,
  79. <a target=_blank href='$exampleUrl&format=PHP&prettyDisplay=true$token_auth'>PHP</a>,
  80. <a target=_blank href='$exampleUrl&format=JSON$token_auth'>Json</a>,
  81. <a target=_blank href='$exampleUrl&format=Csv$token_auth'>Csv</a>,
  82. <a target=_blank href='$exampleUrl&format=Html$token_auth'>Basic html</a>
  83. $lastNUrls
  84. ]";
  85. }
  86. else
  87. {
  88. $str .= " [ No example available ]";
  89. }
  90. $str .= "</span>";
  91. }
  92. $str .= '</small>';
  93. $str .= "\n<br>";
  94. }
  95. }
  96. return $str;
  97. }
  98. /**
  99. * Returns a string containing links to examples on how to call a given method on a given API
  100. * It will export links to XML, CSV, HTML, JSON, PHP, etc.
  101. * It will not export links for methods such as deleteSite or deleteUser
  102. *
  103. * @param string the class
  104. * @param methodName the method
  105. * @return string|false when not possible
  106. */
  107. protected function getExampleUrl($class, $methodName, $parametersToSet = array())
  108. {
  109. $knowExampleDefaultParametersValues = array(
  110. 'access' => 'view',
  111. 'userLogin' => 'test',
  112. 'passwordMd5ied' => 'passwordExample',
  113. 'email' => 'test@example.org',
  114. 'languageCode' => 'fr',
  115. );
  116. foreach($parametersToSet as $name => $value)
  117. {
  118. $knowExampleDefaultParametersValues[$name] = $value;
  119. }
  120. // no links for these method names
  121. $doNotPrintExampleForTheseMethods = array(
  122. //Sites
  123. 'deleteSite',
  124. 'addSite',
  125. 'updateSite',
  126. 'addSiteAliasUrls',
  127. //Users
  128. 'deleteUser',
  129. 'addUser',
  130. 'updateUser',
  131. 'setUserAccess',
  132. //Goals
  133. 'addGoal',
  134. 'updateGoal',
  135. 'deleteGoal',
  136. );
  137. if(in_array($methodName,$doNotPrintExampleForTheseMethods))
  138. {
  139. return false;
  140. }
  141. // we try to give an URL example to call the API
  142. $aParameters = Core_API_Proxy::getInstance()->getParametersList($class, $methodName);
  143. $moduleName = Core_API_Proxy::getInstance()->getModuleNameFromClassName($class);
  144. $urlExample = '?module=API&method='.$moduleName.'.'.$methodName.'&';
  145. foreach($aParameters as $nameVariable=> $defaultValue)
  146. {
  147. // if there isn't a default value for a given parameter,
  148. // we need a 'know default value' or we can't generate the link
  149. if($defaultValue instanceof Core_API_Proxy_NoDefaultValue)
  150. {
  151. if(isset($knowExampleDefaultParametersValues[$nameVariable]))
  152. {
  153. $exampleValue = $knowExampleDefaultParametersValues[$nameVariable];
  154. $urlExample .= $nameVariable . '=' . $exampleValue . '&';
  155. }
  156. else
  157. {
  158. return false;
  159. }
  160. }
  161. }
  162. return substr($urlExample,0,-1);
  163. }
  164. /**
  165. * Returns the methods $class.$name parameters (and default value if provided) as a string.
  166. *
  167. * @param string The class name
  168. * @param string The method name
  169. * @return string For example "(idSite, period, date = 'today')"
  170. */
  171. protected function getStrListParameters($class, $name)
  172. {
  173. $aParameters = Core_API_Proxy::getInstance()->getParametersList($class, $name);
  174. $asParameters = array();
  175. foreach($aParameters as $nameVariable=> $defaultValue)
  176. {
  177. $str = $nameVariable;
  178. if(!($defaultValue instanceof Core_API_Proxy_NoDefaultValue))
  179. {
  180. $str .= " = '$defaultValue'";
  181. }
  182. $asParameters[] = $str;
  183. }
  184. $sParameters = implode(", ", $asParameters);
  185. return "($sParameters)";
  186. }
  187. }