source: extensions/AMetaData/amd_root.class.inc.php @ 5183

Last change on this file since 5183 was 5183, checked in by grum, 15 years ago

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)

  • Property svn:executable set to *
File size: 7.3 KB
Line 
1<?php
2/*
3 * -----------------------------------------------------------------------------
4 * Plugin Name: Advanced MetaData
5 * -----------------------------------------------------------------------------
6 * Author     : Grum
7 *   email    : grum@piwigo.org
8 *   website  : http://photos.grum.fr
9 *   PWG user : http://forum.piwigo.org/profile.php?id=3706
10 *
11 *   << May the Little SpaceFrog be with you ! >>
12 *
13 * -----------------------------------------------------------------------------
14 *
15 * See main.inc.php for release information
16 *
17 * AMD_install : classe to manage plugin install
18 * ---------------------------------------------------------------------------
19 */
20
21if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
22
23include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/common_plugin.class.inc.php');
24include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/css.class.inc.php');
25
26include_once('JpegMetaData/JpegMetaData.class.php');
27include_once(JPEG_METADATA_DIR."Common/L10n.class.php");
28
29class AMD_root extends common_plugin
30{
31  protected $css;   //the css object
32  protected $jpegMD;
33
34  public function __construct($prefixeTable, $filelocation)
35  {
36    $this->plugin_name="AMetaData";
37    $this->plugin_name_files="amd";
38    parent::__construct($prefixeTable, $filelocation);
39
40    $tableList=array('used_tags', 'images_tags', 'images', 'selected_tags', 'groups_names', 'groups');
41    $this->set_tables_list($tableList);
42
43    $this->css = new css(dirname($this->filelocation).'/'.$this->plugin_name_files.".css");
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
54
55  /* ---------------------------------------------------------------------------
56  common AIP & PIP functions
57  --------------------------------------------------------------------------- */
58
59  /* this function initialize var $my_config with default values */
60  public function init_config()
61  {
62    $this->my_config=array(
63      'amd_NumberOfItemsPerRequest' => 25,
64      'amd_GetListTags_OrderType' => "tag",
65      'amd_GetListTags_FilterType' => "magic",
66      'amd_GetListTags_ExcludeUnusedTag' => "y",
67      'amd_GetListTags_SelectedTagOnly' => "n",
68      'amd_GetListImages_OrderType' => "value",
69      'amd_FillDataBaseContinuously' => "y",
70      'amd_AllPicturesAreAnalyzed' => "n",
71    );
72  }
73
74  public function load_config()
75  {
76    parent::load_config();
77  }
78
79  public function init_events()
80  {
81    parent::init_events();
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
272
273} // amd_root  class
274
275
276
277?>
Note: See TracBrowser for help on using the repository browser.