[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> populateRevisionLength.php (source)

   1  <?php
   2  /**
   3   * Populates the rev_len and ar_len fields for old revisions created
   4   * before MW 1.10.
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19   * http://www.gnu.org/copyleft/gpl.html
  20   *
  21   * @file
  22   * @ingroup Maintenance
  23   */
  24  
  25  require_once  __DIR__ . '/Maintenance.php';
  26  
  27  /**
  28   * Maintenance script that populates the rev_len and ar_len fields
  29   * for old revisions created before MW 1.10.
  30   *
  31   * @ingroup Maintenance
  32   */
  33  class PopulateRevisionLength extends LoggedUpdateMaintenance {
  34  	public function __construct() {
  35          parent::__construct();
  36          $this->mDescription = "Populates the rev_len and ar_len fields";
  37          $this->setBatchSize( 200 );
  38      }
  39  
  40  	protected function getUpdateKey() {
  41          return 'populate rev_len and ar_len';
  42      }
  43  
  44  	public function doDBUpdates() {
  45          $db = $this->getDB( DB_MASTER );
  46          if ( !$db->tableExists( 'revision' ) ) {
  47              $this->error( "revision table does not exist", true );
  48          } elseif ( !$db->tableExists( 'archive' ) ) {
  49              $this->error( "archive table does not exist", true );
  50          } elseif ( !$db->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
  51              $this->output( "rev_len column does not exist\n\n", true );
  52  
  53              return false;
  54          }
  55  
  56          $this->output( "Populating rev_len column\n" );
  57          $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::selectFields() );
  58  
  59          $this->output( "Populating ar_len column\n" );
  60          $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::selectArchiveFields() );
  61  
  62          $this->output( "rev_len and ar_len population complete "
  63              . "[$rev revision rows, $ar archive rows].\n" );
  64  
  65          return true;
  66      }
  67  
  68      /**
  69       * @param string $table
  70       * @param string $idCol
  71       * @param string $prefix
  72       * @param array $fields
  73       * @return int
  74       */
  75  	protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
  76          $db = $this->getDB( DB_MASTER );
  77          $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
  78          $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
  79          if ( !$start || !$end ) {
  80              $this->output( "...$table table seems to be empty.\n" );
  81  
  82              return 0;
  83          }
  84  
  85          # Do remaining chunks
  86          $blockStart = intval( $start );
  87          $blockEnd = intval( $start ) + $this->mBatchSize - 1;
  88          $count = 0;
  89  
  90          while ( $blockStart <= $end ) {
  91              $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
  92              $res = $db->select(
  93                  $table,
  94                  $fields,
  95                  array(
  96                      "$idCol >= $blockStart",
  97                      "$idCol <= $blockEnd",
  98                      "{$prefix}_len IS NULL"
  99                  ),
 100                  __METHOD__
 101              );
 102  
 103              $db->begin( __METHOD__ );
 104              # Go through and update rev_len from these rows.
 105              foreach ( $res as $row ) {
 106                  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
 107                      $count++;
 108                  }
 109              }
 110              $db->commit( __METHOD__ );
 111  
 112              $blockStart += $this->mBatchSize;
 113              $blockEnd += $this->mBatchSize;
 114              wfWaitForSlaves();
 115          }
 116  
 117          return $count;
 118      }
 119  
 120      /**
 121       * @param stdClass $row
 122       * @param string $table
 123       * @param string $idCol
 124       * @param string $prefix
 125       * @return bool
 126       */
 127  	protected function upgradeRow( $row, $table, $idCol, $prefix ) {
 128          $db = $this->getDB( DB_MASTER );
 129  
 130          $rev = ( $table === 'archive' )
 131              ? Revision::newFromArchiveRow( $row )
 132              : new Revision( $row );
 133  
 134          $content = $rev->getContent();
 135          if ( !$content ) {
 136              # This should not happen, but sometimes does (bug 20757)
 137              $id = $row->$idCol;
 138              $this->output( "Content of $table $id unavailable!\n" );
 139  
 140              return false;
 141          }
 142  
 143          # Update the row...
 144          $db->update( $table,
 145              array( "{$prefix}_len" => $content->getSize() ),
 146              array( $idCol => $row->$idCol ),
 147              __METHOD__
 148          );
 149  
 150          return true;
 151      }
 152  }
 153  
 154  $maintClass = "PopulateRevisionLength";
 155  require_once RUN_MAINTENANCE_IF_MAIN;


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1