mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +08:00
tag and installer&updater use english
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Repositories\TagRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Laravel\Sanctum\PersonalAccessToken;
|
||||
|
||||
class MigrateTorrentTag extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'torrent:migrate_tags';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Migrate exits torrent tags to new structure';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new TagRepository();
|
||||
$result = $rep->migrateTorrentTag();
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query());
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Attendance;
|
||||
use App\Models\Exam;
|
||||
use App\Models\ExamProgress;
|
||||
@@ -10,6 +11,7 @@ use App\Models\HitAndRun;
|
||||
use App\Models\Medal;
|
||||
use App\Models\SearchBox;
|
||||
use App\Models\Snatch;
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
use App\Repositories\AgentAllowRepository;
|
||||
use App\Repositories\AttendanceRepository;
|
||||
@@ -60,10 +62,10 @@ class Test extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$peerId = '-TR3000-uff7q3z5126z';
|
||||
$agent = 'Transmission/3.00';
|
||||
$rep = new AgentAllowRepository();
|
||||
$r = $rep->checkClient($peerId, $agent, true);
|
||||
$r = Tag::query()->paginate();
|
||||
$resource = TagResource::collection($r);
|
||||
dd($resource->response()->getData(true));
|
||||
echo $r->updated_at;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Tag;
|
||||
use App\Repositories\TagRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(TagRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
private function getRules($id = null): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', Rule::unique('tags', 'name')->ignore($id)],
|
||||
'color' => 'required|string',
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = TagResource::collection($result);
|
||||
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate($this->getRules());
|
||||
$data = $request->all();
|
||||
if (isset($data['priority'])) {
|
||||
$data['priority'] = intval($data['priority']);
|
||||
}
|
||||
$result = $this->repository->store($data);
|
||||
$resource = new TagResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$result = Tag::query()->findOrFail($id);
|
||||
$resource = new TagResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate($this->getRules($id));
|
||||
$data = $request->all();
|
||||
if (isset($data['priority'])) {
|
||||
$data['priority'] = intval($data['priority']);
|
||||
}
|
||||
$result = $this->repository->update($data, $id);
|
||||
$resource = new TagResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class TagResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'color' => $this->color,
|
||||
'priority' => $this->priority,
|
||||
'created_at' => format_datetime($this->created_at),
|
||||
'updated_at' => format_datetime($this->updated_at),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class Tag extends NexusModel
|
||||
{
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'id', 'name', 'color', 'priority', 'created_at', 'updated_at'
|
||||
];
|
||||
|
||||
const DEFAULTS = [
|
||||
[
|
||||
'id' => 1,
|
||||
'name' => '禁转',
|
||||
'color' => '#ff0000',
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'name' => '首发',
|
||||
'color' => '#8F77B5',
|
||||
],
|
||||
[
|
||||
'id' => 3,
|
||||
'name' => '官方',
|
||||
'color' => '#0000ff',
|
||||
],
|
||||
[
|
||||
'id' => 4,
|
||||
'name' => 'DIY',
|
||||
'color' => '#46d5ff',
|
||||
],
|
||||
[
|
||||
'id' => 5,
|
||||
'name' => '国语',
|
||||
'color' => '#6a3906',
|
||||
],
|
||||
[
|
||||
'id' => 6,
|
||||
'name' => '中字',
|
||||
'color' => '#006400',
|
||||
],
|
||||
[
|
||||
'id' => 7,
|
||||
'name' => 'HDR',
|
||||
'color' => '#38b03f',
|
||||
],
|
||||
];
|
||||
|
||||
public function torrents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Torrent::class, 'torrent_tags', 'tag_id', 'torrent_id');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -205,4 +205,9 @@ class Torrent extends NexusModel
|
||||
{
|
||||
$query->where('visible', $visible);
|
||||
}
|
||||
|
||||
public function tags(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'torrent_tags', 'torrent_id', 'tag_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class TorrentTag extends NexusModel
|
||||
{
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'torrent_id', 'tag_id', 'priority'
|
||||
];
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Tag;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\TorrentTag;
|
||||
use Illuminate\Support\Collection;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class TagRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = $this->createBasicQuery();
|
||||
return $query->paginate();
|
||||
}
|
||||
|
||||
public function store(array $params)
|
||||
{
|
||||
$model = Tag::query()->create($params);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function update(array $params, $id)
|
||||
{
|
||||
$model = Tag::query()->findOrFail($id);
|
||||
$model->update($params);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function getDetail($id)
|
||||
{
|
||||
$model = Tag::query()->findOrFail($id);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$model = Tag::query()->findOrFail($id);
|
||||
$result = $model->delete();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function createBasicQuery()
|
||||
{
|
||||
return Tag::query()->orderBy('priority', 'desc')->orderBy('id', 'desc');
|
||||
}
|
||||
|
||||
public function renderCheckbox(array $checked = []): string
|
||||
{
|
||||
$html = '';
|
||||
$results = $this->createBasicQuery()->get();
|
||||
foreach ($results as $value) {
|
||||
$html .= sprintf(
|
||||
'<label><input type="checkbox" name="tags[]" value="%s"%s />%s</label>',
|
||||
$value->id, in_array($value->id, $checked) ? ' checked' : '', $value->name
|
||||
);
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function renderSpan(Collection $tagKeyById, array $renderIdArr = [], $withFilterLink = false): string
|
||||
{
|
||||
$html = '';
|
||||
foreach ($renderIdArr as $tagId) {
|
||||
$value = $tagKeyById->get($tagId);
|
||||
if ($value) {
|
||||
$item = "<span style=\"background-color:{$value->color};color:white;border-radius:15%\">{$value->name}</span> ";
|
||||
if ($withFilterLink) {
|
||||
$html .= sprintf('<a href="torrents.php?tag_id=%s">%s</a>', $tagId, $item);
|
||||
} else {
|
||||
$html .= $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function migrateTorrentTag()
|
||||
{
|
||||
$page = 1;
|
||||
$size = 1000;
|
||||
$baseQuery = Torrent::query()->where('tags', '>', 0);
|
||||
do_log("torrent to migrate hr counts: " . (clone $baseQuery)->count());
|
||||
$dateTimeStringNow = date('Y-m-d H:i:s');
|
||||
$tags = [];
|
||||
$priority = count(Tag::DEFAULTS);
|
||||
foreach (Tag::DEFAULTS as $value) {
|
||||
$attributes = [
|
||||
'name' => $value['name'],
|
||||
];
|
||||
$values = [
|
||||
'priority' => $priority,
|
||||
'color' => $value['color'],
|
||||
'created_at' => $dateTimeStringNow,
|
||||
'updated_at' => $dateTimeStringNow,
|
||||
];
|
||||
$tags[] = Tag::query()->firstOrCreate($attributes, $values);
|
||||
$priority--;
|
||||
}
|
||||
do_log("insert default tags done!");
|
||||
|
||||
$sql = "insert into torrent_tags (torrent_id, tag_id, created_at, updated_at) values ";
|
||||
$values = [];
|
||||
while (true) {
|
||||
$logPrefix = "page: $page, size: $size";
|
||||
$results = (clone $baseQuery)->forPage($page, $size)->get();
|
||||
if ($results->isEmpty()) {
|
||||
do_log("$logPrefix, no more data...");
|
||||
break;
|
||||
}
|
||||
foreach ($results as $torrent) {
|
||||
foreach ($tags as $key => $tag) {
|
||||
$currentValue = pow(2, $key);
|
||||
if ($currentValue & $torrent->tags) {
|
||||
//this torrent has this tag
|
||||
$values[] = sprintf("(%d, %d, '%s', '%s')", $torrent->id, $tag->id, $dateTimeStringNow, $dateTimeStringNow);
|
||||
}
|
||||
}
|
||||
}
|
||||
$page++;
|
||||
}
|
||||
$sql .= sprintf("%s on duplicate key update updated_at = values(updated_at)", implode(', ', $values));
|
||||
do_log("migrate sql: $sql");
|
||||
NexusDB::statement($sql);
|
||||
do_log("[MIGRATE_TORRENT_TAG] done!");
|
||||
return count($values);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user