[Home]WikiPatches/PageEmbeddedTitleSearches

UseModWiki | WikiPatches | RecentChanges | Preferences

I wanted to be able to list pages with specific strings in the page title. This among any other content on the page. I think I might have hacked my usemod script a bit too much for a diff to make sense. But here's the gist:

I introduced a tag in the spirit of <toc>. I use <title-search this_or_that_search_text>. Processed like so:

sub WikiToHTML {
  ...
  $pageText =~ s/\<title-search\s+(.*?)\>/&PageList(&SearchTitle($1))/ige;
  while (@HeadingNumbers) {
...
}
(I'm not sure where the "macro" processing should be placed, but it works for me where I put it now)

The other two subs are defined like so:

sub PageList {
  my $pagename;
  my $pageListing;

  foreach $pagename (@_) {
    $pageListing .= ".... "  if ($pagename =~ m|/|);
    $pageListing .= &GetPageLink($pagename) . "<br>\n";
  }

  return $pageListing;
}
and
sub SearchTitle {
  my ($string) = @_;
  my ($name, @found);
  foreach $name (&AllPagesList()) {
      push(@found, $name) if ($name =~ /$string/i);
  }
  return @found;
}
The SearchTitle function assumes you use underscores in the search for pages with spaces in their names.

You might recognize the code inside PageList there? To not do too much of the same processing in several places I did the following refactoring:

sub PrintPageList {
    print "<h2>", Ts('%s pages found:', ($#_ + 1)), "</h2>\n";
    print &PageList(@_);
}
A reasonable refactoring in any case imho.

I just did a crude hack that works on my site. A more careful approach might want to make the processing of the title-search macro optional through config variables and such...

-- PEZ

See also: /EmbeddedBacklinks


UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited January 12, 2008 5:12 pm by JuanmaMP (diff)
Search: