It's ugly and more bugs than code at the moment, but if you do this:
CREATE TABLE semantics (create_page TEXT, source_page TEXT, relation
TEXT, target_page TEXT);
CREATE INDEX i_create_page ON semantics(create_page(50));
CREATE INDEX i_source_page ON semantics(source_page(50));
CREATE INDEX i_target_page ON semantics(target_page(50));
and then add the attached file to LocalSettings.php (tested w/
mediawiki 1.9.3), you can create links like [[x::R::y]] on any
MediaWiki page to indicate that X relates to Y.
Semantic data for each page (even pages that don't exist yet) is
displayed at the bottom of the page.
I tried to edit existing Semantic Mediawiki code for this, but it
really seems married to a "page can only create semantic data for
itself" philosophy.
If there's enough/any interest here, please let me know, and we can
SourceForge this or something (my goals are in the comments near the
end of the attached file)
--
We're just a Bunch Of Regular Guys, a collective group that's trying
to understand and assimilate technology. We feel that resistance to
new ideas and technology is unwise and ultimately futile.
# only necessary while testing, hopefully
$wgEnableParserCache = false; # turn off cacheing
$wgShowSQLErrors = true; # show SQL errors
# define hooks
$wgHooks['ParserBeforeStrip'][] = 'myInternalParseBeforeLinks';
$wgHooks['ArticleSaveComplete'][] = 'myArticleSaveComplete';
# TODO: this is ugly-- what's the global variable of acceptable characters?
$okchars = "[a-zA-Z_]";
# when an article is saved, store [[a::b::c]] [[b::c]] type data
function myArticleSaveComplete (&$article, &$user, &$text, &$summary, &$minoredit, &$watchthis, &$sectionanchor, &$flags) {
global $wgTitle, $okchars;
# delete existing semantic data that this page created
wfGetDB(DB_MASTER)->query("DELETE FROM semantics WHERE create_page = '$wgTitle->mDbkeyform'");
# add semantic data this page defines
# replace [[b::c]] with [[this_page::b::c]], for parsing below (hack?)
$text = preg_replace("/\[\[($okchars+)::($okchars+)\]\]/","[[$wgTitle->mDbkeyform::$1::$2]]",$text);
# store [[a::b::c]]
$text = preg_replace_callback("/\[\[($okchars+)::($okchars+)::($okchars+)\]\]/i", "saveLink", $text);
return 1;
}
function myInternalParseBeforeLinks (&$parser, &$text) {
global $wgTitle, $okchars;
$title = $wgTitle->mDbkeyform; # how this page is ref'd in the db
# replace [[b::c]] with [[this_page::b::c]], for parsing below (hack?)
$text = preg_replace("/\[\[($okchars+)::($okchars+)\]\]/","[[$title::$1::$2]]",$text);
# replace [[a::b::c]] with just [[c]]
$text = preg_replace("/\[\[($okchars+)::($okchars+)::($okchars+)\]\]/i", "[[$3]]", $text);
# add semantic info about this page
$text .= "\n== Semantics ==\n
\n";
$text .= "Source | Relation | Target | Creator |
\n";
# CASE condition (to separate out cases where I am source/target/create page)
# currently unused
$case = "CASE WHEN '$title'=source_page THEN 0
WHEN '$title'=target_page THEN 1
WHEN '$title'=create_page THEN 2 END";
# find all pages where I am source/target/create page, sorted by my role
$res = wfGetDB(DB_SLAVE)->query("SELECT *, $case FROM semantics
WHERE '$title' IN (create_page, source_page, target_page) ORDER BY
$case, relation, source_page, target_page, create_page");
# table of semantic data
while ($row = mysql_fetch_array($res)) {
$text .= "[[$row[source_page]]] | $row[relation] | [[$row[target_page]]] | [[$row[create_page]]] |
\n";
}
# end table
$text .= "
\n";
return 1;
}
# saves [[a::b::c]] to the backend db
function saveLink ($data) {
global $wgTitle;
wfGetDB(DB_MASTER)->query("INSERT INTO semantics
(create_page, source_page, relation, target_page) VALUES
('$wgTitle->mDbkeyform','$data[1]','$data[2]','$data[3]')");
return $data[0]; # just so I can use preg_replace_callback
}
# TODO: laundry list below
# handle "links with spaces" better (auto-change to links_with_spaces?)
# allow queries
# have attributes not just properties
# allow inverse relations (X::husband::Y => Y::wife::X)
# allow simple implicit relations (X::husband::Y => X::related_to::Y)
# allow complex implicit relations:
# X::father::Y && Y::brother::Z => X::uncle::Z (combintaorial explosion?)
# Special: page that handles duplicate relations (which are sometimes OK)
# category stuff should use Wikipedia categories
# semantics shouldn't show up on footer!
# if 2 pages have identical semantic data for X, shouldn't show it twice
# if page X has Y::r::Z, Y and Z should be removed from cache when X is saved
# sections for where I am source/target/creator should be broken out
# allow [x::y::z] to display as something other than z
# worry about pages not in Main namespace
# Semantics section should not have edit link, it can't be edited
# BELOW THIS LINE: utility/debugging functions that really have
# nothing to do with code above (var_dump_ret is stolen from
# php.net's annotation of var_dump, for example) function debug
# ($msg) {error_log("$msg\n",3,"/tmp/hook-debug.txt");}
function var_dump_ret($mixed = null) {
ob_start();
var_dump($mixed);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Semediawiki-user mailing list
Semediawiki-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/semediawiki-user