1<?php 2/** 3 * Action Plugin ArchiveUpload 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Myron Turner <turnermm02@shaw.ca> 7 */ 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 12require_once(DOKU_PLUGIN.'action.php'); 13 14 15class action_plugin_strreplace extends DokuWiki_Action_Plugin { 16 private $do_replace = false; 17 private $metafilename = 'strreplace:searched'; 18 private $metafilepath; 19 private $id; 20 private $suspend = false; 21 /** 22 * Registers our callback functions 23 */ 24 function register(Doku_Event_Handler $controller) { 25 $controller->register_hook('IO_WIKIPAGE_READ', 'AFTER', $this, 'substitutions'); 26 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, '_ini'); 27 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'write_metafile'); 28 } 29 30 function __construct() { 31 $this->metafilepath = metaFN($this->metafilename, '.ser'); 32 $this->suspend = $this->getConf('suspend'); 33 } 34 35 function _ini(&$event, $param) { 36 global $ACT,$INFO; 37 if($this->suspend) return; 38 39 $this->id = $INFO['id']; 40 41 $this->do_replace = $this->getConf('do_replace'); 42 if(!$this->do_replace) { 43 if(file_exists($this->metafilepath)) { 44 unlink($this->metafilepath); 45 } 46 return; 47 } 48 49 if($ACT != 'edit') { 50 return; 51 } 52 53 $searched =$this->get_metadata(); 54 if(in_array($this->id,$searched) && !array_key_exists ('_s' ,$searched)) { 55 $this->do_replace = false; 56 } 57 } 58 59 60 function substitutions(&$event, $param) { 61 global $ACT; 62 if($ACT != 'edit') return; 63 if($this->suspend) return; 64 if(!$this->do_replace) return; 65 if($event->data[1]) { 66 $doc = $event->data[1] . ':' . $event->data[2]; 67 } 68 else { 69 $doc = $event->data[2]; 70 } 71 72 if( $doc!= $this->id) return; // prevents processing of pages loaded by template, e.g. sidebar 73 $count = 0; 74 for($i=1; $i< 5; $i++) { 75 $s_term = 'search_' . $i; 76 $r_term = 'replace_' . $i; 77 $srch = $this->getConf($s_term); 78 $srch = trim($srch); 79 if($srch) { 80 if(preg_match('/^#.*?#$/',$srch)) { 81 $srch .= 'ms'; 82 } 83 else $srch = '#'. preg_quote($srch, '#') .'#ms'; 84 $repl = $this->getConf($r_term); 85 $event->result = preg_replace($srch,$repl,$event->result, -1, $_count); 86 $count += $_count; 87 } 88 } 89 90 $searched = $this->get_metadata(); 91 92 if($count) { 93 $searched['_s'] = $this->id; 94 } 95 elseif(in_array($this->id,$searched)) { 96 return; 97 } 98 else $searched[] = $this->id; 99 io_saveFile($this->metafilepath,serialize($searched)); 100 } 101 102 function write_metafile(&$event, $param) { 103 global $ACT; 104 105 if(!is_array($ACT)) return; 106 if(!$ACT['save']) return; 107 if($this->suspend) return; 108 109 $searched = $this->get_metadata(); 110 111 if( array_key_exists ('_s',$searched) && $searched['_s'] == $this->id) { 112 unset($searched['_s']); 113 } 114 else return; 115 116 if(in_array($this->id,$searched)) return; 117 $searched[] = $this->id; 118 119 io_saveFile($this->metafilepath,serialize($searched)); 120 } 121 122 function get_metadata() { 123 $searched = unserialize(io_readFile($this->metafilepath,false)); 124 if(!$searched) return array(); 125 return $searched; 126 } 127 128 129} 130 131