diff options
author | matthiasgrimm <matthiasgrimm@users.sourceforge.net> | 2005-06-05 20:50:38 +0200 |
---|---|---|
committer | matthiasgrimm <matthiasgrimm@users.sourceforge.net> | 2005-06-05 20:50:38 +0200 |
commit | b59a406b93334a3a9ba2c1f2529a3bc7123361f4 (patch) | |
tree | 94af6ab6cca1a522590d9f882b18de04a0a6e64f /inc/search.php | |
parent | f0481e4f9a2d86fad0757d829e3edbe64539cd61 (diff) | |
download | rpg-b59a406b93334a3a9ba2c1f2529a3bc7123361f4.tar.gz rpg-b59a406b93334a3a9ba2c1f2529a3bc7123361f4.tar.bz2 |
media reference check
This patch implements the first step of a media file reference
checker. Every time the user wanted to delete a media file
it would be ckecked for still existing references to this media
file. File deletion is denied if this media file is still in use.
darcs-hash:20050605185038-7ef76-475e5990609587e1b8cee0e155fa6002f1c5b27c.gz
Diffstat (limited to 'inc/search.php')
-rw-r--r-- | inc/search.php | 81 |
1 files changed, 71 insertions, 10 deletions
diff --git a/inc/search.php b/inc/search.php index 4b1973e98..0caae3d7c 100644 --- a/inc/search.php +++ b/inc/search.php @@ -294,11 +294,6 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ return false; } - //get text - $text = io_readfile($base.'/'.$file); - //lowercase text (u modifier does not help with case) - $lctext = utf8_strtolower($text); - //create regexp from queries $poswords = array(); $negwords = array(); @@ -323,12 +318,77 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')'; $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$'; + search_regex($data,$base,$file,$reg,$poswords); + return true; +} + +/** + * Reference search + * This fuction searches for existing references to a given media file + * and returns an array with the found pages. It doesn't pay any + * attention to ACL permissions to find every reference. The caller + * must check if the user has the appropriate rights to see the found + * page and eventually have to prevent the result from displaying. + * + * @param array $data Reference to the result data structure + * @param string $base Base usually $conf['datadir'] + * @param string $file current file or directory relative to $base + * @param char $type Type either 'd' for directory or 'f' for file + * @param int $lvl Current recursion depht + * @param mixed $opts option array as given to search() + * + * $opts['query'] is the demanded media file name + * + * @author Andreas Gohr <andi@splitbrain.org> + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ +function search_reference(&$data,$base,$file,$type,$lvl,$opts){ + global $conf; + + //we do nothing with directories + if($type == 'd') return true; + + //only search txt files + if(!preg_match('#\.txt$#',$file)) return true; + + //we finish after five references found. The return value + //'false' will skip subdirectories to speed search up. + if(count($data) >= $conf['refcount']) return false; + $reg = '{{ *'.$opts['query'].' *(\|.*)?}}'; + search_regex($data,$base,$file,$reg,array($opts['query'])); + return true; +} + +/* ------------- helper functions below -------------- */ + +/** + * fulltext search helper + * searches a text file with a given regular expression + * no ACL checks are performed. This have to be done by + * the caller if necessary. + * + * @param array $data reference to array for results + * @param string $base base directory + * @param string $file file name to search in + * @param string $reg regular expression to search for + * @param array $words words that should be marked in the results + * + * @author Andreas Gohr <andi@splitbrain.org> + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ +function search_regex(&$data,$base,$file,$reg,$words){ + + //get text + $text = io_readfile($base.'/'.$file); + //lowercase text (u modifier does not help with case) + $lctext = utf8_strtolower($text); + //do the fulltext search $matches = array(); if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){ //this is not the best way for snippet generation but the fastest I could find - $q = $poswords[0]; //use first posword for snippet + $q = $words[0]; //use first word for snippet creation $p = utf8_strpos($lctext,$q); $f = $p - 100; $l = utf8_strlen($q) + 200; @@ -336,20 +396,21 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ $snippet = '<span class="search_sep"> ... </span>'. htmlspecialchars(utf8_substr($text,$f,$l)). '<span class="search_sep"> ... </span>'; - $mark = '('.join('|',$poswords).')'; + $mark = '('.join('|', $words).')'; $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet); $data[] = array( - 'id' => $id, + 'id' => pathID($file), 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches), - 'poswords' => join(' ',$poswords), + 'poswords' => join(' ',$words), 'snippet' => $snippet, ); } - + return true; } + /** * fulltext sort * |