ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilForumPostingFilesMigration.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 Posts 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.pos_pk AS posting_id,
57  frm_posts.pos_author_id AS owner_id,
58  frm_data.top_frm_fk AS object_id
59 FROM frm_posts
60 JOIN frm_data ON frm_posts.pos_top_fk = frm_data.top_pk
61 WHERE frm_posts.rcid IS NULL OR frm_posts.rcid = ''
62 LIMIT 1;"
63  );
64 
65  $d = $this->helper->getDatabase()->fetchObject($r);
66  if (!($d instanceof stdClass)) {
67  return;
68  }
69 
70  $posting_id = (int) $d->posting_id;
71  $object_id = (int) $d->object_id;
72  $resource_owner_id = (int) $d->owner_id;
73 
74  $base_path = $this->buildBasePath();
75  $filename_pattern = '/^' . $object_id . '\_' . $posting_id . '\_(.*)/m';
76  $pattern = '/.*\/' . $object_id . '\_' . $posting_id . '\_(.*)/m';
77 
78  if (is_dir($base_path) && count(scandir($base_path)) > 2) {
79  $collection_id = $this->helper->moveFilesOfPatternToCollection(
80  $base_path,
81  $pattern,
82  $resource_owner_id,
83  ResourceCollection::NO_SPECIFIC_OWNER,
84  $this->getFileNameCallback($filename_pattern),
86  );
87 
88  $save_colletion_id = $collection_id === null ? '-' : $collection_id->serialize();
89  $this->helper->getDatabase()->update(
90  'frm_posts',
91  ['rcid' => ['text', $save_colletion_id]],
92  ['pos_pk' => ['integer', $posting_id],]
93  );
94  } else {
95  $this->helper->getDatabase()->update(
96  'frm_posts',
97  ['rcid' => ['text', '-']],
98  ['pos_pk' => ['integer', $posting_id],]
99  );
100  }
101  }
102 
103  public function getRemainingAmountOfSteps(): int
104  {
105  $r = $this->helper->getDatabase()->query(
106  "SELECT
107  count(frm_posts.pos_pk) AS amount
108 FROM frm_posts
109 JOIN frm_data ON frm_posts.pos_top_fk = frm_data.top_pk
110 WHERE frm_posts.rcid IS NULL OR frm_posts.rcid = '';"
111  );
112  $d = $this->helper->getDatabase()->fetchObject($r);
113 
114  return (int) $d->amount;
115  }
116 
117  protected function buildBasePath(): string
118  {
119  return CLIENT_DATA_DIR . '/forum/';
120  }
121 
122  public function getFileNameCallback(string $pattern): Closure
123  {
124  return static function (string $file_name) use ($pattern): string {
125  if (preg_match($pattern, $file_name, $matches)) {
126  return $matches[1] ?? $file_name;
127  }
128  return $file_name;
129  };
130  }
131 
132  public function getRevisionNameCallback(): Closure
133  {
134  return static function (string $file_name): string {
135  return md5($file_name);
136  };
137  }
138 }
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.
step(Environment $environment)
Run one step of the migration.
getPreconditions(Environment $environment)
Objectives the migration depend on.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
const CLIENT_DATA_DIR
Definition: constants.php:46
ilResourceStorageMigrationHelper $helper
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
$r