Ignore:
Timestamp:
Mar 18, 2010, 11:47:11 PM (14 years ago)
Author:
grum
Message:

database is partially filled during the install process, and fill database picture per picture each time a page is displayed (and until all the database is filled)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/AMetaData/amd_root.class.inc.php

    r5088 r5183  
    3030{
    3131  protected $css;   //the css object
    32 
    33   function AMD_root($prefixeTable, $filelocation)
     32  protected $jpegMD;
     33
     34  public function __construct($prefixeTable, $filelocation)
    3435  {
    3536    $this->plugin_name="AMetaData";
     
    4142
    4243    $this->css = new css(dirname($this->filelocation).'/'.$this->plugin_name_files.".css");
    43   }
     44    $this->jpegMD=new JpegMetaData();
     45  }
     46
     47  public function __destruct()
     48  {
     49    unset($this->jpegMD);
     50    unset($this->css);
     51    //parent::__destruct();
     52  }
     53
    4454
    4555  /* ---------------------------------------------------------------------------
     
    5666      'amd_GetListTags_ExcludeUnusedTag' => "y",
    5767      'amd_GetListTags_SelectedTagOnly' => "n",
    58       'amd_GetListImages_OrderType' => "value"
     68      'amd_GetListImages_OrderType' => "value",
     69      'amd_FillDataBaseContinuously' => "y",
     70      'amd_AllPicturesAreAnalyzed' => "n",
    5971    );
    6072  }
     
    6375  {
    6476    parent::load_config();
    65     if(!$this->css->css_file_exists())
    66     {
    67       $this->css->make_CSS($this->generate_CSS());
    68     }
    6977  }
    7078
     
    7280  {
    7381    parent::init_events();
    74   }
    75 
    76   /*
    77    * generate the css code
    78    */
    79   function generate_CSS()
    80   {
    81     $text = "
    82       .formtable, .formtable P { text-align:left; display:block; }
    83       .formtable tr { vertical-align:top; }
    84       .littlefont { font-size:90%; }
    85       .littlefont td { padding:1px; }
    86       table.littlefont th { padding:3px; text-align:left;}
    87       table.littlefont td { padding:1px 3px; }
    88       #iprogressbar_contener { border:1px solid #606060; margin:0px; padding:0px; display:block; height:20px; }
    89       #iprogressbar_bg { background:#606060; display:block; z-index:100; position:relative; height:20px; }
    90       #iprogressbar_fg { color:#FF3363; width:100%; text-align:center; display: block; z-index:200; position:relative; top:-18px;  }
    91       #iHeaderListTags { width:100%; border:1px solid; border-collapse: collapse; margin-top:3px; }
    92       #iListTags { width:100%; border:1px solid; height:280px; border-top:0px; overflow:auto;}
    93       #iListImages { width:100%; border-bottom:1px solid; overflow:auto;}
    94       #iListTagsNb, #iListImagesNb { width:99%; text-align:right; margin-bottom:8px; padding:2px; font-size:80%; }
    95       #iListTags table, #iListImages table, table.listTags { width:100%; text-align:left; border-collapse: collapse; }
    96       #iListTags table tr:hover { cursor:pointer; background:#303030; }
    97       #iListImages table tr:hover, table.listTags tr:hover { background:#303030; cursor:default; }
    98       #iHeaderListImages { width:100%; border-bottom:1px solid; }
    99       .warning { color:#dd0000; border:1px solid #dd0000; margin-bottom:8px; margin-top:8px; padding:8px; }
    100       .warning p { margin-top:0.5em; margin-bottom:0em; }
    101       .warning ul { margin-top:0em; margin-bottom:0.5em; }
    102       .pctBar { height:6px; background:#FF7700; }
    103       li.groupItems { border:1px solid #666666; margin-bottom:5px; padding:0 5px; width:90%; cursor:move; padding:4px; }
    104       div.addGroup { padding-left:40px; text-align:left; }
    105       #iGroups { list-style: none; }
    106       .ui-dialog { background: #222222; border:2px solid #FF3363; }
    107       .ui-dialog-buttonpane { padding:4px; }
    108       .ui-dialog-buttonpane button { margin-right:8px; }
    109       .ui-dialog-titlebar { background:#111111; font-weight:bold; }
    110       .ui-dialog-title-dialog { text-align: left; }
    111       .ui-dialog-titlebar-close { float: right; }
    112       .ui-dialog-content { overflow:auto; }
    113       .ui-dialog-container { }
    114       .ui-dialog-titlebar-close { display:none; }
    115       .tagListOrder { list-style: none; padding:0px; margin-right:8px; margin-left:35px; }
    116       .tagListOrder li { border:none; background:#333333; padding:1px; margin-bottom:2px; width:100%; }
    117       .groupTags { padding-top:8px; }
    118       .editGroupListButton { margin-left:8px; position:absolute; z-index:1000; }
    119       table.tagListOrderItem { width:100%; border-collapse:collapse; }
    120       .dialogForm { text-align:left; margin:8px; }
    121       #ianalyzestatus { background: #333333; margin:8px; padding:8px; }
    122       #ianalyzestatus ul { margin:0px; padding:0 0 0 20px; }
    123       #iamd_nb_item_per_request_display { display:inline-block; width:70px; }
    124       #iamd_nb_item_per_request_slider { display:inline-block; width:350px; }
    125       #iDialogProgress { margin:16px 8px 8px; }
    126     ";
    127 
    128     return($text);
    129   }
     82
     83
     84    if(!isset($_REQUEST['ajaxfct']) and
     85       $this->my_config['amd_FillDataBaseContinuously']=='y' and
     86       $this->my_config['amd_AllPicturesAreAnalyzed']=='n')
     87    {
     88      /* do analyze for a random picture only if :
     89       *  - config is set to fill database continuously
     90       *  - we are not in an ajax call
     91       */
     92      add_event_handler('init', array(&$this, 'doRandomAnalyze'));
     93    }
     94  }
     95
     96
     97  /**
     98   * returns the number of pictures analyzed
     99   *
     100   * @return Integer
     101   */
     102  protected function getNumOfPictures()
     103  {
     104    $numOfPictures=0;
     105    $sql="SELECT COUNT(imageId) FROM ".$this->tables['images']."
     106            WHERE analyzed='y';";
     107    $result=pwg_query($sql);
     108    if($result)
     109    {
     110      while($row=mysql_fetch_row($result))
     111      {
     112        $numOfPictures=$row[0];
     113      }
     114    }
     115    return($numOfPictures);
     116  }
     117
     118
     119  /**
     120   * this function randomly choose a picture in the list of pictures not
     121   * analyzed, and analyze it
     122   *
     123   */
     124  public function doRandomAnalyze()
     125  {
     126    $sql="SELECT tai.imageId, ti.path FROM ".$this->tables['images']." tai
     127            LEFT JOIN ".IMAGES_TABLE." ti ON tai.imageId = ti.id
     128          WHERE tai.analyzed = 'n'
     129          ORDER BY RAND() LIMIT 1;";
     130    $result=pwg_query($sql);
     131    if($result)
     132    {
     133      // $path = path of piwigo's on the server filesystem
     134      $path=dirname(dirname(dirname(__FILE__)));
     135
     136      while($row=mysql_fetch_assoc($result))
     137      {
     138        $this->analyzeImageFile($path."/".$row['path'], $row['imageId']);
     139      }
     140
     141      $this->makeStatsConsolidation();
     142    }
     143  }
     144
     145
     146  /**
     147   * this function analyze tags from a picture, and insert the result into the
     148   * database
     149   *
     150   * NOTE : only implemented tags are analyzed and stored
     151   *
     152   * @param String $fileName : filename of picture to analyze
     153   * @param Integer $imageId : id of image in piwigo's database
     154   * @param Boolean $loaded  : default = false
     155   *                            WARNING
     156   *                            if $loaded is set to TRUE, the function assume
     157   *                            that the metadata have been alreay loaded
     158   *                            do not use the TRUE value if you are not sure
     159   *                            of the consequences
     160   */
     161  protected function analyzeImageFile($fileName, $imageId, $loaded=false)
     162  {
     163    /*
     164     * the JpegMetaData object is instancied in the constructor
     165     */
     166    if(!$loaded)
     167    {
     168      $this->jpegMD->load(
     169        $fileName,
     170        Array(
     171          'filter' => JpegMetaData::TAGFILTER_IMPLEMENTED,
     172          'optimizeIptcDateTime' => true,
     173          'exif' => true,
     174          'iptc' => true,
     175          'xmp' => true
     176        )
     177      );
     178    }
     179
     180    $sqlInsert="";
     181    $massInsert=array();
     182    $nbTags=0;
     183    foreach($this->jpegMD->getTags() as $key => $val)
     184    {
     185      $value=$val->getLabel();
     186
     187      if($val->isTranslatable())
     188        $translatable="y";
     189      else
     190        $translatable="n";
     191
     192      if($value instanceof DateTime)
     193      {
     194        $value=$value->format("Y-m-d H:i:s");
     195      }
     196      elseif(is_array($value))
     197      {
     198        /*
     199         * array values are stored in a serialized string
     200         */
     201        $value=serialize($value);
     202      }
     203
     204      $sql="SELECT numId FROM ".$this->tables['used_tags']." WHERE tagId = '$key'";
     205
     206      $result=pwg_query($sql);
     207      if($result)
     208      {
     209        $numId=-1;
     210        while($row=mysql_fetch_assoc($result))
     211        {
     212          $numId=$row['numId'];
     213        }
     214
     215        if($numId>0)
     216        {
     217          $nbTags++;
     218          if($sqlInsert!="") $sqlInsert.=", ";
     219          $sqlInsert.="($imageId, '$numId', '".addslashes($value)."')";
     220          $massInsert[]="('$imageId', '$numId', '".addslashes($value)."') ";
     221        }
     222      }
     223    }
     224
     225    if(count($massInsert)>0)
     226    {
     227      $sql="REPLACE INTO ".$this->tables['images_tags']." (imageId, numId, value) VALUES ".implode(", ", $massInsert).";";
     228      pwg_query($sql);
     229    }
     230    //mass_inserts($this->tables['images_tags'], array('imageId', 'numId', 'value'), $massInsert);
     231
     232    $sql="UPDATE ".$this->tables['images']."
     233            SET analyzed = 'y', nbTags=".$nbTags."
     234            WHERE imageId=$imageId;";
     235    pwg_query($sql);
     236
     237
     238    return("$imageId=$nbTags;");
     239  }
     240
     241
     242  /**
     243   * do some consolidations on database to optimize other requests
     244   *
     245   */
     246  protected function makeStatsConsolidation()
     247  {
     248    $sql="UPDATE ".$this->tables['used_tags']." ut,
     249            (SELECT COUNT(imageId) AS nb, numId
     250              FROM ".$this->tables['images_tags']."
     251              GROUP BY numId) nb
     252          SET ut.numOfImg = nb.nb
     253          WHERE ut.numId = nb.numId;";
     254    pwg_query($sql);
     255
     256
     257    $sql="SELECT COUNT(imageId) AS nb
     258          FROM ".$this->tables['images']."
     259          WHERE analyzed = 'n';";
     260    $result=pwg_query($sql);
     261    if($result)
     262    {
     263      while($row=mysql_fetch_assoc($result))
     264      {
     265        $this->my_config['amd_AllPicturesAreAnalyzed']=($row['nb']==0)?'y':'n';
     266      }
     267
     268    }
     269    $this->save_config();
     270  }
     271
    130272
    131273} // amd_root  class
Note: See TracChangeset for help on using the changeset viewer.