MediaWiki  master
populatePPSortKey.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31  public function __construct() {
32  parent::__construct();
33  $this->addDescription( 'Populate the pp_sortkey field' );
34  $this->setBatchSize( 100 );
35  }
36 
37  protected function doDBUpdates() {
38  $dbw = $this->getDB( DB_PRIMARY );
39 
40  $lastProp = null;
41  $lastPageValue = 0;
42 
43  $lastRowCount = 0;
44  $editedRowCount = 0;
45 
46  $this->output( "Populating page_props.pp_sortkey...\n" );
47  while ( true ) {
48  $queryBuilder = $dbw->newSelectQueryBuilder()
49  ->select( [ 'pp_propname', 'pp_page', 'pp_sortkey', 'pp_value' ] )
50  ->from( 'page_props' )
51  ->where( [ 'pp_sortkey' => null ] )
52  ->orderBy( [ 'pp_page', 'pp_propname' ] )
53  ->limit( $this->getBatchSize() );
54  if ( $lastPageValue !== 0 ) {
55  $queryBuilder->andWhere( $dbw->buildComparison( '>', [
56  'pp_page' => $lastPageValue,
57  'pp_propname' => $lastProp,
58  ] ) );
59  }
60 
61  $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
62 
63  if ( $res->numRows() === 0 ) {
64  break;
65  }
66 
67  $this->beginTransaction( $dbw, __METHOD__ );
68 
69  foreach ( $res as $row ) {
70  if ( !is_numeric( $row->pp_value ) ) {
71  continue;
72  }
73  $dbw->update(
74  'page_props',
75  [ 'pp_sortkey' => $row->pp_value ],
76  [
77  'pp_page' => $row->pp_page,
78  'pp_propname' => $row->pp_propname
79  ],
80  __METHOD__
81  );
82  $editedRowCount++;
83  }
84 
85  if ( $editedRowCount !== $lastRowCount ) {
86  $this->output( "Updated " . $editedRowCount . " rows\n" );
87  $lastRowCount = $editedRowCount;
88  }
89 
90  $this->commitTransaction( $dbw, __METHOD__ );
91 
92  // We need to get the last element's page ID
93  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
94  $lastPageValue = $row->pp_page;
95  // And the propname...
96  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
97  $lastProp = $row->pp_propname;
98  }
99 
100  $this->output( "Populating page_props.pp_sortkey complete.\n" );
101  $this->output( "Updated a total of $editedRowCount rows\n" );
102  return true;
103  }
104 
105  protected function getUpdateKey() {
106  return 'populate pp_sortkey';
107  }
108 }
109 
110 $maintClass = PopulatePPSortKey::class;
111 require_once RUN_MAINTENANCE_IF_MAIN;
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Usage: populatePPSortKey.php.
__construct()
Default constructor.
doDBUpdates()
Do the actual work.
getUpdateKey()
Get the update key name to go in the update log table.
const DB_PRIMARY
Definition: defines.php:28