[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> cleanupTitles.php (source)

   1  <?php
   2  /**
   3   * Clean up broken, unparseable titles.
   4   *
   5   * Usage: php cleanupTitles.php [--fix]
   6   * Options:
   7   *   --fix  Actually clean up titles; otherwise just checks for them
   8   *
   9   * Copyright © 2005 Brion Vibber <[email protected]>
  10   * https://www.mediawiki.org/
  11   *
  12   * This program is free software; you can redistribute it and/or modify
  13   * it under the terms of the GNU General Public License as published by
  14   * the Free Software Foundation; either version 2 of the License, or
  15   * (at your option) any later version.
  16   *
  17   * This program is distributed in the hope that it will be useful,
  18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20   * GNU General Public License for more details.
  21   *
  22   * You should have received a copy of the GNU General Public License along
  23   * with this program; if not, write to the Free Software Foundation, Inc.,
  24   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25   * http://www.gnu.org/copyleft/gpl.html
  26   *
  27   * @file
  28   * @author Brion Vibber <brion at pobox.com>
  29   * @ingroup Maintenance
  30   */
  31  
  32  require_once  __DIR__ . '/cleanupTable.inc';
  33  
  34  /**
  35   * Maintenance script to clean up broken, unparseable titles.
  36   *
  37   * @ingroup Maintenance
  38   */
  39  class TitleCleanup extends TableCleanup {
  40  	public function __construct() {
  41          parent::__construct();
  42          $this->mDescription = "Script to clean up broken, unparseable titles";
  43      }
  44  
  45      /**
  46       * @param object $row
  47       */
  48  	protected function processRow( $row ) {
  49          global $wgContLang;
  50          $display = Title::makeName( $row->page_namespace, $row->page_title );
  51          $verified = $wgContLang->normalize( $display );
  52          $title = Title::newFromText( $verified );
  53  
  54          if ( !is_null( $title )
  55              && $title->canExist()
  56              && $title->getNamespace() == $row->page_namespace
  57              && $title->getDBkey() === $row->page_title
  58          ) {
  59              $this->progress( 0 ); // all is fine
  60  
  61              return;
  62          }
  63  
  64          if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
  65              $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
  66              $this->progress( 0 );
  67          } elseif ( is_null( $title ) ) {
  68              $this->output( "page $row->page_id ($display) is illegal.\n" );
  69              $this->moveIllegalPage( $row );
  70              $this->progress( 1 );
  71          } else {
  72              $this->output( "page $row->page_id ($display) doesn't match self.\n" );
  73              $this->moveInconsistentPage( $row, $title );
  74              $this->progress( 1 );
  75          }
  76      }
  77  
  78      /**
  79       * @param string $name
  80       * @return bool
  81       */
  82  	protected function fileExists( $name ) {
  83          // XXX: Doesn't actually check for file existence, just presence of image record.
  84          // This is reasonable, since cleanupImages.php only iterates over the image table.
  85          $dbr = wfGetDB( DB_SLAVE );
  86          $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
  87  
  88          return $row !== false;
  89      }
  90  
  91      /**
  92       * @param object $row
  93       */
  94  	protected function moveIllegalPage( $row ) {
  95          $legal = 'A-Za-z0-9_/\\\\-';
  96          $legalized = preg_replace_callback( "!([^$legal])!",
  97              array( &$this, 'hexChar' ),
  98              $row->page_title );
  99          if ( $legalized == '.' ) {
 100              $legalized = '(dot)';
 101          }
 102          if ( $legalized == '_' ) {
 103              $legalized = '(space)';
 104          }
 105          $legalized = 'Broken/' . $legalized;
 106  
 107          $title = Title::newFromText( $legalized );
 108          if ( is_null( $title ) ) {
 109              $clean = 'Broken/id:' . $row->page_id;
 110              $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
 111              $title = Title::newFromText( $clean );
 112          } elseif ( $title->exists() ) {
 113              $clean = 'Broken/id:' . $row->page_id;
 114              $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
 115              $title = Title::newFromText( $clean );
 116          }
 117  
 118          $dest = $title->getDBkey();
 119          if ( $this->dryrun ) {
 120              $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
 121                  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
 122          } else {
 123              $this->output( "renaming $row->page_id ($row->page_namespace," .
 124                  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
 125              $dbw = wfGetDB( DB_MASTER );
 126              $dbw->update( 'page',
 127                  array( 'page_title' => $dest ),
 128                  array( 'page_id' => $row->page_id ),
 129                  __METHOD__ );
 130          }
 131      }
 132  
 133      /**
 134       * @param object $row
 135       * @param Title $title
 136       */
 137  	protected function moveInconsistentPage( $row, $title ) {
 138          if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
 139              if ( $title->getInterwiki() || !$title->canExist() ) {
 140                  $prior = $title->getPrefixedDBkey();
 141              } else {
 142                  $prior = $title->getDBkey();
 143              }
 144  
 145              # Old cleanupTitles could move articles there. See bug 23147.
 146              $ns = $row->page_namespace;
 147              if ( $ns < 0 ) {
 148                  $ns = 0;
 149              }
 150  
 151              # Namespace which no longer exists. Put the page in the main namespace
 152              # since we don't have any idea of the old namespace name. See bug 68501.
 153              if ( !MWNamespace::exists( $ns ) ) {
 154                  $ns = 0;
 155              }
 156  
 157              $clean = 'Broken/' . $prior;
 158              $verified = Title::makeTitleSafe( $ns, $clean );
 159              if ( !$verified || $verified->exists() ) {
 160                  $blah = "Broken/id:" . $row->page_id;
 161                  $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
 162                  $verified = Title::makeTitleSafe( $ns, $blah );
 163              }
 164              $title = $verified;
 165          }
 166          if ( is_null( $title ) ) {
 167              $this->error( "Something awry; empty title.", true );
 168          }
 169          $ns = $title->getNamespace();
 170          $dest = $title->getDBkey();
 171  
 172          if ( $this->dryrun ) {
 173              $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
 174                  "'$row->page_title') to ($ns,'$dest')\n" );
 175          } else {
 176              $this->output( "renaming $row->page_id ($row->page_namespace," .
 177                  "'$row->page_title') to ($ns,'$dest')\n" );
 178              $dbw = wfGetDB( DB_MASTER );
 179              $dbw->update( 'page',
 180                  array(
 181                      'page_namespace' => $ns,
 182                      'page_title' => $dest
 183                  ),
 184                  array( 'page_id' => $row->page_id ),
 185                  __METHOD__ );
 186              LinkCache::singleton()->clear();
 187          }
 188      }
 189  }
 190  
 191  $maintClass = "TitleCleanup";
 192  require_once RUN_MAINTENANCE_IF_MAIN;


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