Upgrade.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Widget;
  3. use Typecho\Common;
  4. use Typecho\Exception;
  5. use Widget\Base\Options as BaseOptions;
  6. if (!defined('__TYPECHO_ROOT_DIR__')) {
  7. exit;
  8. }
  9. /**
  10. * 升级组件
  11. *
  12. * @author qining
  13. * @category typecho
  14. * @package Widget
  15. */
  16. class Upgrade extends BaseOptions implements ActionInterface
  17. {
  18. /**
  19. * 执行升级程序
  20. *
  21. * @throws \Typecho\Db\Exception
  22. */
  23. public function upgrade()
  24. {
  25. $packages = get_class_methods('Upgrade');
  26. preg_match("/^\w+ ([0-9\.]+)(\/[0-9\.]+)?$/i", $this->options->generator, $matches);
  27. $currentVersion = $matches[1];
  28. $currentMinor = '0';
  29. if (isset($matches[2])) {
  30. $currentMinor = substr($matches[2], 1);
  31. }
  32. $message = [];
  33. foreach ($packages as $package) {
  34. preg_match("/^v([_0-9]+)(r[_0-9]+)?$/", $package, $matches);
  35. $version = str_replace('_', '.', $matches[1]);
  36. if (version_compare($currentVersion, $version, '>')) {
  37. break;
  38. }
  39. if (isset($matches[2])) {
  40. $minor = substr(str_replace('_', '.', $matches[2]), 1);
  41. if (
  42. version_compare($currentVersion, $version, '=')
  43. && version_compare($currentMinor, $minor, '>=')
  44. ) {
  45. break;
  46. }
  47. $version .= '/' . $minor;
  48. }
  49. $options = Options::allocWithAlias($package);
  50. /** 执行升级脚本 */
  51. try {
  52. $result = call_user_func([\Utils\Upgrade::class, $package], $this->db, $options);
  53. if (!empty($result)) {
  54. $message[] = $result;
  55. }
  56. } catch (Exception $e) {
  57. Notice::alloc()->set($e->getMessage(), 'error');
  58. $this->response->goBack();
  59. }
  60. /** 更新版本号 */
  61. $this->update(
  62. ['value' => 'Typecho ' . $version],
  63. $this->db->sql()->where('name = ?', 'generator')
  64. );
  65. Options::destroy($package);
  66. }
  67. /** 更新版本号 */
  68. $this->update(
  69. ['value' => 'Typecho ' . Common::VERSION],
  70. $this->db->sql()->where('name = ?', 'generator')
  71. );
  72. Notice::alloc()->set(
  73. empty($message) ? _t("升级已经完成") : $message,
  74. empty($message) ? 'success' : 'notice'
  75. );
  76. }
  77. /**
  78. * 初始化函数
  79. *
  80. * @throws \Typecho\Db\Exception
  81. * @throws \Typecho\Widget\Exception
  82. */
  83. public function action()
  84. {
  85. $this->user->pass('administrator');
  86. $this->security->protect();
  87. $this->on($this->request->isPost())->upgrade();
  88. $this->response->redirect($this->options->adminUrl);
  89. }
  90. }