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

correct implementation of key-value pairs parsing of annotpage

parent cbf0f4df
...@@ -14,6 +14,7 @@ use SMW\ApplicationFactory; ...@@ -14,6 +14,7 @@ use SMW\ApplicationFactory;
class YATA { class YATA {
public static $annotations = ""; public static $annotations = "";
public static $page_annotations = "";
// //
// Register any render callbacks with the parser // Register any render callbacks with the parser
// //
...@@ -291,7 +292,7 @@ EOT; ...@@ -291,7 +292,7 @@ EOT;
// Render the output of {{#annotcat: list}}. // Render the output of {{#annotcat: list}}.
// //
public static function annotation_categories( $parser, $method, $start_with=null ) { public static function annotation_categories( $parser, $method, $start_with=null ) {
if (! $method == "list") { if ($method != "list") {
return ""; return "";
} }
$dbr = wfGetDB( DB_MASTER ); $dbr = wfGetDB( DB_MASTER );
...@@ -344,10 +345,10 @@ EOT; ...@@ -344,10 +345,10 @@ EOT;
} }
// //
// Rendering of {{#annotpage}} // Rendering of {{#annotpage: list}}
// //
public static function annotation_page( $parser, $method ) { public static function annotation_page( $parser, $method ) {
if (! $method == "list") { if ($method != "list") {
return ""; return "";
} }
$title = $parser->getTitle(); $title = $parser->getTitle();
...@@ -425,6 +426,7 @@ EOT; ...@@ -425,6 +426,7 @@ EOT;
# keep the annotations for saving later (onPageContentSaveComplete) # keep the annotations for saving later (onPageContentSaveComplete)
self::$annotations = $annotations; self::$annotations = $annotations;
self::$page_annotations = $page_annotations;
# 3. replace the content and save it # 3. replace the content and save it
$content = new WikitextContent( $data ); $content = new WikitextContent( $data );
...@@ -456,7 +458,6 @@ EOT; ...@@ -456,7 +458,6 @@ EOT;
# existing annotation found: update wiki_text, comment # existing annotation found: update wiki_text, comment
if ($ex_annot) { if ($ex_annot) {
$annotation_id = $ex_annot->id; $annotation_id = $ex_annot->id;
$ex_annot = self::get_annotation($dbw, $wikiPage, $bookmark);
if ($ex_annot->wiki_text != $annotation["wiki_text"] if ($ex_annot->wiki_text != $annotation["wiki_text"]
or $ex_annot->comment != $annotation["comment"] ) { or $ex_annot->comment != $annotation["comment"] ) {
$dbw->update( $dbw->update(
...@@ -587,6 +588,9 @@ EOT; ...@@ -587,6 +588,9 @@ EOT;
$delete_where $delete_where
); );
} }
foreach (self::$page_annotations as $page_annotation){
}
} }
public static function add_category($dbw, $category_name, $hashtag, $description, $parent) { public static function add_category($dbw, $category_name, $hashtag, $description, $parent) {
...@@ -1033,6 +1037,7 @@ EOT; ...@@ -1033,6 +1037,7 @@ EOT;
# or it was automatically generated. # or it was automatically generated.
$annotations_with_id = array(); $annotations_with_id = array();
$annotations = array(); $annotations = array();
$page_annotations = array();
# as we crawl through the found annotations and give them new ids, # as we crawl through the found annotations and give them new ids,
# we also recreate the wikitext containing all the new ids. # we also recreate the wikitext containing all the new ids.
...@@ -1049,72 +1054,28 @@ EOT; ...@@ -1049,72 +1054,28 @@ EOT;
$start_loc = $value[1] + strlen($value[0]); $start_loc = $value[1] + strlen($value[0]);
# we encountered an annotation ending # we encountered an annotation ending
if ($reg_params["page"][$index][0] === "end") { if ($reg_params["page"][$index][0] === "page") {
# split comment|$category|$bookmark by vertical bar //print($reg_params["page"][$index][0]);
list($comment, $category, $bookmark) = preg_split( $opts = $reg_params["opts"][$index][0];
'/\s*\|\s*/',
$reg_params["opts"][$index][0] // Match key-value pairs.
); // see https://regex101.com/r/wK0eD2/1 for detailed explanations
# more than one category can be split using a comma: // Will match key and values that are:
# category1, category2 // within single quotes ' \'.*?(?<!\\\\)\'
# categories are by default hierarchically organized and must appear // within double quotes " \".*?(?<!\\\\)\"
# in following formats: // with no quotes at all (and no spaces): [^\s]+
# parent_category/child_category $re =
# /top_category '/(?P<key>\'.*?(?<!\\\\)\'|\".*?(?<!\\\\)\"|[^\s]+)'
# top_category .'\s*?=\s*?'
$cat_strs = preg_split("/\s*\,\s*/", $category, -1, PREG_SPLIT_NO_EMPTY); .'(?P<val>\'.*?(?<!\\\\)\'|\".*?(?<!\\\\)\"|[^\s]+)/';
$cats = array(); preg_match_all($re, $opts, $matches, PREG_SET_ORDER, 0);
foreach($cat_strs as $cat_str) { foreach($matches as $match) {
# we got a category id, which consists only of numbers # trim keys and values: remove any spaces and quotes
if ( preg_match("/^\s*\d*\s*$/", $cat_str) ) { $key = trim($match["key"], " '\"");
$cat = self::get_category_and_parent_for_id($dbw, $cat_str); $val = trim($match["val"], " '\"");
} $page_annotations[$key] = $val;
else {
$cat = self::get_category($dbw, $cat_str);
}
if ($cat) {
array_push($cats, $cat);
}
else {
throw new Exception("no such category: $cat_str");
}
}
# we encountered an id: replace it with a unique one,
# if it doesn't exist yet.
$new_bookmark = 0;
if ($bookmark) {
if ( self::check_annotation_bookmark_exist($dbw, $wikiPage, $bookmark))
{
# it is an existing annotation, we keep the id
$new_bookmark = $bookmark;
}
else {
# we replace the manually entered id by a random one
$new_bookmark = self::create_bookmark();
}
$annotations_with_id[$bookmark] = $new_bookmark;
$annotations[$new_bookmark] = array();
$annotations[$new_bookmark]["annotpage"] = true;
$annotations[$new_bookmark]["wiki_text"] = "";
$annotations[$new_bookmark]["is_new"] = true;
$annotations[$new_bookmark]["categories"] = $cats;
$annotations[$new_bookmark]["comment"] = $comment;
}
# we found no bookmark, create a new bookmark.
else {
$new_bookmark = self::create_bookmark();
} }
//var_dump($page_annotations);
# replace category names with their corresponding id's
$new_category = join(',',
array_map(
function($row) { return $row->id; },
$cats
)
);
$new_data = $new_data . "{{#annotpage:$comment|$new_category|$new_bookmark}}";
} }
# we encountered an annotation ending # we encountered an annotation ending
......
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