Problem with upgrade to 3.5.0

Post Reply
andyfrizzle
Jr. Bludit
Posts: 4
Joined: Fri Dec 14, 2018 3:35 pm

Fri Dec 14, 2018 4:50 pm

I have recently upgraded from 2.3.4 to 3.5.0.

I have everything working on the upgraded Bludit other than a search facility (gleaned prevoiusly from Bludit related posts), that posts to a 'search.php' page.

I have an older copy of the site working at http://www.spiccato.co.uk/bludit/livesite/

The search facility is located at the bottom of the sidebar.

I cannot work out why this is not working in 3.5.0, but i think that the structure of the databases have changed in the changes to recent versions of Bludit.

I have included a copy of the page php code to see if anyone can recognise a simple correction/amendment.

Code: Select all

<?php
/**
 * Mandatory Bludit Stuff. session_start() is called by init.php, Hence this is placed at the top before
 * content.
 */
/* Set PHP max execution time to infinite, since it loops through all files and may take a lot of time if
   number of total pages is too high */
ini_set('max_execution_time', 0);
// Load time init
$loadTime = microtime(true);
// Security constant
define('BLUDIT', true);
// Directory separator
define('DS', DIRECTORY_SEPARATOR);
// PHP paths for init
define('PATH_ROOT', __DIR__.DS);
define('PATH_BOOT', PATH_ROOT.'bl-kernel'.DS.'boot'.DS);
// Init
require(PATH_BOOT.'init.php');
?>
<!DOCTYPE html>

<!-- new head content starts here -->
<!-- <html> -->
<!-- <head> -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Search Results Page | Downunderide</title>
<meta name="description" content="Search Page">
<link rel="shortcut icon" href="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/favicon.png" type="image/png">
<link rel="stylesheet" href="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/css/main.css">
<noscript><link rel="stylesheet" type="text/css" href="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/css/noscript.css">
</noscript><link rel="stylesheet" type="text/css" href="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/css/bludit.css">
</head>

<div id="wrapper">

	<!-- Main -->
	<div id="main">
	<div class="inner">


<header id="header">
			<a href="http://www.downunderide.com/" class="logo">
				<strong>Downunderide</strong> The antipodean odyssey of Jen and Andy 			</a>
			<ul class="icons">
				<!-- <li><a href="http://www.downunderide.com/sitemap.xml" class="icon fa-sitemap"><span class="label">Sitemap</span></a></li>			</ul>  -->
		</header>
		
		<section>
		    
		 <header class="main">
       <!-- Title of the page -->
                
                <h1>Search Results</h1>
        </header>


