To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 19ee392d authored by vermeul's avatar vermeul
Browse files

added hashtags, work in progress

parent d28f6d00
......@@ -48,6 +48,8 @@ class YATA {
$query_params,
PREG_OFFSET_CAPTURE
);
$searches = array();
$in_categories = array();
$where = array();
$dbr = wfGetDB( DB_REPLICA );
......@@ -63,6 +65,7 @@ class YATA {
$categories = preg_split('/\s*\,\s*/', $values);
$cat_ids = array();
foreach( $categories as $category ) {
array_push($in_categories, $category);
$cat = self::get_category($dbr, $category);
if ($cat) {
$cat_ids[] = $cat->id;
......@@ -89,6 +92,7 @@ class YATA {
$where["u.username"] = $values;
}
elseif( in_array($field, array('wiki_text', 'comment')) ) {
array_push($searches, $field . ': ' . $values);
if ( preg_match( '/[\%_]/', $values ) ) {
$where[] = "$field LIKE (" . $dbr->addQuotes($values).")";
}
......@@ -109,23 +113,24 @@ class YATA {
# fetch the annotation details
$annotations = $dbr->select(
array(
a => 'yata_annotation',
u => 'user',
ac=> 'yata_annotation_category',
c => 'yata_category',
pc=> 'yata_category',
p => 'page',
'a' => 'yata_annotation',
'u' => 'user',
'ac'=> 'yata_annotation_category',
'c' => 'yata_category',
'pc'=> 'yata_category',
'p' => 'page',
),
array(
category => "c.name",
parent_category => "pc.name",
title => "p.page_title",
comment => "a.comment",
wiki_text => "a.wiki_text",
start_char => "a.start_char",
bookmark => "a.bookmark",
last_edited_by => "u.user_name",
last_modified => "a.modified_date"
'category' => "c.name",
'parent_category' => "pc.name",
'hashtag' => "c.hashtag",
'title' => "p.page_title",
'comment' => "a.comment",
'wiki_text' => "a.wiki_text",
'start_char' => "a.start_char",
'bookmark' => "a.bookmark",
'last_edited_by' => "u.user_name",
'last_modified' => "a.modified_date"
),
$where,
__METHOD__,
......@@ -141,48 +146,61 @@ class YATA {
)
);
$table = "<b>Querystring:</b><pre>$querystring</pre><b>Result</b> ";
if ($searches) {
$table .= "<b>Search for term:</b>";
foreach($searches as $search_string) {
$table .= "<pre>".$search_string."</pre>";
}
}
if ($in_categories) {
$table .= "<b>Search in Categories:</b>";
foreach($in_categories as $category) {
$table .= "<pre>".$category."</pre>";
}
}
$table .= "<b>Results</b> ";
$table .= '([' . $title->getFullURL() . '?action=purge' . ' refresh page]):';
$table .= '
if (count($annotations)>0) {
$table .= '
<table class="wikitable sortable jquery-tablesorter">
<tr>
<th class="headerSort headerSortUp" tabindex="0" role="columnheader button" title="Sort descending"> Category </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Comment </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Link </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Annotated text </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Last edited by </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Modification
date </th>
</tr>';
EOT;
# compose the wiki table
$i = 0;
foreach($annotations as $annotation) {
# if a category has no parent, just show the category name
# in all other cases show parent_category:child_category
$cat = $annotation->parent_category ? $annotation->parent_category . "/" . $annotation->category : $annotation->category;
$wikitext = $annotation->wiki_text;
$wikitext = htmlentities($wikitext, ENT_QUOTES);
$a = array('=', '{', '|', '}');
$b = array('&#61;', '&#123;', '&#124;', '&#125;');
$wikitext = str_replace($a, $b, $wikitext);
$table .= "<tr><td>".$cat
."</td><td>".$annotation->comment
."</td><td>[[".$annotation->title."#".$annotation->bookmark . "]]"
."</td><td><pre>" . $wikitext . "</pre>"
#." || "
."</td><td>" . $annotation->last_edited_by
."</td><td>" . wfTimestamp( TS_ISO_8601, $annotation->last_modified)
."</td></tr>\n";
<tr>
<th class="headerSort headerSortUp" tabindex="0" role="columnheader button" title="Sort descending"> Category </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Comment </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Link </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Annotated text </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Last edited by </th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending"> Modification date </th>
</tr>';
# compose the wiki table
foreach($annotations as $annotation) {
# if a category has no parent, just show the category name
# in all other cases show parent_category:child_category
$cat = $annotation->parent_category ? $annotation->parent_category . "/" . $annotation->category : $annotation->category;
$wikitext = $annotation->wiki_text;
$wikitext = htmlentities($wikitext, ENT_QUOTES);
$a = array('=', '{', '|', '}');
$b = array('&#61;', '&#123;', '&#124;', '&#125;');
$wikitext = str_replace($a, $b, $wikitext);
$table .= "<tr><td>".$cat
."</td><td>".$annotation->comment
."</td><td>[[".$annotation->title."#".$annotation->bookmark . "]]"
."</td><td><pre>" . $wikitext . "</pre>"
#." || "
."</td><td>" . $annotation->last_edited_by
."</td><td>" . wfTimestamp( TS_ISO_8601, $annotation->last_modified)
."</td></tr>\n";
}
$table .= "</table>";
}
else {
$table .= "<b>No results found.</b>";
}
$table .= "</table>";
return $table;
}
......@@ -199,7 +217,7 @@ EOT;
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Comment !! Link !! Annotated text !! Last edited by !! Modification date
! Category !! Hashtag !! Comment !! Link !! Annotated text !! Last edited by !! Modification date
EOT;
# fetch the annotation details
......@@ -217,6 +235,7 @@ EOT;
start_char => "a.start_char",
bookmark => "a.bookmark",
category => "c.name",
hashtag => "c.hashtag",
parent_category => "pc.name",
last_edited_by => "u.user_name",
last_modified => "a.modified_date"
......@@ -248,6 +267,7 @@ EOT;
$cat = $annotation->parent_category ? $annotation->parent_category . "/" . $annotation->category : $annotation->category;
$table .= "|-\n";
$table .= "| ".$cat
." || ".$annotation->hashtag
." || ".$annotation->comment
." || [[$title#".$annotation->bookmark . "]]"
." || " . $wikitext
......@@ -283,7 +303,7 @@ EOT;
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Description !! id !! parent_id
! Category !! Hashtag !! Description !! id !! parent_id
EOT;
......@@ -292,6 +312,7 @@ EOT;
# in all other cases show parent_category:child_category
$table .= "|-\n";
$table .= "| ".$category->name
." || ".$category->hashtag
." || ".$category->description
." || ".$category->id
." || ".$category->parent_id
......@@ -301,6 +322,7 @@ EOT;
foreach ($child_categories as $child_category) {
$table .= "|-\n";
$table .= "|" . str_repeat('&nbsp;&nbsp;&nbsp;', $child_category->level) . $child_category->name
." || ".$child_category->hashtag
." || ".$child_category->description
." || ".$child_category->id
." || ".$child_category->parent_id
......@@ -498,7 +520,7 @@ EOT;
}
}
public static function add_category($dbw, $category_name, $description, $parent) {
public static function add_category($dbw, $category_name, $hashtag, $description, $parent) {
$category_name = trim($category_name);
$parent_id = null;
if (! is_null($parent)) {
......@@ -526,6 +548,7 @@ EOT;
'yata_category',
array(
name => $category_name,
hashtag => $hashtag,
description => trim($description),
parent_id => $parent_id
)
......@@ -552,6 +575,9 @@ EOT;
}
$set["name"] = $args["name"];
}
if (array_key_exists("hashtag", $args) ) {
$set["hashtag"] = $args["hashtag"];
}
if (array_key_exists("description", $args) ) {
$set["description"] = $args["description"];
}
......@@ -588,6 +614,16 @@ EOT;
public static function del_category($dbw, $category_name) {
if (substr($category_name, 0) == '#') {
# we go a hashtag, which should be unique.
$dbw->delete(
'yata_category',
array(
hashtag => $category_name,
)
);
return;
}
$cat_entry = self::get_category($dbw, $category_name);
if (!$cat_entry) {
throw new Exception("Error deleting category - no such category: $category_name");
......@@ -603,7 +639,7 @@ EOT;
private static function split_cat_argstring($arg_string) {
$args = array();
# split by comma, but only if next element is one of the keys (name, description, parent)
$keyvals = preg_split('/\s*\,\s*(?=(name|description|parent))/', $arg_string);
$keyvals = preg_split('/\s*\,\s*(?=(name|hashtag|description|parent))/', $arg_string);
foreach($keyvals as $keyval) {
list($key, $value) = preg_split('/\s*\=\s*/', $keyval);
# remove ay leading and trailing " or '
......@@ -619,14 +655,15 @@ EOT;
$dbw = wfGetDB( DB_MASTER );
# split the arguments (after #annotcat:)
#
# {{#annotcat: add | name=new top category, description=some meaningful comments, parent=grandparent_category/parent_category }}
# {{#annotcat: up | name=new top category, description=some meaningful comments, parent=grandparent_category/parent_category }}
# {{#annotcat: add | name=new top category, hashtag=#shortcut, description=some meaningful comments, parent=grandparent_category/parent_category }}
# {{#annotcat: up | name=new top category, hashtag=#shortcut, description=some meaningful comments, parent=grandparent_category/parent_category }}
# {{#annotcat: del | parent_category_name/category_name}}
# {{#annotcat: del | #shortcut }}
list($method, $arg1, $arg2) = preg_split('/\s*\|\s*/', $matches["params"]);
if ( $method === "add" ) {
$args = self::split_cat_argstring($arg1);
self::add_category($dbw, $args['name'], $args['description'], $args['parent']);
self::add_category($dbw, $args['name'], $args['hashtag'], $args['description'], $args['parent']);
}
elseif ( $method === "up" ) {
$args = self::split_cat_argstring($arg2);
......@@ -678,7 +715,7 @@ EOT;
$categories = $dbr->select(
'yata_category',
array( 'id', 'name', 'description', 'parent_id' ),
array( 'id', 'name', 'hashtag', 'description', 'parent_id' ),
$where,
__METHOD__,
$order_by
......@@ -688,24 +725,46 @@ EOT;
public static function get_category($dbr, $category) {
$category = trim($category);
# get parent and child category: parent_cat/child_cat
list($parent_cat, $child_cat) = preg_split('/\s*\/\s*/', $category);
# we might only have a parent (top) category
if (is_null($child_cat)) {
# search for a top category (no parent)
# we received a hashtag: use that.
if (substr($category, 0) == '#') {
$row = $dbr->selectRow(
array(c=>'yata_category'),
array(
id => 'c.id',
name => 'c.name',
hashtag => 'c.hashtag',
description => 'c.description',
parent_name => "''",
parent_id => 'c.parent_id'
),
array(
name => $parent_cat,
parent_id => null
hashtag => $category,
)
);
return $row;
}
# get parent and child category: parent_cat/child_cat
list($parent_cat, $child_cat) = preg_split('/\s*\/\s*/', $category);
# we might only have a parent (top) category
if (is_null($child_cat)) {
# search for a top category (no parent)
$row = $dbr->selectRow(
array(c=>'yata_category'),
array(
'id' => 'c.id',
'name' => 'c.name',
'hashtag' => 'c.hashtag',
'description' => 'c.description',
'parent_name' => "''",
'parent_id' => 'c.parent_id'
),
array(
'name' => $parent_cat,
'parent_id' => null
)
);
return $row;
......@@ -714,13 +773,13 @@ EOT;
# search for category where parent matches
# by self-joining table via parent_id
$row = $dbr->selectRow(
array(pc=>'yata_category', cc=>'yata_category'),
array('pc'=>'yata_category', 'cc'=>'yata_category'),
array(
id => 'cc.id',
name => 'cc.name',
description => 'cc.description',
parent_name => 'pc.name',
parent_id => 'cc.parent_id'
'id' => 'cc.id',
'name' => 'cc.name',
'description' => 'cc.description',
'parent_name' => 'pc.name',
'parent_id' => 'cc.parent_id'
),
array(
'cc.name' => $child_cat,
......@@ -738,11 +797,12 @@ EOT;
$row = $dbr->selectRow(
array(c=>'yata_category', pc=>'yata_category'),
array(
id => 'c.id',
name => 'c.name',
description => 'c.description',
parent_name => 'pc.name',
parent_id => 'pc.id'
'id' => 'c.id',
'name' => 'c.name',
'hashtag' => 'c.hashtag',
'description' => 'c.description',
'parent_name' => 'pc.name',
'parent_id' => 'pc.id'
),
array(
'c.id' => $id
......@@ -754,13 +814,10 @@ EOT;
return $row;
}
public static function get_child_categories($dbr, $category, $level) {
if (! $level) {
$level = 0;
}
public static function get_child_categories($dbr, $category, $level=0) {
$rows = $dbr->select(
array('yata_category'),
array('id', 'name', 'description', 'parent_id', "$level as level"),
array('id', 'name', 'hashtag', 'description', 'parent_id', "$level as level"),
array(
"parent_id" => $category->id
)
......
......@@ -69,6 +69,7 @@ CREATE TABLE /*_*/yata_category (
-- unique identifier for every annotation category
id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255),
hashtag varchar(255),
description blob,
-- categories are organized as a tree
parent_id int unsigned
......@@ -76,4 +77,5 @@ CREATE TABLE /*_*/yata_category (
CREATE INDEX /*i*/yata_category_name ON /*_*/yata_category (name);
CREATE INDEX /*i*/yata_category_parent ON /*_*/yata_category (parent_id);
CREATE UNIQUE INDEX /*i*/yata_category_hashtag_unq ON /*_*/yata_category(hashtag);
......@@ -48,6 +48,7 @@ CREATE TABLE mediawiki.yata_category
(
id SERIAL NOT NULL,
"name" VARCHAR (255),
hashtag VARCHAR (255),
description TEXT,
parent_id INT,
PRIMARY KEY (id)
......@@ -55,4 +56,5 @@ CREATE TABLE mediawiki.yata_category
CREATE INDEX yata_category_name_idx ON mediawiki.yata_category (name);
CREATE INDEX yata_category_parent_idx ON mediawiki.yata_category (parent_id);
CREATE UNIQUE INDEX yata_category_hashtag_unq ON mediawiki.yata_category(hashtag);
BEGIN;
--
-- Tables for the YATA extension
--
-- Annotations table
CREATE TABLE /*_*/yata_annotation (
-- unique identifier for every annotation
id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
-- the comment itself.
comment blob,
-- the wiki text which is annotated
wiki_text blob,
-- foreign key to page.page_id
page_id int unsigned NOT NULL,
-- foreign key to user.user_id
user_id int unsigned NOT NULL,
-- start position of the annotation according to the wiki sourcecode
start_char int unsigned,
-- a unique bookmark for every annotation
-- so we later can link directly to the part
-- of the page an annotation was found.
bookmark varchar(6),
insert_date varbinary(14),
modified_date varbinary(14)
)/*$wgDBTableOptions*/;
CREATE INDEX /*i*/yata_annot_page ON /*_*/yata_annotation (page_id);
CREATE INDEX /*i*/yata_annot_user ON /*_*/yata_annotation (user_id);
COMMIT;
BEGIN;
--
-- Tables for the YATA extension
--
-- Many to many table for annotation and category.
-- Any given annotation can be assigned to more than one category.
CREATE TABLE /*_*/yata_annotation_category (
-- foreign key to a page (for faster lookups)
page_id int unsigned NOT NULL,
-- foreign key to an annotation
annotation_id int unsigned NOT NULL,
-- foreign key to a category
category_id int unsigned NOT NULL
)/*$wgDBTableOptions*/;
CREATE INDEX /*i*/yata_ann_cat_page_idx ON /*_*/yata_annotation_category (page_id);
CREATE INDEX /*i*/yata_ann_cat_annotation_idx ON /*_*/yata_annotation_category (annotation_id);
CREATE INDEX /*i*/yata_ann_cat_category_idx ON /*_*/yata_annotation_category (category_id);
COMMIT;
BEGIN;
--
-- Tables for the YATA extension
--
-- Annotations table
CREATE TABLE /*_*/yata_annotation_error (
-- unique identifier for every annotation
id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
-- the syntax error which occured while parsing the text
error blob,
-- foreign key to page.page_id
page_id int unsigned NOT NULL,
location_char int unsigned
)/*$wgDBTableOptions*/;
CREATE INDEX /*i*/yata_annot_page ON /*_*/yata_annotation_error (page_id);
COMMIT;
BEGIN;
--
-- Tables for the YATA extension
--
-- Annotation Category table
-- Annotation category table
CREATE TABLE /*_*/yata_category (
-- unique identifier for every annotation category
id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255),
description blob,
-- categories are organized as a tree
parent_id int unsigned
);
CREATE INDEX /*i*/yata_category_name ON /*_*/yata_category (name);
CREATE INDEX /*i*/yata_category_parent ON /*_*/yata_category (parent_id);
COMMIT;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment