Skip to content

Commit

Permalink
shell : url rewrite script , to retry generate url rewrite and clear …
Browse files Browse the repository at this point in the history
…discard rewrite url
  • Loading branch information
root authored and root committed Sep 21, 2016
1 parent 273d88e commit 5c97a57
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 17 deletions.
15 changes: 15 additions & 0 deletions app/console/config/modules/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
?>
<?php
return [
'helper' => [
'class' => '\fecshop\app\console\modules\Helper\Module',
],
];
28 changes: 28 additions & 0 deletions app/console/modules/Helper/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\console\modules\Helper;
use Yii;
use fecshop\app\console\modules\ConsoleModule;
/**
* @author Terry Zhao <[email protected]>
* @since 1.0
*/
class Module extends ConsoleModule
{
public $blockNamespace;
public function init()
{
# ÒÔÏ´úÂë±ØÐëÖ¸¶¨
$nameSpace = __NAMESPACE__;
$this->controllerNamespace = $nameSpace . '\\controllers';
$this->blockNamespace = $nameSpace . '\\block';
parent::init();

}
}
99 changes: 99 additions & 0 deletions app/console/modules/Helper/controllers/UrlrewriteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\console\modules\Helper\controllers;
use Yii;
use yii\base\InlineAction;
use yii\console\Controller;
/**
* @author Terry Zhao <[email protected]>
* @since 1.0
*/
class UrlrewriteController extends Controller
{
protected $_numPerPage = 50;
/**
* 得到当前的时间。
*/
public function actionNowtime(){
echo time();
}

/**
* 得到产品的页数。
*/
public function actionProductpagenum(){
$count = Yii::$service->product->collCount($filter);
echo ceil($count/$this->_numPerPage);
}

/**
* 得到产品的总数。
*/
public function actionProductcount(){
$count = Yii::$service->product->collCount($filter);
echo $count;
}
/**
* 处理产品的重写
*/
public function actionProduct($pageNum){
$filter['numPerPage'] = $this->_numPerPage;
$filter['pageNum'] = $pageNum;
$filter['asArray'] = true;
$products = Yii::$service->product->coll($filter);
$product_ids = [];
foreach($products['coll'] as $one){
Yii::$service->product->save($one);
}

}




/**
* 得到分类的页数。
*/
public function actionCategorypagenum(){
$count = Yii::$service->category->collCount($filter);
echo ceil($count/$this->_numPerPage);
}

/**
* 得到分类的总数。
*/
public function actionCategorycount(){
$count = Yii::$service->category->collCount($filter);
echo $count;
}
/**
* 处理分类的重写
*/
public function actionCategory($pageNum){
$filter['numPerPage'] = $this->_numPerPage;
$filter['pageNum'] = $pageNum;
$filter['asArray'] = true;
$categorys = Yii::$service->category->coll($filter);
$category_ids = [];
foreach($categorys['coll'] as $one){
Yii::$service->category->save($one);
}

}
/**
* 删除时间小于nowtime的
*/
public function actionClearnoactive($nowtime){
echo 'delete date gt '.$nowtime."\n";
Yii::$service->url->rewrite->removeByUpdatedAt($nowtime);
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PriceController extends Controller
public function actionComputefinalprice($pageNum){
$filter['numPerPage'] = $this->_numPerPage;
$filter['pageNum'] = $pageNum;
$filter['asArray'] = false;
$filter['asArray'] = true;
$products = Yii::$service->product->coll($filter);
$product_ids = [];
foreach($products['coll'] as $one){
Expand Down
4 changes: 3 additions & 1 deletion models/mongodb/url/UrlRewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function attributes()
'type',
'custom_url_key',
'origin_url',
'status'
'status',
'updated_at',
'created_at',
];
}
}
5 changes: 4 additions & 1 deletion services/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ protected function actionGetByPrimaryKey($primaryKey){
return $this->_category->getByPrimaryKey($primaryKey);
}

protected function actionCollCount($filter=''){
return $this->_category->collCount($filter);
}


/**
Expand Down Expand Up @@ -89,7 +92,7 @@ protected function actionGetTreeArr($rootCategoryId=0){
* @property $originUrlKey|String , 分类的在修改之前的url key.(在数据库中保存的url_key字段,如果没有则为空)
* 保存分类,同时生成分类的伪静态url(自定义url),如果按照name生成的url或者自定义的urlkey存在,系统则会增加几个随机数字字符串,来增加唯一性。
*/
protected function actionSave($one,$originUrlKey){
protected function actionSave($one,$originUrlKey='catalog/category/index'){
return $this->_category->save($one,$originUrlKey);
}
/**
Expand Down
2 changes: 1 addition & 1 deletion services/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function actionGetCategoryProductIds($product_id_arr,$category_id){
* 保存产品(插入和更新),以及保存产品的自定义url
* 如果提交的数据中定义了自定义url,则按照自定义url保存到urlkey中,如果没有自定义urlkey,则会使用name进行生成。
*/
protected function actionSave($one,$originUrlKey=''){
protected function actionSave($one,$originUrlKey='catalog/product/index'){
return $this->_product->save($one,$originUrlKey);
}
/**
Expand Down
13 changes: 10 additions & 3 deletions services/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ protected function actionSaveRewriteUrlKeyByStr($str,$originUrl,$originUrlKey,$t
* if originUrlKey and originUrl is exist in url rewrite collectons.
*/
$model = $this->find();
$data = $model->where([
$data_one = $model->where([
'custom_url_key' => $originUrlKey,
'origin_url' => $originUrl,
])->asArray()->one();
if(isset($data['custom_url_key'])){
])->one();
if(isset($data_one['custom_url_key'])){
/**
* 只要进行了查询,就要更新一下rewrite url表的updated_at.
*/
$data_one->updated_at = time();
$data_one->save();
return $originUrlKey;
}
}
Expand All @@ -77,7 +82,9 @@ protected function actionSaveRewriteUrlKeyByStr($str,$originUrl,$originUrlKey,$t
]);
if(!isset($UrlRewrite['origin_url'])){
$UrlRewrite = $this->newModel();
$UrlRewrite->created_at = time();
}
$UrlRewrite->updated_at = time();
$UrlRewrite->type = $type;
$UrlRewrite->custom_url_key = $urlKey;
$UrlRewrite->origin_url = $originUrl;
Expand Down
11 changes: 10 additions & 1 deletion services/category/CategoryMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ public function coll($filter=''){
];
}

/**
* 得到总数
*/
public function collCount($filter=''){
$query = Category::find();
$query = Yii::$service->helper->ar->getCollByFilter($query,$filter);
return $query->count();
}

/**
* @property $one|Array , save one data . 分类数组
* @property $originUrlKey|String , 分类的在修改之前的url key.(在数据库中保存的url_key字段,如果没有则为空)
* 保存分类,同时生成分类的伪静态url(自定义url),如果按照name生成的url或者自定义的urlkey存在,系统则会增加几个随机数字字符串,来增加唯一性。
*/
public function save($one,$originUrlKey){
public function save($one,$originUrlKey='catalog/category/index'){
$currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
$primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
if($primaryVal){
Expand Down
3 changes: 2 additions & 1 deletion services/product/ProductMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function getCategoryProductIds($product_id_arr,$category_id){
* 保存产品(插入和更新),以及保存产品的自定义url
* 如果提交的数据中定义了自定义url,则按照自定义url保存到urlkey中,如果没有自定义urlkey,则会使用name进行生成。
*/
public function save($one,$originUrlKey=''){
public function save($one,$originUrlKey='catalog/product/index'){

if(!$this->initSave($one)){
return;
}
Expand Down
4 changes: 4 additions & 0 deletions services/url/Rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ protected function actionRemove($ids){
return $this->_urlRewrite->remove($ids);
}

protected function actionRemoveByUpdatedAt($time){
return $this->_urlRewrite->removeByUpdatedAt($time);
}


protected function actionFind(){
return $this->_urlRewrite->find();
Expand Down
25 changes: 23 additions & 2 deletions services/url/rewrite/RewriteMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public function remove($ids){
$model = UrlRewrite::findOne($id);
if(isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()]) ){
$url_key = $model['url_key'];
Yii::$service->url->removeRewriteUrlKey($url_key);
$model->delete();
}else{
//throw new InvalidValueException("ID:$id is not exist.");
Expand All @@ -110,7 +109,6 @@ public function remove($ids){
$model = UrlRewrite::findOne($id);
if(isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()]) ){
$url_key = $model['url_key'];
Yii::$service->url->removeRewriteUrlKey($url_key);
$model->delete();
}else{
Yii::$service->helper->errors->add("UrlRewrite Remove Errors:ID:$id is not exist.");
Expand All @@ -121,6 +119,29 @@ public function remove($ids){

}

public function removeByUpdatedAt($time){
if($time){
UrlRewrite::deleteAll([
'$or' => [
[
'updated_at' => [
'$lt' => (int)$time,
],
],
[
'updated_at' => [
'$exists' => false
]
]
]

]);
echo "delete complete \n";
}

}


public function find(){
return UrlRewrite::find();
}
Expand Down
10 changes: 8 additions & 2 deletions services/url/rewrite/RewriteMysqldb.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public function remove($ids){
$model = UrlRewrite::findOne($id);
if(isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()]) ){
$url_key = $model['url_key'];
Yii::$service->url->removeRewriteUrlKey($url_key);
$model->delete();
}else{

Expand All @@ -147,7 +146,6 @@ public function remove($ids){
$innerTransaction = Yii::$service->db->beginTransaction();
try {
$url_key = $model['url_key'];
Yii::$service->url->removeRewriteUrlKey($url_key);
$model->delete();
$innerTransaction->commit();
} catch (Exception $e) {
Expand All @@ -163,6 +161,14 @@ public function remove($ids){

}

public function removeByUpdatedAt($time){
if($time){
UrlRewrite::deleteAll([
'<','updated_at',$time
]);
}
}

public function find(){
return UrlRewrite::find();
}
Expand Down
11 changes: 7 additions & 4 deletions shell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ Xun Search 只支持中文,
### 3.computeProductFinalPrice.sh

本脚本为计算每一个产品的final_price的值,
同时会同步信息到搜索表,但是不会处理删除掉的表
因此,执行这个价格完成后,还需要执行 fullSearchSync.sh脚本。


3.1 同时会同步信息到搜索表,但是不会执行删除操作(譬如某个产品
删除了,这个脚本是不会检测,同步删除的,如果要删除需要执行 fullSearchSync.sh脚本。
3.2 url rewrite的产品数据也会同步,同样不会执行删除url rewrite里面的数据(
如果要清除残留的url rewrite数据,请执行urlRewrite.sh脚本)。


### 4.urlRewrite.sh脚本

4.1 将产品的url自定义重新跑一次。包括分类,产品等
4.2 将urlRewrite表中残留的重新数据,但是在产品和分类中不存在的清空。



Expand Down
Loading

0 comments on commit 5c97a57

Please sign in to comment.