Media search indexing, code analysis - This week in Orchard (14/06/2021)

Gábor Domonkos's avatar
Media Library, Translation, This week in Orchard, Content field, Recipes

Improving the code quality of Orchard Core by code analysis, supporting multiple translation providers, and adding a media search indexing feature to Orchard Core! Check out our post now for more!

Orchard Core updates

Code analysis

Software is critical in all aspects of our lives. From entertainment, shopping, and dating to business-critical systems and software where human lives are at stake. And yet, security bugs are all too frequent, mainly because the process of finding vulnerabilities is manual, tedious, and repetitive, and because the expertise is not shared with other security researchers or with developers. LGTM seeks to address this situation.

If you open up that site and enter Orchard Core to the search box on the top, you will see the active alerts for this repository. So, this site will go on GitHub and analyze the source of the projects. It's like a static analyzer for the code that finds potential issues, like this variable is never used, or here you are throwing an exception where you should not, and so on. We can also filter by language to see the alerts related to the C# code of the repository.

The Orchard Core project on LGTM.com

Based on these issues, some fixes have been made to the source code. For example, we could remove some unused variables to make the code cleaner.

Removing unused variables

Do you know that our Lombiq .NET Analyzers repository contains .NET code analyzers and code convention settings for Lombiq projects? We use these to enforce common standards across all our .NET projects, including e.g. in all of our open-source Orchard Core extensions. If you contribute to our open-source projects while using that solution, you'll be guided by these rules too.

If you are interested in how to add these analyzers to your solution too, check out This week in Orchard post, but if you prefer videos, you can find a short presentation about how to use those on YouTube!

Support multiple translation providers

The localization manager should be able to handle multiple instances of ITranslationProvider. This feature is about supporting multiple translation providers. This way, we can create more methods for storing translations, like filesystem plus database. Right now, we have only one ITranslationProvider implementation in Orchard Core, the PoFilesTranslationProvider. This provider is part of the Localization module, which module provides the infrastructure necessary to support the PO (Portable Object) localization file format.

The PoFilesTranslationsProvider provides a translations for po files.

As you can see, the LocalizationManager now injects a list of available ITranslationProvider implementations and calls the LoadTranslations method of every provider to load the translations to the CultureDictionary.

The LocalizationManager now injects the list of available ITranslationProvider.

Recipes Dropdown focus during setup

Let's say you want to set up your Orchard Core site, or you just want to add a new tenant to it. When you move the focus from site name to recipe combo box and press Arrow Down the recipe list pops up. But when you commit your choice via Enter, focus resets to the first item on the page. Not it's fixed. It's a nice to have feature because there are people who prefer using the keyboard to navigate on the site instead of using the mouse.

Recipe dropdown focus

Serve module compile view for package reference

If a module is referenced as a NuGet package e.g., OrchardCore.Application.Cms.Targets and if the NuGet package includes precompiled Views.dll, in development views are not served from the Views.dll, instead, it re-compiles all embedded cshtml resulted in a slow start in development. This code is to just ensure that it doesn't try to recompile if it finds the correct DLLs.

Do not recompile precompiled views

Demos

Media search indexing

The idea here was to allow to search for content in files. This demo shows a feature that provides a simple implementation to index media for search. More precisely, it indexes media files related to content items, so said content items will turn up in search when one of their media files matches the search query. The following data can be indexed for each file referenced from a Media Field:

  • Media Text
  • The textual content of PDF files

To index the textual content of the PDF files, the pull request uses the PdfPig library, which allows users to read and extract text and other content from PDF files. In addition, the library can be used to create simple PDF documents containing text and geometrical shapes.

First of all, let's set up your site using the Blog recipe. This recipe constructs the Blog Post content type that has a Media Field called Banner Image. Let's say we would like to search by using the media text of the Banner Image field. To allow that, open the settings of the Banner Image (Content -> Content Definition -> Content Types -> Blog Post -> Banner Image) and put a tick in the Include this element in the index checkbox.

After that, you have to do some configuration for the search fields. If you navigate to Search -> Settings -> Search, you can find the comma-separated list of fields to use for search pages. You can see the Content.ContentItem.FullText value there by default. Here we need to add the BlogPost.Image one. BlogPost is the technical name of the content type (and in this case of the content part too) and Image is the technical name of the Banner Image Media Field.

Adding BlogPost.Image to the default searched fields

Let's try out this feature quickly! Let's say we just want to edit the predefined blog post with the title Man must explore, and this is exploration at its greatest. Select the browsed image of the Banner Image field and add a media text to it.

Adding media text

Now let's navigate to the predefined search form (https://yoursite.com/search) and try to search for the Astronaut word. This is the one that we have just added as the media text to our blog post. Astronaut is not a word that would actually show up anywhere in the body. As you can see, our blog post has just appeared as a result of the search.

The blog post where the media text of the attached media field contains the Astronaut word

If you attach a PDF file to the Banner Image media field and try to search for a word or a phrase that is inside the PDF file, the query will return that content item too.

This feature is still under development by Zoltán Lehóczky from Lombiq and as we mentioned you can find the PR containing the code here. If you would like to know more about it, don't forget to check out the following recording on YouTube!

News from the community

Orchard Dojo Newsletter

Lombiq's Orchard Dojo Newsletter has 203 subscribers! We have started this newsletter to inform the community around Orchard with the latest news about the platform. By subscribing to this newsletter, you will get an e-mail whenever a new post is published to Orchard Dojo, including This week in Orchard of course.

Do you know of other Orchard enthusiasts who you think would like to read our weekly articles? Tell them to subscribe here!

If you are interested in more news around Orchard and the details of the topics above, don't forget to check out the recording of this week's Orchard meeting!

No Comments

Add a Comment