Commit 3e701523 authored by w3_mispar's avatar w3_mispar

fixed $dbw->insertId problem by re-fetching inserted annotatin

parent b012eabc
......@@ -26,7 +26,7 @@ class YATA {
// Render the output of {{#annot: comment | category | id}}.
//
public static function renderStartAnnot( $parser, $comment='', $category='', $id='' ) {
return "<span id=$id style='background: rgba(0,0,0,0.05)' title='$comment'>";
return "<span id=$id title='$comment'>";
}
//
......@@ -337,17 +337,19 @@ EOT;
}
/*
Hook on when page is saved
we change the content where necessary, e.g.
- change {{#annotend}} to {{#annotend:}}
- change {{annotlist}} or {{#annotlist}} to {{#annotlist:}}
*/
public static function onPageContentSave(WikiPage &$wikiPage, User &$user, Content &$content, $summary, $isMinor, $isWatch, $section, $flags, Status &$status ){
/*
1. manage all categories (see manage_categories)
onPageContentSave: Hook on when page is saved
we first replace syntax sugar where necessary, e.g.
- change {{#annotend}} to {{#annotend:}}
- change {{annotlist}} or {{#annotlist}} to {{#annotlist:}}
next steps:
1. add/delete/update categories (see manage_categories)
2. parse the annotations (see parse_annotations)
3. replace the wiki_text
The annotations are saved in the next hook: onPageContentSaveComplete (see below)
*/
......@@ -395,14 +397,16 @@ EOT;
This function is called after the save page request has been processed.
@see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete
save all new or updated annotations. Annotations which are
no longer present are being deleted.
- save all new or update chaned annotations
- Annotations which are no longer present should be deleted.
- all entries in annotation_category which are no longer used should be deleted.
*/
public static function onPageContentSaveComplete($wikiPage, $user, $content, $summary,
$isMinor, $isWatch, $section, $flags, $revision, $status, $baseRevId ) {
$dbw = wfGetDB( DB_MASTER );
$seen_ids = array();
$seen_bookmarks = array();
$seen_annotation_ids = array();
$i = 0;
foreach ( self::$annotations as $bookmark => $annotation ){
$annotation_id = 0;
$ex_annot = self::get_annotation($dbw, $wikiPage, $bookmark);
......@@ -431,21 +435,42 @@ EOT;
# new annotation found: insert annotation
else {
# write annotation
$dbw->insert(
'yata_annotation',
array(
page_id => $wikiPage->getId(),
comment => $annotation['comment'],
wiki_text => $annotation['wiki_text'],
bookmark => $bookmark,
user_id => $user->getId(),
insert_date => $dbw->timestamp(),
modified_date => $dbw->timestamp(),
)
);
$annotation_id = $dbw->insertId();
try {
$data = array(
page_id => $wikiPage->getId(),
comment => $annotation['comment'],
wiki_text => $annotation['wiki_text'],
bookmark => $bookmark,
user_id => $user->getId(),
insert_date => $dbw->timestamp(),
modified_date => $dbw->timestamp(),
);
$dbw->insert(
'yata_annotation',
array(
page_id => $wikiPage->getId(),
comment => $annotation['comment'],
wiki_text => $annotation['wiki_text'],
bookmark => $bookmark,
user_id => $user->getId(),
insert_date => $dbw->timestamp(),
modified_date => $dbw->timestamp(),
)
);
$ex_annot = self::get_annotation($dbw, $wikiPage, $bookmark);
$annotation_id = $ex_annot->id;
}
catch (Exception $e) {
print($e->getMessage());
die;
}
}
array_push($seen_ids, $annotation_id);
# keep track of all the annotations we have seen on this page,
# to delete later all the annotations which have been removed from this page.
array_push($seen_bookmarks, $bookmark);
array_push($seen_annotation_ids, $annotation_id);
# insert / update the annotation categories
if ( $annotation['categories'] ) {
......@@ -480,36 +505,32 @@ EOT;
);
}
}
# find all all annotations on that page...
$where = [
"page_id" => $wikiPage->getId()
];
if ($seen_ids) {
$where[] = 'id NOT IN(' .$dbw->makeList($seen_ids). ')';
#...which are no longer present
if ($seen_annotation_ids) {
$where[] = 'id NOT IN(' .$dbw->makeList($seen_annotation_ids). ')';
}
# find all all annotations which are no longer present
$annotations_to_delete = $dbw->select(
'yata_annotation',
array( 'id' ),
$where
);
# delete all annotations which are no longer present
foreach( $annotations_to_delete as $annotation) {
try {
$dbw->delete(
'yata_annotation',
array(
'id' => $annotation->id
)
);
}
catch (Exception $e) {
print($e->getMessage());
die;
}
$dbw->delete(
'yata_annotation',
array(
'id' => $annotation->id
)
);
# delete all categories assigned to annotations
# which do not exist anymore
......@@ -519,16 +540,10 @@ EOT;
annotation_id => $annotation->id
];
try {
$dbw->delete(
'yata_annotation_category',
$delete_where
);
}
catch (Exception $e) {
print($e->getMessage());
die;
}
$dbw->delete(
'yata_annotation_category',
$delete_where
);
}
}
......@@ -736,8 +751,14 @@ EOT;
}
$categories = $dbr->select(
'yata_category',
array( 'id', 'name', 'hashtag', 'description', 'parent_id' ),
array('c' => 'yata_category'),
array(
'c.id',
'c.name',
'c.hashtag',
'c.description',
'c.parent_id'
),
$where,
__METHOD__,
$order_by
......
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