forked from NoahY/q2a-book
-
Notifications
You must be signed in to change notification settings - Fork 2
/
qa-plugin.php
executable file
·450 lines (371 loc) · 15.5 KB
/
qa-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
}
qa_register_plugin_module('module', 'qa-book-admin.php', 'qa_book_admin', 'Book Export');
qa_register_plugin_overrides('qa-book-overrides.php');
qa_register_plugin_module('widget', 'qa-book-widget.php', 'qa_book_widget', 'Book Widget');
qa_register_plugin_phrases('qa-book-lang-*.php', 'book');
require 'util-book.php';
function qa_book_catselect(){
if(qa_opt('book_plugin_catex'))
{
$ex = qa_opt('book_plugin_catex');
$excat = "(select categoryid from ^categories where parentid in ($ex) union
select categoryid from ^categories where parentid in (select categoryid from ^categories where parentid in ($ex)) union
select categoryid from ^categories where categoryid in ($ex))";
return $excat;
}
else return '()';
}
function qa_book_getallcats(&$cats, $all=false){
$cats = qa_db_read_all_assoc(
qa_db_query_sub(
'SELECT c.categoryid, concat(ifnull(concat(p.title," "), ""), c.title) as title FROM ^categories c left join ^categories p on (c.parentid = p.categoryid) '.// categoryid not in (select parentid from ^categories where parentid is not null) '.
(!$all?
(qa_opt('book_plugin_catex')?' where c.categoryid NOT IN '.qa_book_catselect():'')
:'')
.' order by title'
)
);
$navcats = array();
foreach($cats as $cat)
$navcats[$cat['categoryid']] = $cat;
return $navcats;
}
function qa_book_plugin_createBook($return=false) {
$book = qa_opt('book_plugin_template');
// static replacements
$extras='';
if(qa_opt('qa-mathjax-enable'))
{
$extras.= '<script type="text/x-mathjax-config">'. qa_opt('qa-mathjax-config').'</script>';
$extras .= '<script async type="text/javascript" src="'.qa_opt('qa-mathjax-url').'"></script>';
}
if(qa_opt("qa-pretiffy-enable"))
{
$extras.='<script async type="text/javascript" src="'.qa_opt('qa-pretiffy-url').'"></script>';
}
$book = str_replace('[css]',qa_opt('book_plugin_css'),$book);
$book = str_replace('[script]',$extras,$book);
$book = str_replace('[front]',qa_opt('book_plugin_template_front'),$book);
$book = str_replace('[back]',qa_opt('book_plugin_template_back'),$book);
$shuffle = qa_opt('book_plugin_shuffle');
$iscats = qa_opt('book_plugin_cats');
// categories
$cats = array(false);
if($iscats) {
$navcats = qa_book_getallcats($cats);
}
// intro
$intro = '<p>'.qa_opt('book_plugin_intro').'</p>';
$ack = '<p>'.qa_opt('book_plugin_ack').'</p>';
$intro = str_replace('[sort_questions]',qa_lang('book/'.(qa_opt('book_plugin_sort_q') == 0?'sort_upvotes':'sort_date')),$intro);
$intro = str_replace('[sort_categories]',$iscats?qa_lang('book/sort_categories'):'',$intro);
$intro = str_replace('[restrict_questions]',qa_opt('book_plugin_req_qv')?qa_lang_sub('book/restrict_q_x_votes',qa_opt('book_plugin_req_qv_no')):qa_lang('book/all_questions'),$intro);
$rq = array();
if(qa_opt('book_plugin_req_sel'))
$rq[] = qa_lang('book/restrict_selected');
if(qa_opt('book_plugin_req_abest'))
$rq[] = qa_lang('book/restrict_best_a');
if(qa_opt('book_plugin_req_av'))
$rq[] = qa_lang_sub('book/restrict_a_x_votes',qa_opt('book_plugin_req_av_no'));
if(empty($rq))
$intro = str_replace('[restrict_answers]','',$intro);
else {
$rqs = qa_lang('book/restrict_answers_clause_'.count($rq));
foreach($rq as $i => $v)
$rqs = str_replace('('.($i+1).')',$v,$rqs);
$intro = str_replace('[restrict_answers]',$rqs,$intro);
}
$tocout = '';
$qout = '';
$ccount=0;
$catanchor = '';
foreach($cats as $cat) {
$qcount=0;
$incsql = '';
$anssql = ' and ans.type = \'A\' ';
$sortsql = '';
$toc = '';
$qhtml = '';
if(qa_opt('book_plugin_sort_q') == 0)
$sortsql='ORDER BY qs.netvotes DESC, qs.created ASC';
else
$sortsql='ORDER BY qs.created ASC';
if(qa_opt('book_plugin_req_sel'))
$incsql .= ' AND qs.selchildid=ans.postid';
if(qa_opt('book_plugin_req_abest'))
$sortsql.=', ans.netvotes DESC'; // get all, limit later with break
if(qa_opt('book_plugin_req_qv'))
$incsql .= ' AND qs.netvotes >= '.(int)qa_opt('book_plugin_req_qv_no');
if(qa_opt('book_plugin_req_av'))
$anssql .= ' AND ans.netvotes >= '.(int)qa_opt('book_plugin_req_av_no');
$skipanswers = !qa_opt('book_plugin_show_a');
$reqanswers = qa_opt('book_plugin_req_ans');
$introsuffix = '';
if(qa_opt('book_plugin_enable_custom_filter1')){
$incsql .= " and (".qa_opt('book_plugin_custom_filter1').")";
$introsuffix.='<p>'. qa_opt('book_plugin_custom_filter1_desc').'</p>';
}
if(qa_opt('book_plugin_enable_custom_filter2')){
$incsql.=" and (".qa_opt('book_plugin_custom_filter2').")";
$introsuffix.='<p>'. qa_opt('book_plugin_custom_filter2_desc').'</p>';
}
if(qa_opt('book_plugin_enable_custom_filter3')){
$incsql .= " and (".qa_opt('book_plugin_custom_filter3').")";
$introsuffix.='<p>'. qa_opt('book_plugin_custom_filter3_desc').'</p>';
}
if(qa_opt('book_plugin_enable_custom_filter4')){
$incsql .= " and (".qa_opt('book_plugin_custom_filter4').")";
$introsuffix.='<p>'. qa_opt('book_plugin_custom_filter4_desc').'</p>';
}
if(qa_opt('book_plugin_enable_custom_filter5')){
$incsql .= " and (".qa_opt('book_plugin_custom_filter5').")";
$introsuffix.='<p>'. qa_opt('book_plugin_custom_filter5_desc').'</p>';
}
if($skipanswers){
$anssql .= ' AND ans.postid < 0 ';
}
$sortsql.=', ans.netvotes DESC'; // get all, limit later with break
$selectspec="SELECT qs.postid AS postid, BINARY qs.title AS title, BINARY qs.content AS content, qs.format AS format, qs.netvotes AS netvotes, qs.tags as tags, qs.selchildid as selected, ans.postid as apostid, BINARY ans.content AS acontent, ans.format AS aformat, ans.userid AS auserid, ans.netvotes AS anetvotes FROM ^posts qs ".($reqanswers?"":"left outer join")." ^posts ans on qs.postid=ans.parentid and qs.type='Q' and ans.type='A' where true ".($iscats?" AND qs.categoryid=".$cat['categoryid']:"") ." ". $incsql." ".$anssql." ".$sortsql;
$qs = qa_db_read_all_assoc(
qa_db_query_sub(
$selectspec
)
);
if(empty($qs)) // no questions in this category
continue;
$cqcount = 0;
$ccount++;
$q2 = array();
foreach($qs as $q) { // group by questions
$q2['q'.$q['postid']][] = $q;
}
//usort($q2,array($this, "mysort"));
if(!$shuffle)
usort($q2, "mysort");
else
{
//shuffle($q2);
}
$catanchor = 'cat'.$cat['categoryid'];
$topicanchor=null;
$oldmint='';
$answers='';//for pushing answers
$tcount = 0;
$answerblockprefix = '<h2 class="answers-block">Answers: <a class="topic-link" href="#topic[topic]">[topicname]</a></h2>';
$topicblockprefix = '<div class="topic-block" id="topic[tlink]"><h2 class="top-title"><a class="topic-link" href="'.qa_opt('site_url').'tag/[topicurl]">[topic]</a></h2><a class="top-link" href=#[top-link]>top</a></div>';
foreach($q2 as $qs) {
usort($qs, "mysortanswers");
// toc entry
$cqcount++;
$mint = mintag($qs[0]);
if($mint !== $oldmint){
if($mint !== '' && !$shuffle)
{
if(qa_opt('book_plugin_push_a') && ($answers != '')){
$answerblock = str_replace("[topic]", gettag($oldmint), $answerblockprefix);
$answerblock = str_replace("[topicname]", $oldmint, $answerblock);
$qhtml .=qa_book_answerblockprefix($oldmint,$answerblockprefix). $answers;
$answers='';
}
$tcount++;
$topicanchor= 'topic'.gettag($mint);
$qcount = 0;
$number="<div class=\"number\">".$ccount.".".$tcount."</div>";
$topic = str_replace("[topic]", $number.' '.$mint, $topicblockprefix);
$topic = str_replace("[topicurl]", gettag($mint), $topic);
$topic = str_replace("[tlink]", gettag($mint), $topic);
$topic = str_replace("[top-link]", $catanchor, $topic);
$qhtml .= $topic;
//$toc.=str_replace('[qlink]','<a href="#question'.$qs[0]['postid'].'">'.$mint.'</a>',qa_opt('book_plugin_template_toc'));
$toc.=str_replace('[qlink]','<a href="#topic'.gettag($mint).'">'.$mint.'</a>',qa_opt('book_plugin_template_toc'));
}
else {
// $toc.=str_replace('[qlink]','<a href="#question'.$qs[0]['postid'].'">'.$qs[0]['title'].'</a>',qa_opt('book_plugin_template_toc'));
}
$oldmint = $mint;
}
// answer html
$as = '';
$nv = false;
$acount= 0;
foreach($qs as $idx => $q) {
if(qa_opt('book_plugin_req_abest') && qa_opt('book_plugin_req_abest_max') && $idx >= qa_opt('book_plugin_req_abest_max'))
break;
if($nv !== false && qa_opt('book_plugin_req_abest') && $nv != $q['anetvotes']) // best answers only
break;
/*arjun*/
if($idx && ($q['anetvotes'] < qa_opt('book_plugin_req_av_no')))
break;
$acount++;
$acontent = '';
if(!empty($q['acontent'])) {
$viewer=qa_load_viewer($q['acontent'], $q['aformat']);
$acontent = $viewer->get_html($q['acontent'], $q['aformat'], array());
$noanswer = false;
}
$a = str_replace('[answer]',$acontent,qa_opt('book_plugin_template_answer'));
if($q['selected'] == $q['apostid'] && $q['apostid'] !== NULL){
$a = str_replace('[best]', 'bestanswer', $a);
$a = str_replace('[bestanswer]', 'bestanswercontent', $a);
$a = str_replace('[beforebest]', '<div class="tick"> <button disabled class="qa-a-select-button"><span class="fa fa-check"></span></button></div><div class="best-text">Selected Answer</div>', $a);
$a = str_replace('[afterbest]', '', $a);
}
else{
$a = str_replace('[best]', '', $a);
$a = str_replace('[bestanswer]', '', $a);
$a = str_replace('[beforebest]', '', $a);
$a = str_replace('[afterbest]', '', $a);
}
if($q['auserid'] !== NULL)
$a = str_replace('[answerer]',qa_get_user_name($q['auserid']),$a);
else {
$a = str_replace('[answerer]','',$a);
}
$nv = $q['anetvotes'];
if($q['anetvotes'] !== NULL)
$a = str_replace('[votes]',$nv,$a);
else {
$a = str_replace('[votes]','',$a);
}
$res = qa_db_query_sub("select points from ^userpoints where userid=#", $q['auserid']);
$points = qa_db_read_one_value($res, true);
if($q['auserid'] !== NULL)
$a = str_replace('[upoints]',qa_format_number($points, 0, true),$a);
else {
$a = str_replace('[upoints]','',$a);
}
$as .= $a;
}
// question html
$qcontent = '';
if(!empty($q['content'])) {
$qcount++;
$viewer=qa_load_viewer($q['content'], $q['format']);
$qcontent = $viewer->get_html($q['content'], $q['format'], array());
}
$tagshtml='';
$tags = qa_tagstring_to_tags($q['tags']);
$mint=mintag($q);
foreach ($tags as $tag)
{
$tagshtml.="<li class=\"qa-q-view-tag-item\"> <a href=\"".qa_opt("site_url")."tag/".$tag."\" class=\"qa-tag-link\">".$tag." </a></li>";
}
if($mint !== '')
$mint.=": ";
$number="<div class=\"number\">".$ccount.".".$tcount.".".$qcount."</div>";
$titleright="<div class=\"title-right\"><a href=\"".qa_opt("site_url").$q['postid']."\">".qa_opt("site_url").$q['postid']."</a></div>";
$oneq = str_replace('[question-title]',$number.$mint.$q['title'].$titleright,qa_opt('book_plugin_template_question'));
$oneq = str_replace('[qanchor]','question'.$q['postid'],$oneq);
$oneq = str_replace('[qurl]',qa_html(qa_q_request($q['postid'],$q['title'])),$oneq);
$oneq = str_replace('[question]',$qcontent,$oneq);
$oneq = str_replace('[top-link]',($topicanchor? $topicanchor:$catanchor),$oneq);
$oneq = str_replace('[tags]', $tagshtml, $oneq);
$oneq = str_replace('[hide]', '', $oneq);
// output with answers
if($skipanswers)
$qhtml .= str_replace('[answers]','',$oneq);
else if(qa_opt('book_plugin_push_a') && ($as != '')) {
$qhtml .= str_replace('[answers]','<a class="answer-link" href="#a-question'.$q['postid'].'">Answer<a/>',$oneq);
$onea = str_replace('[question-title]',$number.$mint.$q['title'].$titleright,qa_opt('book_plugin_template_question'));
$onea = str_replace('[qanchor]','a-question'.$q['postid'],$onea);
$onea = str_replace('[qurl]','#question'.$q['postid'],$onea);
$onea = str_replace('[site-url]','',$onea);
$onea = str_replace('[question]','',$onea);
$onea = str_replace('[top-link]','question'.$q['postid'],$onea);
$onea = str_replace('[tags]', '', $onea);
$onea = str_replace('[hide]', 'hide', $onea);
$answers .= str_replace('[answers]',$as,$onea);
}
else
$qhtml .= str_replace('[answers]',$as,$oneq);
}
if(qa_opt('book_plugin_push_a')){
$qhtml .= qa_book_answerblockprefix($oldmint,$answerblockprefix). $answers;
}
if($iscats) {
$tocout .= '<li><a href="#cat'.$cat['categoryid'].'" onclick="toggle(\'cat'.$cat['categoryid'].'Details\')" class="toc-cat">'.$cat['title'].' ('.$cqcount.')'. '</a><span id="cat'.$cat['categoryid'].'Details"> <ol class="toc-ul">'.$toc.'</ol></li>';
// todo fix category link
$catnumber="<div class=\"number\">$ccount</div>";
$catout = str_replace('[cat-url]',qa_path_html('questions/'.qa_category_path_request($navcats, $cat['categoryid'])),qa_opt('book_plugin_template_category'));
$catout = str_replace('[cat-anchor]','cat'.$cat['categoryid'],$catout);
$catout = str_replace('[cat-title]',$catnumber.' '.$cat['title'].' ('.$cqcount.')',$catout);
$catout = str_replace('[questions]',$qhtml,$catout);
$qout .= $catout;
}
else {
if(!$shuffle)
$tocout .= '<ol class="toc-ul">'.$toc.'</ol>';
$catout = str_replace('[questions]',$qhtml,qa_opt('book_plugin_template_questions'));
$qout .= $catout;
}
}
if($iscats)
$tocout = '<ol class="toc-ul">'.$tocout.'</ol>';
$book = str_replace('[intro]',$intro.$introsuffix,$book);
$book = str_replace('[ack]',$ack,$book);
// add toc and questions
$book = str_replace('[toc]',$tocout,$book);
$book = str_replace('[categories]',$qout,$book);
// misc subs
$book = str_replace('[site-title]',qa_opt('site_title'),$book);
$book = str_replace('[site-url]',qa_opt('site_url'),$book);
$book = str_replace('[date]',date('M j, Y'),$book);
error_log('Q2A Book Created on '.date('M j, Y \a\t H\:i\:s'). ' for '.$selectspec);
if($return){
//qa_opt('book_plugin_refresh_last',time());
return $book;
}
if(file_put_contents(qa_opt('book_plugin_loc'),$book)) {
qa_opt('book_plugin_refresh_last',time());
}
if(qa_opt('book_plugin_pdf'))
qa_book_plugin_create_pdf();
return 'Book Created';
//return 'Error creating '.qa_opt('book_plugin_loc').'; check the error log.';
}
function qa_book_answerblockprefix($oldmint, $answerblockprefix)
{
$answerblock = str_replace("[topic]", gettag($oldmint), $answerblockprefix);
return str_replace("[topicname]", $oldmint, $answerblock);
}
function qa_book_plugin_create_pdf($return=false) {
include 'wkhtmltopdf.php';
//echo $html;
$pdf = new WKPDF();
$pdf->render_q2a();
if($return)
$pdf->output(WKPDF::$PDF_DOWNLOAD,'book.pdf');
else
$pdf->output(WKPDF::$PDF_SAVEFILE,qa_opt('book_plugin_loc_pdf'));
error_log('Q2A PDF Book Created on '.date('M j, Y \a\t H\:i\:s'));
}
function qa_get_user_name($uid) {
$handles = qa_userids_to_handles(array($uid));
$handle = $handles[$uid];
if(QA_FINAL_EXTERNAL_USERS) {
$user_info = get_userdata($uid);
if ($user_info->display_name)
$name = $user_info->display_name;
}
else {
$name = qa_db_read_one_value(
qa_db_query_sub(
'SELECT content AS name FROM ^userprofile '.
'WHERE userid=# AND title=$',
$uid, 'name'
),
true
);
}
if(!@$name)
$name = $handle;
return strlen($handle) ? ('<A HREF="'.qa_path_absolute('user/'.$handle).
'" CLASS="qa-user-link">'.qa_html($name).'</A>') : 'Anonymous';
}
/*
Omit PHP closing tag to help avoid accidental output
*/