1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Jobs;
7
8use App\Libraries\OsuWiki;
9use App\Libraries\WikiRedirect;
10use App\Models\NewsPost;
11use App\Models\Wiki\Image;
12use App\Models\Wiki\Page;
13use App\Models\Wiki\WikiObject;
14use Illuminate\Bus\Queueable;
15use Illuminate\Contracts\Queue\ShouldQueue;
16use Illuminate\Foundation\Bus\Dispatchable;
17use Illuminate\Queue\InteractsWithQueue;
18
19class UpdateWiki implements ShouldQueue
20{
21 use Dispatchable, InteractsWithQueue, Queueable;
22
23 /** @var string */
24 private $oldHash;
25
26 /** @var string */
27 private $newHash;
28
29 /**
30 * Create a new job instance.
31 *
32 * @param string $oldHash
33 * @param string $newHash
34 * @return void
35 */
36 public function __construct($oldHash, $newHash)
37 {
38 $this->oldHash = $oldHash;
39 $this->newHash = $newHash;
40 }
41
42 /**
43 * Execute the job.
44 *
45 * @return void
46 */
47 public function handle()
48 {
49 $files = OsuWiki::getUpdatedFiles($this->oldHash, $this->newHash);
50
51 foreach ($files as $file) {
52 if ($file['status'] === 'renamed') {
53 optional($this->getObject($file['previous_filename']))->sync(true);
54 }
55
56 optional($this->getObject($file['filename']))->sync(true);
57 }
58 }
59
60 private function getObject(string $path): ?WikiObject
61 {
62 $parsed = OsuWiki::parseGithubPath($path);
63
64 switch ($parsed['type'] ?? '') {
65 case 'page':
66 return Page::lookup($parsed['path'], $parsed['locale']);
67 case 'image':
68 return new Image($parsed['path']);
69 case 'redirect':
70 return new WikiRedirect();
71 case 'news_post':
72 return NewsPost::lookup($parsed['slug']);
73 default:
74 return null;
75 }
76 }
77}