Kaldari has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/306314

Change subject: [WIP] Refactoring job queue logic for PageAssessments
......................................................................

[WIP] Refactoring job queue logic for PageAssessments

Change-Id: If3711a2a2c89e970c6476439c7bcda4221ff41eb
---
M PageAssessments.hooks.php
M PageAssessmentsBody.php
M PageAssessmentsSaveJob.php
3 files changed, 85 insertions(+), 159 deletions(-)


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

diff --git a/PageAssessments.hooks.php b/PageAssessments.hooks.php
index c516b22..7447745 100644
--- a/PageAssessments.hooks.php
+++ b/PageAssessments.hooks.php
@@ -48,6 +48,23 @@
        }
 
        /**
+        * Modify list of DataUpdates to perform when page content is modified
+        * @param Title $title Title of the page that was edited
+        * @param Content $old Content object representing the page's content 
before the edit
+        * @param bool $recursive Whether DataUpdates should trigger recursive 
updates
+        * @param ParserOutput $parserOutput The rendered version of the page 
after the edit
+        * @param array $updates A list of DataUpdate objects
+        */
+       public static function onSecondaryDataUpdates( Title $title, $old, 
$recursive, ParserOutput $parserOutput, &$updates ) {
+               if ( $parserOutput->getExtensionData( 
'ext-pageassessment-assessmentdata' ) !== null ) {
+                       $assessmentData = $parserOutput->getExtensionData( 
'ext-pageassessment-assessmentdata' );
+                       // Get Title object for the subject page for the talk 
page
+                       $subjectTitle = $title->getSubjectPage();
+                       $updates[] = new PageAssessmentsUpdate( $subjectTitle, 
$assessmentData );
+               }
+       }
+
+       /**
         * Run database updates
         * @param DatabaseUpdater $updater DatabaseUpdater object
         */
@@ -58,10 +75,16 @@
        }
 
        /**
-        * Delete assessment records when page is deleted
+        * Delete all records for a given page when page is deleted
+        * Note: We don't take care of undeletions explicitly, the records are 
restored
+        * when the page is parsed again.
         */
        public static function onArticleDeleteComplete( &$article, &$user, 
$reason, $id, $content = null, $logEntry ) {
-               PageAssessmentsBody::deleteRecordsForPage( $id );
+               $dbw = wfGetDB( DB_MASTER );
+               $conds = array(
+                       'pa_page_id' => $id,
+               );
+               $dbw->delete( 'page_assessments', $conds, __METHOD__ );
        }
 
 }
diff --git a/PageAssessmentsBody.php b/PageAssessmentsBody.php
index 1a75b4d..db45eb1 100644
--- a/PageAssessmentsBody.php
+++ b/PageAssessmentsBody.php
@@ -27,63 +27,6 @@
        protected static $projectNames = [];
 
        /**
-        * Driver function
-        * @param object $titleObj Title class object
-        * @param array $assessmentData Data for all assessments compiled
-        */
-       public static function execute( $titleObj, $assessmentData ) {
-               $pageId = $titleObj->getArticleID();
-               $revisionId = $titleObj->getLatestRevID();
-               // Compile a list of projects to find out which ones to be 
deleted afterwards
-               $projects = array();
-               foreach ( $assessmentData as $parserData ) {
-                       // For each project, get the corresponding ID from 
page_assessments_projects table
-                       $projectId = PageAssessmentsBody::getProjectId( 
$parserData[0] );
-                       if ( $projectId === false ) {
-                               $projectId = 
PageAssessmentsBody::insertProject( $parserData[0] );
-                       }
-                       $projects[$parserData[0]] = $projectId;
-               }
-               $projectsInDb = PageAssessmentsBody::getAllProjects( $pageId );
-               $toInsert = array_diff( $projects, $projectsInDb );
-               $toDelete = array_diff( $projectsInDb, $projects );
-               $toUpdate = array_intersect( $projects, $projectsInDb );
-               $jobs = array();
-               foreach ( $assessmentData as $parserData ) {
-                       $projectId = $projects[$parserData[0]];
-                       if ( $projectId ) {
-                               $class = $parserData[1];
-                               $importance = $parserData[2];
-                               $values = array(
-                                       'pa_page_id' => $pageId,
-                                       'pa_project_id' => $projectId,
-                                       'pa_class' => $class,
-                                       'pa_importance' => $importance,
-                                       'pa_page_revision' => $revisionId
-                               );
-                               if ( in_array( $projectId, $toInsert ) ) {
-                                       $values['job_type'] = 'insert';
-                               } elseif ( in_array( $projectId, $toUpdate ) ) {
-                                       $values['job_type'] = 'update';
-                               }
-                               $jobs[] = new PageAssessmentsSaveJob( 
$titleObj, $values );
-                       }
-               }
-               // Add deletion jobs to job array
-               foreach ( $toDelete as $project ) {
-                       $values = array(
-                               'pa_page_id' => $pageId,
-                               'pa_project_id' => $project,
-                               'job_type' => 'delete'
-                       );
-                       $jobs[] = new PageAssessmentsSaveJob( $titleObj, 
$values );
-               }
-               JobQueueGroup::singleton()->push( $jobs );
-               return;
-       }
-
-
-       /**
         * Get name for the given wikiproject
         * @param integer $projectId The ID of the project
         * @return string|false The name of the project or false if not found
@@ -109,7 +52,6 @@
                return false;
        }
 
-
        /**
         * Get project ID for a give wikiproject title
         * @param string $project Project title
@@ -123,68 +65,6 @@
                        [ 'pap_project_title' => $project ]
                );
        }
-
-
-       /**
-        * Insert a new wikiproject into the projects table
-        * @param string $project Wikiproject title
-        * @return int Insert Id for new project
-        */
-       public static function insertProject( $project ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $values = array(
-                       'pap_project_title' => $project,
-                       'pap_project_id' => $dbw->nextSequenceValue( 
'pap_project_id_seq' )
-               );
-               $dbw->insert( 'page_assessments_projects', $values, __METHOD__ 
);
-               $id = $dbw->insertId();
-               return $id;
-       }
-
-
-       /**
-        * Update record in DB if there are new values
-        * @param array $values New values to be entered into the DB
-        * @return bool true
-        */
-       public static function updateRecord( $values ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               $conds = array(
-                       'pa_page_id' => $values['pa_page_id'],
-                       'pa_project_id' => $values['pa_project_id']
-               );
-               // Check if there are no updates to be done
-               $record = $dbr->select(
-                       'page_assessments',
-                       array( 'pa_class', 'pa_importance', 'pa_project_id', 
'pa_page_id' ),
-                       $conds
-               );
-               foreach ( $record as $row ) {
-                       if ( $row->pa_importance == $values['pa_importance'] &&
-                               $row->pa_class == $values['pa_class']
-                       ) {
-                               // Return if no updates
-                               return true;
-                       }
-               }
-               // Make updates if there are changes
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->update( 'page_assessments', $values, $conds, __METHOD__ );
-               return true;
-       }
-
-
-       /**
-        * Insert a new record in DB
-        * @param array $values New values to be entered into the DB
-        * @return bool true
-        */
-       public static function insertRecord( $values ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->insert( 'page_assessments', $values, __METHOD__ );
-               return true;
-       }
-
 
        /**
         * Get all records for give page
@@ -206,40 +86,6 @@
                }
                return $results;
        }
-
-
-       /**
-        * Delete a record from DB
-        * @param array $values Conditions for looking up records to delete
-        * @return bool true
-        */
-       public static function deleteRecord( $values ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $conds = array(
-                       'pa_page_id' => $values['pa_page_id'],
-                       'pa_project_id' => $values['pa_project_id']
-               );
-               $dbw->delete( 'page_assessments', $conds, __METHOD__ );
-               return true;
-       }
-
-
-       /**
-        * Delete all records for a given page when page is deleted
-        * Note: We don't take care of undeletions explicitly, the records are 
restored
-        * when the page is parsed again.
-        * @param int $id Page ID of deleted page
-        * @return bool true
-        */
-       public static function deleteRecordsForPage( $id ) {
-               $dbw = wfGetDB( DB_MASTER );
-               $conds = array(
-                       'pa_page_id' => $id,
-               );
-               $dbw->delete( 'page_assessments', $conds, __METHOD__ );
-               return true;
-       }
-
 
        /**
         * Function called on parser init
diff --git a/PageAssessmentsSaveJob.php b/PageAssessmentsSaveJob.php
index 76b9e80..5a61b33 100644
--- a/PageAssessmentsSaveJob.php
+++ b/PageAssessmentsSaveJob.php
@@ -29,7 +29,7 @@
                                'pa_importance' => 
$this->params['pa_importance'],
                                'pa_page_revision' => 
$this->params['pa_page_revision']
                        );
-                       PageAssessmentsBody::insertRecord( $values );
+                       self::insertRecord( $values );
                } elseif ( $jobType == 'update' ) {
                        // Compile the array to be inserted to the DB
                        $values = array(
@@ -39,15 +39,72 @@
                                'pa_importance' => 
$this->params['pa_importance'],
                                'pa_page_revision' => 
$this->params['pa_page_revision']
                        );
-                       PageAssessmentsBody::updateRecord( $values );
+                       self::updateRecord( $values );
                } elseif ( $jobType == 'delete' ) {
                        $values = array(
                                'pa_page_id' => $this->params['pa_page_id'],
                                'pa_project_id' => 
$this->params['pa_project_id']
                        );
-                       PageAssessmentsBody::deleteRecord( $values );
+                       self::deleteRecord( $values );
                }
                return true;
        }
 
+       /**
+        * Update record in DB if there are new values
+        * @param array $values New values to be entered into the DB
+        * @return bool true
+        */
+       public static function updateRecord( $values ) {
+               $dbr = wfGetDB( DB_SLAVE );
+               $conds = array(
+                       'pa_page_id' => $values['pa_page_id'],
+                       'pa_project_id' => $values['pa_project_id']
+               );
+               // Check if there are no updates to be done
+               $record = $dbr->select(
+                       'page_assessments',
+                       array( 'pa_class', 'pa_importance', 'pa_project_id', 
'pa_page_id' ),
+                       $conds
+               );
+               foreach ( $record as $row ) {
+                       if ( $row->pa_importance == $values['pa_importance'] &&
+                               $row->pa_class == $values['pa_class']
+                       ) {
+                               // Return if no updates
+                               return true;
+                       }
+               }
+               // Make updates if there are changes
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->update( 'page_assessments', $values, $conds, __METHOD__ );
+               return true;
+       }
+
+       /**
+        * Insert a new record in DB
+        * @param array $values New values to be entered into the DB
+        * @return bool true
+        */
+       public static function insertRecord( $values ) {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->insert( 'page_assessments', $values, __METHOD__ );
+               return true;
+       }
+
+       /**
+        * Delete a record from DB
+        * @param array $values Conditions for looking up records to delete
+        * @return bool true
+        */
+       public static function deleteRecord( $values ) {
+               $dbw = wfGetDB( DB_MASTER );
+               $conds = array(
+                       'pa_page_id' => $values['pa_page_id'],
+                       'pa_project_id' => $values['pa_project_id']
+               );
+               $dbw->delete( 'page_assessments', $conds, __METHOD__ );
+               return true;
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If3711a2a2c89e970c6476439c7bcda4221ff41eb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PageAssessments
Gerrit-Branch: master
Gerrit-Owner: Kaldari <rkald...@wikimedia.org>

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

Reply via email to