. */ namespace App; use Wikimate; use Illuminate\Support\Str; use App\Models\Language; class Wikipage { protected $game; /** * @var Wikimate **/ protected $api; protected $content; protected $covername; public function __construct($game) { $this->game = $game; if (!env('APP_DEBUG') && !empty(env('WIKI'))) { try { // Log in to a wiki $wiki = new Wikimate(env('WIKI')); $wiki->login(env('WIKIUSER'), env('WIKIPASSWORD')); $this->api = $wiki; } catch (\Exception $e) { echo 'Ошибка соединения.'.PHP_EOL; echo $e->getMessage(); if (env('APP_DEBUG')) { echo $e->getTraceAsString(); } return; } } } public function getContent() { if (empty($this->content)) { $this->makeContent(); } return $this->content; } public function create() { if (env('APP_DEBUG') || empty(env('WIKI'))) { return true; } if (!empty($this->game->image_url)) { $filename = preg_replace('/\?.*/', '', basename($this->game->image_url)); $extension = pathinfo($filename, PATHINFO_EXTENSION); $this->covername = Str::slug($this->game->title, '_').'.'.$extension; } $pagetitle = strtr($this->game->title, [ '|' => '-' ]); if (empty($pagetitle)) { echo 'ERROR: Page has no title.'; print_r($this->game); return; } $exists = $this->exists($pagetitle); if (!env('APP_DEBUG') && !$exists) { if (!empty($this->game->image_url) && isset($filename)) { if (!$this->api->getFile($this->covername)->exists()) { try { $image = file_get_contents($this->game->image_url); file_put_contents($filename, $image); $this->api->uploadFile($filename, $this->covername); unlink($filename); } catch (\Exception $e) { echo '(Обложка не найдена)'.PHP_EOL; } } } } $this->makeContent(); if (!env('APP_DEBUG') && !$exists) { $page = $this->api->getPage($pagetitle); $page->setText($this->content); return true; } else { if ($exists) { echo "Страница игры уже существует. Автосоздание невозможно.\n"; echo $this->content; return false; } if (env('APP_DEBUG')) { echo "Черновой режим. Автосоздание невозможно.\n"; } echo $this->content; return true; } } protected function makeContent() { $this->content = '{{game info'; $this->txtadd('title', ' |название='.$this->game->title); $authors = $this->game->authors->all(); if (count($authors) > 0) { $this->content .= PHP_EOL.' |автор='; $i = 0; $l = count($authors); foreach ($authors as $author) { $author_name = $author->name; $this->content .= '[[Автор::'.$author_name.']]'; $i++; if ($i < $l) { $this->content .= '; '; } } } if (!empty($this->game->release_date)) { $date = (new \DateTime($this->game->release_date))->format('d.m.Y'); $this->content .= PHP_EOL.' |вышла='.$date; } $platforms = $this->game->platforms()->pluck('title')->toArray(); if (!empty($platforms)) { $this->txtadd('platform', ' |платформа='.implode(',', $platforms)); } $this->txtadd('image', ' |обложка='.$this->covername); $languages = $this->game->languages()->pluck('title_ru')->toArray(); if (!empty($languages)) { $this->txtadd('language', ' |язык='.implode(',', $languages)); } $ru = Language::findByCode('ru'); $tags = $this->game->tags()->where('language_id', $ru->id)->pluck('title')->toArray(); if (!empty($tags)) { $this->content .= PHP_EOL.' |темы='.implode(',', $tags); } $this->content .= "\n}}\n"; $this->txtadd('description', $this->game->description); if (!empty($this->game->url_download) || !empty($this->game->url_play_online)) { $this->content .= "\n== Версии =="; } $this->txtadd('url_online', PHP_EOL.'* ['.$this->game->url_play_online.' '.$this->game->url_online_description.']'); if (!empty($this->game->url_download)) { if (!empty($this->game->url_download_description)) { $this->content .= PHP_EOL.'* ['.$this->game->url_download.' '.$this->game->url_download_description.']'; } else { $this->content .= PHP_EOL.'* ['.$this->game->url_download.' Скачать игру]'; } } if (!empty($this->game->url_discussion) || !empty($this->game->url)) { $this->content .= "\n== Ссылки =="; } if ($this->game->url_discussion === $this->game->url) { $this->txtadd('url', '* {{ссылка|на='.$this->game->url.'|Страница игры с обсуждением}}'); } else { $this->txtadd('url_discussion', '* {{ссылка|на='.$this->game->url_discussion.'|Обсуждение игры}}'); $this->txtadd('url', '* {{ссылка|на='.$this->game->url.'|Страница игры}}'); } } protected function txtadd($param, $text) { if (!empty($this->game->$param)) { $this->content .= PHP_EOL.rtrim($text); } } /** * Checks if the page exists. * * @param string $pagename * @return boolean */ protected function exists($pagename) { if (env('APP_DEBUG') || empty(env('WIKI'))) { return false; } $page = $this->api->getPage((string) $pagename); return $page->exists(); } }