function BuildPages(&$pages, $callback = null, $all = FALSE) { global $DB, $C, $L; // One at a time please $wouldblock = FALSE; $fd = fopen("{$GLOBALS['BASE_DIR']}/data/_build_lock", 'w'); flock($fd, LOCK_EX | LOCK_NB, $wouldblock); if ($wouldblock) { return; } // Clear old build history $DB->Update('DELETE FROM `tx_build_history` WHERE `date_start` < DATE_ADD(?, INTERVAL -14 DAY)', array(MYSQL_NOW)); $DB->Update('INSERT INTO `tx_build_history` VALUES (?,?,?,?,?,?,?)', array(null, MYSQL_NOW, null, null, count($pages), 0, null)); $GLOBALS['build_history_id'] = $DB->InsertID(); if (!preg_match('~^\\d\\d\\d$~', $C['page_permissions'])) { $C['page_permissions'] = $GLOBALS['FILE_PERMISSIONS']; } else { $C['page_permissions'] = octdec('0' . $C['page_permissions']); } // Clear records of currently used galleries on these pages ClearUsedGalleries($pages, $all); // Process the clicklog ProcessClickLog(); // Cache icons if (!isset($GLOBALS['ICON_CACHE'])) { $GLOBALS['ICON_CACHE'] =& $DB->FetchAll('SELECT * FROM `tx_icons`', null, 'icon_id'); } // Cache categories by tag if (!isset($GLOBALS['CATEGORY_CACHE_TAG'])) { $GLOBALS['CATEGORY_CACHE_TAG'] =& $DB->FetchAll('SELECT * FROM `tx_categories` ORDER BY `name`', null, 'tag'); } // Cache categories by id if (!isset($GLOBALS['CATEGORY_CACHE_ID'])) { $GLOBALS['CATEGORY_CACHE_ID'] =& $DB->FetchAll('SELECT * FROM `tx_categories`', null, 'category_id'); $GLOBALS['CATEGORY_CACHE_ID'][''] = array('name' => $L['MIXED'], 'category_id' => 0); } // Cache sponsors by id if (!isset($GLOBALS['SPONSOR_CACHE'])) { $GLOBALS['SPONSOR_CACHE'] =& $DB->FetchAll('SELECT * FROM `tx_sponsors`', null, 'sponsor_id'); } // Clear data on galleries used during the previous build $DB->Update('UPDATE `tx_gallery_used` SET `this_build`=0,`new`=0'); // Remove galleries scheduled for deletion $result = $DB->Query('SELECT * FROM `tx_galleries` WHERE `date_deletion` <= ?', array(MYSQL_NOW)); while ($gallery = $DB->NextRow($result)) { DeleteGallery($gallery['gallery_id'], $gallery); } $DB->Free($result); // Delete old submitted galleries that are currently holding $result = $DB->Query('SELECT * FROM `tx_galleries` WHERE `status`=? AND `type`=? AND `date_displayed` <= SUBDATE(?, INTERVAL ? DAY)', array('holding', 'submitted', MYSQL_NOW, $C['submitted_hold'])); while ($gallery = $DB->NextRow($result)) { DeleteGallery($gallery['gallery_id'], $gallery); } $DB->Free($result); // Rotate permanent galleries from holding back to approved queue $DB->Update('UPDATE `tx_galleries` SET ' . "`status`='approved', " . "`date_displayed`=NULL, " . "`build_counter`=IF(?, 0, `build_counter`), " . "`used_counter`=IF(?, 0, `used_counter`), " . "`clicks`=IF(?, 0, `clicks`) " . "WHERE `status`='holding' AND `type`='permanent' AND `date_displayed` <= SUBDATE(?, INTERVAL ? DAY)", array(intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), MYSQL_NOW, $C['permanent_hold'])); // Count total thumbs and galleries $GLOBALS['_totals'] = $DB->Row("SELECT COUNT(*) AS `galleries`,SUM(`thumbnails`) AS `thumbnails` FROM `tx_galleries` WHERE `status` IN ('approved','used')"); $GLOBALS['_totals']['categories'] = count($GLOBALS['CATEGORY_CACHE_ID']) - 1; // Build each page foreach ($pages as $page) { $page_all = $DB->Row('SELECT * FROM `tx_pages` WHERE `page_id`=?', array($page['page_id'])); if ($page_all['locked'] && !$GLOBALS['override_page_lock']) { continue; } if ($callback) { call_user_func($callback, $page_all); } $DB->Update('DELETE FROM `tx_gallery_used_page`'); $DB->Update('DELETE FROM `tx_ads_used_page`'); $DB->Update('UPDATE `tx_build_history` SET `current_page_url`=? WHERE `history_id`=?', array($page_all['page_url'], $GLOBALS['build_history_id'])); BuildPage($page_all); $DB->Update('UPDATE `tx_build_history` SET `pages_built`=`pages_built`+1 WHERE `history_id`=?', array($GLOBALS['build_history_id'])); unset($page_all); unset($page); } // Mark newly selected galleries as used // Update counters for galleries used this build $DB->Update('UPDATE `tx_galleries` JOIN `tx_gallery_used` USING (`gallery_id`) SET ' . '`build_counter`=`build_counter`+1, ' . '`used_counter`=`used_counter`+1, ' . '`times_selected`=IF(`new`=1, `times_selected`+1, `times_selected`), ' . '`status`=?, ' . '`date_displayed`=IF(`new`=1, ?, `date_displayed`) ' . 'WHERE `this_build`=1', array('used', MYSQL_NOW)); // Move no longer used galleries to holding queue (or rotate back to approved if permanent holding period is 0) if ($C['permanent_hold'] == 0) { $DB->Update('UPDATE `tx_galleries` LEFT JOIN `tx_gallery_used` USING (`gallery_id`) SET ' . "`status`=IF(`type`='permanent', 'approved', 'holding'), " . "`date_displayed`=IF(`type`='permanent', NULL, `date_displayed`), " . "`date_approved`=IF(`type`='permanent', NULL, `date_approved`), " . "`date_scheduled`=IF(`type`='permanent', NULL, `date_scheduled`), " . "`build_counter`=IF(`type`='permanent', IF(?, 0, `build_counter`+1), `build_counter`+1), " . "`used_counter`=IF(`type`='permanent', IF(?, 0, `used_counter`), `used_counter`), " . "`clicks`=IF(`type`='permanent', IF(?, 0, `clicks`), `clicks`) " . "WHERE `status`='used' AND `page_id` IS NULL", array(intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), intval($C['reset_on_rotate']), 'used')); } else { $DB->Update('UPDATE `tx_galleries` LEFT JOIN `tx_gallery_used` USING (`gallery_id`) SET `status`=?,`build_counter`=`build_counter`+1 WHERE `status`=? AND `page_id` IS NULL', array('holding', 'used')); } $DB->Update('UPDATE `tx_build_history` SET `date_end`=?,`current_page_url`=NULL WHERE `history_id`=?', array(gmdate(DF_DATETIME, TimeWithTz()), $GLOBALS['build_history_id'])); flock($fd, LOCK_UN); fclose($fd); @chmod("{$GLOBALS['BASE_DIR']}/data/_build_lock", 0666); }
} if (!IsEmptyString($args['pages'])) { if (preg_match('~(\\d+)-(\\d+)~', $args['pages'], $matches)) { $args['pages'] = join(',', range($matches[1], $matches[2])); } BuildSelected(explode(',', $args['pages'])); } else { if (!IsEmptyString($args['tags'])) { BuildTagged($args['tags']); } else { BuildAll(); } } break; case '--process-clicklog': ProcessClickLog(); break; case '--backup': $args = ParseCommandLine(); if (IsEmptyString($args['sql-file'])) { echo "ERROR: You must specify at least a SQL data backup filename when using the --backup function\n" . "Example:\n" . "{$_SERVER['_']} {$path}/{$GLOBALS['argv'][0]} --backup --sql-file=sql-backup.txt --thumbs-file=thumbs-backup.txt --archive-file=backup.tar.gz\n"; break; } DoDatabaseBackup($args, TRUE); break; case '--restore': $args = ParseCommandLine(); if (IsEmptyString($args['sql-file'])) { echo "ERROR: You must specify at least a SQL data backup filename when using the --restore function\n" . "Example:\n" . "{$_SERVER['_']} {$path}/{$GLOBALS['argv'][0]} --restore --sql-file=sql-backup.txt --thumbs-file=thumbs-backup.txt\n"; break; }