source: tmp/version-2_5-test/data/class/pages/LC_Page_Sitemap.php @ 18609

Revision 18609, 11.2 KB checked in by kajiwara, 14 years ago (diff)

正式版にナイトリービルド版をマージしてみるテスト

  • 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    var $arrPageList;
64
65
66    // }}}
67    // {{{ functions
68
69    /**
70     * Page を初期化する.
71     *
72     * @return void
73     */
74    function init() {
75        parent::init();
76       
77        $this->staticURL = array();
78       
79        $this->staticURL[] = SITE_URL . 'rss/' . DIR_INDEX_URL;
80        if (USE_MOBILE !== false) {
81            $this->staticURL[] = MOBILE_SITE_URL;
82        }
83    }
84
85    /**
86     * Page のプロセス.
87     *
88     * @return void
89     */
90    function process() {
91        // ページのデータを取得
92        $this->arrPageList = $this->getPageData();
93
94        $objQuery = new SC_Query();
95
96        //キャッシュしない(念のため)
97        header("Paragrama: no-cache");
98
99        //XMLテキスト
100        header("Content-type: application/xml; charset=utf-8");
101
102        // 必ず UTF-8 として出力
103        mb_http_output("UTF-8");
104        ob_start('mb_output_handler');
105
106        print("<?xml version='1.0' encoding='UTF-8'?>\n");
107        print("<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n");
108
109        // TOPページを処理
110        $topPage = $this->getTopPage($this->arrPageList);
111        $this->createSitemap($topPage[0]['url'],
112                             $this->date2W3CDatetime($topPage[0]['update_date']),
113                             'daily', 1.0);
114
115        // 静的なページを処理
116        foreach ($this->staticURL as $url) {
117            $this->createSitemap($url, '', 'daily', 1.0);
118        }
119
120        // 編集可能ページを処理
121        $editablePages = $this->getEditablePage($this->arrPageList);
122        foreach ($editablePages as $editablePage) {
123            $this->createSitemap($editablePage['url'],
124                                 $this->date2W3CDatetime($editablePage['update_date']));
125        }
126
127        // 商品一覧ページを処理
128        $products = $this->getAllProducts();
129        foreach ($products as $product) {
130            $this->createSitemap($product['url'], '', 'daily');
131        }
132
133        // 商品詳細ページを処理
134        $details = $this->getAllDetail();
135        foreach ($details as $detail) {
136            $this->createSitemap($detail['url'],
137                                 $this->date2W3CDatetime($detail['update_date']));
138        }
139
140        print("</urlset>\n");
141    }
142
143    /**
144     * デストラクタ.
145     *
146     * @return void
147     */
148    function destroy() {
149        parent::destroy();
150    }
151
152    /**
153     * Sitemap の <url /> を生成する.
154     *
155     * @param string $loc ページの URL ※必須
156     * @param string $lastmod ファイルの最終更新日 YYYY-MM-DD or W3C Datetime 形式
157     * @param string $changefreq ページの更新頻度
158     * @param double $priority URL の優先度
159     * @return Sitemap 形式の <url />
160     * @see https://www.google.com/webmasters/tools/docs/ja/protocol.html#xmlTagDefinitions
161     * TODO Smarty に移行すべき?
162     */
163    function createSitemap($loc, $lastmod = "", $changefreq = "",
164                           $priority = "") {
165        printf("\t<url>\n");
166        printf("\t\t<loc>%s</loc>\n", htmlentities($loc, ENT_QUOTES, "UTF-8"));
167        if (!empty($lastmod)) {
168            printf("\t\t<lastmod>%s</lastmod>\n", $lastmod);
169        }
170        if (!empty($changefreq)) {
171            printf("\t\t<changefreq>%s</changefreq>\n", $changefreq);
172        }
173        if(!empty($priority)) {
174            printf("\t\t<priority>%01.1f</priority>\n", $priority);
175        }
176        printf("\t</url>\n");
177    }
178
179    /**
180     * TOPページの情報を取得する.
181     *
182     * @param array $pageData すべてのページ情報の配列
183     * @return array TOPページの情報
184     */
185    function getTopPage($pageData) {
186        $arrRet = array();
187        foreach ($pageData as $page) {
188            if ($page['page_id'] == "1") {
189                $arrRet[0] = $page;
190                return $arrRet;
191            }
192        }
193    }
194
195    /**
196     * すべての編集可能ページの情報を取得する.
197     *
198     * @param array $pageData すべてのページ情報の配列
199     * @return array 編集可能ページ
200     */
201    function getEditablePage($pageData) {
202        $arrRet = array();
203        foreach ($pageData as $page) {
204            if ($page['page_id'] > 4) {
205                $arrRet[] = $page;
206            }
207        }
208        return $arrRet;
209    }
210
211    /**
212     * すべての商品一覧ページを取得する.
213     *
214     * @return array 検索エンジンからアクセス可能な商品一覧ページの情報
215     */
216    function getAllProducts() {
217       
218        // XXX: 商品登録の無いカテゴリーは除外する方が良い気もする
219        $conn = new SC_DBConn();
220        $sql = "SELECT category_id FROM dtb_category WHERE del_flg = 0";
221        $result = $conn->getAll($sql);
222
223        $arrRet = array();
224        foreach ($result as $row) {
225            // :TODO: カテゴリの最終更新日を取得できるようにする
226           
227            $page["url"] = SITE_URL . 'products/list.php?category_id=' . $row['category_id'];
228            $arrRet[] = $page;
229           
230            // モバイルサイト
231            if (USE_MOBILE !== false) {
232                $page["url"] = MOBILE_SITE_URL . 'products/list.php?category_id=' . $row['category_id'];
233                $arrRet[] = $page;
234            }
235        }
236        return $arrRet;
237    }
238
239    /**
240     * すべての商品詳細ページを取得する.
241     *
242     * @return array 検索エンジンからアクセス可能な商品詳細ページの情報
243     */
244    function getAllDetail() {
245        $conn = new SC_DBConn();
246        $sql = "SELECT product_id, update_date FROM dtb_products WHERE del_flg = 0 AND status = 1";
247        $result = $conn->getAll($sql);
248
249        $arrRet = array();
250        foreach ($result as $row) {
251           
252            $page["update_date"] = $row['update_date'];
253           
254            $page["url"] = SITE_URL . substr(DETAIL_P_HTML, strlen(URL_DIR)) . $row['product_id'];
255            $arrRet[] = $page;
256           
257            // モバイルサイト
258            if (USE_MOBILE !== false) {
259                $page["url"] = SITE_URL . substr(MOBILE_DETAIL_P_HTML, strlen(URL_DIR)) . $row['product_id'];
260                $arrRet[] = $page;
261            }
262        }
263        return $arrRet;
264    }
265
266
267    /**
268     * ブロック情報を取得する.
269     *
270     * @param string $where WHERE句
271     * @param array  $arrVal WHERE句の値を格納した配列
272     * @return ブロック情報
273     */
274    function getPageData($where = '', $arrVal = ''){
275        $objDBConn = new SC_DbConn;     // DB操作オブジェクト
276        $sql = "";                      // データ取得SQL生成用
277        $arrRet = array();              // データ取得用
278
279        // SQL生成(url と update_date 以外は不要?)
280        $sql .= " SELECT";
281        $sql .= " page_id";             // ページID
282        $sql .= " ,page_name";          // 名称
283        $sql .= " ,url";                // URL
284        $sql .= " ,php_dir";            // php保存先ディレクトリ
285        $sql .= " ,tpl_dir";            // tpl保存先ディdレクトリ
286        $sql .= " ,filename";           // ファイル名称
287        $sql .= " ,header_chk ";        // ヘッダー使用FLG
288        $sql .= " ,footer_chk ";        // フッター使用FLG
289        $sql .= " ,author";             // authorタグ
290        $sql .= " ,description";        // descriptionタグ
291        $sql .= " ,keyword";            // keywordタグ
292        $sql .= " ,update_url";         // 更新URL
293        $sql .= " ,create_date";        // データ作成日
294        $sql .= " ,update_date";        // データ更新日
295        $sql .= " FROM ";
296        $sql .= "     dtb_pagelayout";
297
298        // where句の指定があれば追加
299        if ($where != '') {
300            $sql .= " WHERE " . $where;
301        }
302
303        $sql .= " ORDER BY page_id";
304
305        $pageData = $objDBConn->getAll($sql, $arrVal);
306       
307        // URL にプロトコルの記載が無い場合、SITE_URL を前置する。
308        foreach (array_keys($pageData) as $key) {
309            $page =& $pageData[$key];
310            if (!preg_match('|^https?://|i', $page['url'])) {
311                $page['url'] = SITE_URL . $page['url'];
312            }
313            $page['url'] = preg_replace('|/' . preg_quote(DIR_INDEX_FILE) . '$|', '/' . DIR_INDEX_URL, $page['url']);
314        }
315        unset($page);
316       
317        return $pageData;
318    }
319
320    /**
321     * date形式の文字列を W3C Datetime 形式に変換して出力する.
322     *
323     * @param date $date 変換する日付
324     * @return void
325     */
326    function date2W3CDatetime($date) {
327        $arr = array();
328        // 正規表現で文字列を抽出
329        ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})",
330             $date, $arr);
331        // :TODO: time zone も取得するべき...
332        return sprintf("%04d-%02d-%02dT%02d:%02d:%02d+09:00",
333                       $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6]);
334    }
335
336}
337
338?>
Note: See TracBrowser for help on using the repository browser.