ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilForumDraftsFilesMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
26 {
28 
29  public function getLabel(): string
30  {
31  return 'Migration of Files in Forum Drafts to the Resource Storage Service.';
32  }
33 
35  {
36  return 10000;
37  }
38 
39  public function getPreconditions(Environment $environment): array
40  {
42  }
43 
44  public function prepare(Environment $environment): void
45  {
46  $this->helper = new ilResourceStorageMigrationHelper(
48  $environment
49  );
50  }
51 
52  public function step(Environment $environment): void
53  {
54  $r = $this->helper->getDatabase()->query(
55  "SELECT
56  frm_posts_drafts.draft_id AS draft_id,
57  frm_posts_drafts.post_author_id AS owner_id
58 FROM frm_posts_drafts
59 JOIN frm_data ON frm_posts_drafts.forum_id = frm_data.top_pk
60 WHERE frm_posts_drafts.rcid IS NULL OR frm_posts_drafts.rcid = ''
61 LIMIT 1;"
62  );
63 
64  $d = $this->helper->getDatabase()->fetchObject($r);
65  if (!($d instanceof stdClass)) {
66  return;
67  }
68 
69  $draft_id = (int) $d->draft_id;
70  $resource_owner_id = (int) $d->owner_id;
71 
72  $base_path = $this->buildBasePath() . $draft_id . '/';
73  $pattern = '/(.+)/m';
74 
75  if (is_dir($base_path) && count(scandir($base_path)) > 2) {
76  $collection_id = $this->helper->moveFilesOfPatternToCollection(
77  $base_path,
78  $pattern,
79  $resource_owner_id,
80  ResourceCollection::NO_SPECIFIC_OWNER,
81  null,
83  );
84 
85  $save_colletion_id = $collection_id === null ? '-' : $collection_id->serialize();
86  $this->helper->getDatabase()->update(
87  'frm_posts_drafts',
88  ['rcid' => ['text', $save_colletion_id]],
89  ['draft_id' => ['integer', $draft_id]]
90  );
91  } else {
92  $this->helper->getDatabase()->update(
93  'frm_posts_drafts',
94  ['rcid' => ['text', '-']],
95  ['draft_id' => ['integer', $draft_id]]
96  );
97  }
98  }
99 
100  public function getRemainingAmountOfSteps(): int
101  {
102  $r = $this->helper->getDatabase()->query(
103  "SELECT
104  count(frm_posts_drafts.draft_id) AS amount
105 FROM frm_posts_drafts
106 JOIN frm_data ON frm_posts_drafts.forum_id = frm_data.top_pk
107 WHERE frm_posts_drafts.rcid IS NULL OR frm_posts_drafts.rcid = '';"
108  );
109  $d = $this->helper->getDatabase()->fetchObject($r);
110 
111  return (int) $d->amount;
112  }
113 
114  protected function buildBasePath(): string
115  {
116  return CLIENT_DATA_DIR . '/forum/drafts/';
117  }
118 
119  public function getFileNameCallback(string $pattern): Closure
120  {
121  return static function (string $file_name) use ($pattern): string {
122  if (preg_match($pattern, $file_name, $matches)) {
123  return $matches[1] ?? $file_name;
124  }
125  return $file_name;
126  };
127  }
128 
129  public function getRevisionNameCallback(): Closure
130  {
131  return static function (string $file_name): string {
132  return md5($file_name);
133  };
134  }
135 }
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
step(Environment $environment)
Run one step of the migration.
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
prepare(Environment $environment)
Prepare the migration by means of some environment.
getPreconditions(Environment $environment)
Objectives the migration depend on.
const CLIENT_DATA_DIR
Definition: constants.php:46
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
ilResourceStorageMigrationHelper $helper
$r