BuildSocialSearchIndex.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author call-me-matt <nextcloud@matthiasheinisch.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Migration;
  25. use OCP\BackgroundJob\IJobList;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\IRepairStep;
  30. class BuildSocialSearchIndex implements IRepairStep {
  31. /** @var IDBConnection */
  32. private $db;
  33. /** @var IJobList */
  34. private $jobList;
  35. /** @var IConfig */
  36. private $config;
  37. /**
  38. * @param IDBConnection $db
  39. * @param IJobList $jobList
  40. * @param IConfig $config
  41. */
  42. public function __construct(IDBConnection $db,
  43. IJobList $jobList,
  44. IConfig $config) {
  45. $this->db = $db;
  46. $this->jobList = $jobList;
  47. $this->config = $config;
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function getName() {
  53. return 'Register building of social profile search index as background job';
  54. }
  55. /**
  56. * @param IOutput $output
  57. */
  58. public function run(IOutput $output) {
  59. // only run once
  60. if ($this->config->getAppValue('dav', 'builtSocialSearchIndex') === 'yes') {
  61. $output->info('Repair step already executed');
  62. return;
  63. }
  64. $query = $this->db->getQueryBuilder();
  65. $query->select($query->func()->max('cardid'))
  66. ->from('cards_properties')
  67. ->where($query->expr()->eq('name', $query->createNamedParameter('X-SOCIALPROFILE')));
  68. $maxId = (int)$query->execute()->fetchOne();
  69. if ($maxId === 0) {
  70. return;
  71. }
  72. $output->info('Add background job');
  73. $this->jobList->add(BuildSocialSearchIndexBackgroundJob::class, [
  74. 'offset' => 0,
  75. 'stopAt' => $maxId
  76. ]);
  77. // no need to redo the repair during next upgrade
  78. $this->config->setAppValue('dav', 'builtSocialSearchIndex', 'yes');
  79. }
  80. }