Ottomata has uploaded a new change for review. ( 
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, 98 insertions(+), 48 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventBus 
refs/changes/03/360703/1

diff --git a/EventBus.hooks.php b/EventBus.hooks.php
index 45c75e7..b6cf86b 100644
--- a/EventBus.hooks.php
+++ b/EventBus.hooks.php
@@ -51,55 +51,8 @@
         * @param integer $flags
         */
        public static function onRevisionInsertComplete( $revision, $data, 
$flags ) {
-               global $wgDBname;
                $events = [];
-
-               // 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 ) && $parentId !== 0 ) {
-                       $attrs['rev_parent_id'] = $parentId;
-               }
+               $attrs = EventBus::createRevisionAttrs( $revision );
 
                $events[] = EventBus::createEvent(
                        EventBus::getArticleURL( $revision->getTitle() ),
@@ -431,6 +384,42 @@
        }
 
        /**
+        * 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 = [];
+               $attrs = EventBus::createRevisionAttrs( $revision );
+
+               $events[] = EventBus::createEvent(
+                       EventBus::getArticleURL( $revision->getTitle() ),
+                       'mediawiki.page-create',
+                       $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..54f2bcc 100644
--- a/EventBus.php
+++ b/EventBus.php
@@ -175,6 +175,64 @@
        }
 
        /**
+        * 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: newchange
Gerrit-Change-Id: Ib8dc8c9957c806681d27a6f4cf5b8b812260513a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventBus
Gerrit-Branch: master
Gerrit-Owner: Ottomata <ao...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to