Skip to content

Commit 2ce4ff5

Browse files
committed
More correct 4.6 and SQLisms
1 parent 64375f2 commit 2ce4ff5

File tree

1 file changed

+90
-82
lines changed

1 file changed

+90
-82
lines changed

smd_tags.php

Lines changed: 90 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,18 @@
362362
'#smd_tag_report_pane { display:none; position:absolute; left:200px; max-width:500px; border:3px ridge #999; opacity:.92; filter:alpha(opacity:92); padding:15px 20px; background-color:#e2dfce; color:#80551e; }
363363
#smd_tag_report_pane .publish { float:right; }',
364364
);
365+
} elseif (txpinterface === 'public') {
366+
if (class_exists('\Textpattern\Tag\Registry')) {
367+
Txp::get('\Textpattern\Tag\Registry')
368+
->register('smd_tag_list')
369+
->register('smd_tag_name')
370+
->register('smd_tag_count')
371+
->register('smd_tag_info')
372+
->register('smd_if_tag')
373+
->register('smd_if_tag_list')
374+
->register('smd_related_tags');
375+
}
376+
365377
}
366378

367379
if (!defined('SMD_TAG')) {
@@ -904,77 +916,72 @@ function smd_tags_table_exist($all='0') {
904916
function smd_tags_table_install($showpane='1') {
905917
global $DB;
906918

907-
$version = mysql_get_server_info();
908919
$debug = gps('debug');
909920
$GLOBALS['txp_err_count'] = 0;
910-
if ($version < "4.1.2") {
911-
$GLOBALS['txp_err_count']++;
912-
trigger_error("smd_tags requires MySQL v4.1.2 or greater.");
913-
} else {
914-
$ret = '';
915-
$sql = array();
916-
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_TAG."` (
917-
`id` int(6) NOT NULL auto_increment,
918-
`name` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
919-
`type` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
920-
`parent` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
921-
`lft` int(6) NOT NULL default '0',
922-
`rgt` int(6) NOT NULL default '0',
923-
`title` varchar(255) NOT NULL default '' COLLATE utf8_general_ci,
924-
`description` text NULL COLLATE utf8_general_ci,
925-
`desc_html` text NULL COLLATE utf8_general_ci,
926-
PRIMARY KEY (`id`)
927-
) ENGINE=MyISAM PACK_KEYS=1 AUTO_INCREMENT=5 CHARACTER SET=utf8";
928-
929-
if (!smd_tags_table_exist()) {
930-
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (1, 'root', 'article', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
931-
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (2, 'root', 'link', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
932-
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (3, 'root', 'image', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
933-
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (4, 'root', 'file', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
934-
}
935921

936-
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_TAGC."` (
937-
`tag_id` int(6) NOT NULL default '0',
938-
`cat_id` int(6) NOT NULL default '0',
939-
PRIMARY KEY (`tag_id`,`cat_id`)
940-
) ENGINE=MyISAM";
922+
$ret = '';
923+
$sql = array();
924+
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_TAG."` (
925+
`id` int(6) NOT NULL auto_increment,
926+
`name` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
927+
`type` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
928+
`parent` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
929+
`lft` int(6) NOT NULL default '0',
930+
`rgt` int(6) NOT NULL default '0',
931+
`title` varchar(255) NOT NULL default '' COLLATE utf8_general_ci,
932+
`description` text NULL COLLATE utf8_general_ci,
933+
`desc_html` text NULL COLLATE utf8_general_ci,
934+
PRIMARY KEY (`id`)
935+
) ENGINE=MyISAM PACK_KEYS=1 AUTO_INCREMENT=5 CHARACTER SET=utf8";
941936

942-
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_TAGU."` (
943-
`item_id` int(11) NOT NULL default '0',
944-
`tag_id` int(6) NOT NULL default '0',
945-
`type` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
946-
PRIMARY KEY (`item_id`,`tag_id`)
947-
) ENGINE=MyISAM CHARACTER SET=utf8";
937+
if (!smd_tags_table_exist()) {
938+
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (1, 'root', 'article', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
939+
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (2, 'root', 'link', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
940+
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (3, 'root', 'image', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
941+
$sql[] = "INSERT INTO `".PFX.SMD_TAG."` VALUES (4, 'root', 'file', '', 1, 2, 'root', 'DO NOT DELETE', NULL)";
942+
}
948943

949-
if($debug) {
950-
dmp($sql);
951-
}
952-
foreach ($sql as $qry) {
953-
$ret = safe_query($qry);
954-
if ($ret===false) {
955-
$GLOBALS['txp_err_count']++;
956-
echo "<b>" . $GLOBALS['txp_err_count'] . ".</b> " . mysql_error() . "<br />\n";
957-
echo "<!--\n $qry \n-->\n";
958-
}
944+
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_TAGC."` (
945+
`tag_id` int(6) NOT NULL default '0',
946+
`cat_id` int(6) NOT NULL default '0',
947+
PRIMARY KEY (`tag_id`,`cat_id`)
948+
) ENGINE=MyISAM";
949+
950+
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_TAGU."` (
951+
`item_id` int(11) NOT NULL default '0',
952+
`tag_id` int(6) NOT NULL default '0',
953+
`type` varchar(64) NOT NULL default '' COLLATE utf8_general_ci,
954+
PRIMARY KEY (`item_id`,`tag_id`)
955+
) ENGINE=MyISAM CHARACTER SET=utf8";
956+
957+
if($debug) {
958+
dmp($sql);
959+
}
960+
foreach ($sql as $qry) {
961+
$ret = safe_query($qry);
962+
if ($ret===false) {
963+
$GLOBALS['txp_err_count']++;
964+
echo "<b>" . $GLOBALS['txp_err_count'] . ".</b> " . mysqli_error($DB->link) . "<br />\n";
965+
echo "<!--\n $qry \n-->\n";
959966
}
967+
}
960968

961-
// Upgrade table collation if necessary
962-
$ret = getRows("SHOW TABLE STATUS WHERE name IN ('".PFX.SMD_TAG."', '".PFX.SMD_TAGU."')");
963-
if ($ret[0]['Collation'] != 'utf8_general_ci') {
964-
$ret = safe_alter(SMD_TAG, 'CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci', $debug);
965-
}
966-
if ($ret[1]['Collation'] != 'utf8_general_ci') {
967-
$ret = safe_alter(SMD_TAGU, 'CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci', $debug);
968-
}
969+
// Upgrade table collation if necessary
970+
$ret = getRows("SHOW TABLE STATUS WHERE name IN ('".PFX.SMD_TAG."', '".PFX.SMD_TAGU."')");
971+
if ($ret[0]['Collation'] != 'utf8_general_ci') {
972+
$ret = safe_alter(SMD_TAG, 'CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci', $debug);
973+
}
974+
if ($ret[1]['Collation'] != 'utf8_general_ci') {
975+
$ret = safe_alter(SMD_TAGU, 'CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci', $debug);
976+
}
969977

970-
// Add the description column on upgrade
971-
$flds = getThings('SHOW COLUMNS FROM `'.PFX.SMD_TAG.'`');
972-
if (!in_array('description',$flds)) {
973-
safe_alter(SMD_TAG, "ADD `description` TEXT NULL AFTER `title`", $debug);
974-
}
975-
if (!in_array('desc_html',$flds)) {
976-
safe_alter(SMD_TAG, "ADD `desc_html` TEXT NULL AFTER `description`", $debug);
977-
}
978+
// Add the description column on upgrade
979+
$flds = getThings('SHOW COLUMNS FROM `'.PFX.SMD_TAG.'`');
980+
if (!in_array('description',$flds)) {
981+
safe_alter(SMD_TAG, "ADD `description` TEXT NULL AFTER `title`", $debug);
982+
}
983+
if (!in_array('desc_html',$flds)) {
984+
safe_alter(SMD_TAG, "ADD `desc_html` TEXT NULL AFTER `description`", $debug);
978985
}
979986

980987
if ($GLOBALS['txp_err_count'] == 0) {
@@ -991,6 +998,8 @@ function smd_tags_table_install($showpane='1') {
991998
// ------------------------
992999
// Drop tag tables if in database
9931000
function smd_tags_table_remove() {
1001+
global $DB;
1002+
9941003
$ret = '';
9951004
$sql = array();
9961005
$GLOBALS['txp_err_count'] = 0;
@@ -1005,7 +1014,7 @@ function smd_tags_table_remove() {
10051014
$ret = safe_query($qry);
10061015
if ($ret===false) {
10071016
$GLOBALS['txp_err_count']++;
1008-
echo "<b>" . $GLOBALS['txp_err_count'] . ".</b> " . mysql_error() . "<br />\n";
1017+
echo "<b>" . $GLOBALS['txp_err_count'] . ".</b> " . mysqli_error($DB->link) . "<br />\n";
10091018
echo "<!--\n $qry \n-->\n";
10101019
}
10111020
}
@@ -3717,28 +3726,33 @@ function smd_related_tags($atts, $thing='') {
37173726
}
37183727
}
37193728

3729+
$cfs = getCustomfields();
3730+
$cfKeys = implode(',', array_map(
3731+
function($k) { return 'custom_' . $k; }, array_keys($cfs)
3732+
));
3733+
37203734
// Lookup table for making SQL queries
37213735
$sqlStubs = array(
37223736
"article" => array(
3723-
"select" => "*,unix_timestamp(Posted) as uPosted, unix_timestamp(LastMod) as uLastMod, unix_timestamp(Expires) as uExpires, COUNT(smt.name) as tag_sum",
3737+
"select" => "txp.ID, Posted, Expires, AuthorID, LastMod, LastModID, txp.Title, Title_html, Body, Body_html, Excerpt, Excerpt_html, Image, Category1, Category2, Annotate, AnnotateInvite, comments_count, Status, textile_body, textile_excerpt, Section, override_form, Keywords, txp.description, url_title" . ($cfKeys ? ','. $cfKeys : '') . ", uid, feed_time, position, unix_timestamp(Posted) as uPosted, unix_timestamp(LastMod) as uLastMod, unix_timestamp(Expires) as uExpires, COUNT(smt.name) as tag_sum",
37243738
"table" => "textpattern",
37253739
"gtags" => $thisarticle,
37263740
"gid" => "thisid",
37273741
),
37283742
"image" => array(
3729-
"select" => "*, COUNT(smt.name) as tag_sum",
3743+
"select" => "txp.id, txp.name, txp.category, txp.ext, txp.w, txp.h, txp.alt, txp.caption, txp.date, txp.author, txp.thumb_w, txp.thumb_h, txp.thumbnail, tu.item_id, tu.tag_id, smt.id AS smtid, smt.name AS smtname, smt.desc_html AS smtdescription, smt.type, smt.parent, smt.lft, smt.rgt, smt.title, COUNT(smt.name) as tag_sum",
37303744
"table" => "txp_image",
37313745
"gtags" => $thisimage,
37323746
"gid" => "id",
37333747
),
37343748
"file" => array(
3735-
"select" => "*, COUNT(smt.name) as tag_sum",
3749+
"select" => "txp.id, txp.filename, txp.title, txp.category, txp.permissions, txp.description, txp.downloads, txp.status, txp.modified, txp.created, txp.size, tu.item_id, tu.tag_id, smt.id AS smtid, smt.name AS smtname, smt.desc_html AS smtdescription, smt.type, smt.parent, smt.lft, smt.rgt, smt.title, COUNT(smt.name) as tag_sum",
37363750
"table" => "txp_file",
37373751
"gtags" => $thisfile,
37383752
"gid" => "id",
37393753
),
37403754
"link" => array(
3741-
"select" => "*, COUNT(smt.name) as tag_sum",
3755+
"select" => "txp.id, txp.date, txp.category, txp.url, txp.linkname, txp.linksort, txp.description, tu.item_id, tu.tag_id, smt.id AS smtid, smt.name AS smtname, smt.desc_html AS smtdescription, smt.type, smt.parent, smt.lft, smt.rgt, smt.title, COUNT(smt.name) as tag_sum",
37423756
"table" => "txp_link",
37433757
"gtags" => $thislink,
37443758
"gid" => "id",
@@ -3849,11 +3863,11 @@ function smd_related_tags($atts, $thing='') {
38493863

38503864
switch ($type) {
38513865
case "article":
3852-
$rs = getRows("SELECT SQL_CALC_FOUND_ROWS ".$sqlStubs[$type]["select"]." FROM ".safe_pfx($sqlStubs[$type]["table"])." AS txp
3866+
$rs = getRows("SELECT SQL_CALC_FOUND_ROWS " . $sqlStubs[$type]["select"] . " FROM " . safe_pfx($sqlStubs[$type]["table"]) . " AS txp
38533867
LEFT JOIN ".PFX.SMD_TAGU. " AS tu ON txp.id = tu.item_id
38543868
LEFT JOIN ".PFX.SMD_TAG. " AS smt ON tu.tag_id = smt.id
38553869
WHERE tu.type = 'article'" . $statSQL . $excludeClause . $sectionClause . $matchClause . ' GROUP BY txp.id' . $and_modeClause . $orderBy, $debug);
3856-
$contentResult = mysql_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
3870+
$contentResult = mysqli_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
38573871
$contentCount = $contentResult['found'];
38583872

38593873
if ($rs) {
@@ -3872,13 +3886,11 @@ function smd_related_tags($atts, $thing='') {
38723886
}
38733887
break;
38743888
case "image":
3875-
$rs = getRows("
3876-
SELECT SQL_CALC_FOUND_ROWS txp.id, txp.name, txp.category, txp.ext, txp.w, txp.h, txp.alt, txp.caption, txp.date, txp.author, txp.thumb_w, txp.thumb_h, txp.thumbnail, tu.item_id, tu.tag_id, smt.id AS smtid, smt.name AS smtname, smt.desc_html AS smtdescription, smt.type, smt.parent, smt.lft, smt.rgt, smt.title
3877-
FROM ".safe_pfx('txp_image')." AS txp
3889+
$rs = getRows("SELECT SQL_CALC_FOUND_ROWS " . $sqlStubs[$type]["select"] . " FROM " . safe_pfx($sqlStubs[$type]["table"]) . " AS txp
38783890
LEFT JOIN ".PFX.SMD_TAGU. " AS tu ON txp.id = tu.item_id
38793891
LEFT JOIN ".PFX.SMD_TAG. " AS smt ON tu.tag_id = smt.id
38803892
WHERE tu.type = 'image'" . $excludeClause . $matchClause . ' GROUP BY txp.id' . $and_modeClause . $orderBy, $debug);
3881-
$contentResult = mysql_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
3893+
$contentResult = mysqli_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
38823894
$contentCount = $contentResult['found'];
38833895

38843896
if ($rs) {
@@ -3896,13 +3908,11 @@ function smd_related_tags($atts, $thing='') {
38963908
}
38973909
break;
38983910
case "file":
3899-
$rs = getRows("
3900-
SELECT SQL_CALC_FOUND_ROWS txp.id, txp.filename, txp.title, txp.category, txp.permissions, txp.description, txp.downloads, txp.status, txp.modified, txp.created, txp.size, tu.item_id, tu.tag_id, smt.id AS smtid, smt.name AS smtname, smt.desc_html AS smtdescription, smt.type, smt.parent, smt.lft, smt.rgt, smt.title
3901-
FROM ".safe_pfx('txp_file')." AS txp
3911+
$rs = getRows("SELECT SQL_CALC_FOUND_ROWS " . $sqlStubs[$type]["select"] . " FROM " . safe_pfx($sqlStubs[$type]["table"]) . " AS txp
39023912
LEFT JOIN ".PFX.SMD_TAGU. " AS tu ON txp.id = tu.item_id
39033913
LEFT JOIN ".PFX.SMD_TAG. " AS smt ON tu.tag_id = smt.id
39043914
WHERE tu.type = 'file'" . $statSQL . $excludeClause . $matchClause . ' GROUP BY txp.id' . $and_modeClause . $orderBy, $debug);
3905-
$contentResult = mysql_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
3915+
$contentResult = mysqli_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
39063916
$contentCount = $contentResult['found'];
39073917

39083918
if ($rs) {
@@ -3920,13 +3930,11 @@ function smd_related_tags($atts, $thing='') {
39203930
}
39213931
break;
39223932
case "link":
3923-
$rs = getRows("
3924-
SELECT SQL_CALC_FOUND_ROWS txp.id, txp.date, txp.category, txp.url, txp.linkname, txp.linksort, txp.description, tu.item_id, tu.tag_id, smt.id AS smtid, smt.name AS smtname, smt.desc_html AS smtdescription, smt.type, smt.parent, smt.lft, smt.rgt, smt.title
3925-
FROM ".safe_pfx('txp_link')." AS txp
3933+
$rs = getRows("SELECT SQL_CALC_FOUND_ROWS " . $sqlStubs[$type]["select"] . " FROM " . safe_pfx($sqlStubs[$type]["table"]) . " AS txp
39263934
LEFT JOIN ".PFX.SMD_TAGU. " AS tu ON txp.id = tu.item_id
39273935
LEFT JOIN ".PFX.SMD_TAG. " AS smt ON tu.tag_id = smt.id
39283936
WHERE tu.type = 'link'" . $excludeClause . $matchClause . ' GROUP BY txp.id' . $and_modeClause . $orderBy, $debug);
3929-
$contentResult = mysql_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
3937+
$contentResult = mysqli_fetch_assoc(safe_query('SELECT FOUND_ROWS() AS found', $debug));
39303938
$contentCount = $contentResult['found'];
39313939

39323940
if ($rs) {

0 commit comments

Comments
 (0)