<?php
// Config
$minimumCharactersAllowed = 3; // Set minimum characters required for search
$searchUsername = true;
$searchDescription = true;
$enableRecursiveSearch = true; // Recusively search by exploding "hello world" to "hello", "world"
$enableStopWordFilter = true; // Only for recursive search tokens
// Use InnoDb Stopwords https://mariadb.com/kb/en/mariadb/stopwords/
$stopWordsFilter = array('a', 'about', 'an', 'are', 'as', 'at', 'be', 'by', 'com', 'de', 'en', 'for', 'from', 'how', 'i', 'in', 'is', 'it', 'la', 'of', 'on', 'or', 'that', 'the', 'this', 'to', 'was', 'what', 'when', 'where', 'who', 'will', 'with', 'und', 'the', 'www');
// Initialise Variables like search count
$entryCount = 0;
$resultCount = 0;
$resultsArray = array();
$pagesDb = PATH_DATABASES . 'pages.php';
$validTokensArray = array();
// Remove first line function. Helper function to remove string "<?php defined('BLUDIT')" from db
// Thanks ComFreek http://stackoverflow.com/questions/7740405/php-delete-the-first-line-of-a-text-and-return-the-rest
function stripFirstLine($text)
{
    return substr( $text, strpos($text, "\n")+1 );
}
// START Search function:
// Usage Example:
// search($pagesDb, "pages");
// displayResults() must be called after using this function.
// Score system:
// title		+15 points
// tags			+10 points
// description	+5 points
// username		+3 points
// content		+1 point each line
// If recusiveSearch is enabled. Divide each by 20.
function search($db, $contentType)
{
    // Variable scope fix
    global $entryCount, $resultCount, $searchQuery, $resultsArray, $searchUsername, $searchDescription, $minimumCharactersAllowed, $enableRecursiveSearch, $enableStopWordFilter, $stopWordsFilter, $validTokensArray;
    // Reject Query if it's length is too small
    if (strlen($searchQuery) < $minimumCharactersAllowed) {
        echo "<p class='error'>ERROR: Too few characters. Add more characters to widen search.</p>";
        exit;
    }
    /**
     * contentType Check: Reject Query if function is called in the wrong way
     */
    $validTypes = array("pages");
    if (in_array($contentType, $validTypes) == false) {
        echo "<p class='error'>ERROR: Invalid search function parameter.</p>";
        exit;
    }
    // Break the query into several words
    $tokensArray = explode(' ', $searchQuery);
    $enoughTokensAvailable = false;
    foreach ($tokensArray as $token) {
        $trimmedToken = trim($token);
        if (!empty($trimmedToken) && strlen($trimmedToken) >= $minimumCharactersAllowed) {
            // Do not add if exists in stopWordFilter
            if (in_array($trimmedToken, $stopWordsFilter) && $enableStopWordFilter) {
                // Do nothing
            } else {
                // Add to valid array
                array_push($validTokensArray, $trimmedToken);
            }
        }
    }
    // Get Unique values
    $validTokensArray = array_unique($validTokensArray);
    // print_r($validTokensArray);
    // Set a variable called enoughTokensAvailable. Use recursive search only when this is true.
    if (count($validTokensArray) > 0) {
        $enoughTokensAvailable = true;
    }
    $json = stripFirstLine(file_get_contents($db));
    $json = json_decode($json);
    //print_r($json);
    foreach ($json as $obj => $values) {
        // Initialise
        $score = 0;
        $results = array();
        //echo $obj . "<br>"; if you need the key
        // Variables to search
        $description = $values->description;
        $username = $values->username;
        $tagsArray = (array)$values->tags;
        $tagsString = implode(" ", $tagsArray);
        $contentPath = "bl-content/" . $contentType . "/" . $obj . DS . FILENAME; // index.txt
        // Thanks http://stackoverflow.com/questions/4521936/quickest-way-to-read-first-line-from-file
        $title = fgets(fopen($contentPath, 'r'));
        // Search Description
        if ($searchDescription && stripos($description, $searchQuery)!==false) {
            array_push($results, "<b>Description:</b>" . htmlspecialchars($description) . "<br>");
            $score += 5;
        }
        // Recursive Search Description
        if ($enableRecursiveSearch && $enoughTokensAvailable) {
            foreach ($validTokensArray as $token) {
                if ($searchDescription && stripos($description, $token)!==false) {
                    array_push($results, "<b>Description:</b>" . htmlspecialchars($description) . "<br>");
                    $score += 0.25;
                }
            }
        }
        // End Recusive Search Description
        // Search Username
        //if ($searchUsername && stripos($username, $searchQuery)!==false) {
        //    array_push($results, "<b>Username:</b>" . htmlspecialchars($username) . "<br>");
        //    $score += 3;
        //}
        // Recursive Search Username
        //if ($enableRecursiveSearch && $enoughTokensAvailable) {
        //    foreach ($validTokensArray as $token) {
        //        if ($searchUsername && stripos($username, $token)!==false) {
        //            array_push($results, "<b>Username:</b>" . htmlspecialchars($username) . "<br>");
        //           $score += 0.15;
        //      }
        //    }
        //}
        // End Recusive Search Username
        // Search Tags
        if (stripos($tagsString, $searchQuery)!==false) {
            array_push($results, "<b>Tag:</b>" . htmlspecialchars($tagsString) . "<br>");
            $score += 10; // Tag found
        }
        // Recursive Search Tags
        if ($enableRecursiveSearch && $enoughTokensAvailable) {
            foreach ($validTokensArray as $token) {
                if (stripos($tagsString, $token)!==false) {
                    array_push($results, "<b>Tag:</b>" . htmlspecialchars($tagsString) . "<br>");
                    $score += 0.5; // Tag found
                }
            }
        }
        // End Recusive Search Tags
        // Search Title
        if (stripos($title, $searchQuery)!==false) {
            array_push($results, "<b>Title:</b> Search term found.<br>");
            $score += 15;
        }
        // Recursive Search Title
        if ($enableRecursiveSearch && $enoughTokensAvailable) {
            foreach ($validTokensArray as $token) {
                if (stripos($title, $token)!==false) {
                    array_push($results, "<b>Title:</b> Search term found.<br>");
                    $score += 0.75;
                }
            }
        }
        // End Recusive Search Title
        // Search Content
        // Case insensitive search
        // Thanks ghbarratt's | http://stackoverflow.com/questions/8032312/find-specific-text-in-multiple-txt-files-in-php
        foreach (file($contentPath) as $fli => $fl) {
            if (stripos($fl, $searchQuery)!==false) {
                array_push($results, '<b>Line ' . ($fli+1) . "</b> " . htmlspecialchars($fl) . "<br>");
                $score += 1; // Content found
            }
            // Recursive content search
            if ($enableRecursiveSearch && $enoughTokensAvailable) {
                foreach ($validTokensArray as $token) {
                    if (stripos($fl, $token)!==false) {
                        array_push($results, '<b>Line ' . ($fli+1) . "</b> " . htmlspecialchars($fl) . "<br>");
                        $score += 0.05; // Content found
                    }
                }
            }
            // End Recusive content search
        }
        // Since we use recursive search, there might be duplicate records. Below is to clean it.
        $results = array_unique($results);
        // Increment search pages count
        ++$entryCount;
        // Add to results only if score is not 0
        if ($score != 0) {
            array_push($resultsArray, array("slug"=>$obj, "type"=>$contentType, "results"=>$results, "score"=>$score));
            ++$resultCount;
        }
    }
    // Reverse sort based on score...
    // Thanks http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value
    // Slightly modified.
    // PHP 5 version
    // WARNING: Floats are not sorted properly in the PHP 5 function. Avoid using this.
    usort($resultsArray, function($a, $b) {
     	return $b['score'] - $a['score'];
    // });
    // PHP 7+ only. Requires spaceship operator. Works fine.
    //usort($resultsArray, function ($a, $b) {
    //    return $b['score'] <=> $a['score'];
    });
    // For debugging
    // echo "<pre>"; print_r($resultsArray); echo "</pre>";
    return 0;
}
// END Search function
// Display Results Function -> Display the array in a pretty way.
function displayResults()
{
    // Variable Scope fix.
    global $resultsArray, $Site , $searchQuery, $validTokensArray;
    // For debug purpose
    // echo "<pre>"; print_r($validTokensArray); echo "</pre>";
    foreach ($resultsArray as $keys => $values) {
        echo "<div class='result'>";
        if ($values["type"]==="pages") {
            echo "<p class='title'>Search Result: <a href='" . $Site->url() . $values["slug"] . "'>" . $values["slug"] . "</a></p>"; // <small> Relevancy Score: {$values['score']}</small>
        } else {
            echo "<p class='error'>ERROR: Invalid search function parameter.</p>";
            exit;
        }
        // Add highlights. HTML tags are not highlighted...
        $tmp = $values["results"];
        $tmpWithHighlights = array();
        foreach ($tmp as $tmpResult) {
            $highlighted = str_ireplace($searchQuery, "<span class='highlight'>$searchQuery</span>", $tmpResult);
            /* Highlight tokens if enabled */
            /* Note: If there are too many tokens, highlight is sometimes buggy. Fix later. Not a big deal.*/
            if (!empty($validTokensArray)) {
                foreach ($validTokensArray as $token) {
                    $highlighted = str_ireplace($token, "<span class='highlight'>$token</span>", $highlighted);
                }
            }
            array_push($tmpWithHighlights, $highlighted);
        }
        $tmpWithHighlights = implode(' ', $tmpWithHighlights);
        echo "<p>$tmpWithHighlights</p>"   ;
        echo "</div>";
    }
}
echo "<form method='get' action='". htmlspecialchars($_SERVER['PHP_SELF']) . "'>
<input type='text' name='q' />
<input type='submit' value='Search'></form>";
if (isset($_GET['q']) && $_GET['q'] != '') {
    $searchQuery = trim($_GET['q']); // Remove spaces from beginning and end.
    // Prevent XSS: htmlspecialchars()
    echo "<p>Search term: <b>" . htmlspecialchars($searchQuery) . "</b></p>";
    search($pagesDb, "pages");
    displayResults();
    // Display count and wall clock time taken to find results.
    echo "\n<p>" . $resultCount . " result(s) found. (" . round((microtime(true) - $loadTime), 2) . " seconds) $entryCount entries searched." ;
}
?>

