Вики-страницы + Anivisual
This commit is contained in:
parent
90d0e020f1
commit
1c75979c9d
5
Game.php
5
Game.php
|
@ -7,6 +7,11 @@ class Game {
|
|||
public $description;
|
||||
public $date;
|
||||
public $image;
|
||||
public $platform;
|
||||
public $url_online;
|
||||
public $url_download;
|
||||
public $url_discussion;
|
||||
public $url_download_description;
|
||||
public function print() {
|
||||
$converter = new HtmlConverter();
|
||||
if (STYLE === 'RUS') {
|
||||
|
|
|
@ -40,7 +40,7 @@ class Anivisual extends Source {
|
|||
if ($date < $this->period) continue;
|
||||
$game = new Game;
|
||||
$link = $gameBlock->find('.novel-ttl a')[0];
|
||||
$game->title = $link->innerHtml;
|
||||
$game->title = htmlspecialchars_decode($link->innerHtml);
|
||||
$game->url = 'http://anivisual.net'.$link->getAttribute('href');
|
||||
$game->description = $gameBlock->find('span')->first()->text;
|
||||
|
||||
|
@ -75,7 +75,7 @@ class Anivisual extends Source {
|
|||
$game->date = $date;
|
||||
unset($date);
|
||||
} catch (\Exception $e) {}
|
||||
$game->title = $gameBlock->find('h1.logo')->text;
|
||||
$game->title = htmlspecialchars_decode($gameBlock->find('h1.logo')->text);
|
||||
// $game->description = $this->dom->find('#content > section > span')->text;
|
||||
return $game;
|
||||
}
|
||||
|
|
126
Wikipage.php
Normal file
126
Wikipage.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
use Mediawiki\Api\Service\FileUploader;
|
||||
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;
|
||||
|
||||
class Wikipage {
|
||||
protected $game;
|
||||
protected $api;
|
||||
protected $services;
|
||||
protected $content;
|
||||
protected $fileUploader;
|
||||
|
||||
public function __construct($game) {
|
||||
global $config;
|
||||
|
||||
$this->game = $game;
|
||||
|
||||
if (!$config['DUMMY']) {
|
||||
// Log in to a wiki
|
||||
$api = new MediawikiApi( $config['WIKI'] );
|
||||
$api->login( new ApiUser( $config['WIKIUSER'], $config['WIKIPASSWORD'] ) );
|
||||
$services = new MediawikiFactory( $api );
|
||||
$this->api = $api;
|
||||
$this->services = $services;
|
||||
$this->fileUploader = $services->newFileUploader();
|
||||
}
|
||||
}
|
||||
public function create() {
|
||||
global $config;
|
||||
|
||||
$this->makeContent();
|
||||
|
||||
$exists = $this->exists($this->game->title);
|
||||
if (!$config['DUMMY'] && !$exists) {
|
||||
if (!empty($this->game->image)) {
|
||||
$filename = filename($this->game->image);
|
||||
if ($this->services->newPageGetter()->getFromTitle($filename)) {
|
||||
$image = file_get_contents($this->game->image);
|
||||
file_put_contents($image, $filename);
|
||||
$this->fileUploader->upload($filename, $filename);
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
$newContent = new Content( $this->content );
|
||||
$title = new Title($this->game->title);
|
||||
$identifier = new PageIdentifier($title);
|
||||
$revision = new Revision($newContent, $identifier);
|
||||
$services->newRevisionSaver()->save($revision);
|
||||
return true;
|
||||
} else {
|
||||
if ($exists) {
|
||||
echo "Страница игры уже существует. Автосоздание невозможно.\n";
|
||||
echo $this->content;
|
||||
return false;
|
||||
}
|
||||
if ($config['DUMMY']) {
|
||||
echo "Черновой режим. Автосоздание невозможно.\n";
|
||||
}
|
||||
echo $this->content;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
protected function makeContent() {
|
||||
$this->content = '{{game info';
|
||||
|
||||
$this->txtadd('title', ' |название='.$this->game->title.'"');
|
||||
if (is_array($this->game->author) && count($this->game->author) === 1) {
|
||||
$this->game->author = trim($this->game->author[0]);
|
||||
}
|
||||
|
||||
if (is_array($this->game->author) && count($this->game->author) > 0) {
|
||||
$this->content .= PHP_EOL.' |автор=';
|
||||
$i = 0;
|
||||
$l = count($this->game->author);
|
||||
foreach ($this->game->author as $author_name) {
|
||||
$this->content .= '[[Автор::'.$author_name.']]';
|
||||
$i++;
|
||||
if ($i < $l) {
|
||||
$this->content .= '; ';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->txtadd('author', ' |автор=[[Автор::'.$this->game->author.']]');
|
||||
}
|
||||
$this->txtadd('date', ' |вышла='.$this->game->date);
|
||||
$this->txtadd('platform', ' |платформа='.$this->game->platform);
|
||||
$this->txtadd('image', ' |обложка='.filename($this->game->image));
|
||||
|
||||
$this->content .= "\n}}\n";
|
||||
|
||||
$this->txtadd('description', "#{@game.description}");
|
||||
if (!empty($this->game->url_download) || !empty($this->game->url_online)) {
|
||||
$this->content .= "\n== Версии ==";
|
||||
}
|
||||
$this->txtadd('url_online', "\n* [#{@game.url_online} #{@game.url_online_description}]");
|
||||
if (!empty($this->game->url_download) && !empty($this->game->url_download_description)) {
|
||||
$this->content .= "\n* [$this->game->url_download $this->game->url_download_description]";
|
||||
}
|
||||
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.trim($text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the page exists.
|
||||
*
|
||||
* @param string $pagename
|
||||
* @return boolean
|
||||
*/
|
||||
protected function exists($pagename) {
|
||||
return !empty($this->services->newPageGetter()->getFromTitle($pagename));
|
||||
}
|
||||
}
|
7
wiki.php
7
wiki.php
|
@ -3,6 +3,7 @@
|
|||
require "vendor/autoload.php";
|
||||
require "Game.php";
|
||||
require "Source.php";
|
||||
require "Wikipage.php";
|
||||
$config = parse_ini_file("./config.ini");
|
||||
$loader = new \Aura\Autoload\Loader;
|
||||
$loader->register();
|
||||
|
@ -15,9 +16,6 @@ if (!isset($argv[1])) {
|
|||
$url = strtolower($argv[1]);
|
||||
$game = new Game;
|
||||
|
||||
function wikipage($game) {
|
||||
var_dump($game);
|
||||
}
|
||||
function check($classname) {
|
||||
global $game;
|
||||
global $url;
|
||||
|
@ -27,7 +25,8 @@ function check($classname) {
|
|||
if ($cl->checkPage($url)) {
|
||||
$game = $cl->page($url);
|
||||
if ($game) {
|
||||
wikipage($game);
|
||||
$page = new Wikipage($game);
|
||||
$page->create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue