Matthias Mullie has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/382514 )

Change subject: Add page_prop entry for uploads, linking to MediaInfo entity
......................................................................

Add page_prop entry for uploads, linking to MediaInfo entity

Bug: T177023
Change-Id: Ieae0fb31c40959f5e903eb636b64df2f5a9752a1
---
M extension.json
A maintenance/CreatePageProps.php
M src/WikibaseMediaInfoHooks.php
3 files changed, 168 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseMediaInfo 
refs/changes/14/382514/1

diff --git a/extension.json b/extension.json
index ccf7452..63aa794 100644
--- a/extension.json
+++ b/extension.json
@@ -41,6 +41,12 @@
                ],
                "ImagePageAfterImageLinks": [
                        
"Wikibase\\MediaInfo\\WikibaseMediaInfoHooks::onImagePageAfterImageLinks"
+               ],
+               "LoadExtensionSchemaUpdates": [
+                       
"Wikibase\\MediaInfo\\WikibaseMediaInfoHooks::onLoadExtensionSchemaUpdates"
+               ],
+               "FileUpload": [
+                       
"Wikibase\\MediaInfo\\WikibaseMediaInfoHooks::onFileUpload"
                ]
        },
        "MessagesDirs": {
diff --git a/maintenance/CreatePageProps.php b/maintenance/CreatePageProps.php
new file mode 100644
index 0000000..dc0412a
--- /dev/null
+++ b/maintenance/CreatePageProps.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace Wikibase\MediaInfo;
+
+use BatchRowIterator;
+use BatchRowUpdate;
+use BatchRowWriter;
+use LoggedUpdateMaintenance;
+use MediaWiki\MediaWikiServices;
+use RowUpdateGenerator;
+use Wikibase\MediaInfo\DataModel\MediaInfo;
+use Wikibase\Repo\WikibaseRepo;
+
+$basePath = getenv( 'MW_INSTALL_PATH' ) !== false
+       ? getenv( 'MW_INSTALL_PATH' )
+       : __DIR__ . '/../../..';
+
+require_once $basePath . '/maintenance/Maintenance.php';
+require_once $basePath . '/includes/utils/BatchRowWriter.php';
+require_once $basePath . '/includes/utils/RowUpdateGenerator.php';
+
+/**
+ * Maintenance script for populating page_props mediainfo_item for existing 
media files.
+ */
+class CreatePageProps extends LoggedUpdateMaintenance {
+
+       public function __construct() {
+               parent::__construct();
+
+               $this->addDescription( 'Populates page_props mediainfo_item for 
existing media files' );
+
+               $this->setBatchSize( 300 );
+       }
+
+       protected function getUpdateKey() {
+               return __CLASS__;
+       }
+
+       protected function doDBUpdates() {
+               $dbr = wfGetDB( DB_REPLICA );
+               $dbw = wfGetDB( DB_MASTER );
+
+               // find all pages in NS_FILE that don't have a 
page_props.pp_propname='mediainfo_item' yet
+               $iterator = new BatchRowIterator(
+                       $dbr,
+                       [ 'page', 'page_props' ],
+                       'page_id',
+                       $this->mBatchSize
+               );
+               $iterator->setFetchColumns( [ 'page_id' ] );
+               $iterator->addConditions( [
+                       'page_namespace' => NS_FILE,
+                       'pp_propname' => null,
+               ] );
+               $iterator->addJoinConditions( [
+                       'page_props' => [
+                               'LEFT JOIN',
+                               [
+                                       'pp_page = page_id',
+                                       'pp_propname' => 'mediainfo_item',
+                               ]
+                       ]
+               ]);
+
+               $updater = new BatchRowUpdate(
+                       $iterator,
+                       new PagePropsUpdateWriter( $dbw, 'page_props' ),
+                       new PagePropsUpdateGenerator()
+               );
+               $updater->execute();
+
+               return true;
+       }
+}
+
+class PagePropsUpdateGenerator implements RowUpdateGenerator {
+       public function update( $row ) {
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+               $entityId = 
$wikibaseRepo->getEntityIdComposer()->composeEntityId(
+                       '',
+                       MediaInfo::ENTITY_TYPE,
+                       $row->page_id
+               );
+
+               return [
+                       'pp_page' => $row->page_id,
+                       'pp_propname' => 'mediainfo_item',
+                       'pp_value' => $entityId->getLocalPart(),
+               ];
+       }
+}
+
+class PagePropsUpdateWriter extends BatchRowWriter {
+       public function write( array $updates ) {
+               $lbFactory = 
MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+
+               $updates = array_column( $updates, 'changes' );
+               $this->db->insert(
+                       $this->table,
+                       $updates,
+                       __METHOD__
+               );
+
+               $lbFactory->waitForReplication();
+       }
+}
+
+$maintClass = CreatePageProps::class;
+require_once RUN_MAINTENANCE_IF_MAIN;
diff --git a/src/WikibaseMediaInfoHooks.php b/src/WikibaseMediaInfoHooks.php
index 18100fc..12753a2 100644
--- a/src/WikibaseMediaInfoHooks.php
+++ b/src/WikibaseMediaInfoHooks.php
@@ -2,6 +2,9 @@
 
 namespace Wikibase\MediaInfo;
 
+use DatabaseUpdater;
+use DeferredUpdates;
+use File;
 use ImagePage;
 use MediaWiki\MediaWikiServices;
 use Wikibase\MediaInfo\DataModel\MediaInfo;
@@ -14,7 +17,6 @@
  * @author Bene* < [email protected] >
  */
 class WikibaseMediaInfoHooks {
-
        /**
         * Hook to register the MediaInfo entity namespaces for 
EntityNamespaceLookup.
         *
@@ -65,4 +67,54 @@
                $html .= '<h2>' . $linkHtml . '</h2>';
        }
 
+       /**
+        * Schema changes.
+        *
+        * @param DatabaseUpdater $updater
+        */
+       public static function onLoadExtensionSchemaUpdates( DatabaseUpdater 
$updater ) {
+               require_once __DIR__.'/../maintenance/CreatePageProps.php';
+               $updater->addPostDatabaseUpdateMaintenance( 
'Wikibase\\MediaInfo\\CreatePageProps' );
+       }
+
+       /**
+        * After file upload, insert a page_props entry referring to the 
MediaInfo entity id.
+        * Hook: FileUpload
+        *
+        * @param File $file
+        */
+       public static function onFileUpload( File $file ) {
+               // wrap creation of the page_props entry inside a deferred 
update or it'd
+               // get blasted away right after having been created as part of 
finishing
+               // up the upload process
+               DeferredUpdates::addCallableUpdate(
+                       function () use ( $file ) {
+                               $title = $file->getTitle();
+                               if ( $title === null ) {
+                                       return;
+                               }
+                               $pageId = $title->getArticleID();
+
+                               $wikibaseRepo = 
WikibaseRepo::getDefaultInstance();
+                               $entityId = 
$wikibaseRepo->getEntityIdComposer()->composeEntityId(
+                                       '',
+                                       MediaInfo::ENTITY_TYPE,
+                                       $pageId
+                               );
+
+                               $file->getRepo()->getMasterDB()->insert(
+                                       'page_props',
+                                       [
+                                               'pp_page' => $pageId,
+                                               'pp_propname' => 
'mediainfo_item',
+                                               'pp_value' => 
$entityId->getLocalPart(),
+                                       ],
+                                       __METHOD__
+                               );
+                       },
+                       DeferredUpdates::POSTSEND,
+                       wfGetDB( DB_MASTER )
+               );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/382514
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieae0fb31c40959f5e903eb636b64df2f5a9752a1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseMediaInfo
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to