ifhub
/
ifnews
Archived
1
0
Fork 0

Создание страниц на Mediawiki

This commit is contained in:
Alexander Yakovlev 2020-07-29 20:16:43 +07:00
parent 6038b42459
commit 5bbe74ba56
4 changed files with 1602 additions and 663 deletions

46
app/Commands/Wiki.php Normal file
View File

@ -0,0 +1,46 @@
<?php
namespace App\Commands;
use LaravelZero\Framework\Commands\Command;
use Log;
use App\Models\Game;
use App\Wikipage;
class Wiki extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'wiki {id}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Make a Mediawiki article about a game #ID';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$id = $this->argument('id');
if (!is_numeric($id)) {
Log::error('ID should be numeric.');
return;
}
$game = Game::where('id', $id)->first();
if (!isset($game)) {
Log::error('Game not found.');
return;
}
$page = new Wikipage($game);
$page->create();
}
}

195
app/Wikipage.php Normal file
View File

@ -0,0 +1,195 @@
<?php
/*
A set of utilities for tracking text-based game releases
Copyright (C) 2017-2018 Alexander Yakovlev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App;
use Mediawiki\Api\MediawikiApi;
use Mediawiki\Api\ApiUser;
use Mediawiki\Api\MediawikiFactory;
use Mediawiki\DataModel\Content;
use Mediawiki\DataModel\Title;
use Mediawiki\DataModel\PageIdentifier;
use Mediawiki\DataModel\Revision;
use Illuminate\Support\Str;
use App\Models\Language;
class Wikipage {
protected $game;
protected $api;
protected $services;
protected $content;
protected $fileUploader;
protected $covername;
public function __construct($game) {
$this->game = $game;
if (!env('DRY_RUN')) {
try {
// Log in to a wiki
$api = new MediawikiApi( env('WIKI') );
$api->login( new ApiUser( env('WIKIUSER'), env('WIKIPASSWORD') ) );
$services = new MediawikiFactory( $api );
$this->api = $api;
$this->services = $services;
$this->fileUploader = $services->newFileUploader();
} catch (\Exception $e) {
echo 'Ошибка соединения.'.PHP_EOL;
echo $e->getMessage();
echo $e->getTraceAsString();
return;
}
}
}
public function create() {
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('DRY_RUN') && !$exists) {
if (!empty($this->game->image_url) && isset($filename)) {
if ($this->services->newPageGetter()->getFromTitle($this->covername)) {
$image = file_get_contents($this->game->image_url);
file_put_contents($filename, $image);
$extension = mime_content_type($filename);
if (strpos($extension, 'image') !== FALSE) {
$extension = str_replace('image/', '', $extension);
$this->covername = Str::slug($this->game->title, '_').'.'.$extension;
$this->fileUploader->upload($this->covername, $filename);
}
unlink($filename);
}
}
}
$this->makeContent();
if (!env('DRY_RUN') && !$exists) {
$newContent = new Content( $this->content );
$title = new Title($pagetitle);
$identifier = new PageIdentifier($title);
$revision = new Revision($newContent, $identifier);
$this->services->newRevisionSaver()->save($revision);
return true;
} else {
if ($exists) {
echo "Страница игры уже существует. Автосоздание невозможно.\n";
echo $this->content;
return false;
}
if (env('DRY_RUN')) {
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== Ссылки ==";
}
$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('DRY_RUN')) {
return false;
}
$page = $this->services->newPageGetter()->getFromTitle((string) $pagename);
return !($page->getId() === 0);
}
}

View File

@ -18,7 +18,6 @@
"require": {
"php": "^7.2",
"addwiki/mediawiki-api": "^0.7.2",
"cocur/slugify": "^3.2",
"illuminate/cache": "^7.0",
"illuminate/database": "^7.0",
"laravel-zero/framework": "^7.0",
@ -34,7 +33,7 @@
"illuminate/log": "^7.0",
"mockery/mockery": "^1.0",
"monolog/monolog": "^2.0",
"nunomaduro/larastan": "^0.5",
"nunomaduro/larastan": "^0.6",
"phpunit/phpunit": "^9.0"
},
"autoload": {

2021
composer.lock generated

File diff suppressed because it is too large Load Diff