SymlinkDumper.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Package;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. use Composer\Util\Filesystem as ComposerFilesystem;
  14. use Symfony\Bridge\Doctrine\RegistryInterface;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Finder\Finder;
  17. use Packagist\WebBundle\Entity\Version;
  18. use Doctrine\DBAL\Connection;
  19. /**
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class SymlinkDumper
  23. {
  24. /**
  25. * Doctrine
  26. * @var RegistryInterface
  27. */
  28. protected $doctrine;
  29. /**
  30. * @var Filesystem
  31. */
  32. protected $fs;
  33. /**
  34. * @var ComposerFilesystem
  35. */
  36. protected $cfs;
  37. /**
  38. * @var string
  39. */
  40. protected $webDir;
  41. /**
  42. * @var string
  43. */
  44. protected $buildDir;
  45. /**
  46. * @var UrlGeneratorInterface
  47. */
  48. protected $router;
  49. /**
  50. * Data cache
  51. * @var array
  52. */
  53. private $rootFile;
  54. /**
  55. * Data cache
  56. * @var array
  57. */
  58. private $listings = array();
  59. /**
  60. * Data cache
  61. * @var array
  62. */
  63. private $individualFiles = array();
  64. /**
  65. * Modified times of individual files
  66. * @var array
  67. */
  68. private $individualFilesMtime = array();
  69. /**
  70. * Stores all the disk writes to be replicated in the second build dir after the symlink has been swapped
  71. * @var array
  72. */
  73. private $writeLog = array();
  74. /**
  75. * Generate compressed files.
  76. * @var int 0 disabled, 9 maximum.
  77. */
  78. private $compress;
  79. /**
  80. * Constructor
  81. *
  82. * @param RegistryInterface $doctrine
  83. * @param Filesystem $filesystem
  84. * @param UrlGeneratorInterface $router
  85. * @param string $webDir web root
  86. * @param string $targetDir
  87. * @param int $compress
  88. */
  89. public function __construct(RegistryInterface $doctrine, Filesystem $filesystem, UrlGeneratorInterface $router, $webDir, $targetDir, $compress)
  90. {
  91. $this->doctrine = $doctrine;
  92. $this->fs = $filesystem;
  93. $this->cfs = new ComposerFilesystem;
  94. $this->router = $router;
  95. $this->webDir = realpath($webDir);
  96. $this->buildDir = $targetDir;
  97. $this->compress = $compress;
  98. }
  99. /**
  100. * Dump a set of packages to the web root
  101. *
  102. * @param array $packageIds
  103. * @param Boolean $force
  104. * @param Boolean $verbose
  105. */
  106. public function dump(array $packageIds, $force = false, $verbose = false)
  107. {
  108. $cleanUpOldFiles = date('i') == 0;
  109. // prepare build dir
  110. $webDir = $this->webDir;
  111. $buildDirA = $this->buildDir.'/a';
  112. $buildDirB = $this->buildDir.'/b';
  113. // initialize
  114. $initialRun = false;
  115. if (!is_dir($buildDirA) || !is_dir($buildDirB)) {
  116. $initialRun = true;
  117. if (!$this->removeDirectory($buildDirA) || !$this->removeDirectory($buildDirB)) {
  118. throw new \RuntimeException('Failed to delete '.$buildDirA.' or '.$buildDirB);
  119. }
  120. $this->fs->mkdir($buildDirA);
  121. $this->fs->mkdir($buildDirB);
  122. }
  123. // set build dir to the not-active one
  124. if (realpath($webDir.'/p') === realpath($buildDirA)) {
  125. $buildDir = realpath($buildDirB);
  126. $oldBuildDir = realpath($buildDirA);
  127. } else {
  128. $buildDir = realpath($buildDirA);
  129. $oldBuildDir = realpath($buildDirB);
  130. }
  131. // copy existing stuff for smooth BC transition
  132. if ($initialRun && !$force) {
  133. if (!file_exists($webDir.'/p') || is_link($webDir.'/p')) {
  134. @rmdir($buildDir);
  135. @rmdir($oldBuildDir);
  136. throw new \RuntimeException('Run this again with --force the first time around to make sure it dumps all packages');
  137. }
  138. if ($verbose) {
  139. echo 'Copying existing files'.PHP_EOL;
  140. }
  141. foreach (array($buildDir, $oldBuildDir) as $dir) {
  142. $this->cloneDir($webDir.'/p', $dir);
  143. }
  144. }
  145. if ($verbose) {
  146. echo 'Web dir is '.$webDir.'/p ('.realpath($webDir.'/p').')'.PHP_EOL;
  147. echo 'Build dir is '.$buildDir.PHP_EOL;
  148. }
  149. // clean the build dir to start over if we are re-dumping everything
  150. if ($force) {
  151. // disable the write log since we copy everything at the end in forced mode
  152. $this->writeLog = false;
  153. if ($verbose) {
  154. echo 'Cleaning up existing files'.PHP_EOL;
  155. }
  156. if (!$this->clearDirectory($buildDir)) {
  157. return false;
  158. }
  159. }
  160. $dumpTimeUpdates = [];
  161. try {
  162. $modifiedIndividualFiles = array();
  163. $total = count($packageIds);
  164. $current = 0;
  165. $step = 50;
  166. while ($packageIds) {
  167. $dumpTime = new \DateTime;
  168. $packages = $this->doctrine->getRepository('PackagistWebBundle:Package')->getPackagesWithVersions(array_splice($packageIds, 0, $step));
  169. if ($verbose) {
  170. echo '['.sprintf('%'.strlen($total).'d', $current).'/'.$total.'] Processing '.$step.' packages'.PHP_EOL;
  171. }
  172. $current += $step;
  173. // prepare packages in memory
  174. foreach ($packages as $package) {
  175. $affectedFiles = array();
  176. $name = strtolower($package->getName());
  177. // clean up versions in individual files
  178. if (file_exists($buildDir.'/'.$name.'.files')) {
  179. $files = json_decode(file_get_contents($buildDir.'/'.$name.'.files'));
  180. foreach ($files as $file) {
  181. if (substr_count($file, '/') > 1) { // handle old .files with p/*/*.json paths
  182. $file = preg_replace('{^p/}', '', $file);
  183. }
  184. $this->loadIndividualFile($buildDir.'/'.$file, $file);
  185. if (isset($this->individualFiles[$file]['packages'][$name])) {
  186. unset($this->individualFiles[$file]['packages'][$name]);
  187. $modifiedIndividualFiles[$file] = true;
  188. }
  189. }
  190. }
  191. // (re)write versions in individual files
  192. foreach ($package->getVersions() as $version) {
  193. foreach (array_slice($version->getNames(), 0, 150) as $versionName) {
  194. if (!preg_match('{^[A-Za-z0-9_-][A-Za-z0-9_.-]*/[A-Za-z0-9_-][A-Za-z0-9_.-]*$}', $versionName) || strpos($versionName, '..')) {
  195. continue;
  196. }
  197. $file = $buildDir.'/'.$versionName.'.json';
  198. $key = $versionName.'.json';
  199. $this->dumpVersionToIndividualFile($version, $file, $key);
  200. $modifiedIndividualFiles[$key] = true;
  201. $affectedFiles[$key] = true;
  202. }
  203. }
  204. // store affected files to clean up properly in the next update
  205. $this->fs->mkdir(dirname($buildDir.'/'.$name));
  206. $this->writeFile($buildDir.'/'.$name.'.files', json_encode(array_keys($affectedFiles)));
  207. $dumpTimeUpdates[$dumpTime->format('Y-m-d H:i:s')][] = $package->getId();
  208. }
  209. unset($packages, $package, $version);
  210. $this->doctrine->getManager()->clear();
  211. if ($current % 250 === 0 || !$packageIds) {
  212. if ($verbose) {
  213. echo 'Dumping individual files'.PHP_EOL;
  214. }
  215. $this->dumpIndividualFiles($buildDir);
  216. }
  217. }
  218. // prepare individual files listings
  219. if ($verbose) {
  220. echo 'Preparing individual files listings'.PHP_EOL;
  221. }
  222. $safeFiles = array();
  223. $individualHashedListings = array();
  224. $finder = Finder::create()->files()->ignoreVCS(true)->name('*.json')->in($buildDir)->depth('1');
  225. foreach ($finder as $file) {
  226. // skip hashed files
  227. if (strpos($file, '$')) {
  228. continue;
  229. }
  230. $key = basename(dirname($file)).'/'.basename($file);
  231. if ($force && !isset($modifiedIndividualFiles[$key])) {
  232. continue;
  233. }
  234. // add hashed provider to listing
  235. $listing = $this->getTargetListing($file);
  236. $hash = hash_file('sha256', $file);
  237. $key = substr($key, 0, -5);
  238. $safeFiles[] = $key.'$'.$hash.'.json';
  239. $this->listings[$listing]['providers'][$key] = array('sha256' => $hash);
  240. $individualHashedListings[$listing] = true;
  241. }
  242. // prepare root file
  243. $rootFile = $buildDir.'/packages.json';
  244. $this->rootFile = array('packages' => array());
  245. $url = $this->router->generate('track_download', array('name' => 'VND/PKG'));
  246. $this->rootFile['notify'] = str_replace('VND/PKG', '%package%', $url);
  247. $this->rootFile['notify-batch'] = $this->router->generate('track_download_batch');
  248. $this->rootFile['providers-url'] = $this->router->generate('home') . 'p/%package%$%hash%.json';
  249. $this->rootFile['search'] = $this->router->generate('search', array('_format' => 'json')) . '?q=%query%&type=%type%';
  250. if ($verbose) {
  251. echo 'Dumping individual listings'.PHP_EOL;
  252. }
  253. // dump listings to build dir
  254. foreach ($individualHashedListings as $listing => $dummy) {
  255. list($listingPath, $hash) = $this->dumpListing($buildDir.'/'.$listing);
  256. $hashedListing = basename($listingPath);
  257. $this->rootFile['provider-includes']['p/'.str_replace($hash, '%hash%', $hashedListing)] = array('sha256' => $hash);
  258. $safeFiles[] = $hashedListing;
  259. }
  260. if ($verbose) {
  261. echo 'Dumping root'.PHP_EOL;
  262. }
  263. $this->dumpRootFile($rootFile);
  264. } catch (\Exception $e) {
  265. // restore files as they were before we started
  266. $this->cloneDir($oldBuildDir, $buildDir);
  267. throw $e;
  268. }
  269. try {
  270. if ($verbose) {
  271. echo 'Putting new files in production'.PHP_EOL;
  272. }
  273. // move away old files for BC update
  274. if ($initialRun && file_exists($webDir.'/p') && !is_link($webDir.'/p')) {
  275. rename($webDir.'/p', $webDir.'/p-old');
  276. }
  277. $this->switchActiveWebDir($webDir, $buildDir);
  278. } catch (\Exception $e) {
  279. @symlink($oldBuildDir, $webDir.'/p');
  280. throw $e;
  281. }
  282. try {
  283. if ($initialRun || !is_link($webDir.'/packages.json') || $force) {
  284. if ($verbose) {
  285. echo 'Writing/linking the packages.json'.PHP_EOL;
  286. }
  287. if (file_exists($webDir.'/packages.json')) {
  288. unlink($webDir.'/packages.json');
  289. }
  290. if (file_exists($webDir.'/packages.json.gz')) {
  291. unlink($webDir.'/packages.json.gz');
  292. }
  293. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  294. $sourcePath = $buildDir.'/packages.json';
  295. if (!copy($sourcePath, $webDir.'/packages.json')) {
  296. throw new \RuntimeException('Could not copy the packages.json file');
  297. }
  298. } else {
  299. $sourcePath = 'p/packages.json';
  300. if (!symlink($sourcePath, $webDir.'/packages.json')) {
  301. throw new \RuntimeException('Could not symlink the packages.json file');
  302. }
  303. if ($this->compress && !symlink($sourcePath.'.gz', $webDir.'/packages.json.gz')) {
  304. throw new \RuntimeException('Could not symlink the packages.json.gz file');
  305. }
  306. }
  307. }
  308. } catch (\Exception $e) {
  309. $this->switchActiveWebDir($webDir, $oldBuildDir);
  310. throw $e;
  311. }
  312. // clean up old dir if present on BC update
  313. if ($initialRun) {
  314. $this->removeDirectory($webDir.'/p-old');
  315. }
  316. // clean the old build dir if we re-dumped everything
  317. if ($force) {
  318. if ($verbose) {
  319. echo 'Cleaning up old build dir'.PHP_EOL;
  320. }
  321. if (!$this->clearDirectory($oldBuildDir)) {
  322. throw new \RuntimeException('Unrecoverable inconsistent state (old build dir could not be cleared), run with --force again to retry');
  323. }
  324. }
  325. // copy state to old active dir
  326. if ($force) {
  327. if ($verbose) {
  328. echo 'Copying new contents to old build dir to sync up'.PHP_EOL;
  329. }
  330. $this->cloneDir($buildDir, $oldBuildDir);
  331. } else {
  332. if ($verbose) {
  333. echo 'Replaying write log in old build dir'.PHP_EOL;
  334. }
  335. $this->copyWriteLog($buildDir, $oldBuildDir);
  336. }
  337. // clean up old files once an hour
  338. if (!$force && $cleanUpOldFiles) {
  339. if ($verbose) {
  340. echo 'Cleaning up old files'.PHP_EOL;
  341. }
  342. $this->cleanOldFiles($buildDir, $oldBuildDir, $safeFiles);
  343. }
  344. if ($verbose) {
  345. echo 'Updating package dump times'.PHP_EOL;
  346. }
  347. foreach ($dumpTimeUpdates as $dt => $ids) {
  348. $retries = 5;
  349. // retry loop in case of a lock timeout
  350. while ($retries--) {
  351. try {
  352. $this->doctrine->getManager()->getConnection()->executeQuery(
  353. 'UPDATE package SET dumpedAt=:dumped WHERE id IN (:ids)',
  354. [
  355. 'ids' => $ids,
  356. 'dumped' => $dt,
  357. ],
  358. ['ids' => Connection::PARAM_INT_ARRAY]
  359. );
  360. } catch (\Exception $e) {
  361. if (!$retries) {
  362. throw $e;
  363. }
  364. sleep(2);
  365. }
  366. }
  367. }
  368. // TODO when a package is deleted, it should be removed from provider files, or marked for removal at least
  369. return true;
  370. }
  371. private function switchActiveWebDir($webDir, $buildDir)
  372. {
  373. $newLink = $webDir.'/p-new';
  374. $oldLink = $webDir.'/p';
  375. if (file_exists($newLink)) {
  376. unlink($newLink);
  377. }
  378. if (!symlink($buildDir, $newLink)) {
  379. throw new \RuntimeException('Could not symlink the build dir into the web dir');
  380. }
  381. if (!rename($newLink, $oldLink)) {
  382. throw new \RuntimeException('Could not replace the old symlink with the new one in the web dir');
  383. }
  384. }
  385. private function cloneDir($source, $target)
  386. {
  387. $this->removeDirectory($target);
  388. exec('cp -rpf '.escapeshellarg($source).' '.escapeshellarg($target), $output, $exit);
  389. if (0 !== $exit) {
  390. echo 'Warning, cloning a directory using the php fallback does not keep filemtime, invalid behavior may occur';
  391. $this->fs->mirror($source, $target, null, array('override' => true));
  392. }
  393. }
  394. private function cleanOldFiles($buildDir, $oldBuildDir, $safeFiles)
  395. {
  396. $finder = Finder::create()->directories()->ignoreVCS(true)->in($buildDir);
  397. foreach ($finder as $vendorDir) {
  398. $vendorFiles = Finder::create()->files()->ignoreVCS(true)
  399. ->name('/\$[a-f0-9]+\.json$/')
  400. ->date('until 10minutes ago')
  401. ->in((string) $vendorDir);
  402. foreach ($vendorFiles as $file) {
  403. $key = strtr(str_replace($buildDir.DIRECTORY_SEPARATOR, '', $file), '\\', '/');
  404. if (!in_array($key, $safeFiles, true)) {
  405. unlink((string) $file);
  406. if (file_exists($altDirFile = str_replace($buildDir, $oldBuildDir, (string) $file))) {
  407. unlink($altDirFile);
  408. }
  409. }
  410. }
  411. }
  412. // clean up old provider listings
  413. $finder = Finder::create()->depth(0)->files()->name('provider-*.json')->ignoreVCS(true)->in($buildDir)->date('until 10minutes ago');
  414. foreach ($finder as $provider) {
  415. $key = strtr(str_replace($buildDir.DIRECTORY_SEPARATOR, '', $provider), '\\', '/');
  416. if (!in_array($key, $safeFiles, true)) {
  417. $path = (string) $provider;
  418. unlink($path);
  419. if (file_exists($path.'.gz')) {
  420. unlink($path.'.gz');
  421. }
  422. if (file_exists($altDirFile = str_replace($buildDir, $oldBuildDir, $path))) {
  423. unlink($altDirFile);
  424. if (file_exists($altDirFile.'.gz')) {
  425. unlink($altDirFile.'.gz');
  426. }
  427. }
  428. }
  429. }
  430. // clean up old root listings
  431. $finder = Finder::create()->depth(0)->files()->name('packages.json-*')->ignoreVCS(true)->in($buildDir)->date('until 10minutes ago');
  432. foreach ($finder as $rootFile) {
  433. $path = (string) $rootFile;
  434. unlink($path);
  435. if (file_exists($path.'.gz')) {
  436. unlink($path.'.gz');
  437. }
  438. if (file_exists($altDirFile = str_replace($buildDir, $oldBuildDir, $path))) {
  439. unlink($altDirFile);
  440. if (file_exists($altDirFile.'.gz')) {
  441. unlink($altDirFile.'.gz');
  442. }
  443. }
  444. }
  445. }
  446. private function dumpRootFile($file)
  447. {
  448. // sort all versions and packages to make sha1 consistent
  449. ksort($this->rootFile['packages']);
  450. ksort($this->rootFile['provider-includes']);
  451. foreach ($this->rootFile['packages'] as $package => $versions) {
  452. ksort($this->rootFile['packages'][$package]);
  453. }
  454. if (file_exists($file)) {
  455. $timedFile = $file.'-'.time();
  456. rename($file, $timedFile);
  457. if (file_exists($file.'.gz')) {
  458. rename($file.'.gz', $timedFile.'.gz');
  459. }
  460. }
  461. $json = json_encode($this->rootFile);
  462. $time = time();
  463. $this->writeFile($file, $json, $time);
  464. if ($this->compress) {
  465. $this->writeFile($file . '.gz', gzencode($json, $this->compress), $time);
  466. }
  467. }
  468. private function dumpListing($path)
  469. {
  470. $key = basename($path);
  471. // sort files to make hash consistent
  472. ksort($this->listings[$key]['providers']);
  473. $json = json_encode($this->listings[$key]);
  474. $hash = hash('sha256', $json);
  475. $path = substr($path, 0, -5) . '$' . $hash . '.json';
  476. $time = time();
  477. if (!file_exists($path)) {
  478. $this->writeFile($path, $json, $time);
  479. if ($this->compress) {
  480. $this->writeFile($path . '.gz', gzencode($json, $this->compress), $time);
  481. }
  482. }
  483. return array($path, $hash);
  484. }
  485. private function loadIndividualFile($path, $key)
  486. {
  487. if (isset($this->individualFiles[$key])) {
  488. return;
  489. }
  490. if (file_exists($path)) {
  491. $this->individualFiles[$key] = json_decode(file_get_contents($path), true);
  492. $this->individualFilesMtime[$key] = filemtime($path);
  493. } else {
  494. $this->individualFiles[$key] = array();
  495. $this->individualFilesMtime[$key] = 0;
  496. }
  497. }
  498. private function dumpIndividualFiles($buildDir)
  499. {
  500. // dump individual files to build dir
  501. foreach ($this->individualFiles as $file => $dummy) {
  502. $this->dumpIndividualFile($buildDir.'/'.$file, $file);
  503. }
  504. $this->individualFiles = array();
  505. $this->individualFilesMtime = array();
  506. }
  507. private function dumpIndividualFile($path, $key)
  508. {
  509. // sort all versions and packages to make sha1 consistent
  510. ksort($this->individualFiles[$key]['packages']);
  511. foreach ($this->individualFiles[$key]['packages'] as $package => $versions) {
  512. ksort($this->individualFiles[$key]['packages'][$package]);
  513. }
  514. $this->fs->mkdir(dirname($path));
  515. $json = json_encode($this->individualFiles[$key]);
  516. $this->writeFile($path, $json, $this->individualFilesMtime[$key]);
  517. // write the hashed provider file
  518. $hashedFile = substr($path, 0, -5) . '$' . hash('sha256', $json) . '.json';
  519. $this->writeFile($hashedFile, $json);
  520. }
  521. private function dumpVersionToIndividualFile(Version $version, $file, $key)
  522. {
  523. $this->loadIndividualFile($file, $key);
  524. $data = $version->toArray();
  525. $data['uid'] = $version->getId();
  526. $this->individualFiles[$key]['packages'][strtolower($version->getName())][$version->getVersion()] = $data;
  527. $timestamp = $version->getReleasedAt() ? $version->getReleasedAt()->getTimestamp() : time();
  528. if (!isset($this->individualFilesMtime[$key]) || $this->individualFilesMtime[$key] < $timestamp) {
  529. $this->individualFilesMtime[$key] = $timestamp;
  530. }
  531. }
  532. private function clearDirectory($path)
  533. {
  534. if (!$this->removeDirectory($path)) {
  535. echo 'Could not remove the build dir entirely, aborting';
  536. return false;
  537. }
  538. $this->fs->mkdir($path);
  539. return true;
  540. }
  541. private function removeDirectory($path)
  542. {
  543. $retries = 5;
  544. do {
  545. if (!$this->cfs->removeDirectory($path)) {
  546. usleep(200);
  547. }
  548. clearstatcache();
  549. } while (is_dir($path) && $retries--);
  550. return !is_dir($path);
  551. }
  552. private function getTargetListingBlocks($now)
  553. {
  554. $blocks = array();
  555. // monday last week
  556. $blocks['latest'] = strtotime('monday last week', $now);
  557. $month = date('n', $now);
  558. $month = ceil($month / 3) * 3 - 2; // 1 for months 1-3, 10 for months 10-12
  559. $block = new \DateTime(date('Y', $now).'-'.$month.'-01'); // 1st day of current trimester
  560. // split last 12 months in 4 trimesters
  561. for ($i=0; $i < 4; $i++) {
  562. $blocks[$block->format('Y-m')] = $block->getTimestamp();
  563. $block->sub(new \DateInterval('P3M'));
  564. }
  565. $year = (int) $block->format('Y');
  566. while ($year >= 2013) {
  567. $blocks[''.$year] = strtotime($year.'-01-01');
  568. $year--;
  569. }
  570. return $blocks;
  571. }
  572. private function getTargetListing($file)
  573. {
  574. static $blocks;
  575. if (!$blocks) {
  576. $blocks = $this->getTargetListingBlocks(time());
  577. }
  578. $mtime = filemtime($file);
  579. foreach ($blocks as $label => $block) {
  580. if ($mtime >= $block) {
  581. return "provider-${label}.json";
  582. }
  583. }
  584. return "provider-archived.json";
  585. }
  586. private function writeFile($path, $contents, $mtime = null)
  587. {
  588. file_put_contents($path, $contents);
  589. if ($mtime !== null) {
  590. touch($path, $mtime);
  591. }
  592. if (is_array($this->writeLog)) {
  593. $this->writeLog[$path] = array($contents, $mtime);
  594. }
  595. }
  596. private function copyWriteLog($from, $to)
  597. {
  598. foreach ($this->writeLog as $path => $op) {
  599. $path = str_replace($from, $to, $path);
  600. $this->fs->mkdir(dirname($path));
  601. file_put_contents($path, $op[0]);
  602. if ($op[1] !== null) {
  603. touch($path, $op[1]);
  604. }
  605. }
  606. }
  607. }