view faptool.php @ 881:7c805dccd4f7

Work on the tool.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Nov 2014 16:31:40 +0200
parents 6db8ef2bb1b5
children 25bcbd6d5682
line wrap: on
line source

#!/usr/bin/php
<?
require_once "mconfig.inc.php";
require_once "msite.inc.php";

stCheckCLIExec();


//
// Create directories
//
function stMakeDir($path, $perm)
{
  if (!file_exists($path))
  {
    echo " - Creating ".$path."\n";
    if (mkdir($path, $perm, TRUE) === false)
      die("Could not create directory '".$path."'\n");
  }
}


function stInitializeDirs()
{
  global $setEntryPath, $setPreviewPath, $setThumbDir,
    $setEntryPathPerms, $setPrevPathPerms;

  echo "Checking for missing directories ...\n";
  stMakeDir($setEntryPath, $setEntryPathPerms);
  stMakeDir($setPreviewPath, $setPrevPathPerms);
  stMakeDir(stMakePath(FALSE, FALSE, array($setPreviewPath, $setThumbDir)), $setPrevPathPerms);

  foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
  {
    stMakeDir(stMakePath(FALSE, FALSE, array($setEntryPath, $compo["cpath"])), $setEntryPathPerms);
  }
}


//
// Main program starts
//
if ($argc < 2)
{
  echo
    "faptool - Do stuff with FAPWeb database\n".
    "(C) Copyright 2014 ccr/TNSP\n".
    "\n".
    "Usage: ".$argv[0]." <mode> [args]\n".
    "Where mode is one of following:\n".
    "\n".
    "  init\n".
    "     Create directories for entries and previews, if needed.\n".
    "\n".
    "  previews <cmd> [args]\n".
    "    Where <cmd> is one of:\n".
    "    status   - List files and show what is missing, etc.\n".
    "\n";
  exit;
}

// Try to connect to database
$spec = stGetSetting("sqlDB");
if (($db = stConnectSQLDBSpec($spec)) === false)
  die("Could not connect to SQL database '".$spec."'.\n");

echo "Using database spec '".$spec."'.\n";

// Fetch non-"hardcoded" settings from SQL database
stReloadSettings();

// Set some globals for our benefit
$setEntryPath = stGetSetting("entryPath");
$setPreviewPath = stGetSetting("previewPath");
$setPreviewURL = stGetSetting("previewURL");
$setThumbDir = stGetSetting("thumbnailSubDir");
$setEntryPathPerms = stGetSetting("entryPathPerms");
$setPrevPathPerms = stGetSetting("previewPathPerms");

if ($setEntryPath === FALSE || $setPreviewPath === FALSE ||
    $setPreviewURL === FALSE || $setThumbDir === FALSE ||
    $setEntryPathPerms === FALSE || $setPrevPathPerms === FALSE)
{
  die("Some required settings not defined in mconfig.inc.php!\n");
}


// Act according to specified command
switch (substr(stCArgLC(1), 0, 4))
{
  case "init":
    //
    // Initialize the data directories etc
    //
    stInitializeDirs();
    break;

  case "prev":
    //
    // Preview files handling
    //
    $mode = substr(stCArgLC(2), 0, 3);
    switch ($mode)
    {
      case "sta":
        // List previews that are found and missing
        foreach (stExecSQL("SELECT * FROM compos WHERE cpath <> '' AND cpath IS NOT NULL") as $compo)
        {
          printf(
            "\n".
            "--[ #%03d - %-30s ]---\n".
            "PrevType : %s\n".
            "PrevPath : %s\n".
            "----------------------------------------------\n",
            $compo["id"], $compo["name"],
            $previewTypeList[$compo["preview_type"]][0],
            stMakePath(FALSE, FALSE, array($setPreviewPath, $compo["cpath"])));

          foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry)
            wtHandleEntryPreview($compo, $entry, $mode);
        }
        break;

      default:
        if ($mode == "")
          die("ERROR! Previews command requires a sub-command argument.\n");
        else
          die("ERROR! Invalid previews sub-command '".stCArg(2)."'.\n");
        break;
    }
    break;

  default:
    echo "ERROR! Invalid operation mode '".stCArg(1)."'.\n";
    break;
}

?>