Usage:
It is very easy to use, just look at the example below.
To only search for a pattern into the text file, do not
define any replacement key.
Example:
$path = '/path/to/search';
create a class instance
$obj = new FileTextSearch();
set the extensions to search files within
$obj = setExtensions(array('xml','xsl');
add an extension whenever you need
$obj = addExtension('css');
set the pattern to search for
$obj = setSearchKey('pattern');
set the replace key if you want to replace matches with it
$obj = setReplacementKey('replace');
finally run the search
$obj = findReplace($path);
/** * Returns TRUE if the template is being used in an Issue or Section. * @return boolean */ public static function InUse($p_templateName) { global $Campsite; global $g_ado_db; $p_templateName = ltrim($p_templateName, '/'); $queryStr = "SELECT * FROM Templates WHERE Name = '{$p_templateName}'"; $row = $g_ado_db->GetRow($queryStr); if (!$row) { return false; } $id = $row['Id']; $queryStr = "SELECT COUNT(*) FROM Issues " . " WHERE IssueTplId = " . $id . " OR SectionTplId = " . $id . " OR ArticleTplId = " . $id; $numMatches = $g_ado_db->GetOne($queryStr); if ($numMatches > 0) { return true; } $queryStr = "SELECT COUNT(*) FROM Sections " . " WHERE SectionTplId = " . $id . " OR ArticleTplId = " . $id; $numMatches = $g_ado_db->GetOne($queryStr); if ($numMatches > 0) { return true; } $tplFindObj = new FileTextSearch(); $tplFindObj->setExtensions(array('tpl', 'css')); $tplFindObj->setSearchKey($p_templateName); $result = $tplFindObj->findReplace($Campsite['TEMPLATE_DIRECTORY']); if (is_array($result) && sizeof($result) > 0) { return $result[0]; } if (pathinfo($p_templateName, PATHINFO_EXTENSION) == 'tpl') { $p_templateName = ' ' . $p_templateName; } $tplFindObj->setSearchKey($p_templateName); $result = $tplFindObj->findReplace($Campsite['TEMPLATE_DIRECTORY']); if (is_array($result) && sizeof($result) > 0) { return $result[0]; } if ($tplFindObj->m_totalFound > 0) { return true; } return false; }
} if ($f_action == "move") { $sql = "SELECT Id FROM TemplateTypes WHERE Name = 'nontpl'"; $nonTplTypeId = $g_ado_db->GetOne($sql); // Move all the templates requested. foreach ($templates as $template) { if ($template->move($f_current_folder, $f_destination_folder)) { $searchKey = $template->getName(); $replacementKey = ltrim($f_destination_folder . '/' . basename($template->getName()), '/'); if ($template->getType() != $nonTplTypeId) { $searchKey = ' ' . $searchKey; $replacementKey = ' ' . $replacementKey; } $replaceObj = new FileTextSearch(); $replaceObj->setExtensions(array('tpl','css')); $replaceObj->setSearchKey($searchKey); $replaceObj->setReplacementKey($replacementKey); $replaceObj->findReplace($Campsite['TEMPLATE_DIRECTORY']); Template::UpdateOnChange($template->getName(), $f_destination_folder . '/' . basename($template->getName())); } } // Clear compiled templates require_once($GLOBALS['g_campsiteDir']."/template_engine/classes/CampTemplate.php"); CampTemplate::singleton()->clear_compiled_tpl(); camp_html_add_msg(getGS("Template(s) moved."), "ok");
/** * Replace key in storage * * @param string $old * @param string $new * @param object $replaceEngine * @return void */ private function replace($old, $new, $replaceEngine = null) { if (!isset($replaceEngine)) { $replaceEngine = new \FileTextSearch(); } $replaceEngine->setExtensions(array('tpl', 'css')); $replaceEngine->setSearchKey($old); $replaceEngine->setReplacementKey($new); $replaceEngine->findReplace($this->root); $tpl1_name = $old; $tpl2_name = $new; if (pathinfo($old, PATHINFO_EXTENSION) == 'tpl') { $tpl1_name = ' ' . $old; $tpl2_name = ' ' . $new; } $replaceEngine->setSearchKey($tpl1_name); $replaceEngine->setReplacementKey($tpl2_name); $replaceEngine->findReplace($this->root); }
$tpl1_name = urldecode($f_path)."/$f_orig_name"; $tpl1 = $Campsite['TEMPLATE_DIRECTORY'].'/'.$tpl1_name; $tpl2_name = urldecode($f_path)."/$f_new_name"; $tpl2 = $Campsite['TEMPLATE_DIRECTORY'].'/'.$tpl2_name; if (rename($tpl1, $tpl2)) { $relativeOldFilePath = ltrim($tpl1_name, '/'); $relativeNewFilePath = ltrim($tpl2_name, '/'); Template::UpdateOnChange($tpl1_name, $tpl2_name); // Clear compiled template require_once($GLOBALS['g_campsiteDir']."/template_engine/classes/CampTemplate.php"); CampTemplate::singleton()->clear_compiled_tpl($relativeOldFilePath); $logtext = getGS('Template object $1 was renamed to $2', $tpl1_name, $tpl2_name); Log::Message($logtext, $g_user->getUserId(), 116); // Replace template name in other template files $replaceObj = new FileTextSearch(); $replaceObj->setExtensions(array('tpl','css')); $replaceObj->setSearchKey($relativeOldFilePath); $replaceObj->setReplacementKey($relativeNewFilePath); $replaceObj->findReplace($Campsite['TEMPLATE_DIRECTORY']); if ($origExtension == 'tpl') { $tpl1_name = ' ' . $relativeOldFilePath; $tpl2_name = ' ' . $relativeNewFilePath; } $replaceObj->setSearchKey($tpl1_name); $replaceObj->setReplacementKey($tpl2_name); $replaceObj->findReplace($Campsite['TEMPLATE_DIRECTORY']); if (camp_is_text_file($tpl2) || camp_is_image_file($tpl2)) { // Go into edit mode. camp_html_goto_page("/$ADMIN/templates/edit_template.php"