Extension talk:TagAsCategory
From MediaWiki.org
[edit] Problem
The links created by the extension are wrong in my wiki. Instead linking to "http://wiki.example.com/index.php?title=Special:Categories"
it links to "http://wiki.example.com/Special%3ACategories" with "%3A" instead of ":" and without "index.php?title=" this does not work.
[edit] Will take a look
Thanks for the heads-up - I'll take a look. I believe our blog is running in the mode that rewrites URLs. There's probably a way to build a link programmatically through the API that will be compatible with both URL syntaxes. --Gbarnett 22:06, 7 August 2007 (UTC)
[edit] File called ArticleTagger.php??
Should the file actually be called ArticleTagger.php? If the require functionis looking for extensions/TagAsCategory/TagAsCategory.php, shouldn't you then have to cretae a directory in extensions called TagAsCategory and then in that directory, name your file TagAsCategory.php?
[edit] Yes, should be called TagAsCategory.php
Sorry, the extension used to be called "ArticleTagger", but we renamed it to "TagAsCategory" because we felt it was more descriptive.
I've replaced the references to "ArticleTagger" in the extension page with "TagAsCategory" -- sorry about the typo.
--Gbarnett 13:23, 10 August 2007 (UTC)
[edit] A function I would like to see.
A very nice feature would be to have a drop-down list of all your categories appearing, or an AJAX auto complete feature showing existing categories as you type.
[edit] Deleteing tags
How do you delete a wrongly named Tag ?
- I just edit the article and manually remove the [[category:xxx]] tag from the article. This extension simply makes it easy to inject the category tags into articles... removing them is still through the normal means. --Brianadkins 21:53, 13 September 2007 (UTC)
Thanks I am New to this. see also Project talk:Categories
[edit] Note for people running a custom skin
You need the "catlinks" div to exist in your skin.
essentially
//name of div identifying categories the page belongs to
if (document.getElementById('catlinks'))
{
document.getElementById('divLocationForTagForm').appendChild(document.getElementById('tagform'));
}
You can just rename the div if you've ignored / heavily modified monobook.
Neilkt 01:53, 16 September 2007 (UTC)
[edit] example sites?
Anyone have any example sites running this extension? I would like to see it in action.
--Smarbin 07:09, 19 September 2007 (UTC)
[edit] Localized message for "Add tag"
For those who haven't figured it out, you can localize the "Add tag" button in the TagAsCategory.php file.
Example with danish text:
# add localized messages for the "Add Tag" and "(none)" interface strings
$wgMessageCache->addMessages(
array (
'addtag' => 'Tilføj kategori',
'notags' => '(none)'
-- Kirjapan 10:33, 17 October 2007 (UTC)
[edit] How to prevent it appearing on defined pages?
Hello, I've tried this excellent script and think it is very helpful for the user to easy add 'tags' to the page. However, I would really like to be able to prevent this add-tag form from appearing on certain defined pages, ie: Main_Page | Foos_Feet etc. It would also be very handy to be able to prevent the add-tag from appearing on all the pages in a certain NameSpace, as well as preventing it from appearing on Category pages. Would this be possible? Another feature that would be great is for non-registered readers to not even see the add-tag form at all. Sadly my coding skills are zeroish so I can't hack it ZooHoo 01:25, 7 December 2007 (UTC)
- I have zero coding skills, but would like to exclude the tag as category for at least the main page... anyone? --Mark Weiser 02:11, 4 July 2008 (UTC)
[edit] Refresh Issue & Possible Fix
Hi,
Greate extension. However, I came across an issue. Whenever I used the Add Tag feature, the tag wouldn't show up until I refreshed the browser, changed articles or edited the article. HOWEVER, when I edited the article, I found that the Tag had been added every time I hit refresh.
I.e.
- I hit Add Tag, then refresh so I can see it. Refresh once more, again and finally once more. I then hit EDIT. The tag now appears four times in the page text (it still only displays once when you view the article).
I fixed this by making a few tweaks to the last section of TagAsCategory.php, as detailed below.
-- AerosAtar
function tagAction($action, $article) {
global $wgUser;
global $wgRequest;
if($action == 'tag' && $wgUser->isAllowed('edit'))
{
# set wgUser??
$content = $article->getContent();
$newCategoryString = "[[Category:" . $wgRequest->getVal('tag') . "]]";
// if $content doesn't already contain the new Category string...
if (strstr($content, $newCategoryString) === false) {
/* I found that strpos doesn't correctly identify the string as existing and therefore this check always fails, */
/* strstr does correctly identify the string as existing or not. I also made it an Identical comparison operator */
/* in order to ensure it only picked up exactly what was typed into the input box. */
// edit the content, appending the new Category string
// (with newlines) at the bottom of the article.
//
// additionally, flag the edit as an 'update', and suppress
// its inclusion in Recent Changes
$article->doEdit(
$content . "\n" . $newCategoryString . "\n",
"Added tag: '" . $wgRequest->getVal('tag') . "'",
EDIT_UPDATE | EDIT_SUPPRESS_RC
);
// Refresh the page to display the new category
header("Location:http://localhost/index.php/".$article->mTitle);
}
/* I added this so the page would refresh in order to make sure the new tag could be seen as having been added. */
/* I couldn't get it to work with $IP, however, so I had to hard-code the opening Location section. */
// view the article, and abort further processing by mediawiki (false)
$article->view();
return false;
}
else
{
// we don't have edit rights -- abort the operation, restoring
// control to the default mediawiki behavior (true)
return true;
}
}
}
[edit] Code Modification: Multiple tags at once
I love this extensions, but wanted to ability to add multiple tags at one time. So I updated the code to do this. I changed the form used to add tags (to make it larger) and modified the tagAction function to allow multiple tags. Here's the updated code:
<?php /** * TagAsCategory * * A folksonomy extension for MediaWiki * * Copyright (c) 2007, Molecular * BSD License * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * * Neither the name of Molecular the names of its contributors may be used * to endorse or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * @version 1 * @copyright 2007 Molecular * @author Glenn Barnett * @author Paul Irish * @link http://www.molecular.com/ Molecular * @license http://www.opensource.org/licenses/bsd-license.php BSD License * * * To activate this functionality, place this file in your extensions/ * subdirectory, and add the following line to LocalSettings.php: * require_once("$IP/extensions/TagAsCategory/TagAsCategory.php"); * * We also recommend changing the "Categories" label to "Tags". This can be * done by going to the "Special:Allmessages" page, and changing the * value of "pagecategories" from "{{PLURAL:$1|Category|Categories}}" to * "{{PLURAL:$1|Tags|Tags}}" . */ if(! defined( 'MEDIAWIKI' ) ) { echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); die( -1 ); } else { $wgExtensionCredits['other'][] = array( 'name' => 'TagAsCategory', 'author' =>'Molecular', 'url' => 'http://www.molecular.com', 'description' => 'This Extension provides "folksonomy"-type tagging to articles, using Categories as the underlying functionality' ); $wgHooks['ArticleViewHeader'][] = 'articleShowTagForm'; $wgHooks['UnknownAction'][] = 'tagAction'; function articleShowTagForm(&$article) { global $wgOut; global $wgScript; global $wgMessageCache; # add localized messages for the "Add Tag" and "(none)" interface strings $wgMessageCache->addMessages( array ( 'addtag' => 'Add Tags', 'notags' => '(none)' ) ); # load the localized messages into string variables $sAddTag = wfMsg('addtag'); $sNoTags = wfMsg('notags'); $sTags = wfMsg('pagecategories'); # defined by default as "{{PLURAL:$1|Category|Categories}}" $sCategoriesLink = wfMsg('pagecategorieslink'); # defined by default as "Special:Categories" # determine the URL for the form action $actionUrl = htmlspecialchars( $wgScript ); # generate the form HTML $tagFormHTML=<<<ENDFORM <!-- TagAsCategory extension START --> <form action="{$actionUrl}" method="get" id="tagform" style="display: none;"> <input type="hidden" name="title" value="{$article->mTitle->getPrefixedDBkey()}"> <input type="hidden" name="action" value="tag"> <input type="text" name="tag" value="" size="50" /> <input type="submit" value="{$sAddTag}" /> </form> <script type="text/javascript"> function moveTaggerBox(){ if (document.getElementById('catlinks')){ // if the Article is already in one or more categories, // 'catlinks' will exist document.getElementById('catlinks').appendChild(document.getElementById('tagform')); } else { // if 'catlinks' doesn't exist (the article is in 0 categories) // we must recreate the HTML var div1=document.createElement('div'); div1.setAttribute('id','catlinks'); var p1=document.createElement('p'); div1.appendChild(p1); var a1=document.createElement('a'); a1.setAttribute('href',escape('{$sCategoriesLink}')); a1.setAttribute('title','{$sCategoriesLink}'); p1.appendChild(a1); var txt1=document.createTextNode('{$sTags}'); a1.appendChild(txt1); var txt2=document.createTextNode(': {$sNoTags}'); p1.appendChild(txt2); document.getElementById('bodyContent').appendChild(div1); div1.appendChild(document.getElementById('tagform')); } document.getElementById('tagform').style.display = ''; // we defined it as hidden. show it once it's placed correctly } hookEvent("load", moveTaggerBox); // hookEvent() defined in wikibits.js </script> <!-- TagAsCategory extension END --> ENDFORM; $wgOut->addHTML($tagFormHTML); return true; } function tagAction($action, $article) { global $wgUser; global $wgRequest; if($action == 'tag' && $wgUser->isAllowed('edit')) { # set wgUser?? $content = $article->getContent(); $newTagsString = $wgRequest->getVal('tag'); $newTagsArray = split(" ", $newTagsString); foreach($newTagsArray as $newCategory) { if (strlen($newCategory) > 1) { $newCategoryString = "[[Category:" . $newCategory . "]]"; // if $content doesn't already contain the new Category string... if (strpos($content, '$newCategoryString') == false) { // edit the content, appending the new Category string // (with newlines) at the bottom of the article. // // additionally, flag the edit as an 'update', and suppress // its inclusion in Recent Changes $content .= "\n" . $newCategoryString; } } } $article->doEdit( $content, "Added tags: '" . $wgRequest->getVal('tag') . "'", EDIT_UPDATE | EDIT_SUPPRESS_RC ); // view the article, and abort further processing by mediawiki (false) $article->view(); return false; } else { // we don't have edit rights -- abort the operation, restoring // control to the default mediawiki behavior (true) return true; } } } ?>
-JCamozzi
- Why split on space? This means that you can't have categories with spaces. Would make more sense to split on a comma, or somesuch. - 220.233.239.174 09:20, 27 May 2008 (UTC)
[edit] Prevent appearance on main_page?
How to prevent it from appearing on certain pages like main_page?

