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

current status

parent ca7567a2
......@@ -14,46 +14,98 @@ class YATA {
// Create a function hook associating the "example" magic word with renderExample()
$parser->setFunctionHook( 'annot', 'YATA::renderStartAnnot' );
$parser->setFunctionHook( 'annotend', 'YATA::renderEndAnnot' );
$parser->setFunctionHook( 'annotlist', 'YATA::list_annotations' );
$parser->setFunctionHook( 'annotlist', 'YATA::annotations_list' );
$parser->setFunctionHook( 'annotcat', 'YATA::annotations_categories' );
$parser->setFunctionHook( 'annotask', 'YATA::annotations_queries' );
}
// Render the output of {{#annot: comment | category | id}}.
public static function renderStartAnnot( $parser, $comment='', $category='', $id='' ) {
return "<span style='background: rgba(0,0,0,0.05)' title='$comment'>comment=$comment, id=$id, category=$category";
return "<span style='background: rgba(0,0,0,0.05)' title='$comment'>";
}
// Render the output of {{#annotend:}}.
public static function renderEndAnnot( $parser, $id='' ) {
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";
}
// Render the output of {{#annotlist:}}.
public static function annotations_list( $parser ) {
$title = $parser->getTitle();
$wikiPage = new WikiPage( $title );
$dbr = wfGetDB( DB_REPLICA );
$str = "";
$result = $dbr->select(
'yata_annotation',
'wiki_text',
''
);
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Comment !! Annotated text
$cat = self::get_category($dbr, 'examples', 'written addition');
#$cat = self::get_category($dbr, 'addition', null);
$str .= $cat->parent . ':' . $cat->name;
EOT;
return "</span> : $str *---";
}
$str = '';
// Render the output of {{#annotlist:}}.
public static function list_annotations( $parser ) {
$dbr = wfGetDB( DB_REPLICA );
$annotations = $dbr->select(
array(
a =>'yata_annotation',
ac=>'yata_annotation_category',
c =>'yata_category',
pc=>'yata_category'
),
array(
comment =>'a.comment',
wiki_text => "a.wiki_text",
start_char => "a.start_char",
end_char => "a.end_char",
category => "c.name",
parent_category => "pc.name"
),
array(
'ac.page_id' => $wikiPage->getId()
),
__METHOD__,
array(
'ORDER BY' => 'a.start_char ASC'
),
array(
'ac' => array( 'INNER JOIN', array( 'ac.annotation_id = a.id' ) ),
'c' => array( 'INNER JOIN', array( 'ac.category_id = c.id' ) ),
'pc' => array( 'LEFT JOIN', array( 'pc.id = c.parent_id') )
)
);
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;
$table .= "|-\n";
$table .= "| ".$cat
." || ".$annotation->comment
." || ".trim($annotation->wiki_text)."\n";
}
$table .= "|}";
return $table;
}
/*
Hook on when page is saved
we change the content where necessary, e.g.
- change {{#annotend}} to {{#annotend:}}
- add errors as hidden text at the beginning of the wikitext
*/
public static function onPageContentSave(WikiPage &$wikiPage, User &$user, Content &$content, $summary, $isMinor, $isWatch, $section, $flags, Status &$status ){
/*
1. get all existing annotations which exist on that page.
2. find out which annotations have been deleted, added.
......@@ -61,7 +113,6 @@ class YATA {
4. assign every annotation to one or more categories
5. assign every annotation to this page
*/
public static function onPageContentSave(WikiPage &$wikiPage, User &$user, Content &$content, $summary, $isMinor, $isWatch, $section, $flags, Status &$status ){
# get database handler
$dbw = wfGetDB( DB_MASTER );
......@@ -96,8 +147,8 @@ class YATA {
# get the last value of the autoincrement field "id".
$annotation_id = $dbw->insertId();
foreach ( $annotation['categories'] as $cat ){
# get parent and child category
$cats = preg_split("/[:]/", $cat);
# get parent and child category: parent_cat/child_cat
$cats = preg_split("/[\/]/", $cat);
$parent_cat = $cats[0];
$child_cat = $cats[1];
/*
......@@ -157,6 +208,65 @@ class YATA {
}
}
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'),
array('id', 'name', 'description', 'path'),
array( id => $id )
);
return $row;
}
public static function get_child_categories($dbr, $category_id) {
$row = self::get_category_for_id($category_id);
$rows = $dbr->select(
array('yata_category'),
array('id', 'name', 'description', 'path'),
"path LIKE CONCAT(".$row->path.",'%')"
);
return $rows;
}
public static function get_parent_categories($dbr, $category_id) {
$row = self::get_category_for_id($category_id);
$rows = $dbr->select(
array('yata_category'),
array('id', 'name', 'description', 'path'),
"'" . $row->path . "' LIKE CONCAT(path, '%')"
);
return $rows;
}
public static function delete_annotations($dbw, $wikiPage) {
$dbw->delete(
'yata_annotation',
......@@ -206,7 +316,6 @@ class YATA {
);
}
public static function parse_annotations($dbw, $data, $wikiPage, $user){
# delete all existing errors
......@@ -277,8 +386,9 @@ class YATA {
# its corresponding end
if ($end = $ends_with_id[$id]) {
$annotations[$match[1]]["end"] = $end;
$annotations[$match[1]]["wiki_text"] =
substr($data, $start, $end - $start);
$wiki_text = substr($data, $start, $end - $start);
$wiki_text = preg_replace('/{{#annot.*?}}/U', '', $wiki_text);
$annotations[$match[1]]["wiki_text"] = $wiki_text;
}
else {
# we didn't find any corresponding end: throw an error
......@@ -291,8 +401,9 @@ class YATA {
foreach($ends_without_id as $annot_end => $value){
if($annot_end > $start) {
$annotations[$match[1]]["end"] = $annot_end;
$annotations[$match[1]]["wiki_text"] =
substr($data, $start, $annot_end - $start);
$wiki_text = substr($data, $start, $annot_end - $start);
$wiki_text = preg_replace('/{{#annot.*?}}/U', '', $wiki_text);
$annotations[$match[1]]["wiki_text"] = $wiki_text;
break;
}
}
......
......@@ -17,4 +17,6 @@ $magicWords['en'] = array(
'annot' => array( 0, 'annot' ),
'annotend' => array( 0, 'annotend' ),
'annotlist' => array( 0, 'annotlist' ),
'annotcat' => array( 0, 'annotcat' ),
'annotask' => array( 0, 'annotask' ),
);
......@@ -3,7 +3,7 @@
"author": "Swen Vermeul",
"version": "0.1.0",
"url": "https://www.mediawiki.org/wiki/Extension:YATA",
"descriptionmsg": "YATA - Yet Another Text Annotation",
"description": "やた - Yet Another Text Annotation",
"license-name": "GPL-2.0+",
"type": "parserhook",
"AutoloadClasses": {
......
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