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

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

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