base unusedmedias
author Johan Guilbaud
email guilbaud [dot] johan [at] gmail [dot] com
date 2011-10-10
name Images et documents orphelins
desc Liste les images et documents inutilisés afin de pouvoir éventuellement les supprimer.
url http://www.dokuwiki.org/plugin:unusedmedias
==== admin/list.php ====
* @version 1.0 (10/10/2011)
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'admin.php';
//safe mode has to be disabled ! (safe mode is deprecated by php)
@set_time_limit(0);
class admin_plugin_unusedmedias_list extends DokuWiki_Admin_Plugin {
private $orphans_medias = array();
private $error = "";
private $ok = "";
public function getMenuSort() { return FIXME; }
public function forAdminOnly() { return true; }
public function handle() {
global $conf, $ID;
//$this->_debug_print_r($data);
//$this->_debug_print_r($_REQUEST);
//If delete requested
if(isset($_REQUEST['media_id_to_delete'])){
if(checkSecurityToken($_REQUEST['sectok'])){
$mediaToDelete = $_REQUEST['media_id_to_delete'];
$file = mediaFN($mediaToDelete);
if(file_exists($file)){
if(media_inuse($mediaToDelete)===false){
// trigger an event - MEDIA_DELETE_FILE
$data['id'] = $mediaToDelete;
$data['name'] = basename($file);
$data['path'] = $file;
$data['size'] = (@file_exists($file)) ? filesize($file) : 0;
$data['unl'] = false;
$data['del'] = false;
$evt = new Doku_Event('MEDIA_DELETE_FILE',$data);
if ($evt->advise_before()) {
$data['unl'] = @unlink($file);
if($data['unl']){
addMediaLogEntry(time(), $mediaToDelete, DOKU_CHANGE_TYPE_DELETE);
$data['del'] = io_sweepNS($mediaToDelete,'mediadir');
}
}
$evt->advise_after();
unset($evt);
$this->ok = sprintf($this->getLang('delete_file_ok'),$mediaToDelete);
;
}else{
$this->error = sprintf($this->getLang('delete_file_in_use'),$mediaToDelete);
}
}else{
$this->error = sprintf($this->getLang('delete_file_not_found'),$mediaToDelete);
}
}//end of csrf check
}
//Searching for orphaned medias
$data = array();
//getting all medias
search($data,$conf['mediadir'],'search_media', array('showmsg'=>true,'depth'=>500),str_replace(':', '/', getNS($ID)));
//check if they are (still) in use or not.
foreach($data as $media){
$isUsed = media_inuse($media['id']);
if($isUsed === false){
$this->orphans_medias[$media['id']] = $media;
}
}
}
public function html() {
global $lang, $ID;
ptln('' . $this->getLang('title') . ' : '.getNS($ID).'
');
if($this->error != ""){
ptln("".$this->error."");
}
if($this->ok != ""){
ptln("".$this->ok."");
}
if(count($this->orphans_medias)>0){
ptln('');
ptln('ID Actions ');
$i=0;
foreach($this->orphans_medias as $id => $media){
ptln('');
ptln('' . $id . ' ');
$link = ml($id,'',true);
$btn_view = '
';
$btn_delete = '
getLang('js_confirm_delete').'")){document.getElementById("unusedmedias_form_'.$i.'_delete").submit();}return false;\'>
';
ptln(' '.$btn_view.' '.$btn_delete.' ');
ptln(' ');
$i++;
}
ptln('
');
}else{
ptln(''.$this->getLang('nomatches').'');
}
}
/**
* Debug method used only during development.
* @param array $array
*/
private function _debug_print_r($array){
echo str_replace("\n","
",print_r($array,true));
echo "
";
}
}
==== lang/fr/lang.php ====
*/
// menu entry for admin plugins
// $lang['menu'] = 'Your menu entry';
// custom language strings for the plugin
// $lang['fixme'] = 'FIXME';
$lang['menu'] = 'Images et documents orphelins (peut mettre beaucoup de temps)';
$lang['title'] = 'Médias orphelins';
$lang['nomatches'] = 'Aucun résultat !';
//Javascript
$lang['js_confirm_delete'] = 'Voulez-vous vraiment supprimer ce média ?';
//Messages de réussite
$lang['delete_file_ok'] = '%s a été supprimé';
//Messages d'erreur
$lang['delete_file_in_use'] = '%s est utilisé sur au moins une page !';
$lang['delete_file_not_found'] = '%s n\'existe pas';
//Setup VIM: ex: et ts=4 :
==== lang/en/lang.php ====
*/
// menu entry for admin plugins
// $lang['menu'] = 'Your menu entry';
// custom language strings for the plugin
// $lang['fixme'] = 'FIXME';
$lang['menu'] = 'Orphaned Medias (can take a long time)';
$lang['title'] = 'Orphaned Medias';
$lang['nomatches'] = 'No matches found !';
//Javascript
$lang['js_confirm_delete'] = 'Are you sure to delete this media ?';
//Success messages
$lang['delete_file_ok'] = '%s deleted.';
//Errors messages
$lang['delete_file_in_use'] = '%s is used by at least one page.';
$lang['delete_file_not_found'] = '%s doesn\'t exist.';
//Setup VIM: ex: et ts=4 :