Jump to content

Add an allowed character to default seach form. [solved]


Recommended Posts

Hello, and thanks again for PW. The more I use it, the more I like it.

I have a silly problem, and since I have tested several (bad) solutions, I come and ask for help. I think I'm just being thick there...

Is there a relatively simple way to add an allowed character to the default search form's sanitizer ?

FYI, I'm working on a dictionary in breton language (60k entries), and I must find a way to allow apostrophes in search input. (In breton language, "CH" and "C'H" are actually two different letters altogether).

I must say I'm a bit confused by the sanitizer. On the regular search form, it appears three times. Once in the input field itself, and twice on the search template.

I did read the sanitizer page and sanitizer class page, but I'm not sure to understand where to go from there. From what I understand, the


methods would accept some extra allowed character, but I seem to have no luck in adding this to my template. Morevover, do I have to change any sanitizer call to a -> name one ?

The sanitizer php core page includes an "allowedASCII" which seems very promising, but is there a way to override it ? Hooks ? (not used to them I'm affraid...)

Morevover, the fact that i'm trying to implement the (') character worries my a bit. Can I escape it ? would something like '\'' do the trick ?

Thank in advance to you, good men. The site building is reaching an end, and I am tired indeed. (you wouldn't believe the state of the initial DB I had to work from...)

Link to comment
Share on other sites

I don't think sanitizers will be the kicker here, but rather which characters are supported by processwire's selector engine and/or mysql's handling of those characters. I'm not sure, but $sanitizer->selectorValue might give at least some insights.

Link to comment
Share on other sites

Well... Thank you for the tip. Might well be, however, I have been looking at selectorValue, and it actually seems to handle apostrophes. (I might be wrong here)

However, Alice would say thing are getting curiouser and curiouser... If I echo my $q and $selector on the search page, and test with a "c'hai", for instance, I do get "c'hai" printed. So the apostrophe might indeed survive the sanitizers.

However, when I try to echo a

$pagechai = $pages->get("title=c'hai");
echo "<p>test appel:{$pagechai->name}</p>";

i get nothing, even if I try a title=c\'hai

Hmmm... Maybe this has some deeper root than a simple sanitizer. Does PW clean direct calls to it's pages titles ? Thiscould be a problem in my specific case. ^_^

Link to comment
Share on other sites


OK. First, thanks for checking. This will definitely spare me hours of pain. Definitely from my end. (I prefer this...)

Processwire is 3.0.42. Only "weird" thing I can think of on my dev server is PHP 7.0 (I will try with 6)

Oh, and I started with a blank profile and just copied the search page from default profile when I needed it. (don't ask me...), although searches mostly work.

Must be some misconfiguration somewhere in my settings. I'll try and start fresh in the morning.

Thank you very much, I would probably have lost hours before thinking of trying on a fresh install. I'll let you know if I discover what is the cause of this.

Link to comment
Share on other sites

I am running PHP 7, so don't worry about that.

I am running the latest PW dev 3.0.57, but I doubt there'd be an issue in 3.0.42, but maybe worth checking.

Did you go with standard MyISAM and UTF8 in your MySQL database? I don't think this should be an issue either, but just something to check.

Link to comment
Share on other sites

UTF8 and MyISAM, yep. MySQL standard from ubuntu repos.

But you might be right, now that you mention it, I can have reasonable doubts about my SQL config. I had to do some dirty things in order to recover data from a massive pile of sugar a while ago. 

I will try to dump this on another server tomorrow. It was planned anyway. Then I'll be sure.

Thanks for the head up. I'll let you know if I have any luck.

Link to comment
Share on other sites


Note for later : sleeping is sometimes useful.

So it eventually had nothing to do with PW search nor sanitizer. It was more about me being a moron.

This came from libreoffice not inserting the correct apostrophe character in the source data sheets. A simple search and replace in my original CSV will sort this out (I had to change a few things anyway).

Thank you all, folks, I could have lost hours before thinking of checking the source material. (as I should probably have done first....)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Falk
      Hi Forum!
      I have the following content in a ckeditor body-field:
      <figure class="align_right"><img alt="some random alt text" src="path/to/image-file" /> <figcaption>some random caption text</figcaption> </figure> <p>some random text</p> I use 
      $text = $sanitizer->truncate($page->body, [ 'type' => 'sentence', 'maxLength' => 400, 'visible' => true ]); $content = "<p>"; $content .= $text; $content .= "</p>"; echo $content; to output the body-field in my template, which results in…
      <p>some random caption text some random text</p>
      Is there any way to tell $sanitizer->truncate to skip/remove/ignore anything that’s inside a <figure> Tag, so that I get rid of the part "some random caption text"?
    • By jacmaes
      I have a series of videos, and the following search form (translated into English here) that allows to filter these videos on the frontend:

      I've built a few of these search forms, but only with text fields, selects and radio buttons. Here I'm using an array with checkboxes ("Level" field above), and it's causing me grief when I try to paginate these results. I've done a lot of searching in the forum and spent too many hours to try to get it to work. Here's how I'm building the selector:
      <?php if(count($input->get)): // Level is an array. Code adapted from Ryan's snippet here: // https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?tab=comments#comment-38042 if($input->get->level) { $level = array(); foreach($input->get->level as $id) $level[] = (int) $id; // sanitize to INTs $level = implode('|', $level); // convert to 123|456|789 string, ready for selector } else { $level = ''; } $data = array( 'training_type' => array('=', (int) $input->get->training_type), 'duration' => array('=', (int) $input->get->duration), 'level' => array('=', $level), 'limit' => array('=', (int) $input->get->limit) ); $selector = ''; // iterate through the $data we made above to create a selector string foreach($data as $field => $a) { list($operator, $value) = $a; if(empty($value)) continue; // send value to the whitelist so that it can be used in pagination $input->whitelist($field, $value); // append to our selector string $selector .= "$field$operator$value, "; } $videos = $page->children("$selector"); When I hit search, I get the expected results. So far so good. The GET parameters are the following with the options selected in the screenshot above:
      videos/?level[]=1476&level[]=1477&training_type=1473&duration=1485&limit=10 $selector echoes the following as the "level" field is an array with a pipe character:
      level=1476|1477, training_type=1473, duration=1485, limit=10 Now, when paginating these results, the following page (page 2) shows these GET parameters:
      videos/page2/?level=1476|1477&training_type=1473&duration=1486&limit=10 And I think that's where the problem lies. The "level" field is "lost" and I'm getting more results than expected on subsequent pages. If I manually add "page2" to the initial results in the URL, just to test, everything works fine:
      videos/page2/?level[]=1476&level[]=1477&training_type=1473&duration=1486&limit=10 But how can I achieve this in code? Do I need to revert to "level[]=1475&level[]=1477" instead of "level=1476|1477" for the pagination to work correctly, and can you PHP gods illuminate me?
      Any help would be really appreciated, really.
    • By cboetens
      Hi guys
      So I'm building a little search index for a client with this recipe I've found on the ProcessWire Recipes website (more info: https://processwire-recipes.com/recipes/set-up-search-index-with-fieldtypecache/)
      I've followed the exact steps in the recipe and the script appears to be working, except for Profields: Repeater Matrix. 🙄
      This is the code I've got so far:
      <?php include('includes/header.php') ?> <?php $q = $sanitizer->selectorValue($input->get->q); if($q) { $results = $pages->find('search_cache%='. $q); } else { $results = new stdClass(); $results->count = 0; } ?> <section class="partners-block-consul mb-5"> <div class="container"> <div class="row"> <div class="col-sm-10 offset-sm-1"> <div class="partners-head sec-p-lg"> <?php if($page->headline) { ?> <h1 class="highlight"><?= $page->headline ?></h1> <?php } ?> <div class="cms text-left"> <p>Er werden <?= $results->count ?> resultaten gevonden.</p> <?php if($results->count > 0){ ?> <ul> <?php foreach($results as $result){ if($result->template->name == "faq_item") { $category = $pages->get("id=".$result->faq_category); $parent_template = $category->rootParent(); ?> <li><?= $result->title ?> - <a href="<?= $parent_template->url . $category->name . "/"; ?>" title="<?= $result->title ?>"><?= $labels->read_more ?></a></li> <?php } elseif($result->template->name == "faq_category") { $parent_template = $result->rootParent(); $faqCategoryUrl = $parent_template->url . $result->name . "/"; ?> <li><?= $result->title ?> - <a href="<?= $faqCategoryUrl; ?>" title="<?= $result->title ?>"><?= $labels->read_more ?></a></li> <?php } else { ?> <li><?= $result->title ?> - <a href="<?= $result->url; ?>" title="<?= $result->title ?>"><?= $labels->read_more ?></a></li> <?php } ?> <?php } ?> </ul> <?php } ?> </div> </div> </div> </div> </div> </section> <?php include('includes/cta.php'); ?> <?php include('includes/footer.php'); ?> In the ProcessWire admin the "search_cache" field is being build as follows (see image 1).

      Then, the field 'search_cache' is being used on the following templates (see image 2 and 3). So, let's say I'm searching the word 'kinderen', which should get a hit on the template 'Kind', the script isn't returning this page as one of the hits (the result should return 'migraine').

      If someone has any idea why the script is not looking for the data that is being put in the field "Profields: Repeater Matrix", he/ she would be my personal hero! 🤗

    • By Robin S
      Sanitizer EasySlugger
      Allows the use of the EasySlugger library as Sanitizer methods.
      Install the Sanitizer EasySlugger module.
      The module adds four new sanitizer methods.
      slugger($string, $options)
      Similar to $sanitizer->pageName() - I'm not sure if there are any advantages over that method. Included because it is one of the methods offered by EasySlugger.
      $slug = $sanitizer->slugger('Lorem Ipsum'); // Result: lorem-ipsum utf8Slugger($string, $options)
      Creates slugs from non-latin alphabets.
      $slug = $sanitizer->utf8Slugger('这个用汉语怎么说'); // Result: zhe-ge-yong-han-yu-zen-me-shuo seoSlugger($string, $options)
      Augments the string before turning it into a slug. The conversions are related to numbers, currencies, email addresses and other common symbols.
      $slug = $sanitizer->seoSlugger('The price is $5.99'); // Result: the-price-is-5-dollars-99-cents See the EasySlugger readme for some more examples.
      seoUtf8Slugger($string, $options)
      A combination of utf8Slugger() and seoSlugger().
      $slug = $sanitizer->seoUtf8Slugger('价钱是 $5.99'); // Result: jia-qian-shi-5-dollars-99-cents $options argument
      Each of the methods can take an $options array as a second argument.
      separator (string): the character that separates words in the slug. Default: - unique (bool): Determines whether a random suffix is added at the end of the slug. Default: false $slug = $sanitizer->utf8Slugger('这个用汉语怎么说', ['separator' => '_', 'unique' => true]); // Result: zhe_ge_yong_han_yu_zen_me_shuo_3ad66c4  
    • By stanoliver
      Hi! I tried to implement a simple site search like in the regular site profile by Ryan.
      I checked out the code on github but I could not make it work for my site. Probably because Ryan uses some advanced output strategy and uikit specific stuff which I don't use or understand.
      For my site I just use:
      - 1 template (name: templatetoparea.php) for the toparea of my site
      - 1 template (name: templatemainare.php) for the main content of my site
      - 1 template (name: templatefooterarea.php) for the footerarea of my site.
      Also I have prepared two empty templates:
      - 1 template (name: templatesearch.php) for the search itself
      - 1 template (name: templatelistsearch.php) for listing the search results
      May somebody could let me know how to implement a very basic site search like in the regular site profile without using any module or complicated output strategies.
      Two things would be a must have:
      - The search should work with standard repeaters (where most of my content lives)
      - The search results/hits should be listed and the search word(s) should have a yellow colored background (inline css is fine) 
  • Create New...