populatePPSortKey.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Populate the pp_sortkey fields in the page_props table
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Usage:
  26. * populatePPSortKey.php
  27. */
  28. class PopulatePPSortKey extends LoggedUpdateMaintenance {
  29. public function __construct() {
  30. parent::__construct();
  31. $this->addDescription( 'Populate the pp_sortkey field' );
  32. $this->setBatchSize( 100 );
  33. }
  34. protected function doDBUpdates() {
  35. $dbw = $this->getDB( DB_MASTER );
  36. $lastProp = null;
  37. $lastPageValue = 0;
  38. $editedRowCount = 0;
  39. $this->output( "Populating page_props.pp_sortkey...\n" );
  40. while ( true ) {
  41. $conditions = [ 'pp_sortkey IS NULL' ];
  42. if ( $lastPageValue !== 0 ) {
  43. $conditions[] = 'pp_page > ' . $dbw->addQuotes( $lastPageValue ) . ' OR ' .
  44. '( pp_page = ' . $dbw->addQuotes( $lastPageValue ) .
  45. ' AND pp_propname > ' . $dbw->addQuotes( $lastProp ) . ' )';
  46. }
  47. $res = $dbw->select(
  48. 'page_props',
  49. [ 'pp_propname', 'pp_page', 'pp_sortkey', 'pp_value' ],
  50. $conditions,
  51. __METHOD__,
  52. [
  53. 'ORDER BY' => 'pp_page, pp_propname',
  54. 'LIMIT' => $this->getBatchSize()
  55. ]
  56. );
  57. if ( $res->numRows() === 0 ) {
  58. break;
  59. }
  60. $this->beginTransaction( $dbw, __METHOD__ );
  61. foreach ( $res as $row ) {
  62. if ( !is_numeric( $row->pp_value ) ) {
  63. continue;
  64. }
  65. $dbw->update(
  66. 'page_props',
  67. [ 'pp_sortkey' => $row->pp_value ],
  68. [
  69. 'pp_page' => $row->pp_page,
  70. 'pp_propname' => $row->pp_propname
  71. ],
  72. __METHOD__
  73. );
  74. $editedRowCount++;
  75. }
  76. $this->output( "Updated " . $editedRowCount . " rows\n" );
  77. $this->commitTransaction( $dbw, __METHOD__ );
  78. // We need to get the last element's page ID
  79. $lastPageValue = $row->pp_page;
  80. // And the propname...
  81. $lastProp = $row->pp_propname;
  82. }
  83. $this->output( "Populating page_props.pp_sortkey complete.\n" );
  84. }
  85. protected function getUpdateKey() {
  86. return 'populate pp_sortkey';
  87. }
  88. }
  89. $maintClass = PopulatePPSortKey::class;
  90. require_once RUN_MAINTENANCE_IF_MAIN;