-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
txplib_publish.php
1359 lines (1150 loc) · 44.9 KB
/
txplib_publish.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2023 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Tools for page routing and handling article data.
*
* @since 4.5.0
* @package Routing
*/
/**
* Build a query qualifier to remove non-frontpage articles from the result set.
*
* @return string An SQL qualifier for a query's 'WHERE' part
*/
function filterFrontPage($field = 'Section', $column = array('on_frontpage'), $not = false)
{
static $filterFrontPage = array();
global $txp_sections;
is_array($column) or $column = do_list_unique($column);
$column = array_intersect($column, array_keys($txp_sections['default']));
sort($column);
$key = $field.'.'.implode('.', $column);
$not = $not ? 'NOT ' : '';
if (!isset($filterFrontPage[$key])) {
$filterFrontPage[$key] = '0';
$rs = array();
if ($field) {
$num_sections = count($txp_sections);
$field = doSlash($field);
foreach ($column as $col) {
$rs += array_filter(array_column($txp_sections, $col, 'name'));
}
if ($count = count($rs)) {
$filterFrontPage[$key] = $count == $num_sections ? '1' : (2*$count < $num_sections ?
"$field IN(".quote_list(array_keys($rs), ',').")" :
"NOT $field IN(".quote_list(array_keys(array_diff_key($txp_sections, $rs)), ',').")"
);
}
} elseif($column) {
foreach ($column as $col) {
$rs[] = is_numeric($txp_sections['default'][$col]) ? $col : "$col > ''";
}
$filterFrontPage[$key] = implode(' AND ', $rs);
}
}
return ' AND '.preg_replace('/^NOT NOT /', '', $not.$filterFrontPage[$key]);
}
/**
* Populates the current article data.
*
* Fills members of $thisarticle global from a database row.
*
* Keeps all article tag-related values in one place, in order to do easy
* bugfixing and ease the addition of new article tags.
*
* @param array $rs An article as an associative array
* @example
* if ($rs = safe_rows_start("*,
* UNIX_TIMESTAMP(Posted) AS uPosted,
* UNIX_TIMESTAMP(Expires) AS uExpires,
* UNIX_TIMESTAMP(LastMod) AS uLastMod",
* 'textpattern',
* "1 = 1"
* ))
* {
* global $thisarticle;
* while ($row = nextRow($rs))
* {
* populateArticleData($row);
* echo $thisarticle['title'];
* }
* }
*/
function populateArticleData($rs)
{
global $production_status, $thisarticle, $trace;
foreach (article_column_map() as $key => $column) {
$thisarticle[$key] = isset($rs[$column]) ? $rs[$column] : null;
}
if ($production_status === 'debug') {
$trace->log("[Article: '{$thisarticle['thisid']}']");
}
}
/**
* Formats article info and populates the current article data.
*
* Fills members of $thisarticle global from a database row.
*
* Basically just converts an article's date values to UNIX timestamps.
* Convenience for those who prefer doing conversion in application end instead
* of in the SQL statement.
*
* @param array $rs An article as an associative array
* @example
* article_format_info(
* safe_row('*', 'textpattern', 'Status = 4 LIMIT 1')
* )
*/
function article_format_info($rs)
{
$rs['uPosted'] = isset($rs['Posted']) && ($unix_ts = strtotime($rs['Posted'])) !== false ? $unix_ts : null;
$rs['uLastMod'] = isset($rs['LastMod']) && ($unix_ts = strtotime($rs['LastMod'])) !== false ? $unix_ts : null;
$rs['uExpires'] = isset($rs['Expires']) && ($unix_ts = strtotime($rs['Expires'])) !== false ? $unix_ts : null;
populateArticleData($rs);
}
/**
* Maps 'textpattern' table's columns to article data values.
*
* This function returns an array of 'data-value' => 'column' pairs.
*
* @return array
*/
function article_column_map()
{
static $column_map = array();
if (empty($column_map)) {
$column_map = array(
'thisid' => 'ID',
'posted' => 'uPosted', // Calculated value!
'expires' => 'uExpires', // Calculated value!
'modified' => 'uLastMod', // Calculated value!
'annotate' => 'Annotate',
'comments_invite' => 'AnnotateInvite',
'authorid' => 'AuthorID',
'title' => 'Title',
'url_title' => 'url_title',
'description' => 'description',
'category1' => 'Category1',
'category2' => 'Category2',
'section' => 'Section',
'keywords' => 'Keywords',
'article_image' => 'Image',
'comments_count' => 'comments_count',
'body' => 'Body_html',
'excerpt' => 'Excerpt_html',
'override_form' => 'override_form',
'status' => 'Status',
);
foreach (getCustomFields() as $i => $name) {
isset($column_map[$name]) or $column_map[$name] = 'custom_'.$i;
}
}
return $column_map;
}
/**
* Find an adjacent article relative to a provided threshold level.
*
* @param scalar $threshold The value to compare against
* @param string $s Optional section restriction
* @param string $type Lesser or greater neighbour? Either '<' (previous) or '>' (next)
* @param array $atts Attribute of article at threshold
* @param string $threshold_type 'cooked': Use $threshold as SQL clause; 'raw': Use $threshold as an escapable scalar
* @return array|bool An array populated with article data, or 'false' in case of no matches
*/
function getNeighbour($threshold, $s, $type, $atts = array(), $threshold_type = 'raw')
{
static $cache = array();
static $types = array(
'>' => array(
'desc' => '>',
'asc' => '<',
),
'<' => array(
'desc' => '<',
'asc' => '>',
),
);
$key = md5($threshold.$s.$type.join(n, $atts));
if (isset($cache[$key])) {
return $cache[$key];
}
$thisid = isset($atts['thisid']) ? intval($atts['thisid']) : 0;
$sortdir = isset($atts['sortdir']) ? strtolower($atts['sortdir']) : 'asc';
$sortby = isset($atts['sortby']) ? $atts['sortby'] : 'Posted';
// Invert $type for ascending sortdir.
$type = ($type == '>') ? $types['>'][$sortdir] : $types['<'][$sortdir];
// Escape threshold and treat it as a string unless explicitly told otherwise.
if ($threshold_type != 'cooked') {
$threshold = "'".doSlash($threshold)."'";
}
$where = isset($atts['?']) ? $atts['?'] : '1';
$tables = isset($atts['#']) ? $atts['#'] : safe_pfx('textpattern');
$columns = isset($atts['*']) ? $atts['*'] : '*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod';
$q = array(
"SELECT $columns FROM $tables",
"WHERE ($sortby $type $threshold OR ".($thisid ? "$sortby = $threshold AND ID $type $thisid" : "0").") AND $where",
"ORDER BY $sortby ".($type == '<' ? 'DESC' : 'ASC').', ID '.($type == '<' ? 'DESC' : 'ASC').' LIMIT 1'
);
$cache[$key] = getRow(join(' ', $q));
return (is_array($cache[$key])) ? $cache[$key] : false;
}
/**
* Find next and previous articles relative to a provided threshold level.
*
* @param int $id The "pivot" article's id; use zero (0) to indicate $thisarticle
* @param scalar $threshold The value to compare against if $id != 0
* @param string $s Optional section restriction if $id != 0
* @return array An array populated with article data
*/
function getNextPrev($id = 0, $threshold = null, $s = '')
{
$threshold_type = 'cooked';
$atts = filterAtts() or $atts = filterAtts(array());
if ($id !== 0) {
// Pivot is specific article by ID: In lack of further information,
// revert to default sort order 'Posted desc'.
$atts += array(
'sortby' => 'Posted',
'sortdir' => 'DESC',
'thisid' => $id,
);
$threshold_type = 'raw';
} else {
// Pivot is $thisarticle: Use article attributes to find its neighbours.
assert_article();
global $thisarticle;
if (!is_array($thisarticle)) {
return array();
}
$s = $thisarticle['section'];
$atts += array(
'thisid' => $thisarticle['thisid'],
'sort' => 'Posted DESC',
);
$atts['sort'] = trim($atts['sort']);
if (empty($atts['sort'])) {
$atts['sortby'] = !empty($atts['id']) ? "FIELD(ID, ".$atts['id'].")" : 'Posted';
$atts['sortdir'] = !empty($atts['id']) ? 'ASC' : 'DESC';
} elseif (preg_match('/^([$\w\x{0080}-\x{FFFF}]+|`[\x{0001}-\x{FFFF}]+`)(?i)(\s+asc|\s+desc)?$/u', $atts['sort'], $m)) {
// The clause's first verb is a MySQL column identifier.
$atts['sortby'] = trim($m[1], ' `');
$atts['sortdir'] = (isset($m[2]) ? trim($m[2]) : 'ASC');
} elseif (preg_match('/^((?>[^(),]|(\((?:[^()]|(?2))*\)))+)(\basc|\bdesc)?$/Ui', $atts['sort'], $m)) {
// More complex unique clause.
$atts['sortby'] = trim($m[1]);
$atts['sortdir'] = (isset($m[3]) ? $m[3] : 'ASC');
} else {
$atts['sortby'] = 'Posted';
$atts['sortdir'] = 'DESC';
}
// Attributes with special treatment.
switch ($atts['sortby']) {
case 'Posted':
$threshold = "FROM_UNIXTIME(".doSlash($thisarticle['posted']).")";
break;
case 'Expires':
$threshold = "FROM_UNIXTIME(".doSlash($thisarticle['expires']).")";
break;
case 'LastMod':
$threshold = "FROM_UNIXTIME(".doSlash($thisarticle['modified']).")";
break;
default:
// Retrieve current threshold value per sort column from $thisarticle.
$threshold_type = 'raw';
$acm = array_flip(article_column_map());
if (isset($acm[$atts['sortby']])) {
$key = $acm[$atts['sortby']];
$threshold = $thisarticle[$key];
} else {
$threshold = safe_field($atts['sortby'], 'textpattern', 'ID='.$atts['thisid']);
}
}
}
ksort($atts);
$out['>'] = getNeighbour($threshold, $s, '>', $atts, $threshold_type);
$out['<'] = getNeighbour($threshold, $s, '<', $atts, $threshold_type);
return $out;
}
/**
* Gets the site last modification date.
*
* @return string
* @package Pref
*/
function lastMod()
{
$last = safe_field("UNIX_TIMESTAMP(val)", 'txp_prefs', "name = 'lastmod'");
return gmdate("D, d M Y H:i:s \G\M\T", $last);
}
/**
* Parse a string and replace any Textpattern tags with their actual value.
*
* @param string $thing The raw string
* @param null|bool $condition Process true/false part
* @return string The parsed string
* @package TagParser
*/
function parse($thing, $condition = true, $in_tag = true)
{
global $pretext, $production_status, $trace, $txp_parsed, $txp_else, $txp_atts, $txp_tag, $txp_current_tag;
static $short_tags = null;
if (!isset($short_tags)) {
$short_tags = get_pref('enable_short_tags', false);
}
if (!isset($thing) || !$short_tags && false === strpos($thing, '<txp:') ||
$short_tags && !preg_match('@<(?:'.TXP_PATTERN.'):@', $thing)) {
$hash = null;
} else {
$hash = txp_hash($thing);
}
if (!empty($txp_atts['not']) && $in_tag) {
$condition = empty($condition);
$not = true;
}
$old_tag = $txp_tag;
$txp_tag = !empty($condition);
$log = $production_status === 'debug';
if ($log) {
$trace->log('['.($condition ? 'true' : 'false').']');
}
if (!isset($hash) || !isset($txp_parsed[$hash]) && !txp_tokenize($thing, $hash)) {
$thing = $condition ? ($thing === null ? '1' : $thing) : '';
if (isset($txp_atts['$query']) && $in_tag) {
$thing = txp_eval(array('query' => $txp_atts['$query'], 'test' => $thing));
}
return $thing;
}
$tag = $txp_parsed[$hash];
list($first, $last) = $txp_else[$hash];
if ($condition) {
$last = $first - 2;
$first = 1;
} elseif ($first <= $last) {
$first += 2;
} else {
return '';
}
$this_tag = $txp_current_tag;
$isempty = false;
$dotest = !empty($txp_atts['evaluate']) && $in_tag;
$evaluate = !$dotest ? null :
($txp_atts['evaluate'] === true ? true : do_list($txp_atts['evaluate']));
if (isset($txp_else[$hash]['test']) && (!$evaluate || $evaluate === true)) {
$evaluate = $txp_else[$hash]['test'];
}
if ($evaluate) {
$test = is_array($evaluate) ? array_fill_keys($evaluate, array()) : false;
$isempty = $last >= $first;
}
if (empty($test)) {
for ($out = $tag[$first - 1]; $first <= $last; $first++) {
$txp_tag = $tag[$first];
$txp_current_tag = $txp_tag[0].$txp_tag[3].$txp_tag[4];
$nextag = processTags($txp_tag[1], $txp_tag[2], $txp_tag[3], $log);
$out .= $nextag.$tag[++$first];
$isempty = $isempty && trim($nextag) === '';
}
} else {
if ($pre = !isset($test[0])) {
$test[0] = array();
}
$out = array($first-1 => $tag[$first-1]);
for ($n = $first; $n <= $last; $n++) {
$txp_tag = $tag[$n];
$out[$n] = null;
if (isset($test[($n+1)/2])) {
$test[($n+1)/2][] = $n;
$isempty = true;
} elseif (isset($test[$txp_tag[1]])) {
$test[$txp_tag[1]][] = $n;
$isempty = true;
} else {
$test[0][] = $n;
}
$out[$n] = $tag[++$n];
}
foreach ($test as $k => $t) {
if (!$k && $pre && $dotest && $isempty == empty($not)) {
$out = false;
break;
}
foreach ($t as $n) {
$txp_tag = $tag[$n];
$txp_current_tag = $txp_tag[0].$txp_tag[3].$txp_tag[4];
$nextag = processTags($txp_tag[1], $txp_tag[2], $txp_tag[3], $log);
$out[$n] = $nextag;
$k and ($isempty = $isempty && trim($nextag) === '');
}
}
if (is_array($out)) {
$out = implode('', $out);
}
}
if ($dotest && $isempty == empty($not)) {
$out = false;
} elseif (isset($txp_atts['$query']) && $in_tag) {
$out = txp_eval(array('query' => $txp_atts['$query'], 'test' => $out));
}
$out !== false or $condition = false;
$txp_tag = $old_tag || !empty($condition);
$txp_current_tag = $this_tag;
return $out;
}
/**
* Parse a tag for attributes and hand over to the tag handler function.
*
* @param string $tag The tag name
* @param string $atts The attribute string
* @param string|null $thing The tag's content in case of container tags
* @param bool $log Trace log
* @return string Parsed tag result
* @package TagParser
*/
function processTags($tag, $atts = '', $thing = null, $log = false)
{
global $pretext, $txp_atts, $txp_tag, $trace;
static $registry = null, $globals;
if (empty($tag)) {
return;
}
if ($registry === null) {
$registry = Txp::get('\Textpattern\Tag\Registry');
$globals = array_filter(
$registry->getRegistered(true),
function ($v) {
return !is_bool($v);
}
);
}
$old_atts = $txp_atts;
if ($log) {
$tag_stop = $txp_tag[4];
$trace->start($txp_tag[0]);
}
if ($atts) {
$split = splat($atts);
if (isset($txp_atts['evaluate']) && strpos($txp_atts['evaluate'], '<+>') !== false) {
$txp_atts['$query'] = $txp_atts['evaluate'];
unset($txp_atts['evaluate']);
}
} else {
$txp_atts = null;
$split = array();
}
$txp_tag = null;
$out = $registry->process($tag, $split, $thing);
if ($out === false) {
trigger_error($tag.' '.gTxt('unknown_tag'), E_USER_WARNING);
$out = '';
} else {
if ($txp_tag === null && !empty($txp_atts['not'])) {
$out = $out ? '' : '1';
} elseif (isset($txp_atts['$query']) && $txp_tag !== false) {
$out = txp_eval(array('query' => $txp_atts['$query'], 'test' => $out));
}
unset($txp_atts['not'], $txp_atts['evaluate'], $txp_atts['$query']);
if ($txp_atts && $txp_tag !== false) {
$pretext['_txp_atts'] = true;
foreach ($txp_atts as $attr => &$val) {
if (isset($val) && isset($globals[$attr])) {
$out = $registry->processAttr($attr, $txp_atts, $out);
}
}
$pretext['_txp_atts'] = false;
}
}
$txp_atts = $old_atts;
if ($log) {
$trace->stop($tag_stop);
}
return $out;
}
/**
* Checks a named item's existence in a database table.
*
* The given database table is prefixed with 'txp_'. As such this function can
* only be used with core database tables.
*
* @param string $table The database table name
* @param string $val The name to look for
* @param bool $debug Dump the query
* @return bool|string The item's name, or FALSE when it doesn't exist
* @package Filter
* @example
* if ($r = ckEx('section', 'about'))
* {
* echo "Section '{$r}' exists.";
* }
*/
function ckEx($table, $val, $debug = false)
{
$table === 'textpattern' or $table = 'txp_'.$table;
if (is_array($val)) {
$fields = implode(',', array_keys($val));
$where = join_qs(quote_list(array_filter($val)), ' AND ');
return safe_row($fields, $table, $where." LIMIT 1", $debug);
} else {
$fields = 'name';
$where = "name = '".doSlash($val)."'";
return safe_field($fields, $table, $where." LIMIT 1", $debug);
}
}
/**
* Checks if the given category exists.
*
* @param string $type The category type, either 'article', 'file', 'link', 'image'
* @param string $val The category name to look for
* @param bool $debug Dump the query
* @return bool|array The category's data, or FALSE when it doesn't exist
* @package Filter
* @see ckEx()
* @example
* if ($r = ckCat('article', 'development'))
* {
* echo "Category {$r['name']} exists.";
* }
*/
function ckCat($type, $val, $debug = false)
{
$res = safe_row("name, title, description, type", 'txp_category', "name = '".doSlash($val)."' AND type = '".doSlash($type)."' LIMIT 1", $debug);
return ($res && $res['name'] == $val) ? $res : false;
}
/**
* Lookup an article by ID.
*
* This function takes an article's ID, and checks if it's been published. If it
* has, returns the section and the ID as an array. FALSE otherwise.
*
* @param int $val The article ID
* @param bool $debug Dump the query
* @return array|bool Array of ID and section on success, FALSE otherwise
* @package Filter
* @example
* if ($r = ckExID(36))
* {
* echo "Article #{$r['id']} is published, and belongs to the section {$r['section']}.";
* }
*/
function ckExID($val, $debug = false)
{
return safe_row(
"*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod",
'textpattern',
"ID = ".intval($val)." AND Status >= 4 LIMIT 1", $debug
);
}
/**
* Lookup an article by URL title.
*
* This function takes an article's URL title, and checks if the article has
* been published. If it has, returns the section and the ID as an array.
* FALSE otherwise.
*
* @param string $val The URL title
* @param bool $debug Dump the query
* @return array|bool Array of ID and section on success, FALSE otherwise
* @package Filter
* @example
* if ($r = lookupByTitle('my-article-title'))
* {
* echo "Article #{$r['id']} is published, and belongs to the section {$r['section']}.";
* }
*/
function lookupByTitle($val, $debug = false)
{
$res = safe_row(
"*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod",
'textpattern',
"url_title = '".doSlash($val)."' LIMIT 1", $debug
);
return ($res && $res['url_title'] == $val) ? $res : false;
}
/**
* Lookup a published article by URL title and section.
*
* This function takes an article's URL title, and checks if the article has
* been published. If it has, returns the section and the ID as an array.
* FALSE otherwise.
*
* @param string $val The URL title
* @param string $section The section name
* @param bool $debug Dump the query
* @return array|bool Array of ID and section on success, FALSE otherwise
* @package Filter
* @example
* if ($r = lookupByTitleSection('my-article-title', 'my-section'))
* {
* echo "Article #{$r['id']} is published, and belongs to the section {$r['section']}.";
* }
*/
function lookupByTitleSection($val, $section, $debug = false)
{
$res = safe_row(
"*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod",
'textpattern',
"url_title = '".doSlash($val)."' AND Section = '".doSlash($section)."' LIMIT 1", $debug
);
return ($res && $res['url_title'] == $val && $res['Section'] == $section) ? $res : false;
}
/**
* Lookup live article by ID and section.
*
* @param int $id Article ID
* @param string $section Section name
* @param bool $debug
* @return array|bool
* @package Filter
*/
function lookupByIDSection($id, $section, $debug = false)
{
$res = safe_row(
"*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod",
'textpattern',
"ID = ".intval($id)." AND Section = '".doSlash($section)."' LIMIT 1", $debug
);
return ($res && $res['Section'] == $section) ? $res : false;
}
/**
* Lookup live article by ID.
*
* @param int $id Article ID
* @param bool $debug
* @return array|bool
* @package Filter
*/
function lookupByID($id, $debug = false)
{
return safe_row(
"*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod",
'textpattern',
"ID = ".intval($id)." LIMIT 1", $debug
);
}
/**
* Lookup live article by date and URL title.
*
* @param string $when date wildcard
* @param string $title URL title
* @param bool $debug
* @return array|bool
* @package Filter
*/
function lookupByDateTitle($when, $title, $debug = false)
{
if ($when) {
$offset = date('P', strtotime($when));
$dateClause = ($offset ? "CONVERT_TZ(posted, @@session.time_zone, '$offset')" : 'posted')." LIKE '".doSlash($when)."%'";
} else {
$dateClause = '1';
}
$res = safe_row(
"*, UNIX_TIMESTAMP(Posted) AS uPosted, UNIX_TIMESTAMP(Expires) AS uExpires, UNIX_TIMESTAMP(LastMod) AS uLastMod",
'textpattern',
"url_title = '".doSlash($title)."' AND $dateClause LIMIT 1"
);
return ($res && $res['url_title'] == $title) ? $res : false;
}
/**
* Save and retrieve the individual article's attributes plus article list
* attributes for next/prev tags.
*
* @param array $atts
* @param bool $iscustom
* @return array/string
* @since 4.5.0
* @package TagParser
*/
function filterAtts($atts = null, $iscustom = null)
{
global $pretext, $trace, $thisarticle;
static $date_fields = array('posted' => 'Posted', 'modified' => 'LastMod', 'expires' => 'Expires');
static $aggregate = array(
'avg' => 'AVG(?)',
'max' => 'MAX(?)',
'min' => 'MIN(?)',
'sum' => 'SUM(?)',
'list' => "GROUP_CONCAT(? SEPARATOR ',')",
'concat' => "GROUP_CONCAT(? SEPARATOR ',')"
), $windowed = array(
'count' => 'COUNT(*)',
'dense' => 'DENSE_RANK()',
'rank' => 'RANK()',
'row' => 'ROW_NUMBER()'
);
static $out = array();
if ($atts === false) {
return $out = array();
} elseif (!is_array($atts)) {
// TODO: deal w/ nested txp:article[_custom] tags. See https://github.com/textpattern/textpattern/issues/1009
$trace->log('[filterAtts ignored]');
return $out;
} elseif (isset($atts['?'])) {
return $out = $atts;
}
$excluded = isset($atts['exclude']) ? $atts['exclude'] : '';
if ($excluded && $excluded !== true) {
$excluded = array_map('strtolower', do_list_unique($excluded));
$excludid = array_filter($excluded, 'is_numeric');
empty($excludid) or $excluded = array_diff($excluded, $excludid);
} else {
$excluded or $excluded = array();
$excludid = array();
}
$excluded === true or $excluded = array_fill_keys($excluded, true);
$customFields = getCustomFields() + array('url_title' => 'url_title');
$postWhere = $customPairs = $customlAtts = array();
foreach ($customFields as $num => $field) {
$customlAtts[$field] = null;
if (isset($atts['custom_'.$num])) {
$customPairs[$field] = $atts['custom_'.$num];
$customlAtts['custom_'.$num] = null;
} elseif (isset($excluded[$field])) {
$customPairs[$field] = true;
}
}
$extralAtts = array(
'form' => 'default',
'allowoverride' => !$iscustom,
'limit' => 10,
'offset' => 0,
'pageby' => null,
'pgonly' => 0,
'wraptag' => '',
'break' => '',
'label' => '',
'labeltag' => '',
'class' => '',
'searchall' => !$iscustom && !empty($pretext['q']),
);
$sortAtts = array(
'fields' => null,
'sort' => '',
'keywords' => '',
'time' => null,
'status' => empty($atts['id']) ? STATUS_LIVE : true,
'frontpage' => !$iscustom,
'match' => 'Category',
'depth' => 0,
'id' => '',
'excerpted' => '',
'exclude' => ''
);
if ($iscustom) {
$sortAtts += array(
'category' => isset($excluded['category']) ? true : '',
'section' => isset($excluded['section']) ? true : '',
'author' => isset($excluded['author']) ? true : '',
'month' => isset($excluded['month']) ? true : '',
'expired' => isset($excluded['expired']) ? true : get_pref('publish_expired_articles'),
);
} else {
$extralAtts += array(
'listform' => '',
'searchform' => '',
'searchsticky' => 0,
);
}
$coreColumns = array(
'posted' => 'UNIX_TIMESTAMP(Posted) AS uPosted',
'expires' => 'UNIX_TIMESTAMP(Expires) AS uExpires',
'modified' => 'UNIX_TIMESTAMP(LastMod) AS uLastMod',
) + article_column_map();
foreach ($windowed + $coreColumns as $field => $val) {
if (isset($atts['$'.$field])) {
$postWhere['$'.$field] = $atts['$'.$field];
unset($atts['$'.$field]);
}
}
// Getting attributes.
$theAtts = lAtts($sortAtts + $extralAtts + $customlAtts, $atts);
// For the txp:article tag, some attributes are taken from globals;
// override them, then stash all filter attributes.
if (!$iscustom) {
$theAtts['category'] = !empty($pretext['c']) ? $pretext['c'] : '';
$theAtts['section'] = (!empty($pretext['s']) && $pretext['s'] != 'default') ? $pretext['s'] : '';
$theAtts['author'] = (!empty($pretext['author']) ? $pretext['author'] : '');
$theAtts['month'] = (!empty($pretext['month']) ? $pretext['month'] : '');
$theAtts['expired'] = get_pref('publish_expired_articles');
$theAtts['frontpage'] = ($theAtts['frontpage'] && !$theAtts['section']);
$q = trim($pretext['q']);
} else {
$q = '';
}
extract($theAtts, EXTR_SKIP);
// Treat sticky articles differently wrt search filtering, etc.
$issticky = in_array(strtolower($status), array('sticky', STATUS_STICKY));
if ($status === true) {
$status = array(STATUS_LIVE, STATUS_STICKY);
} else {
$status = array($issticky ? STATUS_STICKY : STATUS_LIVE);
}
// Categories
$operator = 'AND';
$match = parse_qs($match);
if (isset($match['category'])) {
isset($match['category1']) or $match['category1'] = $match['category'];
isset($match['category2']) or $match['category2'] = $match['category'];
$operator = 'OR';
}
$categories = $category === true ? false : do_list_unique($category);
$catquery = array();
if ($categories) {
!$depth or $categories = getTree($categories, 'article', '1', 'txp_category', $depth) or $categories = array('/');
$categories = quote_list($categories, ',');
}
for ($i = 1; $i <= 2; $i++) {
$not = isset($excluded["category{$i}"]) ? '!' : '';
if (isset($match['category'.$i])) {
if ($match['category'.$i] === false) {
if ($categories) {
$catquery[] = "$not(Category{$i} IN ($categories))";
} elseif ($category === true || $not) {
$catquery[] = "$not(Category{$i} != '')";
}
} elseif (($val = gps($match['category'.$i], false)) !== false) {
$categories = $depth ? getTree(do_list($val), 'article', '1', 'txp_category', $depth) : do_list($val);
$catquery[] = $categories ? "$not(Category{$i} IN (".quote_list($categories, ',')."))" : "$not 0";
}
} elseif ($not) {
$catquery[] = "(Category{$i} = '')";
}
}
$not = $iscustom && ($excluded === true || isset($excluded['category'])) ? '!' : '';
$catquery = join(" $operator ", $catquery);
$category = !$catquery ? '' : " AND $not($catquery)";
// ID
$not = $excluded === true || isset($excluded['id']) ? 'NOT' : '';
$ids = $id ? ($id === true ? article_id() : join(',', array_map('intval', do_list_unique($id, array(',', '-'))))) : false;
$id = ($ids? " AND ID $not IN ($ids)" : '').(!$excludid ? '' : " AND ID NOT IN (".join(',', $excludid).")");
$getid = $ids && !$not;
// Section
// searchall=0 can be used to show search results for the current
// section only.
if ($q && $searchall && !$issticky) {
$section = '';
}
$not = $iscustom && ($excluded === true || isset($excluded['section'])) ? 'NOT' : '';
$section !== true or $section = processTags('section');
$section = (!$section ? '' : " AND Section $not IN ('".join("','", doSlash(do_list_unique($section)))."')").
($getid || $section && !$not || $searchall? '' : filterFrontPage('Section', 'page'));
// Author
$not = $iscustom && ($excluded === true || isset($excluded['author'])) ? 'NOT' : '';
$author !== true or $author = processTags('author', 'escape="" title=""');
$author = (!$author) ? '' : " AND AuthorID $not IN ('".join("','", doSlash(do_list_unique($author)))."')";
$frontpage = ($frontpage && (!$q || $issticky)) ? filterFrontPage('Section', 'on_frontpage', (int)$frontpage < 0) : '';
$excerpted = (!$excerpted) ? '' : " AND Excerpt !=''";