Skip to content

Commit

Permalink
Fix issue with determining used file for imageSequence
Browse files Browse the repository at this point in the history
imegaSequence does not mention all the files it uses in the xml so the media
and quota panel does not correctly show the files in use.
A similar issue exists for import pages. Not all the sequence files are copied,
only the first and last.
Extend XerteXmlInspector to know which files are used and use that in the media
and quota panel and in determining which files are to be copied when a page is
imported
  • Loading branch information
torinfo committed Jul 12, 2022
1 parent c35a79c commit 24ee9e4
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 21 deletions.
1 change: 1 addition & 0 deletions editor/importpages/import-pagedata.php
Expand Up @@ -72,6 +72,7 @@
$page = $x->pages[$i];
$type = $page->type;
$page->icon = $pageIcons->$type;
unset($page->node);
}

echo str_replace("'", "\\'", json_encode($x));
Expand Down
46 changes: 28 additions & 18 deletions editor/importpages/merge.php
Expand Up @@ -5,7 +5,8 @@
require( "../../" . $xerte_toolkits_site->php_library_path . "screen_size_library.php" );
require( "../../" . $xerte_toolkits_site->php_library_path . "template_status.php" );
require( "../../" . $xerte_toolkits_site->php_library_path . "display_library.php" );
require( "../../" . $xerte_toolkits_site->php_library_path . "user_library.php" );

require( "../../" . $xerte_toolkits_site->php_library_path . "xmlInspector.php" );

function merge_pages_to_project($source_project_id, $source_pages, $target_project, $target_page_location, $merge_glossary)
{
Expand All @@ -31,8 +32,11 @@ function merge_pages_to_project($source_project_id, $source_pages, $target_proje

$xmlTarget = new DOMDocument();
$xmlTarget->load($target_file);
$xmlSource = new DOMDocument();
$xmlSource->load($source_file);
$xmlSource = new DOMDocument();
$xmlSource->load($source_file);
$xmlSourceInspector = new XerteXMLInspector();
$xmlSourceInspector->loadTemplateXML($source_file);

$nodes = array();
$i = 0;

Expand Down Expand Up @@ -112,7 +116,7 @@ function merge_pages_to_project($source_project_id, $source_pages, $target_proje

// Convert to text, do filemapping, go back to xml
$nodeXmlStr = $xmlSource->saveXML($node);
$nodeXmlStr = doFileMapping($nodeXmlStr, $filemapping, $filesToCopy);
$nodeXmlStr = doFileMapping($nodeXmlStr, $filemapping, $filesToCopy, $page, $xmlSourceInspector);
$fragment = $xmlTarget->createDocumentFragment();
$fragment->appendXml($nodeXmlStr);

Expand Down Expand Up @@ -172,26 +176,32 @@ function getFileMapping($source_media_folder, $target_media_folder)
}

function recursive_scanDir($dir){
$result = [];
foreach(scandir($dir) as $filename) {
if ($filename[0] === '.') continue;
$filePath = $dir . '/' . $filename;
if (is_dir($filePath)) {
foreach (recursive_scanDir($filePath) as $childFilename) {
$result[] = $filename . "/" . $childFilename;
}
} else {
$result[] = $filename;
$result = [];
foreach(scandir($dir) as $filename) {
if ($filename[0] === '.') continue;
$filePath = $dir . '/' . $filename;
if (is_dir($filePath)) {
foreach (recursive_scanDir($filePath) as $childFilename) {
$result[] = $filename . "/" . $childFilename;
}
} else {
$result[] = $filename;
}
return $result;
}
return $result;
}

function doFileMapping($str, $filemapping, &$fileToCopy)
function doFileMapping($str, $filemapping, &$fileToCopy, $page=null, $xmlSourceInspector=null)
{
foreach ($filemapping as $file => $mapping) {
$pos = strpos($str, 'media/' . $file);
if ($pos !== false)
if ($page !== null) {
$found = $xmlSourceInspector->fileIsUsed($file, $page);
}
else {
$pos = strpos($str, 'media/' . $file);
$found = $pos !== false;
}
if ($found)
{
array_push($fileToCopy, $file);
$str = str_replace('media/' . $file, 'media/' . $mapping, $str);
Expand Down
11 changes: 9 additions & 2 deletions website_code/php/properties/media_and_quota_template.php
Expand Up @@ -27,6 +27,7 @@
*/

require_once("../../../config.php");
require_once("../xmlInspector.php");

_load_language_file("/website_code/php/properties/media_and_quota_template.inc");
_load_language_file("/properties.inc");
Expand Down Expand Up @@ -86,7 +87,7 @@ function in_use($file_name)

function media_folder_loop($folder_name){

global $dir_path, $new_path, $temp_dir_path, $temp_new_path, $quota, $result_string, $delete_string, $xerte_toolkits_site, $end_of_path;
global $dir_path, $new_path, $temp_dir_path, $temp_new_path, $quota, $result_string, $delete_string, $xerte_toolkits_site, $end_of_path, $dataInspector, $previewInspector;

$result = "";

Expand All @@ -104,7 +105,7 @@ function media_folder_loop($folder_name){
$path = $xerte_toolkits_site->site_url . "USER-FILES/" . $end_of_path . "/media/" . $folder_name . $f;
$buttonlbl = MEDIA_AND_QUOTA_DOWNLOAD;

if(in_use('media/' . $folder_name . $f)){
if($dataInspector->fileIsUsed($folder_name . $f) || $previewInspector->fileIsUsed($folder_name . $f)){
$result = "<div class=\"filename found\" style=\"cursor:hand; cursor:pointer;\" onClick=\"setup_download_link('" . $path . "', '" . $buttonlbl . "', '" . $end_of_path . "/media/" . $folder_name . $f . "')\">" . $folder_name . $f . "</div><div class=\"filesize found\">" . substr((filesize($full)/1000000),0,4) . " MB</div><span class=\"fileinuse found foundtextcolor\">" . MEDIA_AND_QUOTA_USE . " </span>";

}else{
Expand Down Expand Up @@ -156,6 +157,12 @@ function media_folder_loop($folder_name){

$previewpath = $xerte_toolkits_site->users_file_area_full . $end_of_path . "/preview.xml";

$dataInspector = new XerteXMLInspector();
$dataInspector->loadTemplateXML($xmlpath);

$previewInspector = new XerteXMLInspector();
$previewInspector->loadTemplateXML($previewpath);

if(file_exists($xerte_toolkits_site->users_file_area_full . $end_of_path . "/preview.xml")){

$quota = filesize($xerte_toolkits_site->users_file_area_full . $end_of_path . "/data.xml") + filesize($xerte_toolkits_site->users_file_area_full . $end_of_path . "/preview.xml");
Expand Down
160 changes: 159 additions & 1 deletion website_code/php/xmlInspector.php
Expand Up @@ -17,10 +17,95 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

class XertePage
{
private $pageFiles = null;
public $name;
public $node;
public $type;
public $index;

private function getImgNum($fileName) {
$info = pathinfo($fileName);
$name = $info['filename'];
$num = '';

// extract the number from the end of the file name
for ($k=strlen($name)-1; $k>-1; $k--) {
if (is_numeric($name[$k]))
{
$num = $name[$k] . $num;
} else {
$name = substr($name, 0, strlen($name) - strlen($num));
break;
}
}

// return the file name (without number) & number separately
$ret = new stdClass();
$ret->name = $name;
$ret->num = ($num != '' ? intval($num) : $num);
$ret->addZeros = strlen($num) - strlen((string)((int)$num));
return $ret;
}


public function getImageSequenceFiles()
{
if ($this->type != "imageSequence")
{
return array();
}
if ($this->pageFiles === null)
{
$this->pageFiles = array();
// This piece of code know way too much of the structure of the XML of imageSequence
// Create sequence structure for all cases
$cases = $this->node->xpath("case");
foreach ($cases as $case)
{
$series = $case->xpath("imgSeries");
foreach ($series as $seq)
{
$thisSeries = new stdClass();
// Build sequence structure like in page
$firstImg = str_replace("FileLocation + 'media/", "", (string)$seq['firstImg']);
$lastImg = str_replace("FileLocation + 'media/", "", (string)$seq['lastImg']);
// Remove last character
$firstImg = substr($firstImg, 0, strlen($firstImg) - 1);
$lastImg = substr($lastImg, 0, strlen($lastImg) - 1);
$firstImgInfo = pathinfo($firstImg);
$lastImgInfo = pathinfo($lastImg);

$thisSeries->firstImg = $this->getImgNum($firstImgInfo['basename']);
$thisSeries->lastImg = $this->getImgNum($lastImgInfo['basename']);

// first & last images must be in the same folder & have same file name except for number at the end & have same file extension
// first image should be the image with the lowest number and last image should be the image with the highest number
$thisSeries->imgFolder = $firstImgInfo['dirname'];
$thisSeries->imgExt = $firstImgInfo['extension'];
if ($thisSeries->firstImg->addZeros > 0)
{
$thisSeries->numLength = strlen((string)($thisSeries->firstImg->num)) + $thisSeries->firstImg->addZeros;
}
// Generate filenames and add to file array
for ($i=$thisSeries->firstImg->num; $i<=$thisSeries->lastImg->num; $i++)
{
$this->pageFiles[] = $thisSeries->imgFolder . "/" . $thisSeries->firstImg->name . str_pad($i, $thisSeries->numLength, "0", STR_PAD_LEFT) . "." . $thisSeries->imgExt;
}
}
}
}
return $this->pageFiles;
}
}

class XerteXMLInspector
{

private $fname;
private $xmlstr;
private $xml;
private $name;
private $models;
Expand All @@ -44,7 +129,7 @@ private function addModel($model)

private function addPage($node, $i)
{
$page = new stdClass();
$page = new XertePage();
$name = (string)$node['name'];
// This may contain HTML tags, convert to plain text
// Remove the HTML tags
Expand All @@ -54,6 +139,7 @@ private function addPage($node, $i)
// encode
$name = base64_encode($name);
$page->name = $name;
$page->node = $node;
$page->type = $node->getName();
$page->index = $i;

Expand Down Expand Up @@ -190,6 +276,9 @@ public function loadTemplateXML($name)
}

$xml = file_get_contents($name);
// decode filenames in the XML
$xml = rawurldecode($xml);
$xml = html_entity_decode($xml);
if (!$this->isValidXml($xml)) {
// Try and fix it ?
_debug("Invalid XML found; trying to repair");
Expand All @@ -204,6 +293,7 @@ public function loadTemplateXML($name)
$xml = file_get_contents($name);
}
}
$this->xmlstr = $xml;

$this->xml = simplexml_load_string($xml);
if (strlen((string)$this->xml['glossary'])>0)
Expand Down Expand Up @@ -337,6 +427,11 @@ public function glossaryUsed()
return $this->glossary;
}

public function getPage($page)
{
return $this->pages[$page];
}

public function getPages()
{
return $this->pages;
Expand All @@ -353,6 +448,69 @@ public function getLOAttribute($attr)
return false;
}
}
/**
* @par $filename is supposed to be the filename with media (so do NOT include media)
* @return bool
*/
public function fileIsUsed($filename, $pagenr=null)
{
if ($pagenr != null)
{
// get the page and check if the file is used
$page = $this->getPage($pagenr);
$node = $page->node;
$nodeXmlStr = $node->asXML();
$pos = strpos($nodeXmlStr, 'media/' . $filename);
if ($pos !== false)
{
return true;
}
else
{
if ($page->type === 'imageSequence')
{
$files = $page->getImageSequenceFiles();
// find filename in files
foreach ($files as $file)
{
if ($file == $filename)
{
return true;
}
}
}
return false;
}
}
else
{
// check if the file is used
$pos = strpos($this->xmlstr, 'media/' . $filename);
if ($pos !== false)
{
return true;
}
else
{
foreach($this->pages as $page)
{
if ($page->type === 'imageSequence')
{
$files = $page->getImageSequenceFiles();
// find filename in files
foreach ($files as $file)
{
if ($file == $filename)
{
return true;
}
}
}
}
return false;
}
}
}
}

//$template = new XerteXMLInspector();
Expand Down

0 comments on commit 24ee9e4

Please sign in to comment.