diff options
author | Andreas Gohr <andi@splitbrain.org> | 2013-07-31 11:15:12 -0700 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2013-07-31 11:15:12 -0700 |
commit | 15754217312575b58b0594cd63f1e1db57623b42 (patch) | |
tree | 8d1e59c132c764889d2c3c9b7cefd19623be35c1 /inc | |
parent | 20587314b0587d68f5e18e442a0e2af152c22af2 (diff) | |
parent | 0320882f767e5685df9d7ac4bec5a3e3f1b1f216 (diff) | |
download | rpg-15754217312575b58b0594cd63f1e1db57623b42.tar.gz rpg-15754217312575b58b0594cd63f1e1db57623b42.tar.bz2 |
Merge pull request #244 from splitbrain/mediause_index
Index media usage data
Diffstat (limited to 'inc')
-rw-r--r-- | inc/fulltext.php | 48 | ||||
-rw-r--r-- | inc/indexer.php | 8 | ||||
-rw-r--r-- | inc/media.php | 2 | ||||
-rw-r--r-- | inc/parser/metadata.php | 30 | ||||
-rw-r--r-- | inc/search.php | 39 |
5 files changed, 50 insertions, 77 deletions
diff --git a/inc/fulltext.php b/inc/fulltext.php index 1afff25dd..c03126994 100644 --- a/inc/fulltext.php +++ b/inc/fulltext.php @@ -151,42 +151,28 @@ function ft_backlinks($id, $ignore_perms = false){ /** * Returns the pages that use a given media file * - * Does a quick lookup with the fulltext index, then - * evaluates the instructions of the found pages + * Uses the relation media metadata property and the metadata index. * - * Aborts after $max found results + * Note that before 2013-07-31 the second parameter was the maximum number of results and + * permissions were ignored. That's why the parameter is now checked to be explicitely set + * to true (with type bool) in order to be compatible with older uses of the function. + * + * @param string $id The media id to look for + * @param bool $ignore_perms Ignore hidden pages and acls (optional, default: false) + * @return array A list of pages that use the given media file */ -function ft_mediause($id,$max){ - if(!$max) $max = 1; // need to find at least one +function ft_mediause($id, $ignore_perms = false){ + $result = idx_get_indexer()->lookupKey('relation_media', $id); - $result = array(); + if(!count($result)) return $result; - // quick lookup of the mediafile - // FIXME use metadata key lookup - $media = noNS($id); - $matches = idx_lookup(idx_tokenizer($media)); - $docs = array_keys(ft_resultCombine(array_values($matches))); - if(!count($docs)) return $result; - - // go through all found pages - $found = 0; - $pcre = preg_quote($media,'/'); - foreach($docs as $doc){ - $ns = getNS($doc); - preg_match_all('/\{\{([^|}]*'.$pcre.'[^|}]*)(|[^}]+)?\}\}/i',rawWiki($doc),$matches); - foreach($matches[1] as $img){ - $img = trim($img); - if(media_isexternal($img)) continue; // skip external images - list($img) = explode('?',$img); // remove any parameters - resolve_mediaid($ns,$img,$exists); // resolve the possibly relative img - - if($img == $id){ // we have a match - $result[] = $doc; - $found++; - break; - } + // check ACL permissions + foreach(array_keys($result) as $idx){ + if(($ignore_perms !== true && ( + isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ + )) || !page_exists($result[$idx], '', false)){ + unset($result[$idx]); } - if($found >= $max) break; } sort($result); diff --git a/inc/indexer.php b/inc/indexer.php index 2f3ab25dc..8f0ba7ec6 100644 --- a/inc/indexer.php +++ b/inc/indexer.php @@ -10,7 +10,7 @@ if(!defined('DOKU_INC')) die('meh.'); // Version tag used to force rebuild on upgrade -define('INDEXER_VERSION', 5); +define('INDEXER_VERSION', 6); // set the minimum token length to use in the index (note, this doesn't apply to numeric tokens) if (!defined('IDX_MINWORDLENGTH')) define('IDX_MINWORDLENGTH',2); @@ -1365,6 +1365,12 @@ function idx_addPage($page, $verbose=false, $force=false) { $metadata['relation_references'] = array_keys($references); else $metadata['relation_references'] = array(); + + if (($media = p_get_metadata($page, 'relation media', METADATA_RENDER_UNLIMITED)) !== null) + $metadata['relation_media'] = array_keys($media); + else + $metadata['relation_media'] = array(); + $data = compact('page', 'body', 'metadata', 'pid'); $evt = new Doku_Event('INDEXER_PAGE_ADD', $data); if ($evt->advise_before()) $data['body'] = $data['body'] . " " . rawWiki($page); diff --git a/inc/media.php b/inc/media.php index fbe1363ec..c76f2986c 100644 --- a/inc/media.php +++ b/inc/media.php @@ -178,7 +178,7 @@ function media_inuse($id) { global $conf; $mediareferences = array(); if($conf['refcheck']){ - $mediareferences = ft_mediause($id,$conf['refshow']); + $mediareferences = ft_mediause($id,true); if(!count($mediareferences)) { return false; } else { diff --git a/inc/parser/metadata.php b/inc/parser/metadata.php index e17b82f8b..d64fe4d77 100644 --- a/inc/parser/metadata.php +++ b/inc/parser/metadata.php @@ -282,8 +282,10 @@ class Doku_Renderer_metadata extends Doku_Renderer { function internallink($id, $name = NULL){ global $ID; - if(is_array($name)) + if(is_array($name)) { $this->_firstimage($name['src']); + if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + } $default = $this->_simpleTitle($id); @@ -304,8 +306,10 @@ class Doku_Renderer_metadata extends Doku_Renderer { } function externallink($url, $name = NULL){ - if(is_array($name)) + if(is_array($name)) { $this->_firstimage($name['src']); + if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + } if ($this->capture){ $this->doc .= $this->_getLinkTitle($name, '<' . $url . '>'); @@ -313,8 +317,10 @@ class Doku_Renderer_metadata extends Doku_Renderer { } function interwikilink($match, $name = NULL, $wikiName, $wikiUri){ - if(is_array($name)) + if(is_array($name)) { $this->_firstimage($name['src']); + if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + } if ($this->capture){ list($wikiUri, $hash) = explode('#', $wikiUri, 2); @@ -324,8 +330,10 @@ class Doku_Renderer_metadata extends Doku_Renderer { } function windowssharelink($url, $name = NULL){ - if(is_array($name)) + if(is_array($name)) { $this->_firstimage($name['src']); + if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + } if ($this->capture){ if ($name) $this->doc .= $name; @@ -334,8 +342,10 @@ class Doku_Renderer_metadata extends Doku_Renderer { } function emaillink($address, $name = NULL){ - if(is_array($name)) + if(is_array($name)) { $this->_firstimage($name['src']); + if ($name['type'] == 'internalmedia') $this->_recordMediaUsage($name['src']); + } if ($this->capture){ if ($name) $this->doc .= $name; @@ -347,6 +357,7 @@ class Doku_Renderer_metadata extends Doku_Renderer { $height=NULL, $cache=NULL, $linking=NULL){ if ($this->capture && $title) $this->doc .= '['.$title.']'; $this->_firstimage($src); + $this->_recordMediaUsage($src); } function externalmedia($src, $title=NULL, $align=NULL, $width=NULL, @@ -439,6 +450,15 @@ class Doku_Renderer_metadata extends Doku_Renderer { $this->firstimage = $src; } } + + function _recordMediaUsage($src) { + global $ID; + + list ($src, $hash) = explode('#', $src, 2); + if (media_isexternal($src)) return; + resolve_mediaid(getNS($ID), $src, $exists); + $this->meta['relation']['media'][$src] = $exists; + } } //Setup VIM: ex: et ts=4 : diff --git a/inc/search.php b/inc/search.php index 6927fff5f..884aa7b23 100644 --- a/inc/search.php +++ b/inc/search.php @@ -273,45 +273,6 @@ function search_allpages(&$data,$base,$file,$type,$lvl,$opts){ 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(substr($file,-4) != '.txt') return true; - - //we finish after 'cnt' references found. The return value - //'false' will skip subdirectories to speed search up. - $cnt = $conf['refshow'] > 0 ? $conf['refshow'] : 1; - if(count($data) >= $cnt) return false; - - $reg = '\{\{ *\:?'.$opts['query'].' *(\|.*)?\}\}'; - search_regex($data,$base,$file,$reg,array($opts['query'])); - return true; -} - /* ------------- helper functions below -------------- */ /** |