<?php /** * Changes since 0.4: (by Edmond Hui aka admun) * - TechnoratiTags template var mode * - fix empty tags row in table * Changes since 0.3: (by Emdond Hui aka admun) * - fixed tags between body and more bug * Changes since 0.2.5: * - + -> switch * - %d for the taglook * Changes since 0.2: * - Only one additional query per page * Changes since 0.1: * - There are now some new admin fields for defining the look&feel of * the tags * - Don't display tags if count($args) == 1 && $args[0]=='' * todo: * - add option for none string */ if (!function_exists('sql_table')){ function sql_table($name){ return 'nucleus_'.$name; } } class NP_TechnoratiTags extends NucleusPlugin { function init(){ $this->tablename = sql_table('plug_technoratitags'); $this->cachedTagsPerPost = array(); $this->queried = FALSE; } function getName() { return 'TechnoratiTags'; } function getAuthor() { return 'Horst Gutmann, mod by Edmond Hui'; } function getURL() { return 'http://weblog.zerokspot.com/posts/192/'; } function getVersion() { return '0.5'; } function getDescription() { return 'Adds a way to specify Technorati tags for each post'; } function getEventList(){ return array( 'AddItemFormExtras', 'EditItemFormExtras', 'PostDeleteItem', 'PostAddItem', 'PreUpdateItem', 'PreItem', 'PostItem' ); } function supportsFeature($f){ switch($f){ case 'SqlTablePrefix': return 1; default: return 0; } } /** * Creates the technoratitags table if it doesn't exist yet */ function install(){ sql_query('CREATE TABLE IF NOT EXISTS '.$this->tablename.' (itemid INT(9) NOT NULL, tags VARCHAR(255) , INDEX(itemid))'); $this->createOption('ListLook','Look of the list:','textarea','<br/><br/>tags: %l'); $this->createOption('TagSeparator','Separator of the tags when being displayed:','textarea',', '); $this->createOption('TagLook','Look of the tags:','textarea','<a href="http://technorati.com/tag/%t" rel="tag">%d</a>'); $this->createOption('NoneText','Text string for no tag','text','none'); $this->createOption('Cleanup','TechnoratiTags table should be removed when uninstalling this plugin','yesno','no'); $this->createOption('PlusSwitch','Display "+" as " " (space)?','yesno','no'); $this->createOption('AppendTag','Insert tags at the end of post?','yesno','yes'); } /** * Asks the user if the technoratitags table should be deleted * and deletes it if yes */ function unInstall(){ if ($this->getOption('Cleanup') == 'yes'){ sql_query('DROP TABLE '.$this->tablename); } } /** * Returns array of tables to be additionally included in the * backup process */ function getTableList(){ return $this->tablename; } /** * Returns the tag-string from the database for the given * $postID * @return array of tags */ function getTags($itemID){ if (!$this->queried){ $result = mysql_query('SELECT * FROM '.$this->tablename); while($row = mysql_fetch_object($result)){ $row->tags = explode(' ',$row->tags); $this->cachedTagsPerPost[$row->itemid]=$row->tags; } mysql_free_result($result); $this->queried = TRUE; } if (!array_key_exists($itemID,$this->cachedTagsPerPost)){ /* Hm.... this item has no entry in the tags table... * will be created the next time someone edits the * item. */ return array(); } else { return $this->cachedTagsPerPost[$itemID]; } } function event_AddItemFormExtras($data){ $output = <<<EOD <h3>Technorati Tags</h3> <p> <label for="plugin_technoratitags_field">Tags:</label> <input type="text" name="plugin_technoratitags_field" id="plugin_technoratitags_field"/> </p> EOD; echo $output; } function event_EditItemFormExtras($data){ $output = <<<EOD <h3>Technorati Tags</h3> <p> <label for="plugin_technoratitags_field">Tags:</label> <input type="text" name="plugin_technoratitags_field" id="plugin_technoratitags_field" value="{TAGS}"/> </p> EOD; $tags = $this->getTags($data['itemid']); $tags = implode(" ",$tags); $output = str_replace('{TAGS}',$tags,$output); echo $output; } /** * Create a new row for tags of this post */ function event_PostAddItem($data){ $itemid = $data['itemid']; $tags = requestVar('plugin_technoratitags_field'); /* Let's do some cleanup, just in case :-) */ $tags = mysql_escape_string(htmlspecialchars(urldecode($tags))); mysql_query("INSERT INTO ".$this->tablename." (itemid,tags) VALUES (".$itemid.",'".$tags."')"); } /** * There seems to be no PostUpdateItem event so here we go */ function event_PreUpdateItem($data){ $mode = 'insert'; $itemid = $data['itemid']; $tags = requestVar('plugin_technoratitags_field'); /* First check if there is already a row for this post */ $result = mysql_query("SELECT * FROM ".$this->tablename." WHERE itemid=".$data['itemid']); if (mysql_num_rows($result) > 0){ $mode = 'update'; } mysql_free_result($result); if ($mode == 'insert'){ $query = "INSERT INTO ".$this->tablename." (itemid,tags) VALUES (".$itemid.",'".$tags."')"; } // insert else { $query = "UPDATE ".$this->tablename." SET tags = '".$tags."' WHERE itemid = ".$itemid; } // update mysql_query($query); } /** * Remove the technoratitags rows for the specified post */ function event_PostDeleteItem($data){ $itemid = $data['itemid']; mysql_query('DELETE FROM '.$this->tablename.' WHERE itemid = '.$itemid); } /** * Insert the tags into the item-body so that they are * also displayed in the short view without having to alter * any templates ;-) Lazy one inside ^_^ */ function event_PreItem($data){ if ($this->getOption('AppendTag') == 'no'){ return; } $tags = $this->getTags($data['item']->itemid); if (count($tags) > 0){ if (count($tags) == 1 && $tags[0]== ''){ $this->originalPost = NULL; return; } if ($data['item']->more == "") $body = &$data['item']->body; else $body = &$data['item']->more; $content = $this->getOption('ListLook'); $itemlook = $this->getOption('TagLook'); $separator = $this->getOption('TagSeparator'); $list = ""; for($i = 0 ; $i<count($tags) ; $i++){ $t = $tags[$i]; if ($t == '') continue; if ($this->getOption('PlusSwitch') == 'yes'){ $displayed_tag = str_replace('+',' ',$t); } else { $displayed_tag = $t; } $tag=str_replace('%t',$t,$itemlook); $tag=str_replace('%d',$displayed_tag,$tag); $list.=$tag; /* If this isn't the last tag, append the seperator */ if ($i < count($tags)-1){ $list.=$separator; } } $content = str_replace('%l',$list,$content); $body = $body.$content; } else { $this->originalPost = NULL; } } /** * Restore orignal post content just to be on the safe side */ function event_PostItem($data){ if ($this->originalPost != NULL){ } } /** * <%TechnoratiTags%> template function */ function doTemplateVar(&$item, $what = ''){ $tags = $this->getTags($item->itemid); if ($what == "rss"){ if (count($tags) > 0){ if (count($tags) == 1 && $tags[0]== ''){ return; } for($i = 0 ; $i<count($tags) ; $i++){ $t = $tags[$i]; if ($t == '') continue; echo "<category>" . $t . "</category>"; } } return; } if (count($tags) > 0){ if (count($tags) == 1 && $tags[0]== ''){ echo "none"; $this->originalPost = NULL; return; } if ($data['item']->more == "") $body = &$data['item']->body; else $body = &$data['item']->more; $content = $this->getOption('ListLook'); $itemlook = $this->getOption('TagLook'); $separator = $this->getOption('TagSeparator'); $list = ""; for($i = 0 ; $i<count($tags) ; $i++){ $t = $tags[$i]; if ($t == '') continue; if ($this->getOption('PlusSwitch') == 'yes'){ $displayed_tag = str_replace('+',' ',$t); } else { $displayed_tag = $t; } $tag=str_replace('%t',$t,$itemlook); $tag=str_replace('%d',$displayed_tag,$tag); $list.=$tag; /* If this isn't the last tag, append the seperator */ if ($i < count($tags)-1){ $list.=$separator; } } $content = str_replace('%l',$list,$content); echo $content; } else { echo($this->getOption('NoneText')) ; } } } ?>