1

Topic: Search engine does not work after upgrade

Hello,

I've just updated my WolfCMS installation from old 0.7.5 to the newest 0.8.2.
Everythink works fine, except search engine.

My search form:

<div class="search-form">
    <form action="<?php echo URI_PUBLIC; ?>search-results<?php echo URL_SUFFIX;?>" method="post">
        <input type="text" name="search" class="text" value="<?php echo ( isset($_POST['search']) ) ? strip_tags($_POST['search']) : '' ;?>">
        <input type="submit" name="searchsubmit" value="Search" class="button">
    </form>
</div>

and now when I am trying to search something, I am getting an error message:

Fatal error: Cannot access private property Page::$parent in /home/users/netapp/public_html/wolf/app/models/Page.php(382) : eval()'d code on line 24

I will appreciate any help.

--
Piotr

2

Re: Search engine does not work after upgrade

What code are you using?
I like to test in on my Wolf installation to see what is causing it.

3

Re: Search engine does not work after upgrade

Hi Piotr! Good to see you. smile

If you're using the system described in the wiki, then the problem is likely not the form, but the "snippet". Try replacing yours with the one that is now in the wiki -- if you do a "diff" between page versions in the wiki, you'll see that it needed adjustments for 0.7.7.

Also, Tina's Site Search plugin works well, if you want to experiment with that.

Hope this helps!

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

4

Re: Search engine does not work after upgrade

Hello,

David: Yes, I am using the snippet which is (or was) on the wiki.
Nice to see you also :-)

Svanlare: Here is my old snippet which works fine on older version of Wold:

<?php

$searchcriteria = '';

if (isset($_POST['search'])) {
    $searchcriteria = $_POST['search'];
}
else if (isset($_GET['search'])) {
    $searchcriteria = $_GET['search'];
}

function is_in_array($str, $array) {
    return preg_grep('/^' . preg_quote($str, '/') . '$/i', $array);
}

function snippet_sitesearch($parent, $searchcriteria) {
    $exclude = array("search-results");
    $mysearch = html_encode($searchcriteria);
    $childs = $parent->children();
    $out = '';

    if (count($childs) > 0){
        foreach ($childs as $child) {
            if(!in_array($child->parent->slug,$exclude)) {
                $myparent = snippet_sitesearch($child, $searchcriteria);
                $mylink = $child->link();
                $url_segments = explode('/', $child->url);
                $url_seg_count = count($url_segments);

                if($url_seg_count > '1')
                    $mycrumb = ucfirst(str_replace('-',' ',$child->parent->breadcrumb)).' / '.$child->breadcrumb;
                else
                    $mycrumb = $child->breadcrumb;

                if( stristr($child->content(),$mysearch) || stristr($child->title(),$mysearch) || is_in_array($mysearch,$child->tags()) ){
                    $out .= '<li><strong><a href="'.$child->url.URL_SUFFIX.'">'.$mycrumb.'</a></strong></li>';
                }
                $out .= snippet_sitesearch($child, $searchcriteria);
            }
        }
    }
    $out = str_replace("<li><li>", "<li>", $out);
    $out = str_replace("</li></li>", "</li>", $out);
    return $out;
}

?>


<div id="listing">
<?php
$searchdata = "";
$parent = $this->find('/');

$searchdata .= snippet_sitesearch($parent, $searchcriteria);

$occurrences = substr_count($searchdata,"<li>");
if($searchdata != NULL){
    $results = ($occurrences > '1') ? "pages" : "page" ;
    echo "<p>Searched string: <strong>" . $searchcriteria . "</strong>, was found in links $results:</p>";
    echo "<ol>";
    echo $searchdata;
    echo "</ol>";
} else {
    echo "<p>Searched for: <strong>" . $searchcriteria . "</strong></p>";
    echo "<p>Found nothing.</p>";
}
?>
</div>

Then I've changed above snippet for the new one from Wiki, and now I am getting a lots of errors but below the errors I can see the seatch results which seems to be ok.

Here are some of the errors:

Notice: Undefined variable: out in /home/users/netapp/public_html/wolf/app/models/Page.php(382) : eval()'d code on line 29

