Index: trunk/extensions/ArticleFeedback/populateAFRevisions.php |
— | — | @@ -0,0 +1,143 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +$IP = getenv( 'MW_INSTALL_PATH' ); |
| 5 | +if ( $IP === false ) { |
| 6 | + $IP = dirname( __FILE__ ) . '/../..'; |
| 7 | +} |
| 8 | +require( "$IP/maintenance/Maintenance.php" ); |
| 9 | + |
| 10 | +class PopulateAFRevisions extends Maintenance { |
| 11 | + const REPORTING_INTERVAL = 100; |
| 12 | + const BATCH_SIZE = 100; |
| 13 | + |
| 14 | + public function __construct() { |
| 15 | + parent::__construct(); |
| 16 | + $this->mDescription = "Populates the article_feedback_revisions table"; |
| 17 | + } |
| 18 | + |
| 19 | + public function syncDBs() { |
| 20 | + // FIXME: Copied from updateCollation.php, should be centralized somewhere |
| 21 | + $lb = wfGetLB(); |
| 22 | + // bug 27975 - Don't try to wait for slaves if there are none |
| 23 | + // Prevents permission error when getting master position |
| 24 | + if ( $lb->getServerCount() > 1 ) { |
| 25 | + $dbw = $lb->getConnection( DB_MASTER ); |
| 26 | + $pos = $dbw->getMasterPos(); |
| 27 | + $lb->waitForAll( $pos ); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public function execute() { |
| 32 | + global $wgArticleFeedbackRatings; |
| 33 | + |
| 34 | + $this->output( "Populating article_feedback_revisions table ...\n" ); |
| 35 | + |
| 36 | + // Data structure where we accumulate the data |
| 37 | + // We need this because more recent ratings of the same user to the same page |
| 38 | + // need to overwrite older ratings |
| 39 | + // array( pageid => array( 'userid|anontoken' => array( 'revid' => revid, 'ratings' => array( id => value ) ) ) ) |
| 40 | + $data = array(); |
| 41 | + |
| 42 | + $lastRevID = 0; |
| 43 | + $i = 0; |
| 44 | + $dbr = wfGetDB( DB_SLAVE ); |
| 45 | + $dbw = wfGetDB( DB_MASTER ); |
| 46 | + $this->output( "Reading data from article_feedback ...\n" ); |
| 47 | + while ( true ) { |
| 48 | + // Get the next revision ID |
| 49 | + $row = $dbr->selectRow( 'article_feedback', array( 'aa_revision', 'aa_page_id' ), |
| 50 | + "aa_revision > $lastRevID", __METHOD__, |
| 51 | + array( 'ORDER BY' => 'aa_revision', 'LIMIT' => 1 ) |
| 52 | + ); |
| 53 | + if ( $row === false ) { |
| 54 | + // No next revision, we're done |
| 55 | + break; |
| 56 | + } |
| 57 | + $revid = intval( $row->aa_revision ); |
| 58 | + $pageid = intval( $row->aa_page_id ); |
| 59 | + |
| 60 | + // Get all article_feedback rows for this revision |
| 61 | + $res = $dbr->select( 'article_feedback', |
| 62 | + array( 'aa_rating_id', 'aa_rating_value', 'aa_user_id', 'aa_user_anon_token' ), |
| 63 | + array( 'aa_revision' => $revid ), |
| 64 | + __METHOD__ |
| 65 | + ); |
| 66 | + |
| 67 | + // Initialize counts and sums for each rating |
| 68 | + // If $wgArticleFeedbackRatings = array( 1, 2, 3, 4 ) this initializes them |
| 69 | + // to array( 1 => 0, 2 => 0, 3 => 0, 4 => 0 ) |
| 70 | + $counts = $sums = array_combine( $wgArticleFeedbackRatings, |
| 71 | + array_fill( 0, count( $wgArticleFeedbackRatings ), 0 ) |
| 72 | + ); |
| 73 | + |
| 74 | + // Process each of the queried rows and update $data |
| 75 | + foreach ( $res as $row ) { |
| 76 | + $u = "{$row->aa_user_id}|{$row->aa_user_anon_token}"; |
| 77 | + // Add entry if not present |
| 78 | + if ( !isset( $data[$pageid][$u] ) ) { |
| 79 | + $data[$pageid][$u] = array( 'revid' => $revid ); |
| 80 | + } |
| 81 | + // Update the entry if this row belongs to the same or a more recent revision |
| 82 | + // for the specific user |
| 83 | + if ( $data[$pageid][$u]['revid'] <= $revid ) { |
| 84 | + $data[$pageid][$u]['ratings'][$row->aa_rating_id] = $row->aa_rating_value; |
| 85 | + $data[$pageid][$u]['revid'] = $revid; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + $lastRevID = $revid; |
| 90 | + |
| 91 | + $i++; |
| 92 | + if ( $i % self::REPORTING_INTERVAL ) { |
| 93 | + $this->output( "$lastRevID\n" ); |
| 94 | + } |
| 95 | + } |
| 96 | + $this->output( "done\n" ); |
| 97 | + |
| 98 | + // Reorganize the data into per-revision counts and totals |
| 99 | + $data2 = array(); // array( revid => array( 'pageid' => pageid, 'ratings' => array( ratingid => array( 'count' => count, 'total' => total ) ) |
| 100 | + foreach ( $data as $pageid => $pageData ) { |
| 101 | + foreach ( $pageData as $user => $userData ) { |
| 102 | + $data2[$userData['revid']]['pageid'] = $pageid; |
| 103 | + foreach ( $userData['ratings'] as $id => $value ) { |
| 104 | + if ( !isset( $data2[$userData['revid']]['ratings'][$id] ) ) { |
| 105 | + $data2[$userData['revid']]['ratings'][$id] = array( 'count' => 0, 'total' => 0 ); |
| 106 | + } |
| 107 | + $data2[$userData['revid']]['ratings'][$id]['count']++; |
| 108 | + $data2[$userData['revid']]['ratings'][$id]['total'] += $value; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + // Reorganize the data again, into DB rows this time |
| 113 | + $rows = array(); |
| 114 | + foreach ( $data2 as $revid => $revData ) { |
| 115 | + foreach ( $revData['ratings'] as $ratingID => $ratingData ) { |
| 116 | + $rows[] = array( |
| 117 | + 'afr_page_id' => $revData['pageid'], |
| 118 | + 'afr_revision' => $revid, |
| 119 | + 'afr_rating_id' => $ratingID, |
| 120 | + 'afr_total' => $ratingData['total'], |
| 121 | + 'afr_count' => $ratingData['count'] |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + $this->output( "Writing data to article_feedback_revisions ...\n" ); |
| 127 | + $rowsInserted = 0; |
| 128 | + while ( $rows ) { |
| 129 | + $batch = array_splice( $rows, 0, self::BATCH_SIZE ); |
| 130 | + $dbw->replace( 'article_feedback_revisions', |
| 131 | + array( array( 'afr_page_id', 'afr_rating_id', 'afr_revision' ) ), |
| 132 | + $batch, __METHOD__ |
| 133 | + ); |
| 134 | + $rowsInserted += count( $batch ); |
| 135 | + $this->syncDBs(); |
| 136 | + $this->output( "$rowsInserted rows\n" ); |
| 137 | + } |
| 138 | + $this->output( "done\n" ); |
| 139 | + |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +$maintClass = "PopulateAFRevisions"; |
| 144 | +require_once( DO_MAINTENANCE ); |
\ No newline at end of file |
Property changes on: trunk/extensions/ArticleFeedback/populateAFRevisions.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 145 | + native |