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;
class YATA {
public static $annotations = "";
public static $page_annotations = "";
//
// Register any render callbacks with the parser
//
......@@ -291,7 +292,7 @@ EOT;
// Render the output of {{#annotcat: list}}.
//
public static function annotation_categories( $parser, $method, $start_with=null ) {
if (! $method == "list") {
if ($method != "list") {
return "";
}
$dbr = wfGetDB( DB_MASTER );
......@@ -344,10 +345,10 @@ EOT;
}
//
// Rendering of {{#annotpage}}
// Rendering of {{#annotpage: list}}
//
public static function annotation_page( $parser, $method ) {
if (! $method == "list") {
if ($method != "list") {
return "";
}
$title = $parser->getTitle();
......@@ -425,6 +426,7 @@ EOT;
# keep the annotations for saving later (onPageContentSaveComplete)
self::$annotations = $annotations;
self::$page_annotations = $page_annotations;
# 3. replace the content and save it
$content = new WikitextContent( $data );
......@@ -456,7 +458,6 @@ EOT;
# existing annotation found: update wiki_text, comment
if ($ex_annot) {
$annotation_id = $ex_annot->id;
$ex_annot = self::get_annotation($dbw, $wikiPage, $bookmark);
if ($ex_annot->wiki_text != $annotation["wiki_text"]
or $ex_annot->comment != $annotation["comment"] ) {
$dbw->update(
......@@ -587,6 +588,9 @@ EOT;
$delete_where
);
}
foreach (self::$page_annotations as $page_annotation){
}
}
public static function add_category($dbw, $category_name, $hashtag, $description, $parent) {
......@@ -1033,6 +1037,7 @@ EOT;
# or it was automatically generated.
$annotations_with_id = array();
$annotations = array();
$page_annotations = array();
# as we crawl through the found annotations and give them new ids,
# we also recreate the wikitext containing all the new ids.
......@@ -1049,72 +1054,28 @@ EOT;
$start_loc = $value[1] + strlen($value[0]);
# we encountered an annotation ending
if ($reg_params["page"][$index][0] === "end") {
# split comment|$category|$bookmark by vertical bar
list($comment, $category, $bookmark) = preg_split(
'/\s*\|\s*/',
$reg_params["opts"][$index][0]
);
# 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
# top_category
$cat_strs = preg_split("/\s*\,\s*/", $category, -1, PREG_SPLIT_NO_EMPTY);
$cats = array();
foreach($cat_strs as $cat_str) {
# we got a category id, which consists only of numbers
if ( preg_match("/^\s*\d*\s*$/", $cat_str) ) {
$cat = self::get_category_and_parent_for_id($dbw, $cat_str);
}
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();
}
# 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}}";
if ($reg_params["page"][$index][0] === "page") {
//print($reg_params["page"][$index][0]);
$opts = $reg_params["opts"][$index][0];
// Match key-value pairs.
// see https://regex101.com/r/wK0eD2/1 for detailed explanations
// Will match key and values that are:
// within single quotes ' \'.*?(?<!\\\\)\'
// within double quotes " \".*?(?<!\\\\)\"
// with no quotes at all (and no spaces): [^\s]+
$re =
'/(?P<key>\'.*?(?<!\\\\)\'|\".*?(?<!\\\\)\"|[^\s]+)'
.'\s*?=\s*?'
.'(?P<val>\'.*?(?<!\\\\)\'|\".*?(?<!\\\\)\"|[^\s]+)/';
preg_match_all($re, $opts, $matches, PREG_SET_ORDER, 0);
foreach($matches as $match) {
# trim keys and values: remove any spaces and quotes
$key = trim($match["key"], " '\"");
$val = trim($match["val"], " '\"");
$page_annotations[$key] = $val;
}
//var_dump($page_annotations);
}
# 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