Created
September 19, 2015 13:41
-
-
Save sergant210/4a5e631a7005bea680da to your computer and use it in GitHub Desktop.
Скрипт переноса данных из HitsPage в siteStatistics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tstart = microtime(true); | |
$hits = array(); | |
$q = $modx->newQuery('modResource'); | |
$q->select('id,properties'); | |
$q->sortby('id'); | |
// Для поэтапной обработки указываем limit и offset | |
//$q->limit(100,0); | |
if ($q->prepare() && $q->stmt->execute()) { | |
while($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) { | |
if (!empty($row['properties'])) { | |
$prop = $modx->fromJSON($row['properties']); | |
//print_r($prop); | |
if (isset($prop['hitspage'])) { | |
$hits[$row['id']] = $prop['hitspage']['hitts']; | |
//echo $row['id'],'-',$hits[$row['id']],"<br />"; | |
} | |
} | |
} | |
} | |
// Определяем пользователя, для которого укажем количество просмотра. Лучше указать админа | |
$user = 1; // | |
// Дата, на которую будут регистрироваться просмотры из Hitspage. | |
$date = '2015-01-01'; | |
//Контекст | |
$context = 'web'; | |
$modx->addPackage('sitestatistics', $this->modx->getOption('core_path') . 'components/sitestatistics/model/'); | |
$query = $modx->newQuery('UserStatistics', array('uid' => $user)); | |
$query->select('user_key'); | |
if (!$userKey = $modx->getValue($query->prepare())) { | |
$userKey = md5(rand() . time() . rand()); | |
$statUser = $modx->newObject('UserStatistics'); | |
$statUser->fromArray(array( | |
'user_key' => $userKey, | |
'date' => $date, | |
'uid' => $user, | |
'rid' => 0, | |
'context' => $context, | |
),'',true | |
); | |
if (!$statUser->save()) print("Ошибка при создании пользователя"); | |
} | |
print("<pre>"); | |
foreach ($hits as $k=>$v) { | |
$count = $modx->getCount('PageStatistics',array( | |
'rid' => $k, | |
'user_key' => $userKey, | |
'date' => $date | |
)); | |
$data = array( | |
$k, | |
$modx->quote($userKey), | |
$modx->quote($date), | |
$modx->quote(date('Y-m',strtotime($date))), | |
$modx->quote(date('Y',strtotime($date))), | |
$v | |
); | |
if ($count) { | |
$sql = "UPDATE {$modx->getTableName('PageStatistics')} SET `views`= `views`+{$v} WHERE `rid`={$k} AND `user_key`='{$userKey}' AND `date`='{$date}'"; | |
} else { | |
$sql = "INSERT INTO {$modx->getTableName('PageStatistics')} (`rid`,`user_key`,`date`,`month`,`year`,`views`) VALUES (" . implode(',',$data).")"; | |
} | |
$query = $modx->prepare($sql); | |
if (!$query->execute()) { | |
print "Ошибка переноса"."<br />"; | |
print_r($query->errorInfo()); | |
break; | |
} | |
} | |
print "Обработано ".count($hits).' ресурсов.'."<br />"; | |
print "Время выполнения - " . number_format(round(microtime(true) - $tstart, 7), 7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment