Last active
December 19, 2015 05:49
-
-
Save jpdevries/5906628 to your computer and use it in GitHub Desktop.
Run this php script from outside MODX to Sync Page hits from HitsPage to Hits. HitsPage uses Template Variables this will import those values into a custom table for found resources. See comments for usage.
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); | |
set_time_limit(0); | |
require 'config.core.php'; // absolute path to your MODX config | |
require MODX_CORE_PATH . 'model/modx/modx.class.php'; | |
$modx = modX::getInstance(); | |
$modx->initialize('mgr'); | |
$modx->setLogLevel(modX::LOG_LEVEL_INFO); | |
$modx->setLogTarget('ECHO'); | |
$tv = $argv[1]; // id of the tv to grab hits from | |
if($tv) { | |
$modx->log(modX::LOG_LEVEL_INFO, "Syncing Hit Counts with TV $tv"); | |
try { | |
// pull tv values of resources with $tv value (hitCount) | |
$rows= array(); | |
$prefix = $modx->getOption(modX::OPT_TABLE_PREFIX); | |
$stmt= $modx->query("SELECT contentid,value from {$prefix}site_tmplvar_contentvalues WHERE tmplvarid = $tv;"); | |
if ($stmt) { | |
// loop through the result set and inspect one row at a time | |
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { | |
array_push($rows, $row); | |
} | |
} | |
// for each resource/tv value pair, update the hit | |
foreach($rows as $row) { | |
$modx->runSnippet('Hits',array( // assumes Hits Extra is installed | |
'punch' => $row['contentid'], | |
'amount' => $row['value'], | |
'knockout' => '1' // knock them to 0 before incrementing | |
)); | |
$modx->log(modX::LOG_LEVEL_INFO, "SyncHits updated hitkey {$row['contentid']} to an amount of {$row['value']}."); | |
} | |
} catch (Exception $e) { | |
$tend = microtime(true); | |
$totalTime= sprintf("%2.4f seconds", ($tend - $tstart)); | |
$modx->log(modX::LOG_LEVEL_ERROR, "SyncHits failed in {$totalTime}: {$e->getMessage()}"); | |
} | |
} else { | |
$modx->log(modX::LOG_LEVEL_ERROR, "Please specify the id of the TV to fetch hits from ex: hits.sync.php 13"); | |
} | |
$tend = microtime(true); | |
$totalTime= sprintf("%2.4f seconds", ($tend - $tstart)); | |
$modx->log(modX::LOG_LEVEL_INFO, "SyncHits executed in {$totalTime}"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ID of Template Variable to reference must be passed.
usage for pulling values from TV 13: