Addlink Action
NOT Included in any Wikka versionThis is the development page for the addlink action.
Installation
- Save the code below as action/addpage.php
- Give it the same file permissions as the other php files in that directory.
Code Walktrough
- If you have write and read access:
- Read the current links and build arrays $al_data and $al_category.
- if $_POST["url"] is set:
add link to array, rebuild the layout, save the page and refresh the current page.
- if $_POST["url"] is NOT set:
print the html form.
- If you have DON'T write and read access:
do nothing / print error.
Code
<?php
/**
* This action will allow you to fast add links in a structured way.
*
* @package Actions
* @subpackage Links
* @name AddLink
*
* @author {@link http://wikka.jsnx.com/OnegWR OnegWR} (v0.1)
* @version 0.1
* @copyright Copyright © 2005
* @license {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
* @since Wikka 1.1.6.0
*
* @input NONE ( $_POST["category"], $_POST["url"], $_POST["title"] = string )
* @output html form
*
* @todo {@link http://wikka.jsnx.com/AddLinkAction AddLinkAction} (list)
*/
$al_key_default = $al_key = "First"; // Default key
$al_category["First"] = "First"; // LookUpTable for 'safe'-key-names
$al_data = array(); // to store the links in
$al_select = ""; // $al_category in html-select format
$al_output = $al_last = ""; // rebuild the page for $this->SavePage()
if( $this->HasAccess("write") && $this->HasAccess("read") ){ // Admin only???
$al_page = $this->LoadPage($this->tag);
list($al_header,$al_links,$al_footer) = explode("----", $al_page["body"],3);
preg_match_all("/(==.*?==)|(\[\[.*?\]\])/", $al_links, $matches);
//print "\n<pre>\n"; print_r($matches); print "\n<pre>\n".count($matches[0])."\n";
foreach( $matches[0] as $al_n => $al_item ){
if( preg_match("/==(.*?)==/", $al_item, $match) ){
$al_key_tmp = preg_replace("/[^A-Za-z0-9]/","",$match[1]);
if($al_key_tmp <> ""){ // This would also ignore 'strange' titles
$al_key=$al_key_tmp;
$al_category[$al_key]=$match[1];
}
}else{
$al_data[$al_key][]=$al_item;
}
}
//print "\n<pre>\n"; print_r($al_data); print_r($al_category); print "\n<pre>\n";
if( isset($_POST["url"]) && $_POST["url"]<>"" ){ // add the link + save the page
if( $this->htmlspecialchars_ent($_POST["title"]) == "" ) {
$al_newlink = "[[".$this->cleanUrl($_POST["url"])."]]";
}else{
$al_newlink = "[[".$this->cleanUrl($_POST["url"])." ".$this->htmlspecialchars_ent($_POST["title"])."]]";
}
if( $this->htmlspecialchars_ent($_POST["category"]) == "" ){
$al_key = $al_key_default;
}else{
$al_key = $this->htmlspecialchars_ent($_POST["category"]);
}
$al_data[$al_key][] = $al_newlink; // Goal of this script: Add the link to the array
if(isset($al_data["First"])){ // Here the layout is re-build
$al_output .= "==First==\n~".implode(" ",$al_data["First"])."\n";
unset($al_data["First"]);
}
if(isset($al_data["Last"])) {
$al_last .= "==Last==\n~".implode(" ",$al_data["Last"])."\n";
unset($al_data["Last"]);
}
foreach($al_data as $al_k => $al_v){
$al_output .= "==".$al_category[$al_k]."==\n~".implode(" ",$al_data[$al_k])."\n";
}
$al_output .= $al_last;
$this->SavePage($this->tag, $al_header."----\n".$al_output."----".$al_footer, "Link added in category $al_key");
$this->Redirect($this->Href());
//print "ok";
//print "\n<pre>\n"; print_r($al_data); print "\n<pre>\n";
}else{ // print the form
print $this->FormOpen('',$this->tag);
foreach($al_category as $al_cat_safe => $al_cat_full){
$al_select.="<option value=\"".$al_cat_safe."\">".$al_cat_full."</option>";
}
?>
<table border="0" cellpadding="3" cellspacing="0">
<tr>
<td rowspan="2" valign="top"><select name="category" size="3"><?php print $al_select; ?></select></td>
<td>Url: </td>
<td valign="top"><input type="text" size="50" maxlength="255" name="url" value="<?php print $_GET["url"]; ?>"></td>
<td rowspan="2" valign="bottom"><input type=submit name=submit value="Add"></td>
</tr>
<tr>
<td>Title:</td>
<td valign="bottom"><input type="text" size="50" maxlength="255" name="title" value="<?php print $_GET["title"]; ?>"></td>
</tr>
</table>
<?php
print $this->FormClose();
}
}else{
//print "\n<!-- ERROR: You don't have the needed rights for accessing the action addlink -->\n";
}
?>
/**
* This action will allow you to fast add links in a structured way.
*
* @package Actions
* @subpackage Links
* @name AddLink
*
* @author {@link http://wikka.jsnx.com/OnegWR OnegWR} (v0.1)
* @version 0.1
* @copyright Copyright © 2005
* @license {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
* @since Wikka 1.1.6.0
*
* @input NONE ( $_POST["category"], $_POST["url"], $_POST["title"] = string )
* @output html form
*
* @todo {@link http://wikka.jsnx.com/AddLinkAction AddLinkAction} (list)
*/
$al_key_default = $al_key = "First"; // Default key
$al_category["First"] = "First"; // LookUpTable for 'safe'-key-names
$al_data = array(); // to store the links in
$al_select = ""; // $al_category in html-select format
$al_output = $al_last = ""; // rebuild the page for $this->SavePage()
if( $this->HasAccess("write") && $this->HasAccess("read") ){ // Admin only???
$al_page = $this->LoadPage($this->tag);
list($al_header,$al_links,$al_footer) = explode("----", $al_page["body"],3);
preg_match_all("/(==.*?==)|(\[\[.*?\]\])/", $al_links, $matches);
//print "\n<pre>\n"; print_r($matches); print "\n<pre>\n".count($matches[0])."\n";
foreach( $matches[0] as $al_n => $al_item ){
if( preg_match("/==(.*?)==/", $al_item, $match) ){
$al_key_tmp = preg_replace("/[^A-Za-z0-9]/","",$match[1]);
if($al_key_tmp <> ""){ // This would also ignore 'strange' titles
$al_key=$al_key_tmp;
$al_category[$al_key]=$match[1];
}
}else{
$al_data[$al_key][]=$al_item;
}
}
//print "\n<pre>\n"; print_r($al_data); print_r($al_category); print "\n<pre>\n";
if( isset($_POST["url"]) && $_POST["url"]<>"" ){ // add the link + save the page
if( $this->htmlspecialchars_ent($_POST["title"]) == "" ) {
$al_newlink = "[[".$this->cleanUrl($_POST["url"])."]]";
}else{
$al_newlink = "[[".$this->cleanUrl($_POST["url"])." ".$this->htmlspecialchars_ent($_POST["title"])."]]";
}
if( $this->htmlspecialchars_ent($_POST["category"]) == "" ){
$al_key = $al_key_default;
}else{
$al_key = $this->htmlspecialchars_ent($_POST["category"]);
}
$al_data[$al_key][] = $al_newlink; // Goal of this script: Add the link to the array
if(isset($al_data["First"])){ // Here the layout is re-build
$al_output .= "==First==\n~".implode(" ",$al_data["First"])."\n";
unset($al_data["First"]);
}
if(isset($al_data["Last"])) {
$al_last .= "==Last==\n~".implode(" ",$al_data["Last"])."\n";
unset($al_data["Last"]);
}
foreach($al_data as $al_k => $al_v){
$al_output .= "==".$al_category[$al_k]."==\n~".implode(" ",$al_data[$al_k])."\n";
}
$al_output .= $al_last;
$this->SavePage($this->tag, $al_header."----\n".$al_output."----".$al_footer, "Link added in category $al_key");
$this->Redirect($this->Href());
//print "ok";
//print "\n<pre>\n"; print_r($al_data); print "\n<pre>\n";
}else{ // print the form
print $this->FormOpen('',$this->tag);
foreach($al_category as $al_cat_safe => $al_cat_full){
$al_select.="<option value=\"".$al_cat_safe."\">".$al_cat_full."</option>";
}
?>
<table border="0" cellpadding="3" cellspacing="0">
<tr>
<td rowspan="2" valign="top"><select name="category" size="3"><?php print $al_select; ?></select></td>
<td>Url: </td>
<td valign="top"><input type="text" size="50" maxlength="255" name="url" value="<?php print $_GET["url"]; ?>"></td>
<td rowspan="2" valign="bottom"><input type=submit name=submit value="Add"></td>
</tr>
<tr>
<td>Title:</td>
<td valign="bottom"><input type="text" size="50" maxlength="255" name="title" value="<?php print $_GET["title"]; ?>"></td>
</tr>
</table>
<?php
print $this->FormClose();
}
}else{
//print "\n<!-- ERROR: You don't have the needed rights for accessing the action addlink -->\n";
}
?>
To Do
- Test: how is this script behaving with an German, Chinese,... char set?
- Layout: How to structure the links?
- Code: is there an easy way to add 2 or 3 levels in the titles (like when you save your bookmark as an html file).
- CSS: convert the 'table'-form into pure css layout.
- HTML: What is the max length of an "option" in a "select"?
- Add a switch: "Last added", FIFO with the last ?10? added links...
- Add more checks: is the link or refer in the blacklist?,...
Beta
- I'm experimenting with javascript code to auto add the url and tilte into the form via $_GET variables. So adding a link would only take two mouse-clicks...
- javascript:void(window.location.href='http://server.com/TestAddLink?url='+escape(window.location.href)+'&title='+escape(document.title))
- I don't think it's possible to make this link browser independent, so we'll end up with a few links like this...
CategoryUserContributions