fix field names in PRIMARY KEY spec
[lhc/web/wiklou.git] / maintenance / compressOld.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8 function compressOldPages( $start = 0 ) {
9 $fname = 'compressOldPages';
10
11 $chunksize = 50;
12 print "Starting from old_id $start...\n";
13 $dbw =& wfGetDB( DB_MASTER );
14 $old = $dbw->tableName( 'old' );
15 do {
16 $end = $start + $chunksize;
17 $res = $dbw->select( 'old', array( 'old_id','old_flags','old_namespace','old_title','old_text' ),
18 "old_id>=$start", $fname, array( 'ORDER BY' => 'old_id', 'LIMIT' => $chunksize, 'FOR UPDATE' ) );
19 if( $dbw->numRows( $res ) == 0 ) {
20 break;
21 }
22 $last = $start;
23 while( $row = $dbw->fetchObject( $res ) ) {
24 # print " {$row->old_id} - {$row->old_namespace}:{$row->old_title}\n";
25 compressPage( $row );
26 $last = $row->old_id;
27 }
28 $dbw->freeResult( $res );
29 $start = $last + 1; # Deletion may leave long empty stretches
30 print "$start...\n";
31 } while( true );
32 }
33
34 function compressPage( $row ) {
35 $fname = 'compressPage';
36 if( false !== strpos( $row->old_flags, "gzip" ) ) {
37 print "Already compressed row {$row->old_id}?\n";
38 return false;
39 }
40 $dbw =& wfGetDB( DB_MASTER );
41 $flags = $row->old_flags ? "{$row->old_flags},gzip" : "gzip";
42 $compress = gzdeflate( $row->old_text );
43 $dbw->update( 'old',
44 array( /* SET */
45 'old_flags' => $flags,
46 'old_text' => $compress
47 ), array( /* WHERE */
48 'old_id' => $row->old_id
49 ), $fname, 'LIMIT 1'
50 );
51 return true;
52 }
53
54 define( 'LS_INDIVIDUAL', 0 );
55 define( 'LS_CHUNKED', 1 );
56
57 function compressWithConcat( $startId, $maxChunkSize, $maxChunkFactor, $factorThreshold, $beginDate, $endDate )
58 {
59 $fname = 'compressWithConcat';
60 $loadStyle = LS_CHUNKED;
61
62 $dbw =& wfGetDB( DB_MASTER );
63
64 # First get a list of all pages
65 $pageRes = $dbw->select( 'cur', array('cur_namespace', 'cur_title'), false, $fname );
66
67 # For each of those, get a list of revisions which fit the criteria
68 $conds = array();
69 if ( $beginDate ) {
70 $conds[] = "old_timestamp>'" . $beginDate . "'";
71 }
72 if ( $endDate ) {
73 $conds[] = "old_timestamp<'" . $endDate . "'";
74 }
75 if ( $startId ) {
76 $conds[] = 'old_id>=' . $startId;
77 }
78 if ( $loadStyle == LS_CHUNKED ) {
79 $fields = array( 'old_id', 'old_flags', 'old_text' );
80 $revLoadOptions = 'FOR UPDATE';
81 } else {
82 $fields = array( 'old_id' );
83 $revLoadOptions = array();
84 }
85
86 while ( $pageRow = $dbw->fetchObject( $pageRes ) ) {
87 # Display progress
88 $titleObj = Title::makeTitle( $pageRow->cur_namespace, $pageRow->cur_title );
89 print $titleObj->getPrefixedDBkey() . " ";
90
91 # Load revisions
92 $revRes = $dbw->select( 'old', $fields,
93 array( 'old_namespace' => $pageRow->cur_namespace, 'old_title' => $pageRow->cur_title ) + $conds,
94 $fname,
95 $revLoadOptions
96 );
97 $revs = array();
98 while ( $revRow = $dbw->fetchObject( $revRes ) ) {
99 $revs[] = $revRow;
100 }
101
102 if ( count( $revs ) < 2) {
103 # No revisions matching, no further processing
104 print "\n";
105 continue;
106 }
107
108 # For each chunk
109 $i = 0;
110 while ( $i < count( $revs ) ) {
111 if ( $i < count( $revs ) - $maxChunkSize ) {
112 $thisChunkSize = $maxChunkSize;
113 } else {
114 $thisChunkSize = count( $revs ) - $i;
115 }
116
117 $chunk = new ConcatenatedGzipHistoryBlob();
118 $stubs = array();
119 $dbw->begin();
120 $usedChunk = false;
121 $primaryOldid = $revs[$i]->old_id;
122
123 # Get the text of each revision and add it to the object
124 for ( $j = 0; $j < $thisChunkSize && $chunk->isHappy( $maxChunkFactor, $factorThreshold ); $j++ ) {
125 $oldid = $revs[$i + $j]->old_id;
126
127 # Get text
128 if ( $loadStyle == LS_INDIVIDUAL ) {
129 $textRow = $dbw->selectRow( 'old',
130 array( 'old_flags', 'old_text' ),
131 array( 'old_id' => $oldid ),
132 $fname,
133 'FOR UPDATE'
134 );
135 $text = Article::getRevisionText( $textRow );
136 } else {
137 $text = Article::getRevisionText( $revs[$i + $j] );
138 }
139
140 if ( $text === false ) {
141 print "\nError, unable to get text in old_id $oldid\n";
142 #$dbw->delete( 'old', array( 'old_id' => $oldid ) );
143 }
144
145 if ( $j == 0 ) {
146 $chunk->setText( $text );
147 print '.';
148 } else {
149 # Don't make a stub if it's going to be longer than the article
150 # Stubs are typically about 100 bytes
151 if ( strlen( $text ) < 120 ) {
152 $stub = false;
153 print 'x';
154 } else {
155 $stub = $chunk->addItem( $text );
156 $stub->setLocation( $primaryOldid );
157 $hash = $stub->getHash();
158 $stub = serialize( $stub );
159 print '.';
160 $usedChunk = true;
161 }
162 $stubs[$j] = $stub;
163 }
164 }
165 $thisChunkSize = $j;
166
167 # If we couldn't actually use any stubs because the pages were too small, do nothing
168 if ( $usedChunk ) {
169 # Store the main object
170 $dbw->update( 'old',
171 array( /* SET */
172 'old_text' => serialize( $chunk ),
173 'old_flags' => 'object',
174 ), array( /* WHERE */
175 'old_id' => $primaryOldid
176 )
177 );
178
179 # Store the stub objects
180 for ( $j = 1; $j < $thisChunkSize; $j++ ) {
181 # Skip if not compressing
182 if ( $stubs[$j] !== false ) {
183 $dbw->update( 'old',
184 array( /* SET */
185 'old_text' => $stubs[$j],
186 'old_flags' => 'object',
187 ), array( /* WHERE */
188 'old_id' => $revs[$i + $j]->old_id
189 )
190 );
191 }
192 }
193 }
194 # Done, next
195 print "/";
196 $dbw->commit();
197 $i += $thisChunkSize;
198 }
199 print "\n";
200 }
201 return true;
202 }
203 ?>