<!-- Sidebar -->
	<div id="sidebar">
		<div class="inner">
		<img src="http://www.downunderide.com/bl-content/uploads/DownUnderRideLogo.png" width="50" />
<div class="plugin plugin-pages"><h2 class="plugin-label">Return to Home Page</h2><div class="plugin-content"><ul><li><a href="http://www.downunderide.com">Home page</a></li></ul></div></div>
<!-- Search -->
<div class="plugin plugin-pages"><h2 class="plugin-label">Search</h2><div class="plugin-content"><ul><li>
<form role="search" action="http://www.downunderide.com/search.php">
	<input type="text" name="q" placeholder="Search...">
</form></div></div>

<!-- Footer -->
<footer id="footer">
        <p class="copyright">Copyright Downunderide © 2016-2018<br>Theme: <a href="https://html5up.net">HTML5 UP</a><br>Site Engine: <a href="https://www.bludit.com">BLUDIT</a></p>
</footer>		</div>
	</div>

</div>

<!-- Scripts -->
<script src="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/js/jquery.min.js"></script>
<script src="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/js/skel.min.js"></script>
<script src="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/js/util.js"></script>
<!--[if lte IE 8]><script src="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/js/ie/respond.min.js"></script>
<![endif]--><script src="http://www.downunderide.com/bl-themes/editorial-editorial-v2.1/assets/js/main.js"></script>


</body>
</html>

All and any help would be appreciated.

many thanks

Andy
User avatar
Edi
Site Admin
Posts: 1318
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Fri Dec 14, 2018 5:38 pm

With which version runs the older site?
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
andyfrizzle
Jr. Bludit
Posts: 4
Joined: Fri Dec 14, 2018 3:35 pm

Fri Dec 14, 2018 5:52 pm

V 2.3.4
User avatar
Edi
Site Admin
Posts: 1318
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Fri Dec 14, 2018 6:32 pm

I would upgrade in two steps.

1. Upgrade from v.2.3.4 to v3.4 (with the migration tool):

https://blog.bludit.com/bludit-3-4-0

2. Upgrade from v.3.4 to 3.5:

https://docs.bludit.com/en/getting-star ... rade-guide
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
anaggh
Jr. Bludit
Posts: 9
Joined: Sat Aug 27, 2016 9:05 am

Fri Dec 14, 2018 7:17 pm

Yes, you are right. The db structure has changed and the search.php file you are currently using will not work in Bludit v3.

You have 2 options to fix search:

1. Delete search.php in Bludit v3 and use the new native search plugin by activating it from Admin Dashboard -> Plugins.
2. Delete search.php and use the one released for v3 (Don't forget to copy searchLib.php file too) -> https://github.com/anaggh/Bludit-Search
andyfrizzle
Jr. Bludit
Posts: 4
Joined: Fri Dec 14, 2018 3:35 pm

Fri Dec 14, 2018 7:22 pm

Thanks for the suggestions. I will try them out and update.

Many thanks.

Andy
andyfrizzle
Jr. Bludit
Posts: 4
Joined: Fri Dec 14, 2018 3:35 pm

Fri Dec 14, 2018 7:31 pm

Updated files uploaded as signposted by anaggh :D :D :D :D :D

Search now functioning correctly. :D :D :D :D :D

Just some reformatting of search.php now needed to blend in with current live site.

many thanks for all help and advice.

This thread can now be closed/solved

Andy Frizzle
Post Reply