source: extensions/rv_sitemap/sitemap.php @ 16301

Last change on this file since 16301 was 16301, checked in by rvelices, 12 years ago

rv_sitemap add urls to picture pages

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1<?php
2defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
3
4define('RVS_DIR' , basename(dirname(__FILE__)));
5define('RVS_PATH' , PHPWG_PLUGINS_PATH . RVS_DIR . '/');
6load_language('plugin.lang', RVS_PATH);
7
8function sitemaps_get_config_file_name()
9{
10  global $conf;
11  $dir = $conf['local_data_dir'].'/plugins/';
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"?'.'>
22<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">', $gzip );
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
41function add_url($url, $lastmod=null, $changefreq=null, $priority=null, $images_xml=null)
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
65  if ( isset($images_xml) )
66    $xml .= "\n".$images_xml;
67 
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
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
93$frequencies = array(
94    '',
95    'always',
96    'hourly',
97    'daily',
98    'weekly',
99    'monthly',
100    'yearly',
101    'never',
102  );
103 
104$specials = array(
105  'categories' => array('L'=>l10n('Home'), 'P'=>0.9),
106  'best_rated' => array('L'=>l10n('Best rated'), 'P'=>0.8 ) ,
107  'most_visited' => array('L'=>l10n('Most visited'), 'P'=>0.8 ),
108  'recent_pics' => array('L'=>l10n('Recent photos'), 'P'=>0.8, 'F'=>'weekly' ),
109  'tags' => array('L'=>l10n('Tags'), 'PAGE'=>'tags.php' , 'P'=>0.8 ),
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';
124$result = pwg_query($query);
125$categories = array();
126while ($row = mysql_fetch_assoc($result))
127{
128  $categories[] = $row;
129}
130usort($categories, 'global_rank_compare');
131
132$tags = get_available_tags();
133usort($tags, 'name_compare');
134
135if ( isset($_POST['submit']) )
136{
137  // echo('<pre>'.var_export($_POST,true).'</pre>' );
138
139  if ( $_POST['filename'] != '' )
140    $filename = $_POST['filename'];
141  $gzip = @$_POST['gzip']=='on' ? true : false;
142
143  $selected_specials = array();
144  foreach ($specials as $key=>$value)
145  {
146    if ( @$_POST['special_'.$key]=='on' )
147      array_push( $selected_specials, $key );
148    $specials[$key]['P'] = $_POST['special_prio_'.$key];
149    $specials[$key]['F'] = $_POST['special_freq_'.$key];
150  }
151  if ( isset($_POST['categories']) )
152    $selected_categories = $_POST['categories'];
153  else
154    $selected_categories = array();
155  $prio_categories = $_POST['prio_categories'];
156  $freq_categories = $_POST['freq_categories'];
157
158  if ( isset($_POST['tags']) )
159    $selected_tags = $_POST['tags'];
160  else
161    $selected_tags = array();
162  $prio_tags = $_POST['prio_tags'];
163  $freq_tags = $_POST['freq_tags'];
164
165  $photo_count = intval($_POST['photo_count']);
166 
167  set_make_full_url();
168
169  start_xml($filename, $gzip);
170
171  $r_selected_specials = array_flip($selected_specials);
172  foreach ($specials as $key=>$value)
173  {
174    if (! isset ($r_selected_specials[$key]) )
175      continue;
176    if ( isset($value['PAGE']) )
177      $url = get_root_url().$value['PAGE'];
178    else
179      $url = make_index_url( array('section'=>$key) );
180    add_url($url, null, $value['F'], $value['P'] );
181  }
182
183
184  $r_selected_categories = array_flip($selected_categories);
185  foreach ($categories as $cat)
186  {
187    if (! isset ($r_selected_categories[$cat['id']]) )
188      continue;
189
190    $url = make_index_url(
191            array(
192              'category'=>$cat
193            )
194        );
195    add_url($url, $cat['date_last'], $freq_categories, $prio_categories);
196  }
197
198  $r_selected_tags = array_flip($selected_tags);
199  if ( !empty($selected_tags) )
200  {
201    $query = 'SELECT tag_id, MAX(date_available) AS da, MAX(date_creation) AS dc, MAX(date_metadata_update) AS dm
202  FROM '.IMAGE_TAG_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
203  WHERE tag_id IN ('.implode(',',$selected_tags).')
204  GROUP BY tag_id';
205    $result = pwg_query($query);
206    $tag_infos = array();
207    while ($row = mysql_fetch_assoc($result))
208    {
209      $tag_infos[$row['tag_id']]= max( $row['da'],$row['dc'],$row['dm']);
210    }
211
212    foreach( $tags as $tag)
213    {
214      if (!isset($r_selected_tags[ $tag['id'] ] ) )
215        continue;
216
217      $url = make_index_url(
218            array(
219              'section'=>'tags',
220              'tags'=> array( $tag )
221            )
222        );
223
224      add_url($url, $tag_infos[ $tag['id'] ], $freq_tags, $prio_tags);
225    }
226  }
227 
228  if ($photo_count > 0)
229  {
230    $query = 'SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i
231  INNER JOIN '.IMAGE_CATEGORY_TABLE.' on i.id=image_id
232'.get_sql_condition_FandF( array('forbidden_categories' => 'category_id', 'forbidden_images'=>'i.id'), 'WHERE ' ).'
233  ORDER BY date_available DESC
234  LIMIT '.$photo_count;
235    $result = pwg_query($query);
236    while ($row = mysql_fetch_assoc($result))
237    {
238      $url = make_picture_url( array(
239        'image_id' => $row['id'],
240        'image_file' => $row['file'],
241        ) );
242      $src_image = new SrcImage($row);
243      $images_xml = '';
244      foreach( array(IMG_THUMB, $conf['derivative_default_size']) as $derivative_type)
245      {
246        $deriv_url = DerivativeImage::url($derivative_type, $src_image);
247        $images_xml .= '<image:image><image:loc>'.$deriv_url.'</image:loc></image:image>';
248      }
249      add_url($url, $row['date_available'], null, null, $images_xml);
250    }
251  }
252  unset_make_full_url();
253  end_xml($gzip);
254
255  $page['infos'][] = $url_count.' urls saved';
256
257  // save the data for future use
258  $selected_tag_urls = array();
259  foreach( $tags as $tag)
260  {
261    if (isset($r_selected_tags[ $tag['id'] ] ) )
262      array_push($selected_tag_urls, $tag['url_name']);
263  }
264  $x = compact( 'filename', 'selected_tag_urls', 'prio_tags', 'freq_tags',
265  'selected_categories', 'prio_categories', 'freq_categories',
266  'selected_specials', 'photo_count' );
267  $file = fopen( sitemaps_get_config_file_name(), 'w' );
268  fwrite($file, serialize($x) );
269  fclose( $file );
270}
271else
272{
273  $filename = 'sitemap.xml';
274  $gzip = false;
275  $selected_specials = 'all';
276  $prio_categories = 0.5;
277  $prio_tags = 0.6;
278  $freq_categories = 'monthly';
279  $freq_tags = 'monthly';
280  $photo_count = 0;
281 
282  $conf_file_name = sitemaps_get_config_file_name();
283  $old_file = dirname(__FILE__).'/_sitemap.dat';
284  if (file_exists($old_file) and !file_exists($conf_file_name) )
285  {
286    copy($old_file, $conf_file_name);
287    unlink($old_file);
288  }
289
290  $x = @file_get_contents( $conf_file_name );
291  if ($x!==false)
292  {
293    $x = unserialize($x);
294    extract($x);
295    $selected_tags = array();
296    if (isset($selected_tag_urls))
297    {
298      foreach($tags as $tag)
299      {
300        if ( in_array($tag['url_name'], $selected_tag_urls ) )
301          array_push($selected_tags, $tag['id']);
302      }
303      unset($selected_tag_urls);
304    }
305  }
306
307  if (!is_array(@$selected_categories)) $selected_categories = array();
308  if (!is_array(@$selected_tags)) $selected_tags = array();
309}
310
311// END AS GUEST
312$user = $save_user;
313
314
315$template->assign( array(
316  'FILENAME' => $filename,
317  'U_FILENAME' => get_root_url().$filename.($gzip?'.gz':''),
318  'GZIP_CHECKED' => $gzip ? 'checked="checked"' : '',
319  'PRIO_CATEGORIES' => $prio_categories,
320  'PRIO_TAGS' => $prio_tags,
321  'PHOTO_COUNT' => $photo_count,
322    )
323  );
324
325foreach( $specials as $key=>$value)
326{
327  $checked='';
328  if ($selected_specials=='all' or in_array($key, $selected_specials) )
329    $checked = 'checked="checked"';
330
331  $template->append( 'specials',
332    array(
333      'NAME' => $key,
334      'LABEL' =>  $value['L'],
335      'CHECKED' => $checked,
336      'PRIO' => $value['P'],
337      'FREQ' => isset($value['F']) ? $value['F'] : 'monthly',
338      )
339    );
340}
341
342display_select_categories($categories, $selected_categories, 'categories', false );
343$template->assign('freq_categories_selected', $freq_categories);
344
345
346foreach( $tags as $tag)
347  $template->append( 'tags', array($tag['id']=>$tag['name']), true );
348$template->assign('tags_selected', $selected_tags);
349$template->assign('freq_tags_selected', $freq_tags);
350
351
352$template->assign('frequencies', $frequencies);
353$template->assign('frequenciesT', $frequenciesT);
354
355$template->set_filename('sitemap', dirname(__FILE__).'/sitemap.tpl');
356$template->assign_var_from_handle('ADMIN_CONTENT', 'sitemap');
357
358?>
Note: See TracBrowser for help on using the repository browser.