Example #1
0
 /**
  * Delete synch. records, instances and links that refer to missing or invalid items.
  * 
  * @return void
  */
 function blc_cleanup_database()
 {
     global $blclog;
     //Delete synch. records for container types that don't exist
     $blclog->info('... Deleting invalid container records');
     blcContainerHelper::cleanup_containers();
     //Delete invalid instances
     $blclog->info('... Deleting invalid link instances');
     blc_cleanup_instances();
     //Delete orphaned links
     $blclog->info('... Deleting orphaned links');
     blc_cleanup_links();
 }
Example #2
0
 /**
  * The main worker function that does all kinds of things.
  *
  * @return void
  */
 function work()
 {
     global $wpdb;
     if (!$this->acquire_lock()) {
         //FB::warn("Another instance of BLC is already working. Stop.");
         return;
     }
     if ($this->server_too_busy()) {
         //FB::warn("Server is too busy. Stop.");
         return;
     }
     $this->start_timer();
     $max_execution_time = $this->conf->options['max_execution_time'];
     /*****************************************
     						Preparation
     		******************************************/
     // Check for safe mode
     if (blcUtility::is_safe_mode()) {
         // Do it the safe mode way - obey the existing max_execution_time setting
         $t = ini_get('max_execution_time');
         if ($t && $t < $max_execution_time) {
             $max_execution_time = $t - 1;
         }
     } else {
         // Do it the regular way
         @set_time_limit($max_execution_time * 2);
         //x2 should be plenty, running any longer would mean a glitch.
     }
     //Don't stop the script when the connection is closed
     ignore_user_abort(true);
     //Close the connection as per http://www.php.net/manual/en/features.connection-handling.php#71172
     //This reduces resource usage and may solve the mysterious slowdowns certain users have
     //encountered when activating the plugin.
     //(Disable when debugging or you won't get the FirePHP output)
     if (!headers_sent() && (!defined('BLC_DEBUG') || !constant('BLC_DEBUG'))) {
         @ob_end_clean();
         //Discard the existing buffer, if any
         header("Connection: close");
         ob_start();
         echo 'Connection closed';
         //This could be anything
         $size = ob_get_length();
         header("Content-Length: {$size}");
         ob_end_flush();
         // Strange behaviour, will not work
         flush();
         // Unless both are called !
     }
     //Load modules for this context
     $moduleManager = blcModuleManager::getInstance();
     $moduleManager->load_modules('work');
     /*****************************************
     				Parse posts and bookmarks
     		******************************************/
     $orphans_possible = false;
     $still_need_resynch = $this->conf->options['need_resynch'];
     if ($still_need_resynch) {
         //FB::log("Looking for containers that need parsing...");
         while ($containers = blcContainerHelper::get_unsynched_containers(50)) {
             //FB::log($containers, 'Found containers');
             foreach ($containers as $container) {
                 //FB::log($container, "Parsing container");
                 $container->synch();
                 //Check if we still have some execution time left
                 if ($this->execution_time() > $max_execution_time) {
                     //FB::log('The alloted execution time has run out');
                     blc_cleanup_links();
                     $this->release_lock();
                     return;
                 }
                 //Check if the server isn't overloaded
                 if ($this->server_too_busy()) {
                     //FB::log('Server overloaded, bailing out.');
                     blc_cleanup_links();
                     $this->release_lock();
                     return;
                 }
             }
             $orphans_possible = true;
         }
         //FB::log('No unparsed items found.');
         $still_need_resynch = false;
     } else {
         //FB::log('Resynch not required.');
     }
     /******************************************
     				    Resynch done?
     		*******************************************/
     if ($this->conf->options['need_resynch'] && !$still_need_resynch) {
         $this->conf->options['need_resynch'] = $still_need_resynch;
         $this->conf->save_options();
     }
     /******************************************
     				    Remove orphaned links
     		*******************************************/
     if ($orphans_possible) {
         //FB::log('Cleaning up the link table.');
         blc_cleanup_links();
     }
     //Check if we still have some execution time left
     if ($this->execution_time() > $max_execution_time) {
         //FB::log('The alloted execution time has run out');
         $this->release_lock();
         return;
     }
     if ($this->server_too_busy()) {
         //FB::log('Server overloaded, bailing out.');
         $this->release_lock();
         return;
     }
     /*****************************************
     						Check links
     		******************************************/
     while ($links = $this->get_links_to_check(30)) {
         //Some unchecked links found
         //FB::log("Checking ".count($links)." link(s)");
         foreach ($links as $link) {
             //Does this link need to be checked? Excluded links aren't checked, but their URLs are still
             //tested periodically to see if they're still on the exlusion list.
             if (!$this->is_excluded($link->url)) {
                 //Check the link.
                 //FB::log($link->url, "Checking link {$link->link_id}");
                 $link->check(true);
             } else {
                 //FB::info("The URL {$link->url} is excluded, skipping link {$link->link_id}.");
                 $link->last_check_attempt = time();
                 $link->save();
             }
             //Check if we still have some execution time left
             if ($this->execution_time() > $max_execution_time) {
                 //FB::log('The alloted execution time has run out');
                 $this->release_lock();
                 return;
             }
             //Check if the server isn't overloaded
             if ($this->server_too_busy()) {
                 //FB::log('Server overloaded, bailing out.');
                 $this->release_lock();
                 return;
             }
         }
     }
     //FB::log('No links need to be checked right now.');
     $this->release_lock();
     //FB::log('All done.');
 }
Example #3
0
 /**
  * When a bookmark is deleted, remove the related DB records.
  *
  * @param int $link_id
  * @return void
  */
 function hook_delete_link($link_id)
 {
     //Get the container object.
     $container = blcContainerHelper::get_container(array($this->container_type, $link_id));
     //Get the link(s) associated with it.
     $links = $container->get_links();
     //Remove synch. record & instance records.
     $container->delete();
     //Clean up links associated with this bookmark (there's probably only one)
     $link_ids = array();
     foreach ($links as $link) {
         $link_ids[] = $link->link_id;
     }
     blc_cleanup_links($link_ids);
 }
 function hook_trashed_post_comments($post_id, $statuses)
 {
     foreach ($statuses as $comment_id => $comment_status) {
         if ($comment_status == '1') {
             $container = blcContainerHelper::get_container(array($this->container_type, $comment_id));
             $container->delete();
         }
     }
     blc_cleanup_links();
 }
Example #5
0
 /**
  * The main worker function that does all kinds of things.
  *
  * @return void
  */
 function work()
 {
     global $blclog;
     //Close the session to prevent lock-ups.
     //PHP sessions are blocking. session_start() will wait until all other scripts that are using the same session
     //are finished. As a result, a long-running script that unintentionally keeps the session open can cause
     //the entire site to "lock up" for the current user/browser. WordPress itself doesn't use sessions, but some
     //plugins do, so we should explicitly close the session (if any) before starting the worker.
     if (session_id() != '') {
         session_write_close();
     }
     if (!$this->acquire_lock()) {
         //FB::warn("Another instance of BLC is already working. Stop.");
         $blclog->info('Another instance of BLC is already working. Stop.');
         return;
     }
     if ($this->server_too_busy()) {
         //FB::warn("Server is too busy. Stop.");
         $blclog->warn('Server load is too high, stopping.');
         return;
     }
     $this->start_timer();
     $blclog->info('work() starts');
     $max_execution_time = $this->conf->options['max_execution_time'];
     /*****************************************
     						Preparation
     		******************************************/
     // Check for safe mode
     if (blcUtility::is_safe_mode()) {
         // Do it the safe mode way - obey the existing max_execution_time setting
         $t = ini_get('max_execution_time');
         if ($t && $t < $max_execution_time) {
             $max_execution_time = $t - 1;
         }
     } else {
         // Do it the regular way
         @set_time_limit($max_execution_time * 2);
         //x2 should be plenty, running any longer would mean a glitch.
     }
     //Don't stop the script when the connection is closed
     ignore_user_abort(true);
     //Close the connection as per http://www.php.net/manual/en/features.connection-handling.php#71172
     //This reduces resource usage.
     //(Disable when debugging or you won't get the FirePHP output)
     if (!headers_sent() && (defined('DOING_AJAX') && constant('DOING_AJAX')) && (!defined('BLC_DEBUG') || !constant('BLC_DEBUG'))) {
         @ob_end_clean();
         //Discard the existing buffer, if any
         header("Connection: close");
         ob_start();
         echo 'Connection closed';
         //This could be anything
         $size = ob_get_length();
         header("Content-Length: {$size}");
         ob_end_flush();
         // Strange behaviour, will not work
         flush();
         // Unless both are called !
     }
     //Load modules for this context
     $moduleManager = blcModuleManager::getInstance();
     $moduleManager->load_modules('work');
     $target_usage_fraction = $this->conf->get('target_resource_usage', 0.25);
     //Target usage must be between 1% and 100%.
     $target_usage_fraction = max(min($target_usage_fraction, 1), 0.01);
     /*****************************************
     				Parse posts and bookmarks
     		******************************************/
     $orphans_possible = false;
     $still_need_resynch = $this->conf->options['need_resynch'];
     if ($still_need_resynch) {
         //FB::log("Looking for containers that need parsing...");
         $max_containers_per_query = 50;
         $start = microtime(true);
         $containers = blcContainerHelper::get_unsynched_containers($max_containers_per_query);
         $get_containers_time = microtime(true) - $start;
         while (!empty($containers)) {
             //FB::log($containers, 'Found containers');
             $this->sleep_to_maintain_ratio($get_containers_time, $target_usage_fraction);
             foreach ($containers as $container) {
                 $synch_start_time = microtime(true);
                 //FB::log($container, "Parsing container");
                 $container->synch();
                 $synch_elapsed_time = microtime(true) - $synch_start_time;
                 $blclog->info(sprintf('Parsed container %s[%s] in %.2f ms', $container->container_type, $container->container_id, $synch_elapsed_time * 1000));
                 //Check if we still have some execution time left
                 if ($this->execution_time() > $max_execution_time) {
                     //FB::log('The allotted execution time has run out');
                     blc_cleanup_links();
                     $this->release_lock();
                     return;
                 }
                 //Check if the server isn't overloaded
                 if ($this->server_too_busy()) {
                     //FB::log('Server overloaded, bailing out.');
                     blc_cleanup_links();
                     $this->release_lock();
                     return;
                 }
                 //Intentionally slow down parsing to reduce the load on the server. Basically,
                 //we work $target_usage_fraction of the time and sleep the rest of the time.
                 $this->sleep_to_maintain_ratio($synch_elapsed_time, $target_usage_fraction);
             }
             $orphans_possible = true;
             $start = microtime(true);
             $containers = blcContainerHelper::get_unsynched_containers($max_containers_per_query);
             $get_containers_time = microtime(true) - $start;
         }
         //FB::log('No unparsed items found.');
         $still_need_resynch = false;
     } else {
         //FB::log('Resynch not required.');
     }
     /******************************************
     				    Resynch done?
     		*******************************************/
     if ($this->conf->options['need_resynch'] && !$still_need_resynch) {
         $this->conf->options['need_resynch'] = $still_need_resynch;
         $this->conf->save_options();
     }
     /******************************************
     				    Remove orphaned links
     		*******************************************/
     if ($orphans_possible) {
         $start = microtime(true);
         $blclog->info('Removing orphaned links.');
         blc_cleanup_links();
         $get_links_time = microtime(true) - $start;
         $this->sleep_to_maintain_ratio($get_links_time, $target_usage_fraction);
     }
     //Check if we still have some execution time left
     if ($this->execution_time() > $max_execution_time) {
         //FB::log('The allotted execution time has run out');
         $blclog->info('The allotted execution time has run out.');
         $this->release_lock();
         return;
     }
     if ($this->server_too_busy()) {
         //FB::log('Server overloaded, bailing out.');
         $blclog->info('Server load too high, stopping.');
         $this->release_lock();
         return;
     }
     /*****************************************
     						Check links
     		******************************************/
     $max_links_per_query = 30;
     $start = microtime(true);
     $links = $this->get_links_to_check($max_links_per_query);
     $get_links_time = microtime(true) - $start;
     while ($links) {
         $this->sleep_to_maintain_ratio($get_links_time, $target_usage_fraction);
         //Some unchecked links found
         //FB::log("Checking ".count($links)." link(s)");
         $blclog->info("Checking " . count($links) . " link(s)");
         //Randomizing the array reduces the chances that we'll get several links to the same domain in a row.
         shuffle($links);
         $transactionManager = TransactionManager::getInstance();
         $transactionManager->start();
         foreach ($links as $link) {
             //Does this link need to be checked? Excluded links aren't checked, but their URLs are still
             //tested periodically to see if they're still on the exclusion list.
             if (!$this->is_excluded($link->url)) {
                 //Check the link.
                 //FB::log($link->url, "Checking link {$link->link_id}");
                 $link->check(true);
             } else {
                 //FB::info("The URL {$link->url} is excluded, skipping link {$link->link_id}.");
                 $link->last_check_attempt = time();
                 $link->save();
             }
             //Check if we still have some execution time left
             if ($this->execution_time() > $max_execution_time) {
                 //FB::log('The allotted execution time has run out');
                 $blclog->info('The allotted execution time has run out.');
                 $this->release_lock();
                 return;
             }
             //Check if the server isn't overloaded
             if ($this->server_too_busy()) {
                 //FB::log('Server overloaded, bailing out.');
                 $blclog->info('Server load too high, stopping.');
                 $this->release_lock();
                 return;
             }
         }
         $transactionManager->commit();
         $start = microtime(true);
         $links = $this->get_links_to_check($max_links_per_query);
         $get_links_time = microtime(true) - $start;
     }
     //FB::log('No links need to be checked right now.');
     $this->release_lock();
     $blclog->info('work(): All done.');
     //FB::log('All done.');
 }
 /**
  * Delete custom field synch. records when the post that they belong to is deleted.
  *
  * @param int $post_id
  * @return void
  */
 function post_deleted($post_id)
 {
     //Get the associated container object
     $container =& blcContainerHelper::get_container(array($this->container_type, intval($post_id)));
     //Delete it
     $container->delete();
     //Clean up any dangling links
     blc_cleanup_links();
 }
 /**
  * Remove the synch. record and link instances associated with a post when it's deleted 
  *
  * @param int $post_id
  * @return void
  */
 function post_deleted($post_id)
 {
     //Get the container type matching the type of the deleted post
     $post = get_post($post_id);
     if (!$post) {
         return;
     }
     //Get the associated container object
     $post_container = blcContainerHelper::get_container(array($post->post_type, intval($post_id)));
     if ($post_container) {
         //Delete it
         $post_container->delete();
         //Clean up any dangling links
         blc_cleanup_links();
     }
 }
 public static function hook_post_deleted($post_id)
 {
     if (get_option('mainwp_linkschecker_ext_enabled') !== "Y") {
         return;
     }
     if (!defined('BLC_ACTIVE') || !function_exists('blc_init')) {
         return;
     }
     blc_init();
     //Get the container type matching the type of the deleted post
     $post = get_post($post_id);
     if (!$post) {
         return;
     }
     //Get the associated container object
     $post_container = blcContainerHelper::get_container(array($post->post_type, intval($post_id)));
     if ($post_container) {
         //Delete it
         $post_container->delete();
         //Clean up any dangling links
         blc_cleanup_links();
     }
 }
Example #9
0
 function hook_comment_status($new_status, $old_status, $comment)
 {
     $container = blc_get_container(array($this->container_type, $comment->comment_ID));
     if ($new_status == 'approved') {
         $container->mark_as_unsynched();
     } else {
         $container->delete();
         blc_cleanup_links();
     }
 }
Example #10
0
/**
 * (Re)create synchronization records for all containers and mark them all as unparsed.
 *
 * @param bool $forced If true, the plugin will recreate all synch. records from scratch.
 * @return void
 */
function blc_resynch($forced = false)
{
    global $wpdb, $blclog;
    if ($forced) {
        $blclog->info('... Forced resynchronization initiated');
        //Drop all synchronization records
        $wpdb->query("TRUNCATE {$wpdb->prefix}blc_synch");
    } else {
        $blclog->info('... Resynchronization initiated');
    }
    //(Re)create and update synch. records for all container types.
    $blclog->info('... (Re)creating container records');
    blc_resynch_containers($forced);
    //Delete invalid instances
    $blclog->info('... Deleting invalid link instances');
    blc_cleanup_instances();
    //Delete orphaned links
    $blclog->info('... Deleting orphaned links');
    blc_cleanup_links();
    $blclog->info('... Setting resync. flags');
    blc_got_unsynched_items();
    //All done.
    $blclog->info('Database resynchronization complete.');
}