Subversion-Projekte lars-tiefland.php_share

Revision

Blame | Letzte Änderung | Log anzeigen | RSS feed

<?php

    /**
     * @package   mediaran
     * @author    Lars Tiefland <tiefland@weban.de>
     * @copyright 2010 Webagentur Niewerth
     * @license   propietary http://www.weban.de
     * @version   $Rev: 357 $
     * @filesource
     * 
     */

    /**
     * 
     * @package   mediaran
     * @author    Lars Tiefland <tiefland@weban.de>
     * @copyright 2010 Webagentur Niewerth
     */

    // SVN: $Id: del_old_images.php 357 2010-11-18 11:35:40Z tiefland $

    require_once "Console/GetoptPlus.php";
    require_once "Console/ProgressBar.php";
    require_once "File/Find.php";
    define( "VERSION",
        '$Rev: 357 $ $Date: 2010-11-18 12:35:40 +0100 (Do, 18 Nov 2010) $' );
    // Breite für den ProgressBar
    $width = 120;

    // Konfiguration für Console_GetoptPlus
    $config = array( 'header' => array( 'Alle unbenutzten Bilder loeschen',
        "", ), 'usage' => array( '--site <arg> --type <arg>',
        '--site <arg> --shop <arg> --type <arg>',
        '--site <arg> --shop <arg> --index <arg> --type <arg>' ), 'options' =>
        array( array( 'long' => 'site', 'short' => 'S', 'type' =>
        'mandatory', 'desc' => array( 'arg', 'Definiert den Kunden', '' ) ),
        array( 'long' => 'shop', 'type' => 'mandatory', 'short' => 's',
        'desc' => array( 'arg', 'definiert den Shop',
        'Wenn diese Option nicht angegeben wird, so wird der Wert von --site verwendet',
        "", ) ), array( 'long' => 'index', 'short' => 'i', 'type' =>
        'mandatory', 'desc' => array( 'arg',
        'Definiert den Bild-Index mit dem gearbeitet werden soll',
        'Wenn diese Option nicht angegeben wird, so wird der Wert 1 verwendet',
        "", ) ), array( 'long' => 'type', 'type' => 'mandatory', 'short' =>
        't', 'desc' => array( 'arg',
        'Definiert die DB-Tabelle, mit der gearbeitet werden soll',
        '(artikel, directory, Herstellerkatalog oder news)', "", ), ), array
        ( 'long' => 'version', 'type' => 'noarg', 'short' => 'v', 'desc' =>
        array( 'gibt die Versionsnummer aus und beendet das Skript', "", ) ),
        array( "long" => "progress", "short" => "p", "type" => "noarg",
        "desc" => array( "gibt einen Fortschrittsbalken aus", "" ), ), ),
        'footer' => array( '', ), );

    // Optionen auswerten
    try
    {
        $options = Console_Getoptplus::getoptplus( $config, 'short2long', true,
            'strict', true );
    }
    catch ( Console_GetoptPlus_Exception $e )
    {
        $error = array( $e->getCode(), $e->getMessage() );

        print_r( $error );
        exit;
    }

    if ( isset( $options[0]["version"] ) )
    {
        echo "Version: " . VERSION . "\n";
        exit;
    }
    if ( !( $options[0]["site"] ) )
    {
        //Console_GetoptPlus::help();
        die( "Kein Kunde angegeben!\n" );
    }
    else
    {
        $site = $options[0]["site"];
    }
    if ( !$options[0]["shop"] )
    {
        $shop = $site;
    }
    else
    {
        $shop = $options[0]["shop"];
    }
    if ( !$options[0]["index"] )
    {
        $index = 1;
    }
    else
    {
        $index = $options[0]["index"];
    }
    if ( !$options[0]["type"] || !in_array( $options[0]["type"], array( "artikel",
        "directory", "Herstellerkatalog", "news", "sonderkasten", "aktionen",
        "TesterLogos", ) ) )
    {
        die( "Keine Ahnung welche Tabelle geprueft werden soll.\n" );
    }
    else
    {
        $table = $options[0]["type"];
    }

    $progress = isset( $options[0]["progress"] );

    // Pfad
    if ( !file_exists( "/var/www/vhosts/" ) )
    {
        $path = "/var/www/" . $shop . "/images/";
    }
    else
    {
        $path = "/var/www/vhosts/" . $shop . "/httpdocs/images/";
    }
    switch ( $options[0]["type"] )
    {
        case "artikel":
            $feld = "bild_" . $index . "_url";
            $path_extra = "Bild_" . $index;
            break;
        case "directory":
            $path_extra = "directory/" . $index . "/";
            if ( $index > 1 )
            {
                $feld = "bild_url" . $index;
            }
            else
            {
                $feld = "bild_url";
            }
            break;
        case "Herstellerkatalog":
            $feld = "Logo" . $index;
            $path_extra = "Hersteller/" . $index . "/";
            break;
        case "news":
            $feld = "bild_" . $index . "_url";
            $path_extra = "news/" . $index;
            break;
        case "aktionen":
            $feld = "bild_" . $index . "_url";
            $path_extra = "aktionen/" . $index;
            $table = "Aktionen";
            break;
        case "sonderkasten":
            $feld = "bild_url";
            unset( $index );
            $path_extra = "sonderkasten/";
            break;
        case "TesterLogos":
            $feld = "Logo" . $index;
            $path_extra = "TesterLogos/" . $index . "/";
            $table = "WarenTesterLogos";
            break;
    }

    $res = chdir( $path );
    if ( !$res )
    {
        die( "FEHLER: Ungueltige Pfad " . $path . "!\n" );
    }
    if ( !file_exists( $path_extra ) )
    {
        die( "FEHLER: Ungueltige Index " . $index . "!\n" );
    }

    // Initialisierung
    define( "__SITE__", $site );
    define( "__SHOP__", $shop );
    define( "__USE_MDB2__", true );
    require_once "module/functions.common.php";
    init_shop();

    //SQL
    $sql = "SELECT
            id
        FROM
            $table
        WHERE
            $feld=?
    ";

    // SQL vorbereiten
    $GLOBALS["sql_sth_s"] = $GLOBALS["db"]->prepare( $sql );
    if ( PEAR::isError( $GLOBALS["sql_sth_s"] ) )
    {
        die( $GLOBALS["sql_sth_s"]->getUserInfo() );
    }

    // Dateien zusamensuchen
    $files = File_Find::glob( "/.*\..*/", $path_extra, "perl" );
    $f_anz = count( $files );

    echo "Konfiguration:\n";
    echo "Site: $site\nShop: $shop\nTabelle: $table\nIndex: $index\n";
    echo str_repeat( "=", $width );
    echo "\n";

    $ok = 0;
    $nok = 0;
    $continued = 0;
    $f_nr = 1;

    if ( $f_anz )
    {
        if ( $progress )
        {
            $pb = new Console_ProgressBar( '%fraction% [%bar%] %percent% in %elapsed%',
                '=>', '-', $width, $f_anz );
        } //exit;

        foreach ( $files as $file )
        {
            if ( !is_file( $path_extra . "/" . $file ) )
            {
                // Verzeichnisse etc. überspringen
                echo "$file ==> Keine Datei\n";
                $continued++;
                continue;
                //exit;
            }
            $res = $GLOBALS["sql_sth_s"]->execute( $file );
            if ( PEAR::isError( $res ) )
            {
                die( $res->getUserInfo() );
            }
            if ( $res->numRows() )
            {
                //Bild wird noch verwendet
                //echo "OK\n";
                $ok++;
            }
            else
            {
                if ( !file_exists( $path_extra . "/trash" ) || !is_dir( $path_extra .
                    "/trash" ) )
                {
                    $res = mkdir( $path_extra . "/trash", 0777 );
                    if ( !$res )
                    {
                        die( "Konnte Verzeichnis " . $path_extra .
                            "/trash nicht anlegen!" );
                    }
                    $res = chown( $path_extra . "/trash", "www-data" );
                    if ( !$res )
                    {
                        die( "Konnte Besitzer fuer das Verzeichnis " . $path_extra .
                            "/trash nicht aendern!" );
                    }
                    $res = chgrp( $path_extra . "/trash", "www-data" );
                    if ( !$res )
                    {
                        die( "Konnte Gruppe fuer das Verzeichnis " . $path_extra .
                            "/trash nicht aendern!" );
                    }
                }
                //echo "NOK\n";
                rename( $path_extra . "/" . $file, $path_extra . "/trash/" .
                    $file );
                foreach ( array( "kl", "gr", "mini" ) as $size )
                {
                    if ( file_exists( $path_extra . "/" . $size . "/" . $file ) )
                    {
                        unlink( $path_extra . "/" . $size . "/" . $file );
                    }
                }
                $nok++;
            }
            if ( $progress )
            {
                $pb->update( $f_nr );
            }
            $f_nr++;
        }
        if ( $progress )
        {
            echo "\n";
        }
    }
    echo $f_anz . " Dateien gesamt\n";
    echo $ok . " Dateien beibehalten\n";
    echo $nok . " Dateien geloescht\n";
    echo $continued . " Dateien uebersprungen\n";
    //
 ?>