Futilities/v0.6/clade/Sys/dba/files/DataFolder

From Woozle Writes Code
< Futilities‎ | v0.6‎ | clade‎ | Sys‎ | dba
Revision as of 22:41, 11 November 2025 by Woozle (talk | contribs)
Jump to navigation Jump to search
clade: Sys\dba\files\DataFolder
Clade Family
[WFe]Fo DataFolder (none)
Clade Aliases
Alias Clade
Base* [c,i] Sys\FileSys\Node\Fo
MetaNodeClass Sys\Data\aux\meta\Node
QObj* [c,i] Data\Mem\QVar\Obj
Status* [c,i] Sys\Events\ItWent
Nodes [c,i] Sys\dba\files\DataFiles
SelfIface Sys\dba\files\DataFolder
Subpages

About

Functions

public

Code

as of 2025-11-11 (includes some debugging stuff)

interface iDataFolder extends BaseIface {
    function PairCheck() : StatusIface;
    function ByFName() : NodesIface;
    function BySchm() : NodesIface;
    function FindSchema(string $s) : QObjIface;
}
class cDataFolder extends BaseClass implements SelfIface {

    // ++ CONFIG ++ //

    protected function MetaPairClass() : string { return MetaPairClass::class; }

    // -- CONFIG -- //
    // ++ LISTS ++ //

    private $oByFName = NULL;
    protected function SetByFName(NodesIface $o) { $this->oByFName = $o; }
    public function ByFName() : NodesIface { return $this->oByFName; }

    private $oBySchm = NULL;
    protected function SetBySchm(NodesIface $o) { $this->oBySchm = $o; }
    public function BySchm() : NodesIface { return $this->oBySchm; }
    public function FindSchema(string $s) : QObjIface {
        $oa = $this->oBySchm;
        if (is_object($oa)) {
            $os = $oa->QryIt($s,QObjClass::class);
        } else {
            $os = QObjClass::AsNew();
        }
        return $os;
    }

    // ++ LISTS: processing ++ //


    // DOCS: https://wooz.dev/Futilities/v0.6/clade/Sys/dba/files/DataFolder/@fx/IndexIt
    //  2025-11-02 TODO: doc page needs updating for name-change and also mention that the core function of indexing-by-ext is in $oaDir->IndexByExt().
    public function PairCheck() : StatusIface {
        $oStat = new StatusClass;
        $this->AmHere();
        if ($this->Ident()->Exists()) {
            $oaDir = $this->InOut()->ContentArray();  // read the dir into an array
            $oaExts = $oaDir->IndexByExt();         // index the array by filename extension
            $osJS = $oaExts->QryIt('json');

            #echo '## $oaExts is: '.get_class($oaExts).CRLF;

            $arJSNames = $this->NamesArrayFromQArray($osJS);

            // First, look at all the .sql files:
            $osSQ = $oaExts->QryIt('sql');
            if ($osSQ->HasIt()) { // if any were found --
        $this->AmHere();
                $arSQ = $osSQ->GetIt(); // get the list
                foreach ($arSQ as $nIdx => $oEntry) {
                    // for each .sql file, create a MetaPair object:
                    $fsData = $oEntry->Ident()->SpecFull();
        $this->AmHere("fsData=[$fsData]");
                    $oPair = $this->MetaPairClass()::FromDataSpec($fsData);
                    $oData = $oPair->DataFile();
                    $oMeta = $oPair->MetaFile();

                    $oMetaIO = $oMeta->InOut();
                    $oaMeta = $oMetaIO->OAData();
                    $oaMeta->DataFormat('SQL'); // TODO: should be tied to enum or clade-const
                    $sName = $oaMeta->QSFileName()->GetIt();
                    #$sName2 = $oData->SpecFull();
                    #$this->AmHere("NAME1: [$sName] NAME2: [$sName2]"); die();

                    if (array_key_exists($sName,$arJSNames)) {
                        // There's already a .json metafile for this .sql:
                        $oMeta = $arJSNames[$sName];
                    } else {
                        // JSON-meta file not found; create object...
                        $arJSNames[$sName] = $oMeta;
                        // ...and save data:
                        $oMetaIO->OAData()->Store(); // create/populate the meta file
                    }
                }
                $oStat->SetOkay(TRUE);
            }
        } else {
        $this->AmHere();
            $oStat->SetOkay(FALSE);
            // 2025-02-10 This method will need some rewriting.
            $ffp = $this->Ident()->SpecFull();
            $oStat->AddMsgString("Can't access import folder $ffp. Does it exist?");
        }
        return $oStat;
    }
    // 2025-11-10 FIX LATERs:
    //  * This is a *bit* klugey in that it sounds general but actually is about meta-stuff only.
    //  * (Also, I'm being wildly inconsistent with "meta" vs. "info" vs. "pair".
    //  * [2025-11-11] Klugey cont.: derives 2 indexes ($arByFName and $arBySchm), but sets one internally and returns the other. (...wait, no...)
    protected function NamesArrayFromQArray(QArrIface $qar) : array {
        if ($qar->HasIt()) {
            $arJS = $qar->GetIt();
            $arByFName = [];
            $arBySchm = [];
            foreach ($arJS as $nIdx => $oEntry) {
                $fsMeta = $oEntry->Ident()->SpecFull();
                $oPair = $this->MetaPairClass()::FromInfoSpec($fsMeta);
                $ofMeta = $oPair->MetaFile();
                $oMetaIO = $ofMeta->InOut();
                $oaMeta = $oMetaIO->OAData();
                $oaMeta->Fetch();

                $sName = $oaMeta->QSFileName()->GetIt();
                $arByFName[$sName] = $oPair;
                $qsSchm = $oPair->OSchema()->QSchemaName();
                if ($qsSchm->HasIt()) {
                    $arBySchm[$qsSchm->GetIt()] = $oPair;
                } else {
                    echo self::Screen()->ErrorIt('Data missing:')." data file $fsMeta has no Schema name.".CRLF;
                }
            }
            #$this->SetBySchm($arBySchm);
        }
        return $arByFName;
    }

    // -- LISTS -- //
}

Removed

2025-11-11

Commented out on 2025-11-02:

// 2025-11-02 old version/name
    public function IndexIt() : StatusIface {
        $oStat = new StatusClass;
        $this->AmHere('Indexing the folder');

        if ($this->Ident()->Exists()) {
            $oaDir = $this->InOut()->ContentArray();  // read the dir into an array
            $oaExts = $oaDir->IndexByExt();         // index the array by filename extension
            $osJS = $oaExts->QryIt('json');
            if ($osJS->HasIt()) {
                $arJS = $osJS->GetIt();
                $arByFName = [];
                $arBySchm = [];
                foreach ($arJS as $nIdx => $oEntry) {
                    $fsMeta = $oEntry->Ident()->SpecFull();
                    $oMeta = $this->InfoNodeClass()::FromSpec($fsMeta);
                    $oMetaIO = $oMeta->InOut();
                    $oaMeta = $oMetaIO->OAData();
                    $oaMeta->Fetch();

                    $sName = $oMetaIO->TargetFileName();
                    $arByFName[$sName] = $oMeta;
                    $qsSchm = $oMetaIO->QSchemaName();
                    if ($qsSchm->HasIt()) {
                        $arBySchm[$qsSchm->GetIt()] = $oMeta;
                    } else {
                        echo self::Screen()->ErrorIt('Data missing:')." data file $fsMeta has no Schema name.".CRLF;
                    }
                }
            }
            // TODO 2025-01-27: use data-migration enum as basis for orphaned-file-extension list
            // (...that is, data files which don't have an associated .json index file)
            $osSQ = $oaExts->QryIt('sql');
            if ($osSQ->HasIt()) { // if there are any .sql files...
                $arSQ = $osSQ->GetIt(); // get the list of files
                foreach ($arSQ as $nIdx => $oEntry) {
                    // for each .sql file:
                    $fsTarg = $oEntry->Ident()->SpecFull();
                    $oMeta = $this->DataNodeClass()::FromSpec($fsTarg);
                    $oMetaIO = $oMeta->InOut();
                    $oMetaIO->DataFormat('SQL'); // TODO: should be tied to enum
                    $sName = $oMetaIO->TargetFileName();
                    if (array_key_exists($sName,$arByFName)) {
                        // There's already a .json metafile for this .sql:
                        $oMeta = $arByFName[$sName];
                    } else {
                        // JSON-meta file not found; create object...
                        $arByFName[$sName] = $oMeta;
                        // ...and save data:
                        $oMetaIO->OAData()->Store(); // create/populate the meta file
                    }
                }
            }
            if (isset($arByFName) && (count($arByFName) > 0)) {
                $oStat->SetOkay(TRUE);
                $oaByFName = NodesClass::FromVArray($arByFName);
                $this->SetByFName($oaByFName);
            } else {
                // 2025-02-10 This method will need some rewriting.
                $oStat->AddMsgString("No importable files found in $ffp.");
            }
            if (is_array($arBySchm)) {
                $oaBySchm = NodesClass::FromVArray($arBySchm);
                #$this->AmHere('Setting schema list.');
                $this->SetBySchm($oaBySchm);
            } else {
                $this->AmHere('No array found, for some reason.');
            }
        } else {
          // 2025-02-10 This method will need some rewriting.
          $oStat->AddMsgString("Can't access import folder $ffp. Does it exist?");
        }
        return $oStat;
    }