root/sitemaps/optionals/de.easy-coding.wbb.sitemaps/files/lib/system/event/listener/SitemapsPageWBBListener.class.php @ 812

Revision 812, 7.4 kB (checked in by d0nut, 4 years ago)

maybe solved sitemaps requirements chaos

Line 
1<?php
2// wcf imports
3require_once(WCF_DIR.'lib/system/event/EventListener.class.php');
4
5// wbb imports
6require_once(WBB_DIR.'lib/data/board/SitemapsBoard.class.php');
7require_once(WBB_DIR.'lib/data/thread/SitemapsThread.class.php');
8
9// rewriter util
10require_once(WBB_DIR.'lib/page/PublicSEORewriterWBB.class.php');
11
12/**
13 * extends the sitemap
14 *
15 * @author      Torben Brodt
16 * @package     de.easy-coding.wbb.sitemaps
17 * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-3.0.html>
18 */
19class SitemapsPageWBBListener implements EventListener {
20        protected $wbb = false;
21        protected $thread_posts_per_page=0;
22        protected $board_threads_per_page=0;
23
24        protected $boardIDs = array(), $boards = array();
25
26        protected $eventObj;
27        protected $className;
28       
29        protected $rewriter;
30
31        /**
32         * @see EventListener::execute()
33         */
34        public function execute($eventObj, $className, $eventName) {
35                $this->eventObj = $eventObj;
36                $this->className = $className;
37
38                $this->rewriter = new PublicSEORewriterWBB();
39
40                switch ($eventName) {
41                        case 'readParameters':
42                                $this->readParameters();
43                                break;
44                        case 'readData':
45                                $this->readData();
46                                break;
47                        case 'assignVariables':
48                                $this->assignVariables();
49                                break;
50                }
51        }
52
53        /**
54         * how many posts in one thread
55         */
56        protected function threadPostsPerPage() {
57                if($this->thread_posts_per_page > 0)
58                        return $this->thread_posts_per_page;
59
60                $sql = "SELECT  opt.optionValue
61                        FROM    wcf".WCF_N."_option opt
62                        WHERE   opt.optionName = 'thread_posts_per_page'
63                        LIMIT   1;";
64                $result = WCF::getDB()->sendQuery($sql);
65                $row = WCF::getDB()->fetchArray($result);
66                $this->thread_posts_per_page = $row['optionValue'];
67        }
68
69        /**
70         * how many threads in one forum
71         */
72        protected function boardThreadsPerPage() {
73                if($this->board_threads_per_page > 0)
74                        return $this->board_threads_per_page;
75
76                $sql = "SELECT  opt.optionValue
77                        FROM    wcf".WCF_N."_option opt
78                        WHERE   opt.optionName = 'board_threads_per_page'
79                        LIMIT   1;";
80                $result = WCF::getDB()->sendQuery($sql);
81                $row = WCF::getDB()->fetchArray($result);
82                $this->board_threads_per_page = $row['optionValue'];
83        }
84
85        /**
86         * Gets the threads for the sitemaps.
87         */
88        protected function readThreads() {
89                        require_once(WBB_DIR.'lib/data/thread/FeedThread.class.php');
90                        // accessible boards
91                        require_once(WBB_DIR.'lib/data/board/Board.class.php');
92                        $boardIDs = Board::getAccessibleBoards(array('canViewBoard', 'canEnterBoard', 'canReadThread'));
93
94                        // get threads
95                        if (!empty($boardIDs)) {
96
97                        // read count
98                        $this->threadPostsPerPage();
99
100                        $sql = "SELECT          post.*, thread.*
101                                FROM            wbb".WBB_N."_thread thread
102                                LEFT JOIN       wbb".WBB_N."_post post
103                                ON              (post.postID = thread.firstPostID)
104                                WHERE           thread.boardID IN (".$boardIDs.")
105                                                ".(count($this->boardIDs) ? "AND thread.boardID IN (".implode(',', $this->boardIDs).")" : "")."
106                                                AND thread.isDeleted = 0
107                                                AND thread.isDisabled = 0
108                                                AND thread.firstPostID > 0
109                                ORDER BY        thread.time DESC";
110
111                        // TEMPLATE
112                        $tpl = '<url><loc>'.PAGE_URL.'/%s</loc><lastmod>%s</lastmod></url>';
113
114                        $result = WCF::getDB()->sendQuery($sql);
115                        while ($row = WCF::getDB()->fetchArray($result)) {
116                                $pages = ceil($row['replies']/$this->thread_posts_per_page);
117                                $tmp = new SitemapsThread($row);
118
119                                if($this->rewriter === null) {
120                                        $tmp->url = sprintf('index.php?page=Thread&amp;threadID=%d', $row['threadID']);
121                                } else {
122                                        $this->rewriter->publicCacheThreads($row['threadID'], $row);
123                                        $tmp->url = $this->rewriter->publicParseThreadURLs($row['threadID'], '');
124                                }
125
126                                // PRINT
127                                printf($tpl, $tmp->url, date('c', $tmp->time));
128
129                                for($j=2; $j<$pages; $j++) {
130                                        $tmp = new SitemapsThread($row['threadID'], $row);
131
132                                        if($this->rewriter === null) {
133                                                        $tmp->url = sprintf('index.php?page=Thread&amp;threadID=%d&amp;pageNo=%d', $row['threadID'], $j);
134                                        } else {
135                                                        $tmp->url = $this->rewriter->publicParseMultipleThreadURLs($row['threadID'], $j, '');
136                                        }
137
138                                        // PRINT
139                                        printf($tpl, $tmp->url, date('c', $tmp->time));
140                                }
141
142                                // titles are not used again.. clear cache
143                                if($this->rewriter !== null) $this->rewriter->clearCache();
144                        }
145                }
146        }
147
148        /**
149         * Gets the boards for the sitemaps overview and for the numbered board pages.
150         */
151        protected function readBoards() {
152                // accessible boards
153                $boardIDs = Board::getAccessibleBoards(array('canViewBoard', 'canEnterBoard', 'canReadThread'));
154
155                // get threads
156                if (!empty($boardIDs)) {
157
158                        // read count
159                        $this->boardThreadsPerPage();
160
161                        $sql = "SELECT          board.*,
162                                                t.lastPostTime
163                                FROM            wbb".WBB_N."_board board
164                                NATURAL JOIN    wbb".WBB_N."_board_last_post blp
165                                JOIN            wbb".WBB_N."_thread t
166                                ON              blp.threadID = t.threadID
167                                WHERE           board.threads > 0
168                                AND             board.boardID IN (".$boardIDs.")
169                                ".(count($this->boardIDs) && $this->boardIDs[0] ? "AND board.boardID IN (".implode(',', $this->boardIDs).")" : "")."
170                                ORDER BY        board.time DESC";
171
172                        $i=0;
173                        $result = WCF::getDB()->sendQuery($sql);
174                        while ($row = WCF::getDB()->fetchArray($result)) {
175                                $pages = ceil($row['threads']/$this->board_threads_per_page);
176                                $this->boards[$i] = new SitemapsBoard($row['boardID'], $row);
177                                $this->boards[$i]->full = true;
178                                $this->boards[$i]->time = $row['lastPostTime'];
179
180                                if(intval($this->boards[$i]->getThreads()) == 0) {
181                                        continue;
182                                }
183
184                                if($this->rewriter === null) {
185                                        $this->boards[$i]->url = sprintf('index.php?page=Board&amp;boardID=%d', $row['boardID']);
186                                } else {
187                                        $this->boards[$i]->url = $this->rewriter->publicParseBoardURLs($row['boardID'], '');
188                                }
189
190                                // PRINT
191                               
192
193                                for($j=2; $j<$pages; $j++) {
194                                        $i++;
195                                        $this->boards[$i] = new Board($row['boardID'], $row);
196                                        $this->boards[$i]->time = $row['lastPostTime'];
197
198                                        if($this->rewriter === null) {
199                                                $this->boards[$i]->url = sprintf('index.php?page=Board&amp;boardID=%d&amp;pageNo=%d', $row['boardID'], $j);
200                                        } else {
201                                                $this->boards[$i]->url = $this->rewriter->publicParseMultipleBoardURLs($row['boardID'], $j, '');
202                                        }
203                                }
204
205                                $i++;
206
207
208                                // titles are not used again.. clear cache
209                                if($this->rewriter !== null) $this->rewriter->clearCache();
210                        }
211                }
212        }
213       
214        /**
215         * gets the board for the index
216         */
217        protected function readIndex() {
218                $this->readBoards();
219
220                //index
221                $tpl = '<sitemap><loc>'.PAGE_URL.'/index.php?page=Sitemaps&amp;boardID=%d</loc><lastmod>%s</lastmod></sitemap>';
222                printf($tpl, 0, date('c'));
223
224                foreach(array_filter($this->boards, create_function('$a','return isset($a->full);')) as $board) {
225                        printf($tpl, $board->boardID, date('c', $board->time));
226                }
227        }
228
229        /**
230         * @see Page::readParameters()
231         */
232        protected function readParameters () {
233                if(isset($_GET['boardID'])) {
234                        $this->wbb = true;
235                        $this->boardIDs = ArrayUtil::toIntegerArray(explode(',', $_GET['boardID']));
236                }
237        }
238
239        /**
240         * @see Page::readData()
241         */
242        protected function readData () {
243                if($this->eventObj->index) {
244                        //index
245                        $this->eventObj->setType('sitemapindex');
246                }
247               
248                else if($this->wbb) {
249                        //boards or threads
250                        $this->eventObj->setType('urlset');
251                }
252        }
253       
254        /**
255         * @see Page::assignVariables()
256         */
257        protected function assignVariables () {
258                if($this->eventObj->index) {
259                        //index
260                        $this->readIndex();
261                }
262               
263                else if($this->wbb) {
264                        if($this->boardIDs[0] == 0) {
265                                //boards
266                                $this->readBoards();
267                               
268                                $tpl = '<url><loc>'.PAGE_URL.'/%s</loc><lastmod>%s</lastmod></url>';
269                                foreach($this->boards as $board) {
270                                        printf($tpl, $board->url, date('c', $board->time));
271                                }
272                        } else {
273                                //threads
274                                $this->readThreads();
275                        }
276                }
277        }
278}
279?>
Note: See TracBrowser for help on using the browser.