diff options
Diffstat (limited to 'bin/maintenance.php')
-rwxr-xr-x | bin/maintenance.php | 182 |
1 files changed, 23 insertions, 159 deletions
diff --git a/bin/maintenance.php b/bin/maintenance.php index 293249d..674abe4 100755 --- a/bin/maintenance.php +++ b/bin/maintenance.php @@ -65,11 +65,6 @@ if (isset($_SERVER['HTTP_HOST'])) { require_once('database.class.php'); -/** - * If you want to change some data, use $db->write. It talks to the master db. - * If you're just reading, use $db->read. It talks to a slave. - */ - /** * * Get time as a float. @@ -98,20 +93,11 @@ switch ($action) { case 'weekly': // Lock stats dashboard $db->lockStats(); - - $seven_day_counts = array(); - - $addons_sql = "SELECT id FROM addons"; - echo "Retrieving all add-on ids...\n"; - $addons_result = $db->read($addons_sql); - $affected_rows = mysql_num_rows($addons_result); - - if ($affected_rows > 0 ) { - while ($row = mysql_fetch_array($addons_result)) { - $seven_day_counts[$row['id']] = 0; - } - } + // Reset all weekly counts + $reset_sql = "UPDATE addons SET weeklydownloads = 0"; + echo "Resetting all weekly add-on counts to 0...\n"; + $reset_result = $db->query($reset_sql, true); // Get 7 day counts from the download table. $seven_day_count_sql = " @@ -129,11 +115,12 @@ switch ($action) { "; echo 'Retrieving seven-day counts from `download_counts` ...'."\n"; - $seven_day_count_result = $db->read($seven_day_count_sql); + $seven_day_count_result = $db->query($seven_day_count_sql); $affected_rows = mysql_num_rows($seven_day_count_result); if ($affected_rows > 0 ) { + $seven_day_counts = array(); while ($row = mysql_fetch_array($seven_day_count_result)) { $seven_day_counts[$row['addon_id']] = ($row['seven_day_count']>0) ? $row['seven_day_count'] : 0; } @@ -146,7 +133,7 @@ switch ($action) { "; $seven_day_count_update_result = - $db->write($seven_day_count_update_sql); + $db->query($seven_day_count_update_sql, true); } } @@ -175,7 +162,7 @@ switch ($action) { "; echo 'Retrieving total counts from `download_counts` ...'."\n"; - $total_count_result = $db->read($total_count_sql); + $total_count_result = $db->query($total_count_sql); $affected_rows = mysql_num_rows($total_count_result); @@ -191,7 +178,7 @@ switch ($action) { "; $total_count_update_result = - $db->write($total_count_update_sql); + $db->query($total_count_update_sql, true); } } @@ -227,7 +214,7 @@ switch ($action) { WHERE `expires` < DATE_SUB(CURDATE(), INTERVAL 2 DAY) "; - $session_result = $db->write($session_sql); + $session_result = $db->query($session_sql, true); $affected_rows += mysql_affected_rows($db->write); @@ -255,7 +242,7 @@ switch ($action) { "; // Get file names and IDs of all files to copy. - $files_result = $db->read($files_sql); + $files_result = $db->query($files_sql); $affected_rows = 0; @@ -292,9 +279,9 @@ switch ($action) { ) AS c ON (a.id = c.addon_id) SET a.totalreviews = c.count "; - $reviews_result = $db->write($reviews_sql); + $reviews_result = $db->query($reviews_sql); - $affected_rows = mysql_affected_rows(); + $affected_rows = $reviews_result; break; @@ -319,11 +306,11 @@ switch ($action) { ) AS c ON (a.id = c.addon_id) SET a.averagerating = ROUND(c.avg_rating, 2) "; - $rating_result = $db->write($rating_sql); + $rating_result = $db->query($rating_sql); echo 'Updating bayesian ratings...'."\n"; // get average review count and average rating - $rows = $db->read(" + $rows = $db->query(" SELECT AVG(a.cnt) AS avg_cnt FROM ( SELECT COUNT(*) AS cnt @@ -337,7 +324,7 @@ switch ($action) { $row = mysql_fetch_array($rows); $avg_num_votes = $row['avg_cnt']; - $rows = $db->read(" + $rows = $db->query(" SELECT AVG(a.addon_rating) AS avg_rating FROM ( SELECT AVG(rating) AS addon_rating @@ -360,7 +347,7 @@ switch ($action) { ({$avg_num_votes} + a.totalreviews) ), 0) "; - $rating_result = $db->write($rating_sql); + $rating_result = $db->query($rating_sql); $affected_rows = mysql_affected_rows(); @@ -374,29 +361,13 @@ switch ($action) { case 'unconfirmed': echo "Removing user accounts that haven't been confirmed for two weeks...\n"; $unconfirmed_sql = " - DELETE users + DELETE IGNORE FROM users - LEFT JOIN addons_users on users.id = addons_users.user_id WHERE created < DATE_SUB(CURDATE(), INTERVAL 2 WEEK) - AND confirmationcode != '' - AND addons_users.user_id IS NULL - "; - $res = $db->write($unconfirmed_sql); - - $affected_rows = mysql_affected_rows(); - break; - + AND confirmationcode != ''; + "; // using IGNORE keyword in order to not delete users who are referenced anywhere + $res = $db->query($unconfirmed_sql); - - /** - * Delete password reset codes that have expired. - */ - case 'expired_resetcode': - echo "Removing reset codes that have expired...\n"; - $db->write("UPDATE users - SET resetcode=DEFAULT, - resetcode_expires=DEFAULT - WHERE resetcode_expires < NOW()"); $affected_rows = mysql_affected_rows(); break; @@ -424,7 +395,7 @@ switch ($action) { SET ac.downloads = j.sum "; - $db->write($addons_collections_total); + $db->query($addons_collections_sql); $affected_rows = mysql_affected_rows(); break; @@ -450,119 +421,12 @@ switch ($action) { SET c.downloads = j.sum "; - $db->write($collections_sql); + $db->query($collections_sql); $affected_rows = mysql_affected_rows(); break; - - /** - * Update tag counts for sidebar navigation - */ - case 'tag_totals': - echo "Starting tag counts update...\n"; - // HACK: Wish I had $valid_status from constants.php - $valid_status = join(',', array(1, 2, 3, 4)); - // Modified query inspired by countAddonsInAllCategories() - // in site/app/models/addon.php - $tag_counts_sql = " - UPDATE - tags AS t - INNER JOIN ( - SELECT - at.tag_id, - COUNT(DISTINCT Addon.id) AS ct - FROM - addons AS Addon - INNER JOIN versions AS Version - ON (Addon.id = Version.addon_id) - INNER JOIN applications_versions AS av - ON (av.version_id = Version.id) - INNER JOIN addons_tags AS at - ON (at.addon_id = Addon.id) - INNER JOIN files AS File - ON (Version.id = File.version_id - AND File.status IN ({$valid_status})) - WHERE - Addon.status IN ({$valid_status}) - AND Addon.inactive = 0 - GROUP BY at.tag_id - ) AS j ON (t.id = j.tag_id) - SET - t.count = j.ct - "; - $db->write($tag_counts_sql); - $affected_rows = mysql_affected_rows(); - break; - - - - - /** - * Update global stats counters - */ - case 'global_stats': - echo "Starting global stats update...\n"; - $affected_rows = 0; - $db->write(" - REPLACE INTO global_stats - (name, count, modified) - VALUES - ('addons_downloaded', ( - SELECT SUM(count) - FROM download_counts - ), now()) - "); - $affected_rows += mysql_affected_rows(); - $db->write(" - REPLACE INTO global_stats - (name, count, modified) - VALUES - ('addons_in_use', ( - SELECT SUM(count) - FROM update_counts - WHERE date > ((SELECT MAX(date) FROM update_counts) - INTERVAL 1 DAY) - ), now()) - "); - $affected_rows += mysql_affected_rows(); - break; - - - - /** - * Collection weekly and monthly subscriber counts - */ - case 'collection_subscribers': - echo "Starting collection subscriber update...\n"; - // Clear out existing data. - $db->write("UPDATE collections - SET weekly_subscribers = 0, monthly_subscribers = 0"); - $woohoo = " - UPDATE collections AS c - INNER JOIN ( - SELECT - COUNT(collection_id) AS count, - collection_id - FROM collection_subscriptions - WHERE created >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) - GROUP BY collection_id - ) AS weekly ON (c.id = weekly.collection_id) - INNER JOIN ( - SELECT - COUNT(collection_id) AS count, - collection_id - FROM collection_subscriptions - WHERE created >= DATE_SUB(CURDATE(), INTERVAL 31 DAY) - GROUP BY collection_id - ) AS monthly ON (c.id = monthly.collection_id) - SET c.weekly_subscribers = weekly.count, - c.monthly_subscribers = monthly.count - "; - $result = $db->write($woohoo); - $affected_rows = mysql_affected_rows(); - break; - /** * Unknown command. */ |