Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option fulltextregex to search page content with regex #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions inc/pagequery.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,53 @@ function page_search($query) {
$result = array_keys(ft_pageSearch($query, $highlight));
return $result;
}


/**
* use regex to search all page content
*
* @param $query
* @param $incl_ns
* @param $excl_ns
* @return mixed
*/
function page_search_regex($query, $incl_ns, $excl_ns) {

$query = trim($query);
$pages = idx_get_indexer()->getPages();

// first deal with excluded namespaces, then included, order matters!
$pages = $this->_filter_ns($pages, $excl_ns, true);
$pages = $this->_filter_ns($pages, $incl_ns, false);

$cnt = count($pages);
for ($i = 0; $i < $cnt; $i++) {
$page = $pages[$i];

$text = io_readFile(wikiFN($page, '', false), false);

$matched = preg_match('/' . $query . '/iS', $text);
if ($matched === false) {
return false;
} elseif ($matched == 0) {
unset($pages[$i]);
}
}

foreach($pages as $key => $page) {
if ( ! page_exists($page) || isHiddenPage($page)) {
unset($pages[$key]);
}
}

if (count($pages) > 0) {
return $pages;
} else {
// we always return an array type
return array();
}
}



/**
Expand All @@ -841,7 +888,7 @@ function page_lookup($query, $fullregex, $incl_ns, $excl_ns) {
global $conf;

$query = trim($query);
$pages = file($conf['indexdir'] . '/page.idx');
$pages = idx_get_indexer()->getPages();

if ( ! $fullregex) {
// first deal with excluded namespaces, then included, order matters!
Expand All @@ -864,7 +911,7 @@ function page_lookup($query, $fullregex, $incl_ns, $excl_ns) {
* and the page-exists check above
* The @ prevents problems with invalid queries!
*/
$matched = @preg_match('/' . $query . '/i', $page);
$matched = @preg_match('/' . $query . '/iS', $page);
if ($matched === false) {
return false;
} elseif ($matched == 0) {
Expand Down
1 change: 1 addition & 0 deletions res/toolbar
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pagequery> * | .* | exclude ns: ^ |-ns: | include ns: @ | ns: | . | .. | text | regex
fulltext Full-text search of page content
fullregex Search the full page id using regex
fulltextregex Full-text search of page content using regex
sort=column:direction Keys to sort by, in order of sorting
- a, ab, abc By 1st letter, 2-letters, or 3-letters
- name, pagename By page name [not grouped]
Expand Down
12 changes: 10 additions & 2 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function handle($match, $state, $pos, Doku_Handler $handler) {
$opt['fontsize'] = ''; // base fontsize of pagequery; best to use %
$opt['fullregex'] = false; // power-user regex search option--file name only
$opt['fulltext'] = false; // search full-text; including file contents
$opt['fulltextregex'] = false; // search full-text; including file contents, using preg_match
$opt['group'] = false; // group the results based on sort headings
$opt['hidejump'] = false; // hide the jump to top link
$opt['hidemsg'] = false; // hide any error messages
Expand All @@ -99,6 +100,7 @@ function handle($match, $state, $pos, Doku_Handler $handler) {
case 'casesort':
case 'fullregex':
case 'fulltext':
case 'fulltextregex':
case 'group':
case 'hidejump':
case 'hidemsg':
Expand Down Expand Up @@ -272,8 +274,14 @@ function render($mode, Doku_Renderer $renderer, $opt) {
if ($query == '*') {
$query = '.*';
}
// search by page name or path only
$results = $pq->page_lookup($query, $opt['fullregex'], $incl_ns, $excl_ns);

if($opt['fulltextregex'] == true) {
// search by page name or path only
$results = $pq->page_search_regex($query, $incl_ns, $excl_ns);
} else {
// search by page name or path only
$results = $pq->page_lookup($query, $opt['fullregex'], $incl_ns, $excl_ns);
}
}
$results = $pq->validate_pages($results, $opt['hidestart'], $opt['maxns']);

Expand Down