source: branches/comu-ver2/data/class/pages/LC_Page_Sitemap.php @ 17602

Revision 17602, 11.1 KB checked in by Seasoft, 16 years ago (diff)

Sitemap の改善
・TOPページが重複している問題を解消
r17416 の変更に対応

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