Notice: Undefined variable: mychild in /home/users/netapp/public_html/wolf/app/models/Page.php(382) : eval()'d code on line 22

Notice: Undefined variable: out in /home/users/netapp/public_html/wolf/app/models/Page.php(382) : eval()'d code on line 23

Notice: Undefined variable: out in /home/users/netapp/public_html/wolf/app/models/Page.php(382) : eval()'d code on line 29

Notice: Undefined variable: mychild in /home/users/netapp/public_html/wolf/app/models/Page.php(382) : eval()'d code on line 22
.....

Any idea was can casue the errors?

Last edited by tikky (2015-04-03 12:28)

--
Piotr

5

Re: Search engine does not work after upgrade

This should work without error:

<?php

$searchcriteria = '';

if (isset($_POST['search'])) {
    $searchcriteria = $_POST['search'];
}
else if (isset($_GET['search'])) {
    $searchcriteria = $_GET['search'];
}

function is_in_array($str, $array) {
    return preg_grep('/^' . preg_quote($str, '/') . '$/i', $array);
}

function snippet_sitesearch($parent, $searchcriteria) {
    $exclude = array("search-results");
    $mysearch = html_encode($searchcriteria);
    $childs = $parent->children();
    $out = '';

    if (count($childs) > 0){
        foreach ($childs as $child) {
            if(!in_array($child->parent()->slug,$exclude)) {
                $myparent = snippet_sitesearch($child, $searchcriteria);
                $mylink = $child->link();
                $url_segments = explode('/', $child->url());
                $url_seg_count = count($url_segments);

                if($url_seg_count > '1')
                    $mycrumb = ucfirst(str_replace('-',' ',$child->parent()->breadcrumb)).' / '.$child->breadcrumb;
                else
                    $mycrumb = $child->breadcrumb;

                if( stristr($child->content(),$mysearch) || stristr($child->title(),$mysearch) || is_in_array($mysearch,$child->tags()) ){
                    $out .= '<li><strong><a href="'.$child->url().'">'.$mycrumb.'</a></strong></li>';
                }
                $out .= snippet_sitesearch($child, $searchcriteria);
            }
        }
    }
    $out = str_replace("<li><li>", "<li>", $out);
    $out = str_replace("</li></li>", "</li>", $out);
    return $out;
}

?>


<div id="listing">
<?php
$searchdata = "";
$parent = $this->find('/');

$searchdata .= snippet_sitesearch($parent, $searchcriteria);

$occurrences = substr_count($searchdata,"<li>");
if($searchdata != NULL){
    $results = ($occurrences > '1') ? "pages" : "page" ;
    echo "<p>Searched string: <strong>" . $searchcriteria . "</strong>, was found in links $results:</p>";
    echo "<ol>";
    echo $searchdata;
    echo "</ol>";
} else {
    echo "<p>Searched for: <strong>" . $searchcriteria . "</strong></p>";
    echo "<p>Found nothing.</p>";
}
?>
</div>

6

Re: Search engine does not work after upgrade

Thank you svanlaere.

Results now are displayed correctly, without any errors.
However links are not working because they have doubled extension: ".html.html".

As a bypass I have added extra line of code to filter:

$out = str_replace(".html.html", ".html", $out);

Not sure if my solution is ok, but it works fine.


BTW: What was the problem with my old code?
Why the code from Wiki causes errors?

Last edited by tikky (2015-04-03 15:04)

--
Piotr

7

Re: Search engine does not work after upgrade

tikky wrote:

Thank you svanlaere.

Results now are displayed correctly, without any errors.
However links are not working because they have doubled extension: ".html.html".

As a bypass I have added extra line of code to filter:

$out = str_replace(".html.html", ".html", $out);

Not sure if my solution is ok, but it works fine.


BTW: What was the problem with my old code?
Why the code from Wiki causes errors?

Some changes in Wolf CMS:
$foo->parent->slug changed into $foo->parent()->slug
$foo->url changed into $foo->url()

$foo->url() returns the prefix by default, so just remove .URL_SUFFIX in the snippet.
$foo->url(false) will return the url without a prefix

You can use the previously posted snippet since i've edited it.