Merge "Upgrade wikimedia/remex-html to 2.0.1" into REL1_31
[lhc/web/wiklou.git] / maintenance / fetchText.php
1 <?php
2 /**
3 * Communications protocol.
4 * This is used by dumpTextPass.php when the --spawn option is present.
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 use Wikimedia\Rdbms\IDatabase;
28
29 /**
30 * Maintenance script used to fetch page text in a subprocess.
31 *
32 * @ingroup Maintenance
33 */
34 class FetchText extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( "Fetch the raw revision blob from an old_id.\n" .
38 "NOTE: Export transformations are NOT applied. " .
39 "This is left to backupTextPass.php"
40 );
41 }
42
43 /**
44 * returns a string containing the following in order:
45 * textid
46 * \n
47 * length of text (-1 on error = failure to retrieve/unserialize/gunzip/etc)
48 * \n
49 * text (may be empty)
50 *
51 * note that the text string itself is *not* followed by newline
52 */
53 public function execute() {
54 $db = $this->getDB( DB_REPLICA );
55 $stdin = $this->getStdin();
56 while ( !feof( $stdin ) ) {
57 $line = fgets( $stdin );
58 if ( $line === false ) {
59 // We appear to have lost contact...
60 break;
61 }
62 $textId = intval( $line );
63 $text = $this->doGetText( $db, $textId );
64 if ( $text === false ) {
65 # actual error, not zero-length text
66 $textLen = "-1";
67 } else {
68 $textLen = strlen( $text );
69 }
70 $this->output( $textId . "\n" . $textLen . "\n" . $text );
71 }
72 }
73
74 /**
75 * May throw a database error if, say, the server dies during query.
76 * @param IDatabase $db
77 * @param int $id The old_id
78 * @return string
79 */
80 private function doGetText( $db, $id ) {
81 $id = intval( $id );
82 $row = $db->selectRow( 'text',
83 [ 'old_text', 'old_flags' ],
84 [ 'old_id' => $id ],
85 __METHOD__ );
86 $text = Revision::getRevisionText( $row );
87 if ( $text === false ) {
88 return false;
89 }
90
91 return $text;
92 }
93 }
94
95 $maintClass = FetchText::class;
96 require_once RUN_MAINTENANCE_IF_MAIN;