1
0

Api.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1\Vcs;
  3. use Parsedown;
  4. use PucReadmeParser;
  5. if ( !class_exists(Api::class, false) ):
  6. abstract class Api {
  7. const STRATEGY_LATEST_RELEASE = 'latest_release';
  8. const STRATEGY_LATEST_TAG = 'latest_tag';
  9. const STRATEGY_STABLE_TAG = 'stable_tag';
  10. const STRATEGY_BRANCH = 'branch';
  11. /**
  12. * Consider all releases regardless of their version number or prerelease/upcoming
  13. * release status.
  14. */
  15. const RELEASE_FILTER_ALL = 3;
  16. /**
  17. * Exclude releases that have the "prerelease" or "upcoming release" flag.
  18. *
  19. * This does *not* look for prerelease keywords like "beta" in the version number.
  20. * It only uses the data provided by the API. For example, on GitHub, you can
  21. * manually mark a release as a prerelease.
  22. */
  23. const RELEASE_FILTER_SKIP_PRERELEASE = 1;
  24. /**
  25. * If there are no release assets or none of them match the configured filter,
  26. * fall back to the automatically generated source code archive.
  27. */
  28. const PREFER_RELEASE_ASSETS = 1;
  29. /**
  30. * Skip releases that don't have any matching release assets.
  31. */
  32. const REQUIRE_RELEASE_ASSETS = 2;
  33. protected $tagNameProperty = 'name';
  34. protected $slug = '';
  35. /**
  36. * @var string
  37. */
  38. protected $repositoryUrl = '';
  39. /**
  40. * @var mixed Authentication details for private repositories. Format depends on service.
  41. */
  42. protected $credentials = null;
  43. /**
  44. * @var string The filter tag that's used to filter options passed to wp_remote_get.
  45. * For example, "puc_request_info_options-slug" or "puc_request_update_options_theme-slug".
  46. */
  47. protected $httpFilterName = '';
  48. /**
  49. * @var string The filter applied to the list of update detection strategies that
  50. * are used to find the latest version.
  51. */
  52. protected $strategyFilterName = '';
  53. /**
  54. * @var string|null
  55. */
  56. protected $localDirectory = null;
  57. /**
  58. * Api constructor.
  59. *
  60. * @param string $repositoryUrl
  61. * @param array|string|null $credentials
  62. */
  63. public function __construct($repositoryUrl, $credentials = null) {
  64. $this->repositoryUrl = $repositoryUrl;
  65. $this->setAuthentication($credentials);
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getRepositoryUrl() {
  71. return $this->repositoryUrl;
  72. }
  73. /**
  74. * Figure out which reference (i.e. tag or branch) contains the latest version.
  75. *
  76. * @param string $configBranch Start looking in this branch.
  77. * @return null|Reference
  78. */
  79. public function chooseReference($configBranch) {
  80. $strategies = $this->getUpdateDetectionStrategies($configBranch);
  81. if ( !empty($this->strategyFilterName) ) {
  82. $strategies = apply_filters(
  83. $this->strategyFilterName,
  84. $strategies,
  85. $this->slug
  86. );
  87. }
  88. foreach ($strategies as $strategy) {
  89. $reference = call_user_func($strategy);
  90. if ( !empty($reference) ) {
  91. return $reference;
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * Get an ordered list of strategies that can be used to find the latest version.
  98. *
  99. * The update checker will try each strategy in order until one of them
  100. * returns a valid reference.
  101. *
  102. * @param string $configBranch
  103. * @return array<callable> Array of callables that return Vcs_Reference objects.
  104. */
  105. abstract protected function getUpdateDetectionStrategies($configBranch);
  106. /**
  107. * Get the readme.txt file from the remote repository and parse it
  108. * according to the plugin readme standard.
  109. *
  110. * @param string $ref Tag or branch name.
  111. * @return array Parsed readme.
  112. */
  113. public function getRemoteReadme($ref = 'master') {
  114. $fileContents = $this->getRemoteFile($this->getLocalReadmeName(), $ref);
  115. if ( empty($fileContents) ) {
  116. return array();
  117. }
  118. $parser = new PucReadmeParser();
  119. return $parser->parse_readme_contents($fileContents);
  120. }
  121. /**
  122. * Get the case-sensitive name of the local readme.txt file.
  123. *
  124. * In most cases it should just be called "readme.txt", but some plugins call it "README.txt",
  125. * "README.TXT", or even "Readme.txt". Most VCS are case-sensitive so we need to know the correct
  126. * capitalization.
  127. *
  128. * Defaults to "readme.txt" (all lowercase).
  129. *
  130. * @return string
  131. */
  132. public function getLocalReadmeName() {
  133. static $fileName = null;
  134. if ( $fileName !== null ) {
  135. return $fileName;
  136. }
  137. $fileName = 'readme.txt';
  138. if ( isset($this->localDirectory) ) {
  139. $files = scandir($this->localDirectory);
  140. if ( !empty($files) ) {
  141. foreach ($files as $possibleFileName) {
  142. if ( strcasecmp($possibleFileName, 'readme.txt') === 0 ) {
  143. $fileName = $possibleFileName;
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. return $fileName;
  150. }
  151. /**
  152. * Get a branch.
  153. *
  154. * @param string $branchName
  155. * @return Reference|null
  156. */
  157. abstract public function getBranch($branchName);
  158. /**
  159. * Get a specific tag.
  160. *
  161. * @param string $tagName
  162. * @return Reference|null
  163. */
  164. abstract public function getTag($tagName);
  165. /**
  166. * Get the tag that looks like the highest version number.
  167. * (Implementations should skip pre-release versions if possible.)
  168. *
  169. * @return Reference|null
  170. */
  171. abstract public function getLatestTag();
  172. /**
  173. * Check if a tag name string looks like a version number.
  174. *
  175. * @param string $name
  176. * @return bool
  177. */
  178. protected function looksLikeVersion($name) {
  179. //Tag names may be prefixed with "v", e.g. "v1.2.3".
  180. $name = ltrim($name, 'v');
  181. //The version string must start with a number.
  182. if ( !is_numeric(substr($name, 0, 1)) ) {
  183. return false;
  184. }
  185. //The goal is to accept any SemVer-compatible or "PHP-standardized" version number.
  186. return (preg_match('@^(\d{1,5}?)(\.\d{1,10}?){0,4}?($|[abrdp+_\-]|\s)@i', $name) === 1);
  187. }
  188. /**
  189. * Check if a tag appears to be named like a version number.
  190. *
  191. * @param \stdClass $tag
  192. * @return bool
  193. */
  194. protected function isVersionTag($tag) {
  195. $property = $this->tagNameProperty;
  196. return isset($tag->$property) && $this->looksLikeVersion($tag->$property);
  197. }
  198. /**
  199. * Sort a list of tags as if they were version numbers.
  200. * Tags that don't look like version number will be removed.
  201. *
  202. * @param \stdClass[] $tags Array of tag objects.
  203. * @return \stdClass[] Filtered array of tags sorted in descending order.
  204. */
  205. protected function sortTagsByVersion($tags) {
  206. //Keep only those tags that look like version numbers.
  207. $versionTags = array_filter($tags, array($this, 'isVersionTag'));
  208. //Sort them in descending order.
  209. usort($versionTags, array($this, 'compareTagNames'));
  210. return $versionTags;
  211. }
  212. /**
  213. * Compare two tags as if they were version number.
  214. *
  215. * @param \stdClass $tag1 Tag object.
  216. * @param \stdClass $tag2 Another tag object.
  217. * @return int
  218. */
  219. protected function compareTagNames($tag1, $tag2) {
  220. $property = $this->tagNameProperty;
  221. if ( !isset($tag1->$property) ) {
  222. return 1;
  223. }
  224. if ( !isset($tag2->$property) ) {
  225. return -1;
  226. }
  227. return -version_compare(ltrim($tag1->$property, 'v'), ltrim($tag2->$property, 'v'));
  228. }
  229. /**
  230. * Get the contents of a file from a specific branch or tag.
  231. *
  232. * @param string $path File name.
  233. * @param string $ref
  234. * @return null|string Either the contents of the file, or null if the file doesn't exist or there's an error.
  235. */
  236. abstract public function getRemoteFile($path, $ref = 'master');
  237. /**
  238. * Get the timestamp of the latest commit that changed the specified branch or tag.
  239. *
  240. * @param string $ref Reference name (e.g. branch or tag).
  241. * @return string|null
  242. */
  243. abstract public function getLatestCommitTime($ref);
  244. /**
  245. * Get the contents of the changelog file from the repository.
  246. *
  247. * @param string $ref
  248. * @param string $localDirectory Full path to the local plugin or theme directory.
  249. * @return null|string The HTML contents of the changelog.
  250. */
  251. public function getRemoteChangelog($ref, $localDirectory) {
  252. $filename = $this->findChangelogName($localDirectory);
  253. if ( empty($filename) ) {
  254. return null;
  255. }
  256. $changelog = $this->getRemoteFile($filename, $ref);
  257. if ( $changelog === null ) {
  258. return null;
  259. }
  260. return Parsedown::instance()->text($changelog);
  261. }
  262. /**
  263. * Guess the name of the changelog file.
  264. *
  265. * @param string $directory
  266. * @return string|null
  267. */
  268. protected function findChangelogName($directory = null) {
  269. if ( !isset($directory) ) {
  270. $directory = $this->localDirectory;
  271. }
  272. if ( empty($directory) || !is_dir($directory) || ($directory === '.') ) {
  273. return null;
  274. }
  275. $possibleNames = array('CHANGES.md', 'CHANGELOG.md', 'changes.md', 'changelog.md');
  276. $files = scandir($directory);
  277. $foundNames = array_intersect($possibleNames, $files);
  278. if ( !empty($foundNames) ) {
  279. return reset($foundNames);
  280. }
  281. return null;
  282. }
  283. /**
  284. * Set authentication credentials.
  285. *
  286. * @param $credentials
  287. */
  288. public function setAuthentication($credentials) {
  289. $this->credentials = $credentials;
  290. }
  291. public function isAuthenticationEnabled() {
  292. return !empty($this->credentials);
  293. }
  294. /**
  295. * @param string $url
  296. * @return string
  297. */
  298. public function signDownloadUrl($url) {
  299. return $url;
  300. }
  301. /**
  302. * @param string $filterName
  303. */
  304. public function setHttpFilterName($filterName) {
  305. $this->httpFilterName = $filterName;
  306. }
  307. /**
  308. * @param string $filterName
  309. */
  310. public function setStrategyFilterName($filterName) {
  311. $this->strategyFilterName = $filterName;
  312. }
  313. /**
  314. * @param string $directory
  315. */
  316. public function setLocalDirectory($directory) {
  317. if ( empty($directory) || !is_dir($directory) || ($directory === '.') ) {
  318. $this->localDirectory = null;
  319. } else {
  320. $this->localDirectory = $directory;
  321. }
  322. }
  323. /**
  324. * @param string $slug
  325. */
  326. public function setSlug($slug) {
  327. $this->slug = $slug;
  328. }
  329. }
  330. endif;