store serialized data in binary fields

master
cornernote 2015-08-10 21:46:54 +09:30
parent 9dddd13d5f
commit 805b7f4bc1
10 changed files with 173 additions and 127 deletions

3
.gitignore vendored
View File

@ -1,7 +1,6 @@
.idea
.env
.DS_Store
Thumbs.db
composer.phar
vendor/
_bak/
TestController.php

View File

@ -13,7 +13,8 @@
"kartik-v/yii2-widgets": "^3.4",
"cebe/markdown": "~1.0.1",
"bigpaulie/yii2-fancybox": "^1.0",
"knplabs/github-api": "^1.4"
"knplabs/github-api": "^1.4",
"vlucas/phpdotenv": "^2.0"
},
"require-dev": {
"yiisoft/yii2-debug": "*",

48
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "965938d21f9aeba1f8b806a2097de818",
"hash": "e417d5aeb8ba7262be04e24edd001b90",
"packages": [
{
"name": "bigpaulie/yii2-fancybox",
@ -1768,6 +1768,52 @@
"homepage": "https://symfony.com",
"time": "2015-06-18 19:21:56"
},
{
"name": "vlucas/phpdotenv",
"version": "v2.0.1",
"source": {
"type": "git",
"url": "https://github.com/vlucas/phpdotenv.git",
"reference": "91064290f5b53a09bdff1b939d7f69fb0e7531b5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/91064290f5b53a09bdff1b939d7f69fb0e7531b5",
"reference": "91064290f5b53a09bdff1b939d7f69fb0e7531b5",
"shasum": ""
},
"require": {
"php": ">=5.3.2"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Dotenv\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD"
],
"authors": [
{
"name": "Vance Lucas",
"email": "vance@vancelucas.com",
"homepage": "http://www.vancelucas.com"
}
],
"description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.",
"homepage": "http://github.com/vlucas/phpdotenv",
"keywords": [
"dotenv",
"env",
"environment"
],
"time": "2015-05-30 16:15:01"
},
{
"name": "yiisoft/yii2",
"version": "2.0.6",

View File

@ -23,110 +23,6 @@ class PackageController extends Controller
public function actionImportMtpm()
{
$ignore = [
'lulzpack',
'lift',
'Animalsmod',
'admin_tools',
'extrachests',
'bettercoal',
'slabrealm',
'mccarpet',
'clock',
'helicopter',
'mudslide',
'beacon',
'books_plus',
'nyancats_plus',
'realclocks',
'Marble_Blocks',
'chests_0gb_us',
'kerova',
'chatlog',
'airship',
'treasurer',
'alphabet',
'DOM-modpack-all',
'whereis',
'fireworks',
'components',
'senderman',
'glowcrystals',
'magic_latern',
'mod3',
'greenscreens',
'creative',
'digicode',
'armor',
'xray',
'octublock',
'mudslide',
'secret',
'no_guests',
'papyrus_door',
'terror',
'mblocks',
'uranium',
'fishing',
'bigchests',
'signs',
'future_ban',
'candles',
'birthstones',
'camomod',
'chains',
'docfarming',
'drippingwater',
'oneclick',
'my_mobs',
'mint',
'cgmr',
'watch',
'workbench',
'noncubic',
'nuke',
'weblock',
'sea',
'bedrock',
'bones',
'gauges',
'hovers',
'itemframes',
'item_drop',
'names_per_ip',
'paintings',
'snow',
'flint',
'camouflage',
'glowstone',
'diamond_fist',
'mines',
'mailbox',
'jukebox',
'altertrunks',
'thaumtest',
'blox',
'bone',
'crafting',
'dplus',
'fences',
'compass',
'sethome',
'xfences',
'camo_modpack',
'painting',
'tbm',
'brillantblocks',
'flags',
'gems',
'infinitytools',
'lavacooling',
'chat_channel',
// gitorious
'building_blocks',
'desert_uses',
];
$nameTranslate = [
'Crops' => 'crops',
];
@ -148,9 +44,6 @@ class PackageController extends Controller
if (in_array($name, array_keys($nameTranslate))) {
$name = $nameTranslate[$name];
}
if (in_array($name, $ignore)) {
continue;
}
$url = trim(trim($row['2'], '/')) . '.git';
$url = strtr($url, $urlTranslate);
$package = Package::find()->where(['name' => $name])->one();
@ -160,16 +53,13 @@ class PackageController extends Controller
} else {
$package->checkUrl = false;
}
$package->url = $url;
$package->homepage = trim($row['4']);
$package->authors = [trim($row['0'])];
//$package->checkUrl = false;
if (!$package->save()) {
print_r($package->errors);
print_r($package->attributes);
die;
}
if (!$package->url)
$package->url = $url;
if (!$package->homepage)
$package->homepage = trim($row['4']);
if (!$package->authors)
$package->authors = [trim($row['0'])];
$package->save();
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace app\components;
use yii\base\Object;
/**
* Serialize
*/
class Serialize extends Object
{
/**
* Convert the given value into a gzip compressed blob so it can be stored in the database
* @param mixed $data
* @return string binary blob of data
*/
public static function serialize($data)
{
return self::compress(serialize($data));
}
/**
* Re-inflates and unserializes a blob of compressed data
* @param string $data
* @return mixed false if an error occurred
*/
public static function unserialize($data)
{
if (is_resource($data)) {
// For some databases (like Postgres) binary columns return as a resource, fetch the content first
$data = stream_get_contents($data, -1, 0);
}
$originalData = $data;
$data = self::uncompress($data);
if ($data === false) {
$data = $originalData;
}
$data = @unserialize($data);
if ($data === false) {
$data = $originalData;
}
return $data;
}
/**
* Compress
* @param mixed $data
* @return string binary blob of data
*/
public static function compress($data)
{
return gzcompress($data);
}
/**
* Compress
* @param mixed $data
* @return string binary blob of data
*/
public static function uncompress($data)
{
$originalData = $data;
$data = @gzuncompress($data);
return $data ?: $originalData;
}
}

View File

@ -1,3 +1,6 @@
<?php
require(__DIR__ . '/../../vendor/autoload.php');
(new \Dotenv\Dotenv(__DIR__ . '/../..'))->load();
defined('YII_DEBUG') or define('YII_DEBUG', getenv('YII_DEBUG') ? (getenv('YII_DEBUG') == 'false' ? false : getenv('YII_DEBUG')) : true);
defined('YII_ENV') or define('YII_ENV', getenv('YII_ENV') ? getenv('YII_ENV') : 'dev');

View File

@ -0,0 +1,37 @@
<?php
use yii\db\Migration;
use yii\db\Schema;
class m150810_000003_update_package extends Migration
{
const TABLE = '{{%package}}';
public function up()
{
$this->dropColumn(self::TABLE, 'bower');
$this->dropColumn(self::TABLE, 'readme');
$this->dropColumn(self::TABLE, 'screenshots');
$this->dropColumn(self::TABLE, 'authors');
$this->dropColumn(self::TABLE, 'license');
$this->addColumn(self::TABLE, 'bower', Schema::TYPE_BINARY);
$this->addColumn(self::TABLE, 'readme', Schema::TYPE_BINARY);
$this->addColumn(self::TABLE, 'screenshots', Schema::TYPE_BINARY);
$this->addColumn(self::TABLE, 'authors', Schema::TYPE_BINARY);
$this->addColumn(self::TABLE, 'license', Schema::TYPE_BINARY);
}
public function down()
{
$this->dropColumn(self::TABLE, 'bower');
$this->dropColumn(self::TABLE, 'readme');
$this->dropColumn(self::TABLE, 'screenshots');
$this->dropColumn(self::TABLE, 'authors');
$this->dropColumn(self::TABLE, 'license');
$this->addColumn(self::TABLE, 'bower', Schema::TYPE_TEXT);
$this->addColumn(self::TABLE, 'readme', Schema::TYPE_TEXT);
$this->addColumn(self::TABLE, 'screenshots', Schema::TYPE_TEXT);
$this->addColumn(self::TABLE, 'authors', Schema::TYPE_TEXT);
$this->addColumn(self::TABLE, 'license', Schema::TYPE_TEXT);
}
}

View File

@ -3,6 +3,7 @@
namespace app\models;
use app\components\Git;
use app\components\Serialize;
use app\models\query\PackageQuery;
use bigpaulie\fancybox\FancyBox;
use cebe\markdown\GithubMarkdown;
@ -70,8 +71,8 @@ class Package extends ActiveRecord
[['name', 'url'], 'trim'],
[['name'], 'match', 'pattern' => '/^[-a-z0-9_]+$/', 'message' => '{attribute} can only contain lowercase letters, numbers, "_" and "-"'],
[['url'], 'match', 'pattern' => '%(git|http(s)?)(:(//)?)([\w./\-~]+)(\.git)%', 'message' => '{attribute} must be a valid git endpoint.'],
[['url'], function ($attribute, $params) {
if ($this->checkUrl && Git::getFile($this->$attribute, '') === false) {
[['url'], function ($attribute) {
if ($this->checkUrl && Git::getFile($this->$attribute) === false) {
$this->addError($attribute, 'Could not fetch remote repository.');
}
}],
@ -172,7 +173,7 @@ class Package extends ActiveRecord
if (!$this->serialized) {
foreach ($this->serializeAttributes as $attribute) {
if ($this->$attribute) {
$this->$attribute = json_encode($this->$attribute);
$this->$attribute = [Serialize::serialize($this->$attribute), \PDO::PARAM_LOB];
}
}
$this->serialized = true;
@ -187,7 +188,7 @@ class Package extends ActiveRecord
if ($this->serialized) {
foreach ($this->serializeAttributes as $attribute) {
if ($this->$attribute) {
$this->$attribute = json_decode($this->$attribute, true);
$this->$attribute = Serialize::unserialize($this->$attribute);
}
}
$this->serialized = false;

View File

@ -1,7 +1,6 @@
<?php
require(__DIR__ . '/../src/config/init.php');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../src/config/bootstrap.php');
$config = require(__DIR__ . '/../src/config/main.php');

1
yii
View File

@ -1,7 +1,6 @@
#!/usr/bin/env php
<?php
require(__DIR__ . '/src/config/init.php');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/src/config/bootstrap.php');
$config = require(__DIR__ . '/src/config/main.php');