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

added conversion from category_ids to category_names and back

parent 985e4799
......@@ -295,6 +295,7 @@ EOT;
- 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 ){
#die("3: onPageContentSave");
/*
0. manage all categories (add/delete them)
1. get all existing annotations which exist on that page.
......@@ -321,8 +322,10 @@ EOT;
# get all annotations that exist in the current wikitext
$annotations = self::parse_annotations($dbw, $data, $wikiPage);
# keep the annotations for saving later (onPageContentSaveComplete)
self::$annotations = $annotations;
# replace the changed content
# replace the content and save it
$content = new WikitextContent( $data );
}
}
......@@ -331,7 +334,7 @@ EOT;
* Occurs after the save page request has been processed.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete
*
* All new annotations are then saved or updated. Annotations which are
* save all new or updated annotations. Annotations which are
* no longer present are being deleted.
*/
public static function onPageContentSaveComplete($wikiPage, $user, $content, $summary,
......@@ -383,9 +386,7 @@ EOT;
if ( $annotation['categories'] ) {
#die(var_dump( $annotation['categories'] ));
$seen_acs = array();
foreach ( $annotation['categories'] as $cat ){
$category = self::get_category($dbw, $cat);
foreach ( $annotation['categories'] as $category ){
# assign all found categories to this annotation
if(! is_null($category)) {
self::insert_annotation_category($dbw, $wikiPage, $annotation_id, $category->id);
......@@ -415,8 +416,6 @@ EOT;
);
}
$seen_bookmarks[] = $bookmark;
}
......@@ -634,6 +633,26 @@ EOT;
return $row;
}
public static function get_category_and_parent_for_id($dbr, $id) {
$row = $dbr->selectRow(
array(c=>'yata_category', pc=>'yata_category'),
array(
id => 'c.id',
name => 'c.name',
description => 'c.description',
parent_name => 'pc.name',
parent_id => 'pc.id'
),
array(
'c.id' => $id
),
__METHOD__,
array(),
array('pc' => array('LEFT JOIN', array('c.parent_id = pc.id') ) )
);
return $row;
}
public static function get_child_categories($dbr, $category) {
$rows = $dbr->select(
array('yata_category'),
......@@ -795,7 +814,7 @@ EOT;
$new_data = $new_data . substr($data, $start_loc, $end_loc-$start_loc);
$start_loc = $value[1] + strlen($value[0]);
# we encountered an annotation start
# we encountered an annotation START
if ($reg_params["end"][$index][0] === "") {
# split comment|$category|$bookmark by vertical bar
list($comment, $category, $bookmark) = preg_split(
......@@ -809,7 +828,17 @@ EOT;
# parent_category/child_category
# /top_category
# top_category
$categories = preg_split("/\s*\,\s*/", $category, -1, PREG_SPLIT_NO_EMPTY);
$cat_strs = preg_split("/\s*\,\s*/", $category, -1, PREG_SPLIT_NO_EMPTY);
$cats = array();
foreach($cat_strs as $cat_str) {
$cat = self::get_category($dbw, $cat_str);
if ($cat) {
array_push($cats, $cat);
}
else {
die("could not find category: $cat_str");
}
}
# we encountered an id: replace it with a unique one,
# if it doesn't exist yet.
$new_bookmark = 0;
......@@ -827,7 +856,7 @@ EOT;
$annotations[$new_bookmark] = array();
$annotations[$new_bookmark]["start_char"] = $value[1] + strlen($value[0]);
$annotations[$new_bookmark]["is_new"] = true;
$annotations[$new_bookmark]["categories"] = $categories;
$annotations[$new_bookmark]["categories"] = $cats;
$annotations[$new_bookmark]["comment"] = $comment;
}
# we found no id, push it on the stack to find its
......@@ -835,11 +864,19 @@ EOT;
else {
$new_bookmark = self::create_bookmark();
$value["new_bookmark"] = $new_bookmark;
$value["categories"] = $categories;
$value["categories"] = $cats;
$value["comment"] = $comment;
array_push($starts, $value);
}
$new_data = $new_data . "{{#annot:$comment|$category|$new_bookmark}}";
# replace category names with their corresponding id's
$new_category = join(',',
array_map(
function($row) { return $row->id; },
$cats
)
);
$new_data = $new_data . "{{#annot:$comment|$new_category|$new_bookmark}}";
}
# we encountered an annotation ending
......@@ -910,4 +947,69 @@ EOT;
$updater->addExtensionTable( 'yata_annotation_error', __DIR__ . '/sql/yata_annotation_error.sql');
}
// called right after «Save Changes»
// when an error is present, returns to edit page and prints the error
// used to check whether the syntax of the annotations is correct
public static function onEditFilter($editor, $text, $section, &$error, $summary) {
#$error = "gaht's eigentli no?";
#die("1: onEditFilter");
}
// this is called after «Save changes»
// and after onEditFilter
public static function onArticlePrepareTextForEdit(&$wikiPage, &$popts) {
#die("2: onArticlePrepareTextForEdit");
}
// first hook called when editing a page
// even before any checks whether a user is allowed to
public static function onAlternateEdit(&$editPage) {
# die("onAlternateEdit");
}
// transforms all categories from their internal ID to a text.
// This allows us to change the category names later without having
// to change all source code where this category occurs.
public static function onEditFormInitialText(&$editPage) {
$data = $editPage->textbox1;
$data = preg_replace_callback(
'/(?:{{#annot:(?P<params>.*?)}})/s',
'self::replace_id_with_category',
$data
);
// assign the new content
$editPage->textbox1 = $data;
}
// preload edit forms with some content
public static function onEditFormPreloadText(&$text, &$title) {
#die("onEditFormPreloadText: $text");
}
// callback function for onEditFormInitialText hook
// all category_id's are replaced with the category names themselves
// using the parent_category/child_category naming scheme.
private static function replace_id_with_category($matches) {
$dbw = wfGetDB( DB_MASTER );
list($comment, $cat, $bookmark) = preg_split('/\s*\|\s*/', $matches["params"]);
if (!$cat) {
return $matches[0];
}
# TODO: check whether category is numeric
$categories = preg_split('/\s*,\s*/', $cat);
$cat_and_parents = array();
foreach($categories as $category) {
$cat_and_parent = self::get_category_and_parent_for_id($dbw, $category);
if ($cat_and_parent) {
array_push($cat_and_parents, $cat_and_parent->parent_name .'/'. $cat_and_parent->name);
}
}
$annot = "{{#annot: " . $comment." | ";
$annot.= join(', ', $cat_and_parents);
$annot.= " | $bookmark}}";
return $annot;
}
}
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