source: trunk/www.guidonia.net/wp/wp-content/plugins/parteibuch-aggregator/pba_output_function.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 18.5 KB
Line 
1<?php
2class PBA extends BDPRSS2
3{
4
5 // Redefine the output function by wrapping the old output function
6 function outputwrapper($pba_calling_paras=1){
7 //when called with a single number, we understand output id is meant
8 $numargs = func_num_args();
9 if(!is_array($pba_calling_paras) && $numargs > 0) {
10 if(preg_match('/^([0-9]+)$/', $pba_calling_paras)) {
11 $pba_calling_paras_tmp=$pba_calling_paras;
12 $pba_calling_paras=array();
13 $pba_calling_paras['outputid']=$pba_calling_paras_tmp;
14 }
15 }
16
17 $pbaout['debug'] .= "\n<br>Debug: using new outputwrapper from class PBA extending BDPRSS2";
18 $pbaout['profiler']['Start of outputwrapper']=microtime();
19
20 //global objects
21 global $bdprss_db, $bdprss_search;
22
23 //quick - time is money - to answer questions:
24 //do we want the plugin to show anything at all?
25 //do we need to shutdown wordpress after outputwrapper was called?
26 //what to do - we expect parameter array containing output id, but just have $post
27 //can we cache parameter bootstrap?
28
29 $resultparas=PBALIB::bootstrap_parameters($pba_calling_paras);
30
31 if(!is_array($resultparas)){ //fix sidebar and box displaying and detection of correct output page later?
32 $pbaout['shutdown']=false;
33 $pbaout['result']=false;
34 return $pbaout;
35 }
36
37 //check for redirect - fix me later, this would be much better placed in the library and use the resultparas calculated before
38 if($resultparas['short_cache_link']){
39 $redirect_debug=false;
40 $exp2match="'" . get_option('home') . "/([a-zA-Z0-9-]+)/(index.php)?\?searchphrase=([a-zA-Z0-9-+%_]+)'s";
41 if($redirect_debug) echo "<br/>exp2match: " . $exp2match;
42 $request="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
43 if($redirect_debug) echo "<br>request_uri: " . $request;
44 preg_match($exp2match, $request, $matches);
45 if($redirect_debug) echo "<br>matches: " . PBALIB::get_r($matches);
46 if(isset($matches[1]) && isset($matches[3])){
47 $searchstring=str_replace("_","%5F",$matches[3]);
48 $searchstring=str_replace("+","_",$searchstring);
49 $searchstring=urlencode(str_replace("/","_",urldecode($searchstring)));
50 $redirect = get_option('home') . '/' . $matches[1] . "/s/" . $searchstring . "/";
51 if($redirect_debug) echo "<br/>redirect: " . $redirect;
52 //header("Location: " . $redirect);
53 //exit;
54 $pbaout['redirect']=true;
55 $pbaout['result']=$redirect;
56 return $pbaout;
57 }
58 } // end checking for redirect
59
60 //checkpoint parameter bootstrap finished
61 //the one and only result from all lines before this point shall be the resultparas array
62 //which has exactly the same keys as the default parameter array
63 //but we also got pbaout debug and pbaout profiler
64 if($resultparas["profiler_enabled"]) $pbaout['profiler']['End of parameter bootstrap']=microtime();
65 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: resultparas at checkpoint parameter bootstrap finished: " . PBALIB::get_r($resultparas);
66
67 if($resultparas['pba_full_cache_time'] > 0) {
68 $fullcache=@PBALIB::pba_cache($resultparas, $dummy, 'get', 'full', 'mixed', $resultparas['pba_full_cache_time'], 'OK');
69 if($fullcache[1]) {
70 $profilertmp = $pbaout['profiler'];
71 $pbaout = $fullcache[0];
72 $pbaout['profiler'] = $profilertmp;
73 if($resultparas["profiler_enabled"]) {
74 $pbaout['profiler']['Got full cache']=microtime();
75 $pbaout['profiler']=PBALIB::process_profiler($pbaout['profiler']);
76 }
77 return $pbaout;
78 }
79 }
80
81 //let's do some calculations based on these parameters not producing output
82 //but just producing more parameters, that shall make us living more easy later :-)
83
84 //calculate displayonlybox parameter - this is only a temporary solution and shall be removed later
85 //and unset all other displayonly, makeonly and feedlistrequest parameters
86 //lot's of parameter junk avoided
87 if($resultparas['opmlrequest'] == true || $resultparas['opmlrequest'] == 'Y'){
88 $resultparas['displayonlybox']='opml_box';
89 }elseif($resultparas['feedlistrequest'] == true || $resultparas['feedlistrequest'] == 'Y'){
90 $resultparas['displayonlybox']='feedlist_box';
91 }elseif(strlen($resultparas['displayonlybox'])>1){
92 //displayonlybox parameter is used, we will not care for more displayonly and makeonly parameters
93 true; //no need to unset display only and makeonly values here, unset a few rows later anyway
94 }elseif($resultparas['displayonlykalender'] == 'Y'){
95 if($resultparas['kalender_as_box']){
96 $resultparas['displayonlybox']='kalender_box';
97 }else{
98 $resultparas['displayonlybox']='kalenderlist_box';
99 }
100 }else{
101 true; //no need to unset here, anyway done in next lines, unset($resultparas['displayonlybox']);
102 }
103 unset($resultparas['displayonlykalender']);
104 unset($resultparas['displayonlysearchbox']);
105 unset($resultparas['makeonlyfeedlist']);
106 unset($resultparas['feedlistrequest']);
107 unset($resultparas['opmlrequest']);
108 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: Displayonlybox calculated: " . PBALIB::get_r($resultparas['displayonlybox']);
109
110 //generally available system constants
111 $resultparas["now"]=date('r');
112
113 //quickly calculate some href values for global navigation -
114 //these might be used in boxes and anywhere else in templates
115
116 //calculate some more global href values
117 //uses $resultparas['feedrequest'], $resultparas['feedpage'],
118 //$resultparas['srequri'], $resultparas['baseurl'], $resultparas['short_cache_link']
119 //$resultparas['searchphrase'], $resultparas['archivdate']
120
121 //calculate the href to the feedalized version of this page
122 $resultparas['feedhref']= PBALIB::makefeedhref($resultparas);
123
124 if($resultparas['feedrequest']){
125 $resultparas['htmlhref']= PBALIB::makehtmlhref($resultparas);
126 }
127
128 //calculate the href to the feedlist and opml for this page - where is output_id and list_id?
129 $resultparas['feedlisthref']= PBALIB::makefeedlisthref($resultparas);
130 $resultparas['feedopmlhref']= PBALIB::makefeedopmlhref($resultparas);
131
132 //link to kalender start page makedatepagehref($thedate="", &$resultparas, $regardkalenderlinkpart=false, $forcekalender=false)
133 $resultparas['kalenderhref']=PBALIB::makedatepagehref("", $resultparas, false, true);
134
135 //what page template was requested with this output function call?
136 //let's find out $resultparas['pagetype'], $resultparas['template']
137
138 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Before analyze pagetype']=microtime();
139
140 $pagetype=PBALIB::analysepagetype($resultparas);
141 $resultparas['pagetype']=$pagetype['pagetype'];
142 $resultparas['template']=$pagetype['template'];
143 $pbaout['error'].=$pagetype['error'];
144
145 //$resultparas['pagetype'] is now one of these: 'tickerpage'; 'searchpage'; 'datepage'; 'startpage'; 'feedpage'; 'feedsearchpage'; 'feeddatepage'; 'cachepage'; 'kalpage'; 'feedlistpage'; 'opmlpage'; 'nopage'; 'errorpage';
146 //$resultparas['template'] is one of these: ticker, feed, cache, kalender, $resultparas['displayonlybox']
147 //or a pseudotemplate for boxonlyrequests: kalender_box, kalenderlist_box, search_box, feedlistsidebar_box
148 //or error if we can't find out
149
150 if($resultparas['template'] == 'feed'
151 || $resultparas['template'] == 'feedlist_box'
152 || $resultparas['template'] == 'opml_box'
153 ) $pbaout['shutdown'] = true;
154
155 //calculate resultparas rawtemplate to understand which boxes we will have to build
156 if($resultparas['pagetype']=='nopage') {
157 $resultparas['rawtemplate'] = '###' . strtoupper($resultparas['template']) . '###';
158 } elseif(strlen($resultparas['template'])>0) {
159 //to do: this shall come via resultparas from db later
160 $resultparas['rawtemplate'] = $resultparas['template_' . $resultparas['template']];
161 }
162 if(!isset($resultparas['rawtemplate'])){
163 //to do: this shall come via resultparas from db later
164 $resultparas['rawtemplate'] = $resultparas['template_error'];
165 $pbaout['error'].= "Something went wrong with page type detection and template assignment:" . $resultparas['template'];
166 }
167
168 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: Before analyze needed boxes: pagetype: " . PBALIB::get_r($pagetype) . " Raw Template: " . $resultparas['rawtemplate'];
169 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Before analyze needed boxes']=microtime();
170
171 //now we can analyze our rawtemplate
172 //do we need boxes? which boxes do we need?
173 // we have the following boxes:
174 //1. search_box, 2. kalender_box, 3. kalenderlist_box, 4. feedlistsidebar_box, 5. sendfeedpermail_box - uh missing, but should be here
175
176 //ask config if to change this list, remove boxes, which are forbidden by config, add those explicitely required by config
177 $resultparas['rawtemplate'] .= $resultparas['forceboxmaking'];
178
179 $boxesneeded=PBALIB::analyze_needed_elements($resultparas['rawtemplate'], '_BOX');
180 $resultparas["boxesneeded"]=$boxesneeded['result'];
181
182
183 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: Detected needed boxes: " . PBALIB::get_r($resultparas["boxesneeded"]);
184 //now we know which boxes we need and can make them
185 if(is_array($resultparas["boxesneeded"])){
186 //no array means no box to make
187 foreach($resultparas["boxesneeded"] as $boxtomake => $dummy){
188 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Before making ' . $boxtomake]=microtime();
189 $box[$boxtomake]=PBALIB::makebox($boxtomake, $resultparas, $pbaout);
190 //if($resultparas["profiler_enabled"]) $pbaout['profiler']=array_merge($pbaout['profiler'], $box[$boxtomake]['profiler']);
191 }
192 } //end if(is_array($resultparas["boxesneeded"]))
193
194 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: Box making done. box: " . PBALIB::get_r($box);
195 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Box making done']=microtime();
196
197 if($resultparas["debug"]) $pbaout['debug1'] .= "\n<br>See my box collection: " . PBALIB::get_r($box);
198 //process boxes, shall be much earlier? why pbaout?
199 if(is_array($box)){
200 foreach($box as $boxname => $boxvalue){
201 if($boxvalue['result']) $pbaout[$boxname] = $boxvalue['result'];
202 }
203 }
204 if($resultparas["profiler_enabled"]) $pbaout['profiler']['After copying boxes to pbaout']=microtime();
205
206 if($resultparas['displayonlybox']){
207 $pbaout['result']=$box[$resultparas['displayonlybox']]['result'];
208 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: We exit here, because box making done and resultparas['displayonlybox']: " . $resultparas['displayonlybox'];
209 if($resultparas["profiler_enabled"]) $pbaout['profiler']=PBALIB::process_profiler($pbaout['profiler']);
210 return $pbaout;
211 }
212
213 //to be completed - sensible input and prevent system outpage due to misconfiguration
214 if( $resultparas['maxitems'] > $resultparas['maxitemslimit']
215 || $resultparas['maxitems'] == 0 || $resultparas['maxitems'] == 'N'
216 ) $resultparas['maxitems'] = $resultparas['maxitemslimit'];
217
218 //calculate some basic parameters like used in old output routine
219 //start parameter for db query from tickerpage and maxitems
220 $resultparas['start']=($resultparas['tickerpage']-1) * $resultparas['maxitems'];
221
222 //disable social bookmarks for search results - more conditions for disabling? where to do it best?
223 if($resultparas['searchphrase'] !="") $resultparas['add_social_bookmarks']="N";
224
225 //checkpoint all resultparas calculated - better name would be: before main loop
226 // here are all input resultparas calculated, no more resultparas changing after this line. exception: gettheage set's it's array resultparameter on first call
227 if($resultparas["profiler_enabled"]) $pbaout['profiler']['All resultparas calculated']=microtime();
228 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: resultparas at checkpoint all resultparas calculated: " . PBALIB::get_r($resultparas);
229
230//use new output routine
231//when not a kalender request, - onlybox requests exited already - let's query the ids, for kalender request we need completely diffrent handling
232 if($resultparas['pagetype']!='errorpage' && $resultparas['pagetype']!= 'kalpage' && !$resultparas['displayonlybox']){
233 //prepare some basic values - may be do better later, when explicitely requested?
234
235 // prepare the acceptable tags - it's constant per output id, so it could be made already when storing the output config !!!
236 $resultparas['formattedtagset']=PBALIB::processtagset($resultparas['tagset']);
237
238 //set parameters depending on formattype
239 //possible formattypes: 'countrecentitem', 'daterecentitem', 'sitealpha', 'siteupdate'
240
241 if($resultparas['formattype']=='daterecentitem') $resultparas['fromtimestamp'] = time() - $resultparas['daterecentitemthreshold'];
242 if($resultparas['formattype']=='siteupdate') $resultparas['opsfilter']=true;
243 if($resultparas['formattype']=='sitealpha') {
244 $resultparas['opsfilter']=true;
245 $resultparas['orderbysitename']=true;
246 $resultparas['fromtimestamp'] = time() - $resultparas['sitealphathreshold'];
247 }
248
249 global $bdprsssearchdebug;
250 $bdprsssearchdebug=false;
251 //parameter site ids and parameter feed not used so far
252 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Before search4items']=microtime();
253 $id_result = $bdprss_search->bdprss_search4items($resultparas['searchphrase'], $resultparas['start'], $resultparas['maxitems'], false, $resultparas['listid'], $resultparas['archivdate'], "", $resultparas['fromtimestamp'], $resultparas['totimestamp'], $resultparas['opsfilter'], $resultparas['orderbysitename'], $resultparas['cacheid']);
254 $bdprsssearchdebug=false;
255 global $found_tickeritems;
256 $pbaout['founditems']=$found_tickeritems;
257 $pbaout['startitem']=$resultparas['start'] + 1;
258 if($found_tickeritems == 0) $pbaout['startitem'] = "0";
259 $pbaout['lastitem']=min($found_tickeritems,$resultparas['start']+$resultparas['maxitems']);
260 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: we got an item id list from the search engine: " . PBALIB::get_r($id_result);
261
262//calculate last page and next page href
263 if($resultparas['tickerpage']>1) {
264 $pbaout['lastpageexists']=true;
265 $pbaout['lastpage']=$resultparas['tickerpage'] - 1;
266 $pbaout['lastpagehref']=PBALIB::makelastpagehref($pbaout['lastpage'], $resultparas);
267 }
268
269 if($pbaout['lastitem'] < $pbaout['founditems']) {
270 $pbaout['nextpageexists']=true;
271 $pbaout['nextpage']=$resultparas['tickerpage'] + 1;
272 $pbaout['nextpagehref']=PBALIB::makenextpagehref($pbaout['nextpage'], $resultparas);
273 }
274
275//get the real row data
276//to do better would be to have row wise query in while loop
277 if($id_result){
278
279 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Before getsitenitems']=microtime();
280 $itemset = $bdprss_db->getsitenitems(&$id_result, $resultparas['orderbysitename']);
281 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: we got result from the item table: " . PBALIB::get_r($itemset);
282
283 //might be sensible to make programmer interface to give out original db row results as option?
284 //$pbaout['rawrowdata']=$itemset;
285
286
287 //now let's loop through the data rows and format them
288 if($itemset){
289 if($resultparas["profiler_enabled"]) $pbaout['profiler']['Before formatting items']=microtime();
290
291 if($resultparas['pagetype']!='cachepage') {
292 $itemtemplate=PBALIB::preprocess_itemtemplate($resultparas);
293 }else{
294 $footertemplate=PBALIB::preprocess_headertemplate(&$resultparas, &$pbaout, true);
295 $itemtemplate=$footertemplate;
296 }
297 $itemvaluesneeded=PBALIB::analyze_needed_elements($itemtemplate);
298 if($resultparas['cacheid']>0) $itemtemplate ="";
299 if($resultparas["debug"]) $pbaout['debug'] .= "\n<br>Debug: we need to calculate the following item values: " . PBALIB::get_r($itemvaluesneeded);
300 foreach($itemset as $resultrownumber => $item) {
301 if($resultparas["profiler_enabled"] > 1) $pbaout['profiler']['Before formatitem ' . $resultrownumber]=microtime();
302 $formatteditem = PBALIB::formatitem($item, $resultparas, $itemtemplate, $resultrownumber, $itemvaluesneeded['result']);
303 if(!$resultparas['cacheid']>0) $pbaout['body'] .= $formatteditem['result']; //cache page made in footer!!!
304 if($resultparas["debug"]) $pbaout['debug'] .= $formatteditem['debug'];
305 $pbaout['firstitem_datefeed'] .= $formatteditem['firstitem_datefeed'];
306 }
307 if($resultparas["profiler_enabled"]) $pbaout['profiler']['After formatting items']=microtime();
308 } // end if itemset, to do: fine error output, when no row found
309 } //end if($id_result), to do: fine error output, when no row found
310
311 } //end if (!$resultparas['kalreq'] && !$resultparas['displayonlybox'])
312
313
314 if(!$resultparas['cacheid']>0) $formatteditem = false; // we need these items only for cache displaying
315 //make a header
316 if(!$resultparas['cacheid']>0 && !$resultparas['kalreq'] && !$resultparas['displayonlybox']
317 && !($resultparas['suppressheaderonpage1'] == 'Y' && $resultparas['pagetype'] == 'startpage')
318 ) {
319 $headertemplate=PBALIB::preprocess_headertemplate(&$resultparas, &$pbaout);
320 $formattedheader = PBALIB::formatheader($resultparas, $pbaout, false, false, $headertemplate);
321 $pbaout['header'] = $formattedheader['result'];
322 if($resultparas["debug"]) $pbaout['debug'] .= $formattedheader['debug'];
323 }
324
325 //make a footer
326 if(!$resultparas['displayonlybox']) {
327 if(!$footertemplate) $footertemplate=PBALIB::preprocess_headertemplate(&$resultparas, &$pbaout, true);
328 $formattedfooter = PBALIB::formatheader($resultparas, $pbaout, true, $formatteditem, $footertemplate);
329 $pbaout['footer'] = $formattedfooter['result'];
330 if($resultparas["debug"]) $pbaout['debug'] .= $formattedfooter['debug'];
331 }
332
333 //some more output
334 $pbaout['searchphrase']=$resultparas['searchphrase'];
335 $pbaout['archivdate']=$resultparas['archivdate'];
336 $pbaout['feedhref']=$resultparas['feedhref'];
337 $pbaout['feedlisthref']=$resultparas['feedlisthref'];
338 $pbaout['feedopmlhref']=$resultparas['feedopmlhref'];
339 $pbaout['kalreq']=$resultparas['kalreq'];
340 $pbaout['cacheid']=$resultparas['cacheid'];
341
342 $pbaout['result']=$pbaout['header'] . $pbaout['body'] . $pbaout['footer'];
343 if(strlen($pbaout['error']) >0) $pbaout['result'] .= str_replace('###ERRORMESSAGE###', $pbaout['error'], $resultparas['template_error']);
344 if($resultparas["profiler_enabled"]) $pbaout['profiler']=PBALIB::process_profiler($pbaout['profiler']);
345
346 if($resultparas['pba_full_cache_time'] > 0) {
347 $pba_cachereturn=PBALIB::pba_cache($fullcache[0], $pbaout, 'write', 'full', 'mixed', 0, 'OK');
348 if($resultparas["debug"] && $pba_cachereturn) $pbaout['debug'] .= "\n<br>Wrote cache: full";
349 }
350
351 return $pbaout;
352 } // end of new output function
353
354}
355
356require_once(dirname(__FILE__) . '/pba_output_library.php');
357?>
Note: See TracBrowser for help on using the repository browser.