summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatthiasgrimm <matthiasgrimm@users.sourceforge.net>2005-06-05 20:50:38 +0200
committermatthiasgrimm <matthiasgrimm@users.sourceforge.net>2005-06-05 20:50:38 +0200
commitb59a406b93334a3a9ba2c1f2529a3bc7123361f4 (patch)
tree94af6ab6cca1a522590d9f882b18de04a0a6e64f
parentf0481e4f9a2d86fad0757d829e3edbe64539cd61 (diff)
downloadrpg-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
-rw-r--r--conf/dokuwiki.php2
-rw-r--r--inc/lang/de/lang.php3
-rw-r--r--inc/lang/en/lang.php3
-rw-r--r--inc/search.php81
-rw-r--r--lib/exe/media.php29
5 files changed, 100 insertions, 18 deletions
diff --git a/conf/dokuwiki.php b/conf/dokuwiki.php
index 67b2d9936..bd4eccfb5 100644
--- a/conf/dokuwiki.php
+++ b/conf/dokuwiki.php
@@ -36,6 +36,8 @@ $conf['maxseclevel'] = 3; //Up to which level create editable se
$conf['camelcase'] = 0; //Use CamelCase for linking? (I don't like it) 0|1
$conf['deaccent'] = 1; //convert accented chars to unaccented ones in pagenames?
$conf['useheading'] = 0; //use the first heading in a page as its name
+$conf['refcheck'] = 1; //check references before deleting media files
+$conf['refcount'] = 5; //search only no of references to satisfy the refcheck
/* Antispam Features */
diff --git a/inc/lang/de/lang.php b/inc/lang/de/lang.php
index 236a36aee..7e3e46469 100644
--- a/inc/lang/de/lang.php
+++ b/inc/lang/de/lang.php
@@ -71,6 +71,9 @@ $lang['uploadsucc'] = 'Datei wurde erfolgreich hochgeladen';
$lang['uploadfail'] = 'Hochladen fehlgeschlagen. Keine Berechtigung?';
$lang['uploadwrong'] = 'Hochladen verweigert. Diese Dateiendung ist nicht erlaubt.';
$lang['uploadexist'] = 'Datei existiert bereits. Keine Änderungen vorgenommen.';
+$lang['deletesucc'] = 'Die Datei "%s" wurde gelöscht.';
+$lang['deletefail'] = '"%s" konnte nicht gelöscht werden - prüfen Sie die Berechtigungen.';
+$lang['mediainuse'] = 'Die Datei "%s" wurde nicht gelöscht - sie wird noch verwendet.';
$lang['namespaces'] = 'Namensräume';
$lang['mediafiles'] = 'Vorhandene Dateien in';
diff --git a/inc/lang/en/lang.php b/inc/lang/en/lang.php
index 9dd2f4681..235eeff80 100644
--- a/inc/lang/en/lang.php
+++ b/inc/lang/en/lang.php
@@ -69,6 +69,9 @@ $lang['uploadsucc'] = 'Upload successful';
$lang['uploadfail'] = 'Upload failed. Maybe wrong permissions?';
$lang['uploadwrong'] = 'Upload denied. This file extension is forbidden!';
$lang['uploadexist'] = 'File already exists. Nothing done.';
+$lang['deletesucc'] = 'The file "%s" has been deleted.';
+$lang['deletefail'] = '"%s" couldn\'t be deleted - check perission.';
+$lang['mediainuse'] = 'The file "%s" hasn\'t been deleted - it is still in use.';
$lang['namespaces'] = 'Namespaces';
$lang['mediafiles'] = 'Available files in';
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
*
diff --git a/lib/exe/media.php b/lib/exe/media.php
index 4e193807c..f0d0795ad 100644
--- a/lib/exe/media.php
+++ b/lib/exe/media.php
@@ -34,11 +34,20 @@
}
//handle deletion
- if($DEL && $AUTH >= AUTH_DELETE){
- media_delete($DEL);
+ $mediareferences = array();
+ if($DEL && $AUTH >= AUTH_DELETE){
+ if($conf['refcheck']){
+ search($mediareferences,$conf['datadir'],'search_reference',array('query' => $DEL));
}
+ if(!count($mediareferences)){
+ media_delete($DEL);
+ }else{
+ $text = str_replace('%s',noNS($DEL),$lang['mediainuse']);
+ msg($text,0);
+ }
+ }
- //handle upload
+ //handle upload
if($_FILES['upload']['tmp_name'] && $UPLOADOK){
media_upload($NS,$AUTH);
}
@@ -58,12 +67,16 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/
function media_delete($delid){
+ global $lang;
+
$file = mediaFN($delid);
- if(@unlink($file)){
- return true;
- }
- //something went wrong
- msg("'$file' couldn't be deleted - check permissions",-1);
+ if(@unlink($file)){
+ msg(str_replace('%s',noNS($delid),$lang['deletesucc']),1);
+ return true;
+ }
+ //something went wrong
+ $text = str_replace('%s',$file,$lang['deletefail']);
+ msg($text,-1);
return false;
}