source: extensions/rv_sitemap/sitemap.php @ 20461

Last change on this file since 20461 was 20461, checked in by rvelices, 11 years ago

rv* plugins use pwg_db_* instead of mysql_* functions

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
RevLine 
[3417]1<?php
2defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
3
[8984]4define('RVS_DIR' , basename(dirname(__FILE__)));
5define('RVS_PATH' , PHPWG_PLUGINS_PATH . RVS_DIR . '/');
6load_language('plugin.lang', RVS_PATH);
7
[3417]8function sitemaps_get_config_file_name()
9{
10  global $conf;
[17516]11  $dir = PHPWG_ROOT_PATH.$conf['data_location'].'plugins/';
[3417]12  mkgetdir( $dir );
13  return $dir.basename(dirname(__FILE__)).'.dat';
14}
15
16function start_xml($filename, $gzip)
17{
18  global $file;
19  $file = fopen( $filename.($gzip?'.gz':''), 'w' );
20  out_xml('<?xml version="1.0" encoding="UTF-8"?'.'>
21<?xml-stylesheet type="text/xsl" href="'.get_root_url().'plugins/'.basename(dirname(__FILE__)).'/sitemap.xsl"?'.'>
[16301]22<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">', $gzip );
[3417]23}
24
25function out_xml($xml, $gzip)
26{
27  global $file;
28  if ($gzip)
29    fwrite($file, gzencode($xml, 9) );
30  else
31    fwrite($file, $xml);
32}
33
34function end_xml($gzip)
35{
36  global $file;
37  out_xml('</urlset>',$gzip );
38  fclose( $file );
39}
40
[16301]41function add_url($url, $lastmod=null, $changefreq=null, $priority=null, $images_xml=null)
[3417]42{
43  $xml=
44'<url>
45 <loc>'.$url.'</loc>';
46
47  if ( isset($lastmod) and strlen($lastmod)>0 )
48  {
49    if (strlen($lastmod)>11)
50    {
51      $lastmod[10] = 'T';
52      if (strlen($lastmod)==19)
53        $lastmod .= '-05:00';
54    }
55    $xml.='
56 <lastmod>'.$lastmod.'</lastmod>';
57  }
58
59  if ( isset($changefreq) and $changefreq!='' ) $xml.='
60 <changefreq>'.$changefreq.'</changefreq>';
61
62  if ( isset($priority) and $priority!='' ) $xml.='
63 <priority>'.$priority.'</priority>';
64
[16301]65  if ( isset($images_xml) )
66    $xml .= "\n".$images_xml;
[16940]67
[3417]68  $xml .= '
69</url>';
70global $gzip,$url_count;
71  $url_count++;
72  out_xml($xml, $gzip);
73}
74
75
76
77include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
78check_status(ACCESS_ADMINISTRATOR);
79
80
81
[8984]82$frequenciesT = array(
83    '',
84    l10n('always'),
85    l10n('hourly'),
86    l10n('daily'),
87    l10n('weekly'),
88    l10n('monthly'),
89    l10n('yearly'),
90    l10n('never'),
91  );
92
[3417]93$frequencies = array(
94    '',
95    'always',
96    'hourly',
97    'daily',
98    'weekly',
99    'monthly',
100    'yearly',
101    'never',
102  );
[16940]103
[3417]104$specials = array(
[6260]105  'categories' => array('L'=>l10n('Home'), 'P'=>0.9),
[8984]106  'best_rated' => array('L'=>l10n('Best rated'), 'P'=>0.8 ) ,
[6260]107  'most_visited' => array('L'=>l10n('Most visited'), 'P'=>0.8 ),
[8666]108  'recent_pics' => array('L'=>l10n('Recent photos'), 'P'=>0.8, 'F'=>'weekly' ),
[13075]109  'tags' => array('L'=>l10n('Tags'), 'PAGE'=>'tags.php' , 'P'=>0.8 ),
[3417]110  );
111
112$url_count=0;
113
114// BEGIN AS GUEST
115$save_user = $user;
116$user = build_user( $conf['guest_id'], true);
117
118$query = '
119SELECT id, name, permalink, uppercats, global_rank, IFNULL(date_last, max_date_last) AS date_last
120  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
121  ON id = cat_id AND user_id = '.$conf['guest_id'].'
122  WHERE max_date_last IS NOT NULL
123  ORDER BY global_rank';
[20461]124$categories = array_from_query($query);
[3417]125usort($categories, 'global_rank_compare');
126
127$tags = get_available_tags();
128usort($tags, 'name_compare');
129
130if ( isset($_POST['submit']) )
131{
132  // echo('<pre>'.var_export($_POST,true).'</pre>' );
133
134  if ( $_POST['filename'] != '' )
135    $filename = $_POST['filename'];
136  $gzip = @$_POST['gzip']=='on' ? true : false;
137
138  $selected_specials = array();
139  foreach ($specials as $key=>$value)
140  {
141    if ( @$_POST['special_'.$key]=='on' )
142      array_push( $selected_specials, $key );
143    $specials[$key]['P'] = $_POST['special_prio_'.$key];
144    $specials[$key]['F'] = $_POST['special_freq_'.$key];
145  }
146  if ( isset($_POST['categories']) )
147    $selected_categories = $_POST['categories'];
148  else
149    $selected_categories = array();
150  $prio_categories = $_POST['prio_categories'];
151  $freq_categories = $_POST['freq_categories'];
152
153  if ( isset($_POST['tags']) )
154    $selected_tags = $_POST['tags'];
155  else
156    $selected_tags = array();
157  $prio_tags = $_POST['prio_tags'];
158  $freq_tags = $_POST['freq_tags'];
159
[16301]160  $photo_count = intval($_POST['photo_count']);
[16940]161
[3417]162  set_make_full_url();
163
164  start_xml($filename, $gzip);
165
166  $r_selected_specials = array_flip($selected_specials);
167  foreach ($specials as $key=>$value)
168  {
169    if (! isset ($r_selected_specials[$key]) )
170      continue;
171    if ( isset($value['PAGE']) )
172      $url = get_root_url().$value['PAGE'];
173    else
174      $url = make_index_url( array('section'=>$key) );
175    add_url($url, null, $value['F'], $value['P'] );
176  }
177
178
179  $r_selected_categories = array_flip($selected_categories);
180  foreach ($categories as $cat)
181  {
182    if (! isset ($r_selected_categories[$cat['id']]) )
183      continue;
184
185    $url = make_index_url(
186            array(
187              'category'=>$cat
188            )
189        );
190    add_url($url, $cat['date_last'], $freq_categories, $prio_categories);
191  }
192
193  $r_selected_tags = array_flip($selected_tags);
194  if ( !empty($selected_tags) )
195  {
196    $query = 'SELECT tag_id, MAX(date_available) AS da, MAX(date_creation) AS dc, MAX(date_metadata_update) AS dm
197  FROM '.IMAGE_TAG_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
198  WHERE tag_id IN ('.implode(',',$selected_tags).')
199  GROUP BY tag_id';
200    $result = pwg_query($query);
201    $tag_infos = array();
[20461]202    while ($row = pwg_db_fetch_assoc($result))
[3417]203    {
204      $tag_infos[$row['tag_id']]= max( $row['da'],$row['dc'],$row['dm']);
205    }
206
207    foreach( $tags as $tag)
208    {
209      if (!isset($r_selected_tags[ $tag['id'] ] ) )
210        continue;
211
212      $url = make_index_url(
213            array(
214              'section'=>'tags',
215              'tags'=> array( $tag )
216            )
217        );
218
219      add_url($url, $tag_infos[ $tag['id'] ], $freq_tags, $prio_tags);
220    }
221  }
[16940]222
[17144]223        $selected_derivatives = array();
[16301]224  if ($photo_count > 0)
225  {
[17144]226                if (isset($_POST['selected_derivatives']))
227                        $selected_derivatives = $_POST['selected_derivatives'];
228
229                foreach($selected_derivatives as $type)
230                        $selected_derivatives_params[] = ImageStdParams::get_by_type($type);
231
[16301]232    $query = 'SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i
[16940]233  INNER JOIN '.IMAGE_CATEGORY_TABLE.' on i.id=image_id
[16301]234'.get_sql_condition_FandF( array('forbidden_categories' => 'category_id', 'forbidden_images'=>'i.id'), 'WHERE ' ).'
235  ORDER BY date_available DESC
236  LIMIT '.$photo_count;
237    $result = pwg_query($query);
[20461]238    while ($row = pwg_db_fetch_assoc($result))
[16301]239    {
240      $url = make_picture_url( array(
241        'image_id' => $row['id'],
242        'image_file' => $row['file'],
243        ) );
244      $src_image = new SrcImage($row);
245      $images_xml = '';
[17144]246                        $done_iurls=array();
247                        foreach( $selected_derivatives_params as $params )
[16301]248      {
[17144]249        $deriv_url = DerivativeImage::url($params, $src_image);
250                                if (!isset($done_iurls[$deriv_url]))
251                                {
252                                        $done_iurls[$deriv_url] = 1;
253                                        $images_xml .= '<image:image><image:loc>'.$deriv_url.'</image:loc></image:image>';
254                                }
[16301]255      }
256      add_url($url, $row['date_available'], null, null, $images_xml);
257    }
258  }
[3417]259  unset_make_full_url();
260  end_xml($gzip);
261
262  $page['infos'][] = $url_count.' urls saved';
263
264  // save the data for future use
265  $selected_tag_urls = array();
266  foreach( $tags as $tag)
267  {
268    if (isset($r_selected_tags[ $tag['id'] ] ) )
269      array_push($selected_tag_urls, $tag['url_name']);
270  }
271  $x = compact( 'filename', 'selected_tag_urls', 'prio_tags', 'freq_tags',
272  'selected_categories', 'prio_categories', 'freq_categories',
[17232]273  'selected_specials', 'photo_count', 'selected_derivatives' );
[3417]274  $file = fopen( sitemaps_get_config_file_name(), 'w' );
275  fwrite($file, serialize($x) );
276  fclose( $file );
277}
278else
279{
280  $filename = 'sitemap.xml';
281  $gzip = false;
282  $selected_specials = 'all';
283  $prio_categories = 0.5;
284  $prio_tags = 0.6;
285  $freq_categories = 'monthly';
286  $freq_tags = 'monthly';
[16301]287  $photo_count = 0;
[17144]288        $selected_derivatives = array();
[16940]289
[3417]290  $conf_file_name = sitemaps_get_config_file_name();
291  $old_file = dirname(__FILE__).'/_sitemap.dat';
292  if (file_exists($old_file) and !file_exists($conf_file_name) )
293  {
294    copy($old_file, $conf_file_name);
295    unlink($old_file);
296  }
297
298  $x = @file_get_contents( $conf_file_name );
299  if ($x!==false)
300  {
301    $x = unserialize($x);
302    extract($x);
303    $selected_tags = array();
304    if (isset($selected_tag_urls))
305    {
306      foreach($tags as $tag)
307      {
308        if ( in_array($tag['url_name'], $selected_tag_urls ) )
309          array_push($selected_tags, $tag['id']);
310      }
311      unset($selected_tag_urls);
312    }
313  }
314
315  if (!is_array(@$selected_categories)) $selected_categories = array();
316  if (!is_array(@$selected_tags)) $selected_tags = array();
317}
318
[16301]319// END AS GUEST
320$user = $save_user;
[3417]321
[16301]322
[3417]323$template->assign( array(
324  'FILENAME' => $filename,
325  'U_FILENAME' => get_root_url().$filename.($gzip?'.gz':''),
326  'GZIP_CHECKED' => $gzip ? 'checked="checked"' : '',
327  'PRIO_CATEGORIES' => $prio_categories,
328  'PRIO_TAGS' => $prio_tags,
[16301]329  'PHOTO_COUNT' => $photo_count,
[3417]330    )
331  );
332
333foreach( $specials as $key=>$value)
334{
335  $checked='';
336  if ($selected_specials=='all' or in_array($key, $selected_specials) )
337    $checked = 'checked="checked"';
338
339  $template->append( 'specials',
340    array(
341      'NAME' => $key,
342      'LABEL' =>  $value['L'],
343      'CHECKED' => $checked,
344      'PRIO' => $value['P'],
345      'FREQ' => isset($value['F']) ? $value['F'] : 'monthly',
346      )
347    );
348}
349
350display_select_categories($categories, $selected_categories, 'categories', false );
351$template->assign('freq_categories_selected', $freq_categories);
352
353
354foreach( $tags as $tag)
355  $template->append( 'tags', array($tag['id']=>$tag['name']), true );
356$template->assign('tags_selected', $selected_tags);
357$template->assign('freq_tags_selected', $freq_tags);
358
359
360$template->assign('frequencies', $frequencies);
[8984]361$template->assign('frequenciesT', $frequenciesT);
[3417]362
[17144]363$available_derivatives = array();
364foreach(array_keys(ImageStdParams::get_defined_type_map()) as $type)
365{
366        $available_derivatives[$type] = l10n($type);
367}
368$template->assign( array('available_derivatives'=>$available_derivatives, 'selected_derivatives' => $selected_derivatives));
369
[3417]370$template->set_filename('sitemap', dirname(__FILE__).'/sitemap.tpl');
371$template->assign_var_from_handle('ADMIN_CONTENT', 'sitemap');
372
373?>
Note: See TracBrowser for help on using the repository browser.