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

added annotcatlist, annotcat [add|del] functionality

parent 979579d5
......@@ -15,8 +15,8 @@ class YATA {
$parser->setFunctionHook( 'annot', 'YATA::renderStartAnnot' );
$parser->setFunctionHook( 'annotend', 'YATA::renderEndAnnot' );
$parser->setFunctionHook( 'annotlist', 'YATA::annotations_list' );
$parser->setFunctionHook( 'annotcat', 'YATA::annotations_categories' );
$parser->setFunctionHook( 'annotask', 'YATA::annotations_queries' );
$parser->setFunctionHook( 'annotcatlist', 'YATA::annotation_categories_list' );
}
// Render the output of {{#annot: comment | category | id}}.
......@@ -29,11 +29,6 @@ class YATA {
return "</span>";
}
// Render the output of {{#annocat: add | parent/child}}
public static function annotations_categories( $parser, $method, $category ) {
return "Method: '''$method'''<br/>Category: '''$category'''";
}
// Render the output of {{#annoask:}}
public static function annotations_queries( $parser, $query) {
return "Query provided: $query";
......@@ -87,7 +82,7 @@ EOT;
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;
$cat = $annotation->parent_category ? $annotation->parent_category . "/" . $annotation->category : $annotation->category;
$table .= "|-\n";
$table .= "| ".$cat
." || ".$annotation->comment
......@@ -98,6 +93,56 @@ EOT;
}
// Render the output of {{#annotcatlist:}}.
public static function annotation_categories_list( $parser ) {
$dbr = wfGetDB( DB_REPLICA );
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Parent !! Description
EOT;
$str = '';
$categories = $dbr->select(
array(
c =>'yata_category',
pc=>'yata_category',
gpc=>'yata_category',
),
array(
name => "c.name",
description => "c.description",
parent_name => "pc.name",
grand_parent_name => "gpc.name"
),
array(
#'ac.page_id' => $wikiPage->getId()
),
__METHOD__,
array(
#'ORDER BY' => 'a.start_char ASC'
),
array(
'pc' => array( 'LEFT JOIN', array( 'pc.id = c.parent_id') ),
'gpc' => array( 'LEFT JOIN', array( 'gpc.id = pc.parent_id') )
)
);
foreach($categories as $category) {
# 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
." || ".$category->grand_parent_name . "/" . $category->parent_name
." || ".$category->description."\n";
}
$table .= "|}";
return $table;
}
/*
Hook on when page is saved
......@@ -122,8 +167,14 @@ EOT;
$data = $content->getNativeData();
# replace all convenience {{#annotend}} or {{annotend}} with {{#annotend:}}, where necessary
$data = preg_replace( '/{{#*annotend}}/U','{{#annotend:}}', $data);
# add / delete categories
$data = self::manage_categories($dbw, $data);
# replace the changed content
$content = new WikitextContent( $data );
# delete all existing annotations
self::delete_annotations($dbw, $wikiPage);
self::delete_annotation_categories($dbw, $wikiPage);
......@@ -147,20 +198,7 @@ EOT;
# get the last value of the autoincrement field "id".
$annotation_id = $dbw->insertId();
foreach ( $annotation['categories'] as $cat ){
# get parent and child category: parent_cat/child_cat
$cats = preg_split("/[\/]/", $cat);
$parent_cat = $cats[0];
$child_cat = $cats[1];
/*
parent_category:child_category
:child_category
child_category
*/
if (is_null($child_cat)) {
$child_cat = $parent_cat;
$parent_cat = null;
}
$category = self::get_category($dbr, $child_cat, $parent_cat);
$category = self::get_category($dbr, $cat);
# assign all found categories to this annotation
if(! is_null($category)) {
......@@ -171,15 +209,89 @@ EOT;
}
}
public static function get_category($dbr, $category, $parent) {
if (is_null($parent)) {
public static function add_category($dbw, $category, $description, $parent) {
$category = trim($category);
$parent_path = "";
$parent_id = null;
if (! is_null($parent)) {
$parent_category = self::get_category($dbw, $parent);
$parent_path = $parent_category->path;
$parent_id = $parent_category->id;
}
$dbw->insert(
'yata_category',
array(
name => $category,
description => trim($description),
parent_id => $parent_id
)
);
# update the path for the category just inserted
$dbw->update(
'yata_category',
array(
path => $parent_path . $dbw->insertId() . "/"
),
array(
id => $dbw->insertId()
)
);
}
public static function del_category($dbw, $category) {
$cat_entry = self::get_category($dbw, $category);
$dbw->delete(
'yata_category',
array(
id => $cat_entry->id
)
);
}
private static function category_add_del_callback($matches) {
$dbw = wfGetDB( DB_MASTER );
list($method, $category, $description, $parent) = preg_split('/\s*\|\s*/', $matches["params"]);
if ( $method === "add" ) {
self::add_category($dbw, $category, $description, $parent);
}
elseif ( $method === "del" ) {
self::del_category($dbw, $category);
}
# remove the {{#annotcat:}} commands from the source code
return "";
}
// manage any categories added
public static function manage_categories( $dbw, $data) {
# look for all tags {{#annotcat:(.*)}}
# and pass the matches to the callback function category_add_del_callback
# which will return an empty string in order to remove this tag before saving the page.
$data = preg_replace_callback(
'/({{#annotcat:\s*(?P<params>.*?)}})/s',
'self::category_add_del_callback',
$data
);
return $data;
}
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)
$row = $dbr->selectRow(
'yata_category',
array( 'id', 'name', 'description', 'parent_id' ),
array(
name => $category,
parent_id => $parent
name => $parent_cat,
parent_id => null
)
);
return $row;
......@@ -197,8 +309,8 @@ EOT;
parent_id => 'y1.parent_id'
),
array(
'y1.name' => $category,
'y2.name' => $parent
'y2.name' => $parent_cat,
'y1.name' => $child_cat
),
__METHOD__,
array(),
......@@ -208,34 +320,6 @@ EOT;
}
}
public static function new_category($dbw, $category, $description, $parent_id) {
$parent_path = "";
if (! is_null($parent_id)) {
$parent_category = self::get_category_for_id($dbw, $parent_id);
$parent_path = $parent_category->path();
}
$dbw->insert(
'yata_category',
array(
name => $category,
description => $description,
parent_id => $parent_id
)
);
# update the path for the category just inserted
$dbw->update(
'yata_category',
array(
path => $parent_path . $dbw->insertId() . "/"
),
array(
id => $dbw->insertId()
)
);
}
public static function get_category_for_id($dbr, $id) {
$row = $dbr->selectRow(
array('yata_category'),
......
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