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

Revision 444, 7.4 kB (checked in by d0nut, 5 years ago)

0.2.1 release

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