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 c98bcbfb authored by vermeul's avatar vermeul
Browse files

YATA plugin status of Sep. 7th 2017

parent 8cf0190c
......@@ -39,12 +39,125 @@ class YATA {
//
// Render the output of {{#annoask:}}
//
public static function annotation_query( $parser, $query) {
public static function annotation_query( $parser, $querystring) {
$title = $parser->getTitle();
$wikiPage = new WikiPage( $title );
return "Query provided: $query";
$queries_found = preg_match_all(
'/\[\[(?P<query>.*?)\]\]/s',
$querystring,
$query_params,
PREG_OFFSET_CAPTURE
);
$where = array();
$dbr = wfGetDB( DB_REPLICA );
$seen = array();
foreach($query_params["query"] as $query) {
list($field, $values) = preg_split('/\s*\:\s*/', $query[0]);
if( $field === 'category') {
$categories = preg_split('/\s*\,\s*/', $values);
$cat_ids = array();
foreach( $categories as $category ) {
$cat = self::get_category($dbr, $category);
if ($cat) {
$cat_ids[] = $cat->id;
$child_categories = self::get_child_categories($dbr, $cat);
foreach($child_categories as $child_category) {
$cat_ids[] = $child_category->id;
}
}
else {
die("no such category found: $category");
}
}
$where[] =
"EXISTS(
SELECT NULL
FROM yata_annotation_category ac
WHERE ac.annotation_id = a.id
AND ac.category_id IN ("
. join(',', $cat_ids)
. ")
)";
}
elseif( $field === 'user' ) {
$where["u.username"] = $values;
}
elseif( in_array($field, array('wiki_text', 'comment')) ) {
if ( preg_match( '/[\%_]/', $values ) ) {
$where[] = "$field LIKE (" . $dbr->addQuotes($values).")";
}
else {
$where["a.$field"] = $values;
}
}
}
# 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',
),
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"
),
$where,
__METHOD__,
array(
'ORDER BY' => 'a.start_char ASC'
),
array(
'u' => array( 'LEFT JOIN', array( 'u.user_id = a.user_id') ),
'ac' => array( 'LEFT JOIN', array( 'ac.annotation_id = a.id' ) ),
'c' => array( 'LEFT JOIN', array( 'ac.category_id = c.id' ) ),
'pc' => array( 'LEFT JOIN', array( 'pc.id = c.parent_id') ),
'p' => array( 'INNER JOIN', array( 'p.page_id = a.page_id') ),
)
);
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Comment !! Link !! Annotated text !! Last edited by !! Modification date
EOT;
# 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
$wikitext = preg_replace(
'/{{\s*#annot(?:<end>end)?.*?}}/',
'',
$annotation->wiki_text
);
$cat = $annotation->parent_category ? $annotation->parent_category . "/" . $annotation->category : $annotation->category;
$table .= "|-\n";
$table .= "| ".$cat
." || ".$annotation->comment
." || [[".$annotation->title."#".$annotation->bookmark . "]]"
." || " . $wikitext
."\n || " . $annotation->last_edited_by
."\n || " . wfTimestamp( TS_ISO_8601, $annotation->last_modified)
."\n";
}
$table .= "|}";
return $table;
}
//
// Render the output of {{#annotlist:}}.
......@@ -127,12 +240,11 @@ EOT;
return "";
}
#$dbr = wfGetDB( DB_REPLICA );
$dbr = wfGetDB( DB_MASTER );
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Parent !! Description
! Category !! Description !! Parent
EOT;
......@@ -149,7 +261,7 @@ EOT;
grand_parent_name => "gpc.name"
),
array(
'ac.page_id' => $wikiPage->getId()
#'ac.page_id' => $wikiPage->getId()
),
__METHOD__,
array(
......@@ -165,9 +277,10 @@ EOT;
# if a category has no parent, just show the category name
# in all other cases show parent_category:child_category
$table .= "|-\n";
$table .= "| ".$category->parent_name . "/" . $category->name
$table .= "| ".$category->name
." || ".$category->description
." || ".$category->grand_parent_name . "/" . $category->parent_name
." || ".$category->description."\n";
."\n";
}
$table .= "|}";
return $table;
......@@ -304,15 +417,22 @@ EOT;
array_push($seen_bookmarks, $bookmark);
$seen_bookmarks[] = $bookmark;
}
$where = array();
$where["page_id"] = $wikiPage->getId();
if ($seen_bookmarks) {
$where[] = 'bookmark NOT IN(' .$dbw->makeList($seen_bookmarks). ')';
}
# find all all annotations which are no longer present
$annotations_to_delete = $dbw->select(
'yata_annotation',
array( 'id' ),
array( 'bookmark NOT IN(' .$dbw->makeList($seen_bookmarks). ')' )
$where
);
# delete all annotations which are no longer present
foreach( $annotations_to_delete as $annotation) {
......@@ -336,20 +456,36 @@ EOT;
}
}
public static function add_category($dbw, $category, $description, $parent) {
$category = trim($category);
public static function add_category($dbw, $category_name, $description, $parent) {
$category_name = trim($category_name);
$parent_path = "";
$parent_id = null;
if (! is_null($parent)) {
$parent_category = self::get_category($dbw, $parent);
if(! $parent_category) {
die("no such parent category found: $parent");
}
$parent_path = $parent_category->path;
$parent_id = $parent_category->id;
}
$exists = $dbw->selectRow(
'yata_category',
array('id'),
array(
name => $category_name,
parent_id => $parent_id
)
);
if ($exists) {
return;
}
$dbw->insert(
'yata_category',
array(
name => $category,
name => $category_name,
description => trim($description),
parent_id => $parent_id
)
......@@ -367,9 +503,31 @@ EOT;
);
}
public static function del_category($dbw, $category) {
$cat_entry = self::get_category($dbw, $category);
public static function up_category($dbw, $category_name, $description, $parent_name) {
$category = self::get_category($dbw, $category_name);
$parent_category = self::get_category($dbw, $parent_name);
if ($category) {
$set = array();
if ($description) {
$set["description"] = $description;
}
if ($parent_category) {
$set["parent_id"] = $parent_category->id;
}
$dbw->update(
'yata_category',
$set,
array(
id => $category->id
)
);
}
}
public static function del_category($dbw, $category_name) {
$cat_entry = self::get_category($dbw, $category_name);
$dbw->delete(
'yata_category',
array(
......@@ -380,17 +538,22 @@ EOT;
private static function category_add_del_callback($matches) {
$dbw = wfGetDB( DB_MASTER );
list($method, $category, $description, $parent) = preg_split('/\s*\|\s*/', $matches["params"]);
list($method, $category_name, $description, $parent) = preg_split('/\s*\|\s*/', $matches["params"]);
if ( $method === "add" ) {
self::add_category($dbw, $category, $description, $parent);
self::add_category($dbw, $category_name, $description, $parent);
}
elseif ( $method === "up" ) {
self::up_category($dbw, $category_name, $description, $parent);
}
elseif ( $method === "del" ) {
self::del_category($dbw, $category);
self::del_category($dbw, $category_name);
}
else {
return $matches[0];
}
# remove the {{#annotcat:}} commands from the source code
# remove the {{#annotcat: add|up|del }} commands from the source code
# by returning an empty string
return "";
}
......@@ -471,15 +634,27 @@ EOT;
return $row;
}
public static function get_child_categories($dbr, $category_id) {
$row = self::get_category_for_id($category_id);
public static function get_child_categories($dbr, $category) {
$rows = $dbr->select(
array('yata_category'),
array('id', 'name', 'description', 'path'),
"path LIKE CONCAT(".$row->path.",'%')"
array(
"parent_id" => $category->id
)
);
return $rows;
$all_child_categories = array();
foreach($rows as $row) {
array_push($all_child_categories, $row);
$child_categories = self::get_child_categories($dbr, $row);
foreach($child_categories as $child_category) {
array_push($all_child_categories, $child_category);
}
}
return $all_child_categories;
}
public static function get_parent_categories($dbr, $category_id) {
......@@ -488,7 +663,7 @@ EOT;
$rows = $dbr->select(
array('yata_category'),
array('id', 'name', 'description', 'path'),
"'" . $row->path . "' LIKE CONCAT(path, '%')"
$dbr->addQuotes($row->path) . " LIKE CONCAT(path, '%')"
);
return $rows;
}
......
......@@ -21,6 +21,9 @@
],
"PageContentSave": [
"YATA::onPageContentSave"
],
"PageContentSaveComplete": [
"YATA::onPageContentSaveComplete"
]
},
"manifest_version": 1
......
......@@ -14,20 +14,22 @@ CREATE TABLE /*_*/yata_annotation (
-- the wiki text which is annotated
wiki_text blob,
-- start and end position of the annotation according to the wiki sourcecode
start_char int unsigned,
end_char int unsigned,
-- 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)
bookmark varchar(6),
insert_date varbinary(14),
modified_date varbinary(14),
)/*$wgDBTableOptions*/;
......
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