summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorMichael Hamann <michael@content-space.de>2013-07-31 17:30:08 +0200
committerChristopher Smith <chris@jalakai.co.uk>2013-08-01 11:11:52 +0200
commite5d09fddcd17a2fe896650b64b81313a7d000975 (patch)
tree3ac7a67e64e215cd3438beeab847092153ab0d1e /inc
parent818d2283465b76931db168a9e2c72f2c0f004ecc (diff)
downloadrpg-e5d09fddcd17a2fe896650b64b81313a7d000975.tar.gz
rpg-e5d09fddcd17a2fe896650b64b81313a7d000975.tar.bz2
Index media file usage in the metadata index and use it in ft_mediause()
Diffstat (limited to 'inc')
-rw-r--r--inc/fulltext.php48
-rw-r--r--inc/indexer.php8
-rw-r--r--inc/media.php2
-rw-r--r--inc/parser/metadata.php30
4 files changed, 50 insertions, 38 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 :