| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 4 | * |
|---|
| 5 | * http://www.lockon.co.jp/ |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | // {{{ requires |
|---|
| 9 | require_once(CLASS_PATH . "pages/LC_Page.php"); |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Sitemapプロトコル ファイル生成モジュール. |
|---|
| 13 | * PHP versions 4 and 5 |
|---|
| 14 | * |
|---|
| 15 | * <pre> |
|---|
| 16 | * このモジュールは Sitemapプロトコルに対応した XMLファイルを出力する. |
|---|
| 17 | * EC-CUBE インストールディレクトリの htmlディレクトリへ配置することにより動作する. |
|---|
| 18 | * |
|---|
| 19 | * このモジュールにより, 以下のページのサイトマップが生成される. |
|---|
| 20 | * 1. $staticURL で指定したページ |
|---|
| 21 | * 2. 管理画面のデザイン管理から生成したページ |
|---|
| 22 | * 3. 公開されているすべての商品一覧ページ |
|---|
| 23 | * 4. 公開されているすべての商品詳細ページ |
|---|
| 24 | * 5. html/mobile 以下の上記ページ |
|---|
| 25 | * |
|---|
| 26 | * このモジュールを設置後, 各検索エンジンにサイトマップを登録することにより, 検索エンジンの |
|---|
| 27 | * インデックス化が促進される. |
|---|
| 28 | * </pre> |
|---|
| 29 | * @see https://www.google.com/webmasters/tools/siteoverview?hl=ja |
|---|
| 30 | * @see https://siteexplorer.search.yahoo.com/mysites |
|---|
| 31 | * |
|---|
| 32 | * @author Kentaro Ohkouchi |
|---|
| 33 | * @version $Id:sitemap.php 15532 2007-08-31 14:39:46Z nanasess |
|---|
| 34 | * |
|---|
| 35 | * :TODO: 各ページの changefreq や priority を指定できるようにする |
|---|
| 36 | * :TODO: filemtime 関数を使えば、静的なページの更新時間も取得できそう |
|---|
| 37 | */ |
|---|
| 38 | class LC_Page_Sitemap extends LC_Page { |
|---|
| 39 | |
|---|
| 40 | // }}} |
|---|
| 41 | // {{{ properties |
|---|
| 42 | |
|---|
| 43 | /** 動的に生成しないページの配列 */ |
|---|
| 44 | var $staticURL; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** ページデータ */ |
|---|
| 48 | var $arrPageData; |
|---|
| 49 | |
|---|
| 50 | /** ページリスト */ |
|---|
| 51 | var $arrPageList; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | // }}} |
|---|
| 55 | // {{{ functions |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Page を初期化する. |
|---|
| 59 | * |
|---|
| 60 | * @return void |
|---|
| 61 | */ |
|---|
| 62 | function init() { |
|---|
| 63 | parent::init(); |
|---|
| 64 | $this->staticURL = array(SITE_URL, MOBILE_SITE_URL, SITE_URL . "rss/index.php"); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Page のプロセス. |
|---|
| 69 | * |
|---|
| 70 | * @return void |
|---|
| 71 | */ |
|---|
| 72 | function process() { |
|---|
| 73 | // ページのデータを取得 |
|---|
| 74 | $this->arrPageList = $this->getPageData(); |
|---|
| 75 | |
|---|
| 76 | $objQuery = new SC_Query(); |
|---|
| 77 | |
|---|
| 78 | //キャッシュしない(念のため) |
|---|
| 79 | header("Paragrama: no-cache"); |
|---|
| 80 | |
|---|
| 81 | //XMLテキスト |
|---|
| 82 | header("Content-type: application/xml; charset=utf-8"); |
|---|
| 83 | |
|---|
| 84 | // 必ず UTF-8 として出力 |
|---|
| 85 | mb_http_output("UTF-8"); |
|---|
| 86 | ob_start('mb_output_handler'); |
|---|
| 87 | |
|---|
| 88 | print("<?xml version='1.0' encoding='UTF-8'?>\n"); |
|---|
| 89 | print("<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n"); |
|---|
| 90 | |
|---|
| 91 | // 静的なページを処理 |
|---|
| 92 | foreach($this->staticURL as $url) { |
|---|
| 93 | $this->createSitemap($url, '', 'daily', 1.0); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // TOPページを処理 |
|---|
| 97 | $topPage = $this->getTopPage($this->arrPageList); |
|---|
| 98 | $this->createSitemap($topPage[0]['url'], |
|---|
| 99 | $this->date2W3CDatetime($topPage[0]['update_date']), |
|---|
| 100 | 'daily', 1.0); |
|---|
| 101 | |
|---|
| 102 | // 編集可能ページを処理 |
|---|
| 103 | $editablePages = $this->getEditablePage($this->arrPageList); |
|---|
| 104 | foreach($editablePages as $editablePage) { |
|---|
| 105 | $this->createSitemap($editablePage['url'], |
|---|
| 106 | $this->date2W3CDatetime($editablePage['update_date'])); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // 商品一覧ページを処理 |
|---|
| 110 | $products = $this->getAllProducts(); |
|---|
| 111 | foreach($products as $product) { |
|---|
| 112 | $this->createSitemap($product['url'], '', 'daily'); |
|---|
| 113 | } |
|---|
| 114 | $mobileProducts = $this->getAllProducts(true); |
|---|
| 115 | foreach($mobileProducts as $mobileProduct) { |
|---|
| 116 | $this->createSitemap($mobileProduct['url'], '', 'daily'); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // 商品詳細ページを処理 |
|---|
| 120 | $details = $this->getAllDetail(); |
|---|
| 121 | foreach($details as $detail) { |
|---|
| 122 | $this->createSitemap($detail['url'], |
|---|
| 123 | $this->date2W3CDatetime($detail['update_date'])); |
|---|
| 124 | } |
|---|
| 125 | $mobileDetails = $this->getAllDetail(true); |
|---|
| 126 | foreach($mobileDetails as $mobileDetail) { |
|---|
| 127 | $this->createSitemap($mobileDetail['url'], |
|---|
| 128 | $this->date2W3CDatetime($mobileDetail['update_date'])); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | print("</urlset>\n"); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * デストラクタ. |
|---|
| 136 | * |
|---|
| 137 | * @return void |
|---|
| 138 | */ |
|---|
| 139 | function destroy() { |
|---|
| 140 | parent::destroy(); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Sitemap の <url /> を生成する. |
|---|
| 145 | * |
|---|
| 146 | * @param string $loc ページの URL ※必須 |
|---|
| 147 | * @param string $lastmod ファイルの最終更新日 YYYY-MM-DD or W3C Datetime 形式 |
|---|
| 148 | * @param string $changefreq ページの更新頻度 |
|---|
| 149 | * @param double $priority URL の優先度 |
|---|
| 150 | * @return Sitemap 形式の <url /> |
|---|
| 151 | * @see https://www.google.com/webmasters/tools/docs/ja/protocol.html#xmlTagDefinitions |
|---|
| 152 | * TODO Smarty に移行すべき? |
|---|
| 153 | */ |
|---|
| 154 | function createSitemap($loc, $lastmod = "", $changefreq = "", |
|---|
| 155 | $priority = "") { |
|---|
| 156 | printf("\t<url>\n"); |
|---|
| 157 | printf("\t\t<loc>%s</loc>\n", htmlentities($loc, ENT_QUOTES, "UTF-8")); |
|---|
| 158 | if (!empty($lastmod)) { |
|---|
| 159 | printf("\t\t<lastmod>%s</lastmod>\n", $lastmod); |
|---|
| 160 | } |
|---|
| 161 | if (!empty($changefreq)) { |
|---|
| 162 | printf("\t\t<changefreq>%s</changefreq>\n", $changefreq); |
|---|
| 163 | } |
|---|
| 164 | if(!empty($priority)) { |
|---|
| 165 | printf("\t\t<priority>%01.1f</priority>\n", $priority); |
|---|
| 166 | } |
|---|
| 167 | printf("\t</url>\n"); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * TOPページの情報を取得する. |
|---|
| 172 | * |
|---|
| 173 | * @param array $pageData すべてのページ情報の配列 |
|---|
| 174 | * @return array TOPページの情報 |
|---|
| 175 | */ |
|---|
| 176 | function getTopPage($pageData) { |
|---|
| 177 | $arrRet = array(); |
|---|
| 178 | foreach($pageData as $page) { |
|---|
| 179 | if ($page['page_id'] == "1") { |
|---|
| 180 | $page['url'] = SITE_URL . $page['url']; |
|---|
| 181 | $arrRet[0] = $page; |
|---|
| 182 | return $arrRet; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * すべての編集可能ページの情報を取得する. |
|---|
| 189 | * |
|---|
| 190 | * @param array $pageData すべてのページ情報の配列 |
|---|
| 191 | * @return array 編集可能ページ |
|---|
| 192 | */ |
|---|
| 193 | function getEditablePage($pageData) { |
|---|
| 194 | $arrRet = array(); |
|---|
| 195 | $i = 0; |
|---|
| 196 | foreach($pageData as $page) { |
|---|
| 197 | if ($page['page_id'] > 4) { |
|---|
| 198 | $arrRet[$i] = $page; |
|---|
| 199 | $i++; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | return $arrRet; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * すべての商品一覧ページを取得する. |
|---|
| 207 | * |
|---|
| 208 | * @param boolean $isMobile モバイルページを取得する場合 true |
|---|
| 209 | * @return array 検索エンジンからアクセス可能な商品一覧ページの情報 |
|---|
| 210 | */ |
|---|
| 211 | function getAllProducts($isMobile = false) { |
|---|
| 212 | $conn = new SC_DBConn(); |
|---|
| 213 | $sql = "SELECT category_id FROM dtb_category WHERE del_flg = 0"; |
|---|
| 214 | $result = $conn->getAll($sql); |
|---|
| 215 | |
|---|
| 216 | $mobile = ""; |
|---|
| 217 | if ($isMobile) { |
|---|
| 218 | $mobile = "mobile/"; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | $arrRet = array(); |
|---|
| 222 | for ($i = 0; $i < count($result); $i++) { |
|---|
| 223 | // :TODO: カテゴリの最終更新日を取得できるようにする |
|---|
| 224 | $page = array("url" => SITE_URL . sprintf("%sproducts/list.php?category_id=%d", $mobile, $result[$i]['category_id'])); |
|---|
| 225 | $arrRet[$i] = $page; |
|---|
| 226 | } |
|---|
| 227 | return $arrRet; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /** |
|---|
| 231 | * すべての商品詳細ページを取得する. |
|---|
| 232 | * |
|---|
| 233 | * @param boolean $isMobile モバイルページを取得する場合 true |
|---|
| 234 | * @return array 検索エンジンからアクセス可能な商品詳細ページの情報 |
|---|
| 235 | */ |
|---|
| 236 | function getAllDetail($isMobile = false) { |
|---|
| 237 | $conn = new SC_DBConn(); |
|---|
| 238 | $sql = "SELECT product_id, update_date FROM dtb_products WHERE del_flg = 0 AND status = 1"; |
|---|
| 239 | $result = $conn->getAll($sql); |
|---|
| 240 | |
|---|
| 241 | $mobile = ""; |
|---|
| 242 | if ($isMobile) { |
|---|
| 243 | $mobile = "mobile/"; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | $arrRet = array(); |
|---|
| 247 | for ($i = 0; $i < count($result); $i++) { |
|---|
| 248 | $page = array("url" => SITE_URL. sprintf("%sproducts/detail.php?product_id=%d", $mobile, $result[$i]['product_id']), |
|---|
| 249 | "update_date" => $result[$i]['update_date']); |
|---|
| 250 | $arrRet[$i] = $page; |
|---|
| 251 | } |
|---|
| 252 | return $arrRet; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | /** |
|---|
| 257 | * ブロック情報を取得する. |
|---|
| 258 | * |
|---|
| 259 | * @param string $where WHERE句 |
|---|
| 260 | * @param array $arrVal WHERE句の値を格納した配列 |
|---|
| 261 | * @return ブロック情報 |
|---|
| 262 | */ |
|---|
| 263 | function getPageData($where = '', $arrVal = ''){ |
|---|
| 264 | $objDBConn = new SC_DbConn; // DB操作オブジェクト |
|---|
| 265 | $sql = ""; // データ取得SQL生成用 |
|---|
| 266 | $arrRet = array(); // データ取得用 |
|---|
| 267 | |
|---|
| 268 | // SQL生成(url と update_date 以外は不要?) |
|---|
| 269 | $sql .= " SELECT"; |
|---|
| 270 | $sql .= " page_id"; // ページID |
|---|
| 271 | $sql .= " ,page_name"; // 名称 |
|---|
| 272 | $sql .= " ,url"; // URL |
|---|
| 273 | $sql .= " ,php_dir"; // php保存先ディレクトリ |
|---|
| 274 | $sql .= " ,tpl_dir"; // tpl保存先ディdレクトリ |
|---|
| 275 | $sql .= " ,filename"; // ファイル名称 |
|---|
| 276 | $sql .= " ,header_chk "; // ヘッダー使用FLG |
|---|
| 277 | $sql .= " ,footer_chk "; // フッター使用FLG |
|---|
| 278 | $sql .= " ,author"; // authorタグ |
|---|
| 279 | $sql .= " ,description"; // descriptionタグ |
|---|
| 280 | $sql .= " ,keyword"; // keywordタグ |
|---|
| 281 | $sql .= " ,update_url"; // 更新URL |
|---|
| 282 | $sql .= " ,create_date"; // データ作成日 |
|---|
| 283 | $sql .= " ,update_date"; // データ更新日 |
|---|
| 284 | $sql .= " FROM "; |
|---|
| 285 | $sql .= " dtb_pagelayout"; |
|---|
| 286 | |
|---|
| 287 | // where句の指定があれば追加 |
|---|
| 288 | if ($where != '') { |
|---|
| 289 | $sql .= " WHERE " . $where; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | $sql .= " ORDER BY page_id"; |
|---|
| 293 | |
|---|
| 294 | return $objDBConn->getAll($sql, $arrVal); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | /** |
|---|
| 298 | * date形式の文字列を W3C Datetime 形式に変換して出力する. |
|---|
| 299 | * |
|---|
| 300 | * @param date $date 変換する日付 |
|---|
| 301 | * @return void |
|---|
| 302 | */ |
|---|
| 303 | function date2W3CDatetime($date) { |
|---|
| 304 | $arr = array(); |
|---|
| 305 | // 正規表現で文字列を抽出 |
|---|
| 306 | ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})", |
|---|
| 307 | $date, $arr); |
|---|
| 308 | // :TODO: time zone も取得するべき... |
|---|
| 309 | return sprintf("%04d-%02d-%02dT%02d:%02d:%02d+09:00", |
|---|
| 310 | $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6]); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | ?> |
|---|