build: Upgrade mediawiki-codesniffer from 26.0.0 to 28.0.0
[lhc/web/wiklou.git] / maintenance / fetchText.php
index 9c5a375..8d04adc 100644 (file)
@@ -24,7 +24,9 @@
 
 require_once __DIR__ . '/Maintenance.php';
 
-use Wikimedia\Rdbms\IDatabase;
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Storage\BlobAccessException;
+use MediaWiki\Storage\SqlBlobStore;
 
 /**
  * Maintenance script used to fetch page text in a subprocess.
@@ -32,14 +34,24 @@ use Wikimedia\Rdbms\IDatabase;
  * @ingroup Maintenance
  */
 class FetchText extends Maintenance {
+
        public function __construct() {
                parent::__construct();
-               $this->addDescription( "Fetch the raw revision blob from an old_id.\n" .
+
+               $this->addDescription( "Fetch the raw revision blob from a blob address.\n" .
+                       "Integer IDs are interpreted as referring to text.old_id for backwards compatibility.\n" .
                        "NOTE: Export transformations are NOT applied. " .
-                       "This is left to backupTextPass.php"
+                       "This is left to dumpTextPass.php"
                );
        }
 
+       /**
+        * @return SqlBlobStore
+        */
+       private function getBlobStore() {
+               return MediaWikiServices::getInstance()->getBlobStore();
+       }
+
        /**
         * returns a string containing the following in order:
         *   textid
@@ -51,7 +63,6 @@ class FetchText extends Maintenance {
         * note that the text string itself is *not* followed by newline
         */
        public function execute() {
-               $db = $this->getDB( DB_REPLICA );
                $stdin = $this->getStdin();
                while ( !feof( $stdin ) ) {
                        $line = fgets( $stdin );
@@ -59,38 +70,31 @@ class FetchText extends Maintenance {
                                // We appear to have lost contact...
                                break;
                        }
-                       $textId = intval( $line );
-                       $text = $this->doGetText( $db, $textId );
-                       if ( $text === false ) {
-                               # actual error, not zero-length text
-                               $textLen = "-1";
-                       } else {
+                       $blobAddress = trim( $line );
+
+                       // Plain integers are supported for backwards compatibility with pre-MCR dumps.
+                       if ( strpos( $blobAddress, ':' ) === false && is_numeric( $blobAddress ) ) {
+                               $blobAddress = SqlBlobStore::makeAddressFromTextId( intval( $blobAddress ) );
+                       }
+
+                       try {
+                               $text = $this->getBlobStore()->getBlob( $blobAddress );
                                $textLen = strlen( $text );
+                       } catch ( BlobAccessException $ex ) {
+                               // XXX: log $ex to stderr?
+                               $textLen = '-1';
+                               $text = '';
+                       } catch ( InvalidArgumentException $ex ) {
+                               // XXX: log $ex to stderr?
+                               $textLen = '-1';
+                               $text = '';
                        }
-                       $this->output( $textId . "\n" . $textLen . "\n" . $text );
-               }
-       }
 
-       /**
-        * May throw a database error if, say, the server dies during query.
-        * @param IDatabase $db
-        * @param int $id The old_id
-        * @return string
-        */
-       private function doGetText( $db, $id ) {
-               $id = intval( $id );
-               $row = $db->selectRow( 'text',
-                       [ 'old_text', 'old_flags' ],
-                       [ 'old_id' => $id ],
-                       __METHOD__ );
-               $text = Revision::getRevisionText( $row );
-               if ( $text === false ) {
-                       return false;
+                       $this->output( $blobAddress . "\n" . $textLen . "\n" . $text );
                }
-
-               return $text;
        }
+
 }
 
-$maintClass = "FetchText";
+$maintClass = FetchText::class;
 require_once RUN_MAINTENANCE_IF_MAIN;