Maintenance.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages\Admin;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\Admin;
  13. use ForkBB\Models\Config\Config;
  14. use function \ForkBB\__;
  15. class Maintenance extends Admin
  16. {
  17. /**
  18. * Обслуживание
  19. */
  20. public function view(array $args, string $method): Page
  21. {
  22. $this->c->Lang->load('validator');
  23. $this->c->Lang->load('admin_maintenance');
  24. $config = clone $this->c->config;
  25. if ('POST' === $method) {
  26. $v = $this->c->Validator->reset()
  27. ->addValidators([
  28. 'check_message' => [$this, 'vCheckMessage'],
  29. ])->addRules([
  30. 'token' => 'token:AdminMaintenance',
  31. 'b_maintenance' => 'required|integer|in:0,1',
  32. 'o_maintenance_message' => 'exist|string:trim|max:65000 bytes|check_message|html',
  33. ])->addAliases([
  34. ])->addArguments([
  35. ])->addMessages([
  36. ]);
  37. if ($v->validation($_POST)) {
  38. $this->c->config->b_maintenance = $v->b_maintenance;
  39. $this->c->config->o_maintenance_message = $v->o_maintenance_message;
  40. $this->c->config->save();
  41. return $this->c->Redirect->page('AdminMaintenance')->message('Data updated redirect');
  42. }
  43. $this->fIswev = $v->getErrors();
  44. }
  45. $this->nameTpl = 'admin/maintenance';
  46. $this->aIndex = 'maintenance';
  47. $this->formMaintenance = $this->formMaintenance($config);
  48. $this->formRebuild = $this->formRebuild();
  49. return $this;
  50. }
  51. /**
  52. * Подготавливает массив данных для формы
  53. */
  54. protected function formMaintenance(Config $config): array
  55. {
  56. return [
  57. 'action' => $this->c->Router->link('AdminMaintenance'),
  58. 'hidden' => [
  59. 'token' => $this->c->Csrf->create('AdminMaintenance'),
  60. ],
  61. 'sets' => [
  62. 'maint' => [
  63. 'legend' => 'Maintenance head',
  64. 'fields' => [
  65. 'b_maintenance' => [
  66. 'type' => 'radio',
  67. 'value' => $config->b_maintenance,
  68. 'values' => [1 => __('Yes'), 0 => __('No')],
  69. 'caption' => 'Maintenance mode label',
  70. 'help' => 'Maintenance mode help',
  71. ],
  72. 'o_maintenance_message' => [
  73. 'type' => 'textarea',
  74. 'value' => $config->o_maintenance_message,
  75. 'caption' => 'Maintenance message label',
  76. 'help' => 'Maintenance message help',
  77. ],
  78. ],
  79. ],
  80. ],
  81. 'btns' => [
  82. 'submit' => [
  83. 'type' => 'submit',
  84. 'value' => __('Save changes'),
  85. ],
  86. ],
  87. ];
  88. }
  89. /**
  90. * Подготавливает массив данных для формы
  91. */
  92. protected function formRebuild(): array
  93. {
  94. return [
  95. 'action' => $this->c->Router->link('AdminMaintenanceRebuild'),
  96. 'hidden' => [
  97. 'token' => $this->c->Csrf->create('AdminMaintenanceRebuild'),
  98. ],
  99. 'sets' => [
  100. 'indx-info' => [
  101. 'info' => [
  102. [
  103. 'value' => __('Rebuild index info'),
  104. 'html' => true,
  105. ],
  106. ],
  107. ],
  108. 'indx' => [
  109. 'legend' => 'Rebuild index head',
  110. 'fields' => [
  111. 'limit' => [
  112. 'type' => 'number',
  113. 'min' => '1',
  114. 'max' => '9999',
  115. 'value' => '100',
  116. 'caption' => 'Posts per cycle label',
  117. 'help' => 'Posts per cycle help',
  118. ],
  119. 'start' => [
  120. 'type' => 'number',
  121. 'min' => '1',
  122. 'max' => '9999999999',
  123. 'value' => '1',
  124. 'caption' => 'Starting post label',
  125. 'help' => 'Starting post help',
  126. ],
  127. 'clear' => [
  128. 'type' => 'checkbox',
  129. 'checked' => true,
  130. 'caption' => 'Empty index label',
  131. 'label' => 'Empty index help',
  132. ],
  133. ],
  134. ],
  135. 'indx-info2' => [
  136. 'info' => [
  137. [
  138. 'value' => __('Rebuild completed info'),
  139. 'html' => true,
  140. ],
  141. ],
  142. ],
  143. ],
  144. 'btns' => [
  145. 'rebuild' => [
  146. 'type' => 'submit',
  147. 'value' => __('Rebuild index'),
  148. ],
  149. ],
  150. ];
  151. }
  152. /**
  153. * Подстановка значения по умолчанию
  154. */
  155. public function vCheckMessage(Validator $v, string $value): string
  156. {
  157. if (
  158. 1 === $v->b_maintenance
  159. && 0 === \strlen($value)
  160. ) {
  161. $value = __('Default maintenance message');
  162. }
  163. return $value;
  164. }
  165. /**
  166. * Пересоздание поискового индекса
  167. */
  168. public function rebuild(array $args, string $method): Page
  169. {
  170. $this->c->Lang->load('validator');
  171. $this->c->Lang->load('admin_maintenance');
  172. $v = $this->c->Validator->reset()
  173. ->addValidators([
  174. ])->addRules([
  175. 'token' => 'token:' . ('POST' === $method ? 'AdminMaintenanceRebuild' : 'AdminRebuildIndex'),
  176. 'limit' => 'required|integer|min:1|max:9999',
  177. 'start' => 'required|integer|min:1|max:9999999999',
  178. 'clear' => 'checkbox',
  179. ])->addAliases([
  180. ])->addArguments([
  181. 'token' => $args,
  182. ])->addMessages([
  183. ]);
  184. if (
  185. (
  186. 'POST' === $method
  187. && ! $v->validation($_POST)
  188. )
  189. || (
  190. 'POST' !== $method
  191. && ! $v->validation($args)
  192. )
  193. ) {
  194. $this->fIswev = $v->getErrors();
  195. return $this->view([], 'GET');
  196. }
  197. if (\function_exists('\\set_time_limit')) {
  198. \set_time_limit(0);
  199. }
  200. if (
  201. 'POST' === $method
  202. && $v->clear
  203. ) {
  204. $this->c->search->truncateIndex();
  205. }
  206. $last = $this->c->posts->rebuildIndex($v->start, $v->limit, $v->clear ? 'add' : 'edit');
  207. if ($last) {
  208. $args = [
  209. 'token' => null,
  210. 'limit' => $v->limit,
  211. 'start' => $last + 1,
  212. 'clear' => $v->clear ? '1' : '0',
  213. ];
  214. return $this->c->Redirect->page('AdminRebuildIndex', $args)->message(['Processed posts', $v->start, $last]);
  215. } else {
  216. return $this->c->Redirect->page('AdminMaintenance')->message('Rebuilding index end');
  217. }
  218. }
  219. }