Ottomata has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/360703 )
Change subject: Emit mediawiki.page-create event on PageContentInsertComplete
......................................................................
Emit mediawiki.page-create event on PageContentInsertComplete
This re-uses the mediawiki/revision/create schema. As such, I've factored out
the code that creates the revision create schema attributes into a static
function in EventBus.php.
Along the way I noticed that we don't set rev_parent_id if it is == 0.
The rev_parent_id minimum of 1 was removed and set to 0 in e915290c.
So, this patch will now set rev_parent_id = 0 if it is set to that in the
Revision object.
Bug: T150369
Change-Id: Ib8dc8c9957c806681d27a6f4cf5b8b812260513a
---
M EventBus.hooks.php
M EventBus.php
M extension.json
3 files changed, 116 insertions(+), 46 deletions(-)
Approvals:
Ottomata: Looks good to me, approved
Ppchelko: Looks good to me, but someone else must approve
jenkins-bot: Verified
Kaldari: Looks good to me, but someone else must approve
diff --git a/EventBus.hooks.php b/EventBus.hooks.php
index 45c75e7..f7b376a 100644
--- a/EventBus.hooks.php
+++ b/EventBus.hooks.php
@@ -51,59 +51,22 @@
* @param integer $flags
*/
public static function onRevisionInsertComplete( $revision, $data,
$flags ) {
- global $wgDBname;
$events = [];
+ $topic = 'mediawiki.revision-create';
- // Create a mediawiki revision create event.
- $performer = User::newFromId( $revision->getUser() );
- $performer->loadFromId();
-
- $attrs = [
- // Common Mediawiki entity fields
- 'database' => $wgDBname,
- 'performer' => EventBus::createPerformerAttrs(
$performer ),
- 'comment' => $revision->getComment(),
-
- // revision entity fields
- 'page_id' => $revision->getPage(),
- 'page_title' =>
$revision->getTitle()->getPrefixedDBkey(),
- 'page_namespace' =>
$revision->getTitle()->getNamespace(),
- 'rev_id' => $revision->getId(),
- 'rev_timestamp' => wfTimestamp( TS_ISO_8601,
$revision->getTimestamp() ),
- 'rev_sha1' => $revision->getSha1(),
- 'rev_minor_edit' => $revision->isMinor(),
- 'rev_content_model' => $revision->getContentModel(),
- 'rev_content_format' => $revision->getContentModel(),
- ];
-
- // It is possible rev_len is not known. It's not a required
field,
- // so don't set it if it's NULL
- if ( !is_null( $revision->getSize() ) ) {
- $attrs['rev_len'] = $revision->getSize();
+ if ( is_null( $revision ) ) {
+ wfDebug(
+ __METHOD__ . ' new revision during
RevisionInsertComplete ' .
+ ' is null. Cannot create ' . $topic . ' event.'
+ );
+ return;
}
- // It is possible that the $revision object does not have any
content
- // at the time of RevisionInsertComplete. This might happen
during
- // a page restore, if the revision 'created' during the restore
- // has its content hidden.
- $content = $revision->getContent();
- if ( !is_null( $content ) ) {
- $attrs['page_is_redirect'] = $content->isRedirect();
- } else {
- $attrs['page_is_redirect'] = false;
- }
-
- // The parent_revision_id attribute is not required, but when
supplied
- // must have a minimum value of 1, so omit it entirely when
there is no
- // parent revision (i.e. page creation).
- $parentId = $revision->getParentId();
- if ( !is_null( $parentId ) && $parentId !== 0 ) {
- $attrs['rev_parent_id'] = $parentId;
- }
+ $attrs = EventBus::createRevisionAttrs( $revision );
$events[] = EventBus::createEvent(
EventBus::getArticleURL( $revision->getTitle() ),
- 'mediawiki.revision-create',
+ $topic,
$attrs
);
@@ -431,6 +394,53 @@
}
/**
+ * Occurs after the insert page request has been processed. This is a
page creation event.
+ * Since page creation is really just a special case of revision
create, this event
+ * re-uses the mediawiki/revision/create schema.
+ *
+ * @see
https://www.mediawiki.org/wiki/Manual:Hooks/PageContentInsertComplete
+ *
+ * @param WikiPage $article
+ * @param User $user
+ * @param Content $content
+ * @param string $summary
+ * @param boolean $isMinor
+ * @param boolean $isWatch
+ * @param $section Deprecated
+ * @param integer $flags
+ * @param {Revision|null} $revision
+ */
+ public static function onPageContentInsertComplete( $article, $user,
$content, $summary, $isMinor,
+ $isWatch, $section, $flags, $revision
+ ) {
+ $events = [];
+ $topic = 'mediawiki.page-create';
+
+ if ( is_null( $revision ) ) {
+ wfDebug(
+ __METHOD__ . ' new revision during
PageContentInsertComplete for page_id: ' .
+ $article->getId() . ' page_title: ' .
$article->getTitle() .
+ ' is null. Cannot create ' . $topic . ' event.'
+ );
+ return;
+ }
+
+ $attrs = EventBus::createRevisionAttrs( $revision );
+
+ $events[] = EventBus::createEvent(
+ EventBus::getArticleURL( $revision->getTitle() ),
+ $topic,
+ $attrs
+ );
+
+ DeferredUpdates::addCallableUpdate(
+ function() use ( $events ) {
+ EventBus::getInstance()->send( $events );
+ }
+ );
+ }
+
+ /**
* Occurs after the save page request has been processed.
*
* It's used to detect null edits and create 'resource_change' events
for purges.
diff --git a/EventBus.php b/EventBus.php
index 4276346..059d620 100644
--- a/EventBus.php
+++ b/EventBus.php
@@ -175,6 +175,63 @@
}
/**
+ * Given a Revision $revision, returns an array suitable for
+ * use in mediaiki/revision entity schemas.
+ */
+ public static function createRevisionAttrs( $revision ) {
+ global $wgDBname;
+
+ // Create a mediawiki revision create event.
+ $performer = User::newFromId( $revision->getUser() );
+ $performer->loadFromId();
+
+ $attrs = [
+ // Common Mediawiki entity fields
+ 'database' => $wgDBname,
+ 'performer' => EventBus::createPerformerAttrs(
$performer ),
+ 'comment' => $revision->getComment(),
+
+ // revision entity fields
+ 'page_id' => $revision->getPage(),
+ 'page_title' =>
$revision->getTitle()->getPrefixedDBkey(),
+ 'page_namespace' =>
$revision->getTitle()->getNamespace(),
+ 'rev_id' => $revision->getId(),
+ 'rev_timestamp' => wfTimestamp( TS_ISO_8601,
$revision->getTimestamp() ),
+ 'rev_sha1' => $revision->getSha1(),
+ 'rev_minor_edit' => $revision->isMinor(),
+ 'rev_content_model' => $revision->getContentModel(),
+ 'rev_content_format' => $revision->getContentModel(),
+ ];
+
+ // It is possible rev_len is not known. It's not a required
field,
+ // so don't set it if it's NULL
+ if ( !is_null( $revision->getSize() ) ) {
+ $attrs['rev_len'] = $revision->getSize();
+ }
+
+ // It is possible that the $revision object does not have any
content
+ // at the time of RevisionInsertComplete. This might happen
during
+ // a page restore, if the revision 'created' during the restore
+ // has its content hidden.
+ $content = $revision->getContent();
+ if ( !is_null( $content ) ) {
+ $attrs['page_is_redirect'] = $content->isRedirect();
+ } else {
+ $attrs['page_is_redirect'] = false;
+ }
+
+ // The parent_revision_id attribute is not required, but when
supplied
+ // must have a minimum value of 1, so omit it entirely when
there is no
+ // parent revision (i.e. page creation).
+ $parentId = $revision->getParentId();
+ if ( !is_null( $parentId ) ) {
+ $attrs['rev_parent_id'] = $parentId;
+ }
+
+ return $attrs;
+ }
+
+ /**
* Creates a full user page path
*
* @param string $userName userName
diff --git a/extension.json b/extension.json
index b865a38..0289d8f 100644
--- a/extension.json
+++ b/extension.json
@@ -43,6 +43,9 @@
"ArticleRevisionVisibilitySet": [
"EventBusHooks::onArticleRevisionVisibilitySet"
],
+ "PageContentInsertComplete": [
+ "EventBusHooks::onPageContentInsertComplete"
+ ],
"PageContentSaveComplete": [
"EventBusHooks::onPageContentSaveComplete"
],
--
To view, visit https://gerrit.wikimedia.org/r/360703
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib8dc8c9957c806681d27a6f4cf5b8b812260513a
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/EventBus
Gerrit-Branch: master
Gerrit-Owner: Ottomata <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: Mobrovac <[email protected]>
Gerrit-Reviewer: Ottomata <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits