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

changes of today.

parent 4583e6be
......@@ -8,7 +8,9 @@
*/
class YATA {
//
// Register any render callbacks with the parser
//
public static function onParserSetup( &$parser ) {
// Create a function hook associating the "example" magic word with renderExample()
......@@ -16,26 +18,34 @@ class YATA {
$parser->setFunctionHook( 'annotend', 'YATA::renderEndAnnot' );
$parser->setFunctionHook( 'annotlist', 'YATA::annotations_list' );
$parser->setFunctionHook( 'annotask', 'YATA::annotations_queries' );
$parser->setFunctionHook( 'annotcatlist', 'YATA::annotation_categories_list' );
$parser->setFunctionHook( 'annotcat', 'YATA::annotation_categories' );
}
//
// 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'>";
return "<span id=$id 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 {{#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 );
......@@ -44,7 +54,7 @@ class YATA {
$table = <<<"EOT"
{| class="wikitable sortable"
|-
! Category !! Comment !! Annotated text
! Category !! Comment !! Link !! Annotated text
EOT;
......@@ -62,6 +72,7 @@ EOT;
wiki_text => "a.wiki_text",
start_char => "a.start_char",
end_char => "a.end_char",
bookmark => "a.bookmark",
category => "c.name",
parent_category => "pc.name"
),
......@@ -86,15 +97,23 @@ EOT;
$table .= "|-\n";
$table .= "| ".$cat
." || ".$annotation->comment
." || ".trim($annotation->wiki_text)."\n";
." || [[$title#".$annotation->bookmark . "]]"
." || ".trim($annotation->wiki_text)
."\n";
}
$table .= "|}";
return $table;
}
//
// Render the output of {{#annotcatlist:}}.
public static function annotation_categories_list( $parser ) {
//
public static function annotation_categories( $parser, $method, $start_with ) {
if (! $method == "list") {
return "";
}
$dbr = wfGetDB( DB_REPLICA );
$table = <<<"EOT"
{| class="wikitable sortable"
......@@ -152,11 +171,14 @@ EOT;
*/
public static function onPageContentSave(WikiPage &$wikiPage, User &$user, Content &$content, $summary, $isMinor, $isWatch, $section, $flags, Status &$status ){
/*
0. manage all categories (add/delete them)
1. get all existing annotations which exist on that page.
2. find out which annotations have been deleted, added.
3. updating is possible when an identifier is present (optimization step)
4. assign every annotation to one or more categories
5. assign every annotation to this page
2. find out which annotations have been deleted.
3. find out which annotations have been added -> assign a unique identifier
4. find out which annotations have been updated (using the unique
identifier)
5. assign every annotation to one or more categories
6. assign every annotation to this page
*/
# get database handler
......@@ -191,7 +213,8 @@ EOT;
start_char => $annotation['start'],
end_char => $annotation['end'],
page_id => $wikiPage->getId(),
user_id => $user->getId()
user_id => $user->getId(),
bookmark => $annotation['bookmark']
)
);
......@@ -260,6 +283,9 @@ EOT;
elseif ( $method === "del" ) {
self::del_category($dbw, $category);
}
else {
return $matches[0];
}
# remove the {{#annotcat:}} commands from the source code
return "";
}
......@@ -400,6 +426,11 @@ EOT;
);
}
public static function create_id() {
$id = str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
return $id;
}
public static function parse_annotations($dbw, $data, $wikiPage, $user){
# delete all existing errors
......@@ -425,19 +456,21 @@ EOT;
if ($ids["id"]) {
$ends_with_id[$ids["id"]] = $match[1];
}
# annotation endings without ID
# annotation endings without ID hold the position
else {
$ends_without_id[$match[1]] = true;
$ends_without_id[$match[1]] = $match[1];
}
}
}
die(var_dump($ends_without_id));
die(var_dump($ends_with_id));
# find out where annotations start
$annot_found = preg_match_all('/(?P<annot>{{#annot:.*?}})/s', $data, $annot, PREG_OFFSET_CAPTURE);
if ($annot_found) {
foreach ($annot["annot"] as $match) {
# extract the parameters in each annotation
$params_found = preg_match_all('/{{#annot:\s*(?P<params>.*)}}/s', $match[0], $params);
$params_found = preg_match_all('/{{#annot:\s*(?P<params>.*?)}}/s', $match[0], $params);
if ($params_found) {
# {{#annot: $comment | $category | $id}}
list($comment, $category, $id) = explode('|', $params["params"][0]);
......@@ -445,8 +478,8 @@ EOT;
# more than one category can be split using a comma: category1, category2
# categories are by default hierarchically organized and must appear
# in following formats:
# parent_category:child_category
# :top_category
# parent_category/child_category
# /top_category
# top_category
$categories = preg_split("/[,]/", $category);
foreach ($categories as &$cat){
......@@ -469,6 +502,7 @@ EOT;
# we have an annotation id - we need to connect it to
# its corresponding end
if ($end = $ends_with_id[$id]) {
$annotations[$match[1]]["bookmark"] = $id;
$annotations[$match[1]]["end"] = $end;
$wiki_text = substr($data, $start, $end - $start);
$wiki_text = preg_replace('/{{#annot.*?}}/U', '', $wiki_text);
......
......@@ -18,5 +18,5 @@ $magicWords['en'] = array(
'annotend' => array( 0, 'annotend' ),
'annotlist' => array( 0, 'annotlist' ),
'annotask' => array( 0, 'annotask' ),
'annotcatlist' => array( 0, 'annotcatlist'),
'annotcat' => array( 0, 'annotcat'),
);
......@@ -22,7 +22,12 @@ CREATE TABLE /*_*/yata_annotation (
page_id int unsigned NOT NULL,
-- foreign key to user.user_id
user_id int unsigned NOT NULL
user_id int unsigned NOT NULL,
-- 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)
)/*$wgDBTableOptions*/;
......
......@@ -12,7 +12,9 @@ CREATE TABLE /*_*/yata_annotation_error (
error blob,
-- foreign key to page.page_id
page_id int unsigned NOT NULL
page_id int unsigned NOT NULL,
location_char int unsigned
)/*$wgDBTableOptions*/;
......
......@@ -11,10 +11,12 @@ CREATE TABLE /*_*/yata_category (
id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255),
description blob,
parent_id int unsigned
);
parent_id int unsigned,
path varchar(1000)
) ;
CREATE INDEX /*i*/yata_category_name ON /*_*/yata_category (name);
CREATE INDEX /*i*/yata_category_parent ON /*_*/yata_category (parent_id);
CREATE INDEX /*i*/yata_category_path ON /*_*/yata_category (path);
COMMIT;
<?php
$data = <<<'EOT'
The start. Blalablaljka fjlsd
just before A{{#annot : as}}start A
some text in a
just before thirteen{{#annot: with id || 13}}thirteen starts right here.
before X{{#annot: xs || }}
text for x
end of x{{#annotend:}}
some more text for a
before B{{#annot: bs||}}start b
some text in b
before C{{#annot: cs||}}start c
some text in c
end of c{{#annotend:}}
some more text in b
thirteen ends right there.{{#annotend:13}}
end of b{{#annotend:}}
ending text of a{{#annotend:}}
this is THE END
EOT;
# match either {{#annot: or {{#annotend:
# fetch all options too
# ignore whitespace, except at the beginning
$data = "slkfjsdkf";
$annots_found = preg_match_all('/(?P<annotation>{{#annot(?P<end>end)?\s*:\s*(?P<opts>.*?)\s*}})/s', $data, $reg_params, PREG_OFFSET_CAPTURE);
function create_id() {
$id = str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
return $id;
}
$starts = array();
$ends = array();
$annotations_with_id = array();
$new_annotations = array();
$new_data = "";
# - run through all found {{#annot: and {{#annotend: matches
# - find every ending for every start, especially when no ID is present
# - automatically create a new unique ID if:
# * no ID is present
# * ID was not found in database
#
$starts = array();
$start_loc = 0;
$end_loc = 0;
if ($annots_found) {
foreach($reg_params["annotation"] as $index => $value) {
# $value[1] contains the location where the current tag has been found
$end_loc = $value[1];
$new_data = $new_data . substr($data, $start_loc, $end_loc-$start_loc);
print("end = $end_loc, start = $start_loc\n");
print substr($data, $start_loc, $end_loc-$start_loc);
print "\n------------------------------------\n";
$start_loc = $value[1] + strlen($value[0]);
# we encountered an annotation start
if ($reg_params["end"][$index][0] === "") {
list($comment, $category, $id) = preg_split('/\s*\|\s*/', $reg_params["opts"][$index][0]);
# we encountered an id
if ($id) {
$new_id = create_id();
$annotations_with_id[$id] = $new_id;
$new_annotations[$new_id] = array($value);
$new_data = $new_data . "{{annot:$comment|$category|$new_id}}";
}
# we found no id, push it on the stack to find its ending counterpart later
else {
$new_id = create_id();
$value["new_id"] = $new_id;
array_push($starts, $value);
$new_data = $new_data . "{{#annot:$comment|$category|$new_id}}";
}
}
elseif ($reg_params["end"][$index][0] === "end") {
$id = $reg_params["opts"][$index][0];
# we encountered an id: try to find its beginning
if ($id) {
if ( $annotations_with_id[$id] ) {
$new_id = $annotations_with_id[$id];
array_push($new_annotations[ $new_id ], $reg_params["end"][$index]);
$text_start = $new_annotations[ $new_id ][0][1]
+ strlen($new_annotations[ $new_id ][0][0]);
$text_end = $reg_params["annotation"][$index][1];
array_push(
$new_annotations[ $new_id ],
substr($data, $text_start, $text_end-$text_start)
);
$new_data = $new_data . "{{#annotend:$new_id}}";
}
else {
die("found an annotend with an id which has no corresponding start");
}
}
# we found an ending, but have not enough starts: there seems to be a start-ending mismatch
elseif ( count($starts) == 0 ) {
die("starts and ends do not match!");
}
# we pop the last start from our stack to match it with its ending
else {
$start = array_pop($starts);
$text_start = $start[1] + strlen($start[0]);
$text_end = $reg_params["annotation"][$index][1];
$new_id = $start["new_id"];
$new_annotations[$new_id] = array(
$start,
$reg_params["end"][$index],
substr($data, $text_start, $text_end-$text_start)
);
$reg_params["annotation"][$index][2] = $new_id;
$reg_params["annotation"][$index]["new_annot"] = "{{#annotend:$new_id}}";
$new_data = $new_data . "{{#annotend:$new_id}}";
}
}
}
}
else {
$new_data = $data;
}
print $new_data;
#var_dump($reg_params["annotation"]);
#var_dump($new_annotations);
#foreach($reg_params["annotation"] as $value) {
# $end = $value[1];
# $new_data = $new_data . substr($data, $start, $end);
# $new_data = $new_data . $value["new_annot"];
# $start = $end + strlen($value[0]) + 1;
#}
$blas = preg_split("/[:]/", "child");
print("parent: " .$blas[0]. "\n");
print("child: " . $blas[1] . "\n");
exit;
die();
$data = <<<'EOT'
"Blalablaljka fjlsd {{#annot: here comes a long comment
which spans
over multiple lines | :there }}fjlsdkfj s {{#annot: Kommentar 1 | KatParent:KatChild | eins}}AAsdjf jslajflsfj {{#annot: Kommentar 2 | KatParent2:KatChild2 | zwei}}AAlsjdflksd jflksdj flaskfjZZ{{#annotend: eins}} asöljfölsajöljsdlfjsdlf jsldk fjlskjf asklf jZZ{{#annotend: zwei}} blödesiech sklfjsdf s{{#annotend:}}dlkjfsldjfslkdfj sdlkjfl ksdjfsld fjlsdkf j {{annotend}}
EOT;
#preg_match_all('/(?<all>{{#annot:(?<comment>.*?)|(?<group>.*)|(?<id>)}})/', $data, $matches, PREG_OFFSET_CAPTURE);
......
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