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

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