source: branches/version-2_4-dev/data/class/pages/LC_Page_Sitemap.php @ 18606

Revision 18606, 11.2 KB checked in by shutta, 14 years ago (diff)

#598 対応の続き。
コミュニティ版の r17416, r17487, r17602 を取り込んだ。(少々差異あり)

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