Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter bobiasg

    (@bobiasg)

    Hi,

    For cron job, I make it:
    1. Add function relevanssi_build_index_cron.

    function relevanssi_build_index_cron($offset, $count, $extend = true) {
    	global $wpdb, $relevanssi_table;	
    
    		set_time_limit (1200);
    
    		$type = get_option("relevanssi_index_type");
    		switch ($type) {
    			case "posts":
    				$restriction = " AND post_type = 'post'";
    				break;
    			case "pages":
    				$restriction = " AND post_type = 'page'";
    				break;
    			case "both":
    			default:
    				$restriction = "";
    		}		
    
    		$remaining = 0;
    		$exists = $wpdb->get_var( "SELECT COUNT(DISTINCT(doc)) FROM $relevanssi_table");
    		$total = $wpdb->get_var( "SELECT COUNT(DISTINCT {$wpdb->posts}.ID) FROM {$wpdb->posts} WHERE post_status='publish'" . $restriction );		
    
    		$n = 0;
    
    		if ( $total > 0 && $offset < $total) {
    			// More posts to index
    			$relative = $offset;
    
    			if (!$extend) {
    				// truncate table first
    				$wpdb->query("TRUNCATE TABLE $relevanssi_table");
    				$q = "SELECT ID, post_content, post_title
    				FROM $wpdb->posts WHERE post_status='publish'" . $restriction . "  LIMIT $relative,$count ";
    				update_option('relevanssi_index', '');
    				$exists = 0;
    			}
    			else {
    				// extending, so no truncate and skip the posts already in the index
    				$q = "SELECT ID, post_content, post_title
    				FROM $wpdb->posts WHERE post_status='publish' AND
    				ID NOT IN (SELECT (doc) FROM $relevanssi_table)" . $restriction  . "  LIMIT $relative,$count ";
    			}
    
    			$custom_fields = relevanssi_get_custom_fields();
    
    			$content = $wpdb->get_results($q);
    
    			foreach ($content as $post) {
    				$n += relevanssi_index_doc($post, false, $custom_fields);
    				// n calculates the number of insert queries
    			}
    
    			$remaining = $total - count( $content) - $offset - $exists;
    		}
    
    		if ($remaining == 0)
    			update_option('relevanssi_indexed', 'done');
    }

    2. cronfile.php

    <?php
    global $wpdb;
    if(!is_object($wpdb))
    {
        ob_start();
        require_once(realpath('../../../wp-load.php'));
        ob_end_clean();
    }
    /*
    $check = get_option('search_engine_token');
    if(!isset($_GET['token'])||$_GET['token']!=$check)
        die('Invalid Token');
    */
    require_once "relevanssi.php";
    
    relevanssi_build_index_cron(0, 1000, true);
    ?>

    3. bat file:

    "C:\Program Files\PHP\php-cgi.exe" cronjob.php

    Thread Starter bobiasg

    (@bobiasg)

    Hi msaari,

    For ajax indexing, you can do like this (I modify base on Search-unleashed):

    1. Add action :

    add_action( 'wp_ajax_relevanssi_index', 'relevanssi_build_index_ajax' );
    add_action( 'wp_print_scripts','relevanssi_print_scripts' );
    add_action( 'admin_head','relevanssi_admin_head' );

    2. build function relevanssi_build_index_ajax, relevanssi_print_scripts, relevanssi_admin_head.
    My code

    function relevanssi_build_index_ajax() {
    	global $wpdb, $relevanssi_table;
    	$extend = false;
    
    	$ajax_index = check_ajax_referer( 'relevanssi-index', false, false );
    	$ajax_continue_index = check_ajax_referer( 'relevanssi-continue-index', false, false );
    
    	if ($ajax_continue_index)
    		$extend = true;
    
    	if ( current_user_can( 'administrator' ) && ($ajax_index || $ajax_continue_index) ) {
    		ob_start();
    
    		$offset = intval( $_POST['offset'] );
    		$count = intval( $_POST['limit'] );
    
    		$type = get_option("relevanssi_index_type");
    		switch ($type) {
    			case "posts":
    				$restriction = " AND post_type = 'post'";
    				break;
    			case "pages":
    				$restriction = " AND post_type = 'page'";
    				break;
    			case "both":
    			default:
    				$restriction = "";
    		}		
    
    		$remaining = 0;
    		$exists = $wpdb->get_var( "SELECT COUNT(DISTINCT(doc)) FROM $relevanssi_table");
    		$total = $wpdb->get_var( "SELECT COUNT(DISTINCT {$wpdb->posts}.ID) FROM {$wpdb->posts} WHERE post_status='publish'" . $restriction );		
    
    		$n = 0;
    
    		if ( $total > 0 && $offset < $total) {
    			// More posts to index
    			$relative = 0;// $offset;
    
    			if (!$extend) {
    				// truncate table first
    				$wpdb->query("TRUNCATE TABLE $relevanssi_table");
    				$q = "SELECT ID, post_content, post_title
    				FROM $wpdb->posts WHERE post_status='publish'" . $restriction . "  LIMIT $relative,$count ";
    				update_option('relevanssi_index', '');
    
    				$exists = 0;
    			}
    			else {
    				// extending, so no truncate and skip the posts already in the index
    				$q = "SELECT ID, post_content, post_title
    				FROM $wpdb->posts WHERE post_status='publish' AND
    				ID NOT IN (SELECT (doc) FROM $relevanssi_table)" . $restriction  . "  LIMIT $relative,$count ";
    			}
    
    			$custom_fields = relevanssi_get_custom_fields();
    
    			$content = $wpdb->get_results($q);
    			foreach ($content as $post) {
    				$n += relevanssi_index_doc($post, false, $custom_fields);
    				// n calculates the number of insert queries
    			}
    
    			$remaining = $total - count( $content) - $offset - $exists;
    		}
    
    		ob_end_clean();
    
    		$percent = 100;
    		if ($total > 0)
    			$percent = ( ( $total - $remaining ) / $total ) * 100;
    
    		if ( $remaining > 0 )
    			echo sprintf('%d %d%% ', $remaining, $percent).sprintf( __( '%d of %d / %d%%', 'relevanssi'), $total - $remaining, $total, $percent );
    		else {
    			echo '0 100% '.__('Finished!', 'relevanssi');
    		}	
    
    		if ($remaining == 0)
    			update_option('relevanssi_indexed', 'done');
    
    		die();
    	}
    }
    function relevanssi_print_scripts() {
    	if ( isset($_GET['page']) && $_GET['page'] == 'relevanssi/'.basename( __FILE__ ) ) {
    		wp_enqueue_script( 'relevanssi', plugins_url('/js/admin.js',__FILE__), array( 'jquery-form' ), '1.7.2' );
    	}
    }
    function relevanssi_admin_head() {
    		if ( isset($_GET['page']) && $_GET['page'] == 'relevanssi/'. basename( __FILE__ ) )
    			echo '<link rel="stylesheet" href="'. plugins_url('admin.css',__FILE__) .'" type="text/css" media="screen" title="no title" charset="utf-8"/>';
    	}

    3. Add javascript in function relevanssi_options_form:

    <div id="wrapper" style="display: none"></div>
    <script type="text/javascript" charset="utf-8">
    
    		jQuery(function() {
    			jQuery('#wrapper').Progressor( {
    				start:    jQuery('input[name=continueindex]'),
    				cancel:   '<?php _e( 'Cancel', 'relevanssi' ); ?>',
    				url:      '<?php echo admin_url('admin-ajax.php') ?>',
    				nonce:    '<?php echo wp_create_nonce ('relevanssi-continue-index')?>',
    				finished: '<?php _e( 'Finished!', 'relevanssi'); ?>'
    			});
    		});
    	</script>

    admin.js

    (function($){
     $.fn.Progressor = function(args){
    	args = args || {};
    
    	return this.each(function() {
    		// Store value in a cookie
    		function timer() {
    		  if (running) {
      		  $.ajax({
      		    url: opts.url,
      		    cache: false,
      		    data: ({ action: 'relevanssi_index', offset: offset, limit: opts.limit, _ajax_nonce: opts.nonce }),
      		    type: 'POST',
      		    error: function() {
    					alert('There is an error');
    				  },
      		    success: function(data) {
      		      var parts   = data.split(' ');
      		      var left    = parseInt(parts.shift());
      		      var percent = parts.shift();
      		      var message = parts.join(' ')
    
    		        $(wrapper).find('p').html(message);
    		        $('#' + opts.inside).css('width', percent);
    
      		      if (left > 0) {
      		        // More to come
      		        offset += opts.limit;
              		setTimeout(timer, 0);
      		      }
      		      else {
      		        // Finished
    		          $(opts.start).val(original);
            		  $(opts.loading).hide();
      		        running = false;
      		      }
      		    }
      		  });
      		}
    		}
    
        // Load values from args and merge with defaults
        var opts = $.extend({
          cancel: 'Cancel',
          inside: 'inner',
          limit: 20,
          nonce: '',
          loading: '#loading'
        }, args);
    
    		var offset  = 0;
    		var running = false;
    		var wrapper = this;
    		var original = $(opts.start).val();
    
    		$(opts.start).click(function() {
    		  var button = this;
    
    		  if (running) {
    		    // Cancel
    		    running = false;
    		    $(button).val(original);
      		  $(opts.loading).hide();
    		  }
    		  else {
    		    offset = 0;
      		  running = true;
    
      		  // Hide the button
      		  $(button).val(opts.cancel);
      		  $(opts.loading).show();
    
      		  // Setup the progress bar
      		  $(wrapper).empty();
      		  $(wrapper).append('<div id="' + opts.inside + '"></div><p></p>');
      		  $(wrapper).fadeIn();
            $('#' + opts.inside).css('width', '0px');
    
        		// Now kick-start a timer to perform the progressor
        		setTimeout(timer, 0);
        	}
    
      		return false;
    		});
    	  });
      };
    })(jQuery);
    
    function highlight (index,item)
    {
      jQuery('#color_' + index).css ('backgroundColor', '#' + item.value);
    }

    admin.css

    #wrapper, #wrapper2
    {
    border: 1px solid #666;
    height: 2em;
    position: relative;
    width: 70%;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    margin: 0 auto;
    }
    
    #wrapper p, #wrapper2 p
    {
    margin: 0;
    padding: 0;
    padding-top: 0.3em;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    text-align: center;
    font-weight: bold;
    }
    
    #inner
    {
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    width: 10px;
    height: 2em;
    background-color: #ccc;
    }
    
    #status
    {
    position: absolute;
    top: 0;
    left: 0;
    text-align: center;
    color: #444;
    width: 100%;
    }

    Hi All,

    In function relevanssi_remove_punct, statement “$a = mb_ereg_replace(‘:punct:+’, ‘ ‘, $a);” is error. I change it to “$a = mb_ereg_replace(‘/:punct:+/u’, ‘ ‘, $a);”

    Ex: when $str = “khương”, relevanssi_remove_punct will be return “khư� ng” instead “khương”.

    Thanks

    Sorry about my english

    I found a solution for this problem.

    In wp-admin/edit-page.php, wp-admin/edit.php. It have code below:

    :::line 160

    $sendback = wp_get_referer();
    
    	if (strpos($sendback, 'post.php') !== false) $sendback = get_option('siteurl') .'/wp-admin/post-new.php';
    	elseif (strpos($sendback, 'attachments.php') !== false) $sendback = get_option('siteurl') .'/wp-admin/attachments.php';
    	$sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
    	wp_redirect($sendback);
    	exit();

    function wp_get_referer() return false if $_REQUEST[‘_wp_http_referer’] or $_SERVER[‘HTTP_REFERER’] same $_SERVER[‘REQUEST_URI’]. Then, wp_redirect return false on $sendback as is empty. No page to redirect, and white page return to browser.

    My solution:

    Add code below line $sendback = wp_get_referer();

    $sendback = wp_get_referer();
    	// fix error after delete
    	// return false if same RequestUri
    	if ($sendback === false) $sendback = $_SERVER['REQUEST_URI'];

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)