OSDN Git Service

NP_PubMed v 0.2.1
[nucleus-jp/nucleus-plugins.git] / trunk / NP_SearchenginesSitemapsGenerator / NP_SearchenginesSitemapsGenerator.php
1 <?php
2
3 /** ============================================================================
4   * GoogleSitemap for Nucleus
5   *
6   * Copyright 2005 by Niels Leenheer
7   * ============================================================================
8   * This program is free software and open source software; you can redistribute
9   * it and/or modify it under the terms of the GNU General Public License as
10   * published by the Free Software Foundation; either version 2 of the License,
11   * or (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful, but WITHOUT
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16   * more details.
17   *
18   * You should have received a copy of the GNU General Public License along
19   * with this program; if not, write to the Free Software Foundation, Inc.,
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
21   * http://www.gnu.org/licenses/gpl.html
22   * ============================================================================
23   **/
24
25 /**
26   * History
27   *  0.7    modified release by shizuki
28   *             Generate URL modified from
29   *               'http://example.com/action.php?action=plugin&name=Sitemap' to
30   *               'http://example.com/sitemap.xml' and,or
31   *               'http://example.com/index.php?virtualpath=sitemap.xml'
32   *             Add 'lastmod' attribute
33   *  0.9    SitemapProtocol updated release
34   *             SitemapProtocol ver.0.9 as common for Google, Yahoo! and MSN(Live! Search)
35   *  1.0    Add Sitemap type and chage 'lastmod' generate
36   *             Add 'ROR Sitemap' format
37   *               For details about the ROR format, go to www.rorweb.com
38   *             Modify 'lastmod' attribute
39   *               item posted time or comment posted time or item update time
40   *               item update time generate by NP_UpdateTime
41   *  1.1    Send Sitemaps to Yahoo!
42   **/
43
44 class NP_SearchenginesSitemapsGenerator extends NucleusPlugin
45 {
46
47         function getName()
48         {
49                 return 'SearchenginesSitemapsGenerator';
50         }
51
52         function getAuthor()
53         {
54                 return 'Niels Leenheer + shizuki';
55         }
56
57         function getURL()
58         {
59                 return 'http://japan.nucleuscms.org/wiki/plugins:searchenginesitemapgenerator';
60         }
61
62         function getVersion()
63         {
64                 return '1.1';
65         }
66
67         function getDescription()
68         {
69                 return _G_SITEMAP_DESC;
70         }
71         
72         function getEventList()
73         {
74                 return array(
75                                          'PostAddItem',
76                                          'PreSendContentType'
77                                         );
78         }
79         
80         function supportsFeature($feature)
81         {
82         switch($feature) {
83                 case 'SqlTablePrefix':
84                         return 1;
85                 default:
86                         return 0;
87                 }
88         }
89
90         function event_PreSendContentType($data)
91         {
92                 global $CONF, $manager, $blogid;
93
94                 $mcategories = $this->pluginCheck('MultipleCategories');
95                 if ($mcategories) {
96                         if (method_exists($mcategories, 'getRequestName')) {
97                                 $subReq = $mcategories->getRequestName();
98                         } else {
99                                 $subReq = 'subcatid';
100                         }
101                 }
102                 $npUpdateTime = $this->pluginCheck('UpdateTime');
103
104                 if (!$blogid) {
105                         $blogid = $CONF['DefaultBlog'];
106                 } else {
107                         if (is_numeric($blogid)) {
108                                 $blogid = intval($blogid);
109                         } else {
110                                 $blogid = intval(getBlogIDFromName($blogid));
111                         }
112                 }
113
114                 $b       =& $manager->getBlog($blogid);
115                 $BlogURL = $b->getURL();
116                 if (!$BlogURL) {
117                         $BlogURL = $CONF['IndexURL'];
118                 }
119
120                 if ( substr($BlogURL, -1) != '/'
121                   && substr($BlogURL, -4) != '.php') {
122                         $BlogURL .= '/';
123                 }
124
125                 if (getVar('virtualpath')) {
126                         $info = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', getVar('virtualpath'));
127                 } elseif (serverVar('PATH_INFO')) {
128                         $info = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', serverVar('PATH_INFO'));
129                 } else {
130                         return;
131                 }
132
133                 $path_arr  = explode('/', $info);
134                 $PcMap     = $this->getBlogOption($blogid, 'PcSitemap');
135                 $MobileMap = $this->getBlogOption($blogid, 'MobileSitemap');
136                 if ( end($path_arr) == $PcMap
137                   || end($path_arr) == 'ror.xml'
138                   || (!empty($MobileMap) && end($path_arr) == $MobileMap) ) {
139                         $sitemap = array();
140                         if ( $this->getOption('AllBlogMap') == 'yes'
141                           && $blogid == $CONF['DefaultBlog']) {
142                                 $blogQuery  = 'SELECT * '
143                                                         . 'FROM %s '
144                                                         . 'ORDER BY bnumber';
145                                 $blogQuery  = sprintf($blogQuery, sql_table('blog'));
146                                 $blogResult = sql_query($blogQuery);
147                         } else {
148                                 $blogQuery   = 'SELECT * '
149                                                          . 'FROM %s '
150                                                          . 'WHERE bnumber = %d';
151                                 $blogQuery   = sprintf($blogQuery, sql_table('blog'), $blogid);
152                                 $blogResult  = sql_query($blogQuery);
153                                 $currentBlog = TRUE;
154                         }
155                         while ($blogs = mysql_fetch_array($blogResult)) {
156                                 $blog_id = intval($blogs['bnumber']);
157                                 if (  $this->getBlogOption($blog_id, 'IncludeSitemap') == 'yes'
158                                    || !empty($currentBlog)) {
159                                         $temp_b  =& $manager->getBlog($blog_id);
160                                         $TempURL =  $temp_b->getURL();
161                                         $SelfURL =  $TempURL;
162
163                                         $URLMode = $CONF['URLMode'];
164                                         if (substr($TempURL, -4) == '.php') {
165                                                 $CONF['URLMode'] = 'normal';
166                                         }
167
168                                         $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
169
170                                         if (substr($SelfURL, -1) == '/') {
171
172                                                 if ($usePathInfo) {
173                                                         $SelfURL = substr($SelfURL, 0, -1);
174                                                 } else {
175                                                         $SelfURL = $SelfURL . 'index.php';
176                                                 }
177
178                                         } elseif (substr($SelfURL, -4) != '.php') {
179
180                                                 if ($usePathInfo) {
181                                                         $SelfURL = $SelfURL;
182                                                 } else {
183                                                         $SelfURL = $SelfURL . '/index.php';
184                                                 }
185
186                                         }
187
188                                         $CONF['ItemURL']     = $SelfURL;
189                                         $CONF['CategoryURL'] = $SelfURL;
190
191                                         if ( substr($TempURL, -1) != '/'
192                                           && substr($TempURL, -4) != '.php') {
193                                                 $TempURL .= '/';
194                                         }
195
196                                         $patternURL = '/^' . preg_replace('/\//', '\/', $BlogURL) . '/';
197
198                                         if (preg_match($patternURL, $TempURL)) {
199
200                                                 if (end($path_arr) == 'ror.xml') {
201                                                         $rorTitleURL  = $this->_prepareLink($SelfURL, $TempURL);
202                                                         $rooTitleURL  = htmlspecialchars($rooTitleURL, ENT_QUOTES, _CHARSET);
203                                                         $sitemapTitle = "     <title>ROR Sitemap for " . $rorTitleURL . "</title>\n"
204                                                                                   . "     <link>" . $rorTitleURL . "</link>\n"
205                                                                                   . "     <item>\n"
206                                                                                   . "     <title>ROR Sitemap for " . $rorTitleURL . "</title>\n"
207                                                                                   . "     <link>" . $rorTitleURL . "</link>\n"
208                                                                                   . "     <ror:about>sitemap</ror:about>\n"
209                                                                                   . "     <ror:type>SiteMap</ror:type>\n"
210                                                                                   . "     </item>\n";
211                                                 } else {
212                                                         $sitemap[] = array(
213                                                                 'loc'        => $this->_prepareLink($SelfURL, $TempURL),
214                                                                 'priority'   => '1.0',
215                                                                 'changefreq' => 'daily'
216                                                         );
217                                                 }
218
219                                                 $catQuery  = 'SELECT * '
220                                                                    . 'FROM %s '
221                                                                    . 'WHERE cblog = %d '
222                                                                    . 'ORDER BY catid';
223                                                 $catTable  = sql_table('category');
224                                                 $catQuery  = sprintf($catQuery, $catTable, $blog_id);
225                                                 $catResult = sql_query($catQuery);
226
227                                                 while ($cat = mysql_fetch_array($catResult)) {
228
229                                                         $cat_id = intval($cat['catid']);
230                                                         $Link   = createCategoryLink($cat_id);
231                                                         $catLoc =$this->_prepareLink($SelfURL, $Link);
232
233                                                         if (end($path_arr) != 'ror.xml') {
234                                                                 $sitemap[] = array(
235                                                                         'loc'        => $catLoc,
236                                                                         'priority'   => '1.0',
237                                                                         'changefreq' => 'daily'
238                                                                 );
239                                                         }
240
241                                                         if ($mcategories) {
242                                                                 $scatQuery  = 'SELECT * '
243                                                                                         . 'FROM %s '
244                                                                                         . 'WHERE catid = %d '
245 //                                                                                      . 'ORDER BY scatid';
246                                                                                         . 'ORDER BY ordid';
247                                                                 $scatTable  = sql_table('plug_multiple_categories_sub');
248                                                                 $scatQuery  = sprintf($scatQuery, $scatTable, $cat_id);
249                                                                 $scatResult = sql_query($scatQuery);
250
251                                                                 while ($scat = mysql_fetch_array($scatResult)) {
252
253                                                                         $scat_id = intval($scat['scatid']);
254                                                                         $params  = array($subReq => $scat_id);
255                                                                         $Link    = createCategoryLink($cat_id, $params);
256                                                                         $scatLoc = $this->_prepareLink($SelfURL, $Link);
257
258                                                                         if (end($path_arr) != 'ror.xml') {
259                                                                                 $sitemap[] = array(
260                                                                                         'loc'        => $scatLoc,
261                                                                                         'priority'   => '1.0',
262                                                                                         'changefreq' => 'daily'
263                                                                                 );
264                                                                         }
265
266                                                                 }
267
268                                                         }
269
270                                                 }
271
272                                                 $itemQuery  = 'SELECT *, '
273                                                                         . '       UNIX_TIMESTAMP(itime) AS timestamp '
274                                                                         . 'FROM %s '
275                                                                         . 'WHERE iblog  = %d '
276                                                                         . 'AND   idraft = 0 '
277                                                                         . 'ORDER BY itime DESC';
278                                                 $itemTable  = sql_table('item');
279                                                 $itemQuery  = sprintf($itemQuery, $itemTable, $blog_id);
280                                                 $itemResult = sql_query($itemQuery);
281                                                 while ($item = mysql_fetch_array($itemResult)) {
282
283                                                         $item_id  = intval($item['inumber']);
284                                                         $Link     = createItemLink($item_id);
285                                                         $tz       = date('O', $item['timestamp']);
286                                                         $tz       = substr($tz, 0, 3) . ':' . substr($tz, 3, 2);
287                                                         $itemLoc  = $this->_prepareLink($SelfURL, $Link);
288
289                                                         $mdQuery  = 'SELECT'
290                                                                           . '   UNIX_TIMESTAMP(ctime) AS timestamp'
291                                                                           . ' FROM '
292                                                                           .     sql_table('comment')
293                                                                           . ' WHERE'
294                                                                           . '   citem = ' . $item_id
295                                                                           . ' ORDER BY'
296                                                                           . '   ctime DESC'
297                                                                           . ' LIMIT'
298                                                                           . '   1';
299                                                         $modTime  = sql_query($mdQuery);
300                                                         $itemTime = $item['timestamp'];
301                                                         if (mysql_num_rows($modTime) > 0) {
302                                                                 $lastMod  = mysql_fetch_object($modTime);
303                                                                 $itemTime = $lastMod->timestamp;
304                                                         } elseif ($npUpdateTime) { // NP_UpdateTime exists
305                                                                 $mdQuery = 'SELECT'
306                                                                                  . '   UNIX_TIMESTAMP(updatetime) AS timestamp'
307                                                                                  . ' FROM '
308                                                                                  .     sql_table('plugin_rectime')
309                                                                                  . ' WHERE'
310                                                                                  . '   up_id = ' . $item_id;
311                                                                 $modTime = sql_query($mdQuery);
312                                                                 if (mysql_num_rows($modTime) > 0) { 
313                                                                         $lastMod  = mysql_fetch_object($modTime);
314                                                                         $itemTime = $lastMod->timestamp;
315                                                                 }
316                                                         }
317
318 /*                                                      if (time() - $itemTime < 86400 * 2) {
319                                                                 $fq = 'hourly';
320                                                         } elseif (time() - $itemTime < 86400 * 14) {
321                                                                 $fq = 'daily'; 
322                                                         } elseif (time() - $itemTime < 86400 * 62) {
323                                                                 $fq = 'weekly';
324                                                         } else {
325                                                                 $fq = 'monthly';
326                                                         }*/
327                                                         if ($itemTime < strtotime('-1 month')) {
328                                                                 $fq = 'monthly';
329                                                         } elseif ($itemTime < strtotime('-1 week')) {
330                                                                 $fq = 'weekly';
331                                                         } elseif ($itemTime < strtotime('-1 day')) {
332                                                                 $fq = 'daily'; 
333                                                         } else {
334                                                                 $fq = 'hourly';
335                                                         }
336                                                         $lastmod = gmdate('Y-m-d\TH:i:s', $itemTime) . $tz;
337
338                                                         if (end($path_arr) != 'ror.xml') {
339                                                                 $sitemap[] = array(
340                                                                         'loc'        => $itemLoc,
341                                                                         'lastmod'    => $lastmod,
342                                                                         'priority'   => '1.0',
343                                                                         'changefreq' => $fq
344                                                                 );
345                                                         } else {
346                                                                 $iTitle = $item['ititle'];
347                                                                 if (_CHARSET != 'UTF-8') {
348                                                                         $iTitle = mb_conbert_encoding($iTitle, 'UTF-8', _CHARSET);
349                                                                 }
350                                                                 $sitemap[] = array(
351                                                                         'title'            => $iTitle,
352                                                                         'link'             => $itemLoc,
353                                                                         'ror:updated'      => $lastmod,
354                                                                         'ror:updatePeriod' => 'day',
355                                                                         'ror:sortOrder'    => '0',
356                                                                         'ror:resourceOf'   => 'sitemap',
357                                                                 );
358                                                         }
359
360                                                 }
361
362                                         }
363
364                                 }
365
366                                 if ($CONF['URLMode'] != $URLMode) {
367                                         $CONF['URLMode'] = $URLMode;
368                                 }
369
370                         }
371
372                         $manager->notify('SiteMap', array ('sitemap' => & $sitemap));
373
374                         header ("Content-type: application/xml");
375
376                         if (end($path_arr) == 'ror.xml') {
377
378                         // ror sitemap feed
379                         $sitemapHeader ="<" . "?xml version='1.0' encoding='UTF-8'?" . ">\n\n"
380                                                    . "<!--  This file is a ROR Sitemap for describing this website to the search engines. "
381                                                    . "For details about the ROR format, go to www.rorweb.com.   -->\n"
382                                                    . '<rss version="2.0" xmlns:ror="http://rorweb.com/0.1/" >' . "\n"
383                                                    . "<channel>\n";
384
385                         } else {
386
387                         // old Google sitemap protocol ver.0.84
388 //                      $sitemapHeader  = "<" . "?xml version='1.0' encoding='UTF-8'?" . ">\n\n";
389 //                                                      . "\t<urlset" . ' xmlns="http://www.google.com/schemas/sitemap/0.84"' . "\n";
390 //                                                      . "\t" . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
391 //                                                      . "\t" . 'xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84' . "\n";
392 //                                                      . "\t" . '        http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">' . "\n";
393
394                         // new sitemap common protocol ver 0.9
395                         $sitemapHeader  = "<" . "?xml version='1.0' encoding='UTF-8'?" . ">\n\n"
396                                                         . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n"
397                                                         . '         xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n"
398                                                         . '         http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' . "\n"
399                                                         . '         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
400                         // uncomment and edit next line when you need "example_schema"
401 //                      $sitemapHeader .= '         xmlns:example="http://www.example.com/schemas/example_schema"';
402                         $sitemapHeader .= '>';
403
404                         }
405
406                         echo $sitemapHeader;
407                         if (end($path_arr) == 'ror.xml') {
408                                 echo $sitemapTitle;
409                         }
410
411                         while (list(, $url) = each($sitemap)) {
412
413                                 if (end($path_arr) == 'ror.xml') {
414                                         echo "\t<item>\n";
415                                 } else {
416                                         echo "\t<url>\n";
417                                 }
418
419                                 while (list($key, $value) = each($url)) {
420                                         if ($key == 'loc') {
421                                                 $value = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $value);
422                                                 $data  = "\t\t<" . $key . ">"
423                                                            . htmlspecialchars($value, ENT_QUOTES, _CHARSET)
424                                                            . "</" . $key . ">\n";
425                                         } else {
426                                                 $data  = "\t\t<" . $key . ">"
427                                                            . htmlspecialchars($value, ENT_QUOTES, _CHARSET)
428                                                            . "</" . $key . ">\n";
429                                         }
430                                         echo $data;
431                                 }
432
433                                 if (end($path_arr) == 'ror.xml') {
434                                         echo "\t</item>\n";
435                                 } else {
436                                         echo "\t</url>\n";
437                                 }
438
439                         }
440
441                         if (end($path_arr) == 'ror.xml') {
442                                 echo "</channel>\n</rss>\n";
443                         } else {
444                                 echo "</urlset>\n";
445                         }
446 //                      echo "</urlset>\n";
447                         exit;
448
449                 }
450         }
451
452         function pluginCheck($pluginName)
453         {
454                 global $manager;
455                 if (!$manager->pluginInstalled('NP_' . $pluginName)) {
456                         return;
457                 } else {
458                         $plugin =& $manager->getPlugin('NP_' . $pluginName);
459                         return $plugin;
460                 }
461         }
462
463         function _prepareLink($base, $url) {
464                 if (substr($url, 0, 7) == 'http://') {
465                         return $url;
466                 } else {
467                         return $base . $url;
468                 }
469         }
470
471         function event_PostAddItem(&$data)
472         {
473                 global $manager, $CONF;
474
475                 $item_id =  intval($data['itemid']);
476                 $blog_id =  intval(getBlogIDFromItemID($item_id));
477                 $b       =& $manager->getBlog($blog_id);
478                 $b_url   =  $b->getURL();
479
480                 if (substr($b_url, -4) == '.php') $CONF['URLMode'] = 'normal';
481                 $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
482
483                 if (substr($b_url, -1) == '/') {
484                         if (!$usePathInfo) {
485                                 $b_url .= 'index.php?virtualpath=';
486                         }
487                 } elseif (substr($b_url, -4) == '.php') {
488                         $b_url .= '?virtualpath=';
489                 } else {
490                         if ($usePathInfo) {
491                                 $b_url = $b_url . '/';
492                         } else {
493                                 $b_url = $b_url . '/index.php?virtualpath=';
494                         }
495                 }
496                 $siteMap = $this->getBlogOption($blog_id, 'PcSitemap');
497
498                 if ($this->getBlogOption($blog_id, 'PingGoogle') == 'yes') {
499                         $baseURL = 'http://www.google.com/webmasters/sitemaps/ping?sitemap=';
500                         $utl     = $baseURL . urlencode($b_url . $siteMap);
501                         $url     = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $url);
502                         $fp      = @fopen($url, 'r');
503                         @fclose($fp);
504                         $MobileMap = $this->getBlogOption($blog_id, 'MobileSitemap');
505                         if (!empty($MobileMap)) {
506                                 $url = $baseURL . urlencode($b_url . $MobileMap);
507                                 $url = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $url);
508                                 $fp  = @fopen($url, 'r');
509                                 @fclose($fp);
510                         }
511                 }
512
513                 if ($this->getBlogOption($blog_id, 'PingYahoo') == 'yes' &&
514                         $this->getBlogOption($blog_id, 'YahooAPID') != '') {
515                         $baseURL = 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid='
516                                          . $this->getBlogOption($blog_id, 'YahooAPID')
517                                          . '&url=';
518                         $utl     = $baseURL . urlencode($b_url . $siteMap);
519                         $url     = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $url);
520                         $fp      = @fopen($url, 'r');
521                         @fclose($fp);
522                         $MobileMap = $this->getBlogOption($blog_id, 'MobileSitemap');
523                         if (!empty($MobileMap)) {
524                                 $url = $baseURL . urlencode($b_url . $MobileMap);
525                                 $url = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $url);
526                                 $fp  = @fopen($url, 'r');
527                                 @fclose($fp);
528                         }
529                 }
530
531         }
532
533         function init()
534         {
535                 global $admin;
536                 $language = ereg_replace( '[\\|/]', '', getLanguageName());
537                 if (file_exists($this->getDirectory() . $language.'.php')) {
538                         include_once($this->getDirectory() . $language.'.php');
539                 }else {
540                         include_once($this->getDirectory() . 'english.php');
541                 }
542         }
543
544         function install()
545         {
546                 $this->createOption('AllBlogMap',         _G_SITEMAP_ALLB,   'yesno', 'yes');
547                 $this->createBlogOption('IncludeSitemap', _G_SITEMAP_INC,    'yesno', 'yes');
548                 $this->createBlogOption('PingGoogle',     _G_SITEMAP_PING_G, 'yesno', 'yes');
549                 $this->createBlogOption('PingYahoo',      _G_SITEMAP_PING_Y, 'yesno', 'no');
550                 $this->createBlogOption('YahooAPID',      _G_SITEMAP_YAPID,  'text',  '');
551                 $this->createBlogOption('PcSitemap',      _G_SITEMAP_PCSM,   'text',  'sitemap.xml');
552                 $this->createBlogOption('MobileSitemap',  _G_SITEMAP_MBSM,   'text',  '');
553         }
554 }