source: extensions/rv_sitemap/sitemap.php @ 17144

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

rv_sitemap: ability to select which derivatives to include in the sitemap

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 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        $selected_derivatives = array();
229  if ($photo_count > 0)
230  {
231                if (isset($_POST['selected_derivatives']))
232                        $selected_derivatives = $_POST['selected_derivatives'];
233
234                foreach($selected_derivatives as $type)
235                        $selected_derivatives_params[] = ImageStdParams::get_by_type($type);
236
237    $query = 'SELECT DISTINCT i.* FROM '.IMAGES_TABLE.' i
238  INNER JOIN '.IMAGE_CATEGORY_TABLE.' on i.id=image_id
239'.get_sql_condition_FandF( array('forbidden_categories' => 'category_id', 'forbidden_images'=>'i.id'), 'WHERE ' ).'
240  ORDER BY date_available DESC
241  LIMIT '.$photo_count;
242    $result = pwg_query($query);
243    while ($row = mysql_fetch_assoc($result))
244    {
245      $url = make_picture_url( array(
246        'image_id' => $row['id'],
247        'image_file' => $row['file'],
248        ) );
249      $src_image = new SrcImage($row);
250      $images_xml = '';
251                        $done_iurls=array();
252                        foreach( $selected_derivatives_params as $params )
253      {
254        $deriv_url = DerivativeImage::url($params, $src_image);
255                                if (!isset($done_iurls[$deriv_url]))
256                                {
257                                        $done_iurls[$deriv_url] = 1;
258                                        $images_xml .= '<image:image><image:loc>'.$deriv_url.'</image:loc></image:image>';
259                                }
260      }
261      add_url($url, $row['date_available'], null, null, $images_xml);
262    }
263  }
264  unset_make_full_url();
265  end_xml($gzip);
266
267  $page['infos'][] = $url_count.' urls saved';
268
269  // save the data for future use
270  $selected_tag_urls = array();
271  foreach( $tags as $tag)
272  {
273    if (isset($r_selected_tags[ $tag['id'] ] ) )
274      array_push($selected_tag_urls, $tag['url_name']);
275  }
276  $x = compact( 'filename', 'selected_tag_urls', 'prio_tags', 'freq_tags',
277  'selected_categories', 'prio_categories', 'freq_categories',
278  'selected_specials', 'photo_count' );
279  $file = fopen( sitemaps_get_config_file_name(), 'w' );
280  fwrite($file, serialize($x) );
281  fclose( $file );
282}
283else
284{
285  $filename = 'sitemap.xml';
286  $gzip = false;
287  $selected_specials = 'all';
288  $prio_categories = 0.5;
289  $prio_tags = 0.6;
290  $freq_categories = 'monthly';
291  $freq_tags = 'monthly';
292  $photo_count = 0;
293        $selected_derivatives = array();
294
295  $conf_file_name = sitemaps_get_config_file_name();
296  $old_file = dirname(__FILE__).'/_sitemap.dat';
297  if (file_exists($old_file) and !file_exists($conf_file_name) )
298  {
299    copy($old_file, $conf_file_name);
300    unlink($old_file);
301  }
302
303  $x = @file_get_contents( $conf_file_name );
304  if ($x!==false)
305  {
306    $x = unserialize($x);
307    extract($x);
308    $selected_tags = array();
309    if (isset($selected_tag_urls))
310    {
311      foreach($tags as $tag)
312      {
313        if ( in_array($tag['url_name'], $selected_tag_urls ) )
314          array_push($selected_tags, $tag['id']);
315      }
316      unset($selected_tag_urls);
317    }
318  }
319
320  if (!is_array(@$selected_categories)) $selected_categories = array();
321  if (!is_array(@$selected_tags)) $selected_tags = array();
322}
323
324// END AS GUEST
325$user = $save_user;
326
327
328$template->assign( array(
329  'FILENAME' => $filename,
330  'U_FILENAME' => get_root_url().$filename.($gzip?'.gz':''),
331  'GZIP_CHECKED' => $gzip ? 'checked="checked"' : '',
332  'PRIO_CATEGORIES' => $prio_categories,
333  'PRIO_TAGS' => $prio_tags,
334  'PHOTO_COUNT' => $photo_count,
335    )
336  );
337
338foreach( $specials as $key=>$value)
339{
340  $checked='';
341  if ($selected_specials=='all' or in_array($key, $selected_specials) )
342    $checked = 'checked="checked"';
343
344  $template->append( 'specials',
345    array(
346      'NAME' => $key,
347      'LABEL' =>  $value['L'],
348      'CHECKED' => $checked,
349      'PRIO' => $value['P'],
350      'FREQ' => isset($value['F']) ? $value['F'] : 'monthly',
351      )
352    );
353}
354
355display_select_categories($categories, $selected_categories, 'categories', false );
356$template->assign('freq_categories_selected', $freq_categories);
357
358
359foreach( $tags as $tag)
360  $template->append( 'tags', array($tag['id']=>$tag['name']), true );
361$template->assign('tags_selected', $selected_tags);
362$template->assign('freq_tags_selected', $freq_tags);
363
364
365$template->assign('frequencies', $frequencies);
366$template->assign('frequenciesT', $frequenciesT);
367
368$available_derivatives = array();
369foreach(array_keys(ImageStdParams::get_defined_type_map()) as $type)
370{
371        $available_derivatives[$type] = l10n($type);
372}
373$template->assign( array('available_derivatives'=>$available_derivatives, 'selected_derivatives' => $selected_derivatives));
374
375$template->set_filename('sitemap', dirname(__FILE__).'/sitemap.tpl');
376$template->assign_var_from_handle('ADMIN_CONTENT', 'sitemap');
377
378?>
Note: See TracBrowser for help on using the repository browser.