رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نسخه شئ گرای اسکریپت تولید خودکار SiteMap که قبلاً نوشته بودم
#1
<?php
    /**
     * Automated XML Site Map Generator
     * @author Mohammad Mostafa Shahreki
     * @copyright All rights reserved for barnamenevis
     * @link http://www.ncis.ir
     */
    class SiteMap {
        /**
         * @var array Ignore the file/folder if these words appear anywhere in the name
         */
        private static $alwaysIgnore;
         
        /**
         * @var array Allowed extensions to consider in sitemap
         */
        private static $extensions;
         
        /**
         * @var array The script will not enter these folders
         */
        private static $ignoreDirs;
         
        /**
         * @var array These files will not be linked in the sitemap
         */
        private static $ignoreFiles;
         
        /**
         * @var string Where is the root of the site path
         */
        private static $root;
         
        /**
         * @var string The Url of the site - the last '/' is needed
         */
        private static $url;
         
        /**
         * This function extracts pages
         * @param string $currentDir The current direcotry
         * @return array The array of extracted pages
         */
        private static function getPages($currentDir) {
            $pages = array();
            chdir($currentDir);
            $ext = '{*.' . implode(',*.', self::$extensions) . '}';
            $files = glob($ext, GLOB_BRACE);
            foreach($files as $file) {
                $flag = true;
                if(in_array($file, self::$ignoreFiles)) {
                    $flag = false;
                }
                else {
                    foreach(self::$alwaysIgnore as $ignore) {
                        if(strpos($file, $ignore) !== false) {
                            $flag = false;
                        }
                    }
                }
                if($flag) {
                    $pages[] = self::$url . ($currentDir != self::$root ? $currentDir . '/' : '') . $file;
                }
            }
            $dirs = glob('{*,*.*}', GLOB_BRACE | GLOB_ONLYDIR);
            foreach($dirs as $dir) {
                $flag = true;
                if(in_array($dir, self::$ignoreDirs)) {
                    $flag = false;
                }
                else {
                    foreach(self::$alwaysIgnore as $ignore) {
                        if(strpos($dir, $ignore) !== false) {
                            $flag = false;
                        }
                    }
                }
                if($flag) {
                    $cwd = getcwd();
                    $pages = array_merge($pages, self::getPages(str_replace('\', '/', $dir)));
                    chdir($cwd);
                }
            }
            return $pages;
        }
         
        /**
         * Generate site map and store it in sitemap.xml in the site root folder
         * @param string $url The main URL of the site
         * @param string $root (Optional) The root path of the site (if not specified, the current directory of this file is used)
         * @param string $extensions (Optional) The comma separated string of extensions to index in the site map
         * @param string $alwaysIgnore (Optional) The current processing file/folder is ignored if contains any of words of this comma separated string
         * @param string $ignoreDirs (Optional) The directories mentioned in this comma separated string will not be scanned
         * @param string $ignoreFiles (Optional) The files mentioned in this comma separated string will be ommited from the site map
         */
        public static function Generate($url, $root = NULL, $extensions = NULL, $alwaysIgnore = NULL, $ignoreDirs = NULL, $ignoreFiles = NULL) {
            self::$url = trim($url, '/') . '/';
            self::$root = ($root === NULL ? str_replace('\', '/', dirname(__FILE__)) : $root);
            self::$extensions = ($extensions === NULL ? array('htm', 'html', 'php') : explode(',', $extensions));
            self::$alwaysIgnore = ($alwaysIgnore === NULL ? array('.inc', 'admin', 'image') : explode(',', $alwaysIgnore));
            self::$ignoreDirs = ($ignoreDirs === NULL ? array('admin', 'css', 'images', 'inc', 'js', 'lib', 'styles', 'uploads') : explode(',', $ignoreDirs));
            self::$ignoreFiles = ($ignoreFiles === NULL ? array('404.html', 'config.php', 'include.inc') : explode(',', $ignoreFiles));
            $cwd = getcwd();
            $all_pages = self::getPages(self::$root);
            chdir($cwd);
            $output = '';
            $output .= '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
            $output .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">' . PHP_EOL;
            //Process the files
            foreach ($all_pages as $link) {
                //Find the modified time.
                if(preg_match('#index.w{3,4}$#', $link)) {
                    $link = preg_replace('#index.w{3,4}$#', '', $link);
                }
                $output .= '    <url>' . PHP_EOL;
                $output .= '        <loc>' . htmlentities($link, ENT_QUOTES, 'utf-8') . '</loc>' . PHP_EOL;
                $output .= '    </url>' . PHP_EOL;
            }
            $output .= '</urlset>' . PHP_EOL;
            file_put_contents(self::$root . '/sitemap.xml', $output);
            @chmod(self::$root . '/sitemap.xml', 0644);
        }
    }
?>

مثالی از نحوه استفاده:

<?php
    require_once 'class.sitemap.php';
    SiteMap::Generate('http://localhost/sitemap', NULL, 'php', 'admin', 'languages,plugins,upgrade,uploads,wp-includes', 'wp-cron.php');
?>


یا میتونید توی همون فایل کلاس، این کد رو به انتهاش اضافه کنید:
SiteMap::Generate('http://localhost/sitemap', NULL, 'php', 'admin',  'languages,plugins,upgrade,uploads,wp-includes', 'wp-cron.php');


و توسط Cron Jobs (در فواصل زمانی مشخص) یا توی اسکریپتتون (هرموقع مطالب تغییر کرد) فایل رو ضمیمه کنید.

نکته:
این اسکریپت ازروی فایلهای سایتتون SiteMap رو میسازه. اگه سایتتون Dynamic هست و مطالب با روشهایی مثل Get و... ازطریق یک صفحه خاص استخراج میشن، بهترین و ساده ترین کار، استخراج لینکهای اطلاعات از دیتابیس و تولید فایل SiteMap.xml هست.
پاسخ
تشکر شده توسط: php




کاربران در حال بازدید این موضوع: 1 مهمان