Untested image snapshot feature, fixed complete breakage of default skin by Rob
[lhc/web/wiklou.git] / maintenance / dumpHTML.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 define( 'REPORTING_INTERVAL', 10 );
8
9 require_once( 'includes/ImagePage.php' );
10 require_once( 'includes/CategoryPage.php' );
11 require_once( 'includes/RawPage.php' );
12
13 class DumpHTML {
14 # Destination directory
15 var $dest;
16
17 # Show interlanguage links?
18 var $interwiki = true;
19
20 # Depth of HTML directory tree
21 var $depth = 3;
22
23 # Directory that commons images are copied into
24 var $sharedStaticDirectory;
25
26 # Directory that the images are in, after copying
27 var $destUploadDirectory;
28
29 # Relative path to image directory
30 var $imageRel = 'upload';
31
32 # Copy commons images instead of symlinking
33 var $forceCopy = false;
34
35 # Make a copy of all images encountered
36 var $makeSnapshot = false;
37
38 # Make links assuming the script path is in the same directory as
39 # the destination
40 var $alternateScriptPath = false;
41
42 # Original values of various globals
43 var $oldArticlePath = false, $oldCopyrightIcon = false;
44
45 # Has setupGlobals been called?
46 var $setupDone = false;
47
48 # List of raw pages used in the current article
49 var $rawPages;
50
51 # Skin to use
52 var $skin = 'htmldump';
53
54 function DumpHTML( $settings ) {
55 foreach ( $settings as $var => $value ) {
56 $this->$var = $value;
57 }
58 }
59
60 /**
61 * Write a set of articles specified by start and end page_id
62 * Skip categories and images, they will be done separately
63 */
64 function doArticles( $start, $end = false ) {
65 $fname = 'DumpHTML::doArticles';
66
67 $this->setupGlobals();
68
69 if ( $end === false ) {
70 $dbr =& wfGetDB( DB_SLAVE );
71 $end = $dbr->selectField( 'page', 'max(page_id)', false, $fname );
72 }
73
74 $mainPageObj = Title::newMainPage();
75 $mainPage = $mainPageObj->getPrefixedDBkey();
76
77
78 for ($id = $start; $id <= $end; $id++) {
79 wfWaitForSlaves( 20 );
80 if ( !($id % REPORTING_INTERVAL) ) {
81 print "Processing ID: $id\r";
82 }
83 if ( !($id % (REPORTING_INTERVAL*10) ) ) {
84 print "\n";
85 }
86 $title = Title::newFromID( $id );
87 if ( $title ) {
88 $ns = $title->getNamespace() ;
89 if ( $ns != NS_CATEGORY && $title->getPrefixedDBkey() != $mainPage ) {
90 $this->doArticle( $title );
91 }
92 }
93 }
94 print "\n";
95 }
96
97 function doSpecials() {
98 $this->doMainPage();
99
100 $this->setupGlobals();
101 print "Special:Categories...";
102 $this->doArticle( Title::makeTitle( NS_SPECIAL, 'Categories' ) );
103 print "\n";
104 }
105
106 /** Write the main page as index.html */
107 function doMainPage() {
108
109 print "Making index.html ";
110
111 // Set up globals with no ../../.. in the link URLs
112 $this->setupGlobals( 0 );
113
114 $title = Title::newMainPage();
115 $text = $this->getArticleHTML( $title );
116 $file = fopen( "{$this->dest}/index.html", "w" );
117 if ( !$file ) {
118 print "\nCan't open index.html for writing\n";
119 return false;
120 }
121 fwrite( $file, $text );
122 fclose( $file );
123 print "\n";
124 }
125
126 function doImageDescriptions() {
127 global $wgSharedUploadDirectory;
128
129 $fname = 'DumpHTML::doImageDescriptions';
130
131 $this->setupGlobals();
132
133 /**
134 * Dump image description pages that don't have an associated article, but do
135 * have a local image
136 */
137 $dbr =& wfGetDB( DB_SLAVE );
138 extract( $dbr->tableNames( 'image', 'page' ) );
139 $res = $dbr->select( 'image', array( 'img_name' ), false, $fname );
140
141 $i = 0;
142 print "Writing image description pages for local images\n";
143 $num = $dbr->numRows( $res );
144 while ( $row = $dbr->fetchObject( $res ) ) {
145 wfWaitForSlaves( 10 );
146 if ( !( ++$i % REPORTING_INTERVAL ) ) {
147 print "Done $i of $num\r";
148 }
149 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
150 if ( $title->getArticleID() ) {
151 // Already done by dumpHTML
152 continue;
153 }
154 $this->doArticle( $title );
155 }
156 print "\n";
157
158 /**
159 * Dump images which only have a real description page on commons
160 */
161 print "Writing description pages for commons images\n";
162 $i = 0;
163 for ( $hash = 0; $hash < 256; $hash++ ) {
164 $dir = sprintf( "%01x/%02x", intval( $hash / 16 ), $hash );
165 $paths = array_merge( glob( "{$this->sharedStaticDirectory}/$dir/*" ),
166 glob( "{$this->sharedStaticDirectory}/thumb/$dir/*" ) );
167
168 foreach ( $paths as $path ) {
169 $file = basename( $path );
170 if ( !(++$i % REPORTING_INTERVAL ) ) {
171 print "$i\r";
172 }
173
174 $title = Title::makeTitle( NS_IMAGE, $file );
175 $this->doArticle( $title );
176 }
177 }
178 print "\n";
179 }
180
181 function doCategories() {
182 $fname = 'DumpHTML::doCategories';
183 $this->setupGlobals();
184
185 $dbr =& wfGetDB( DB_SLAVE );
186 print "Selecting categories...";
187 $sql = 'SELECT DISTINCT cl_to FROM ' . $dbr->tableName( 'categorylinks' );
188 $res = $dbr->query( $sql, $fname );
189
190 print "\nWriting " . $dbr->numRows( $res ). " category pages\n";
191 $i = 0;
192 while ( $row = $dbr->fetchObject( $res ) ) {
193 wfWaitForSlaves( 10 );
194 if ( !(++$i % REPORTING_INTERVAL ) ) {
195 print "$i\r";
196 }
197 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
198 $this->doArticle( $title );
199 }
200 print "\n";
201 }
202
203 function doRedirects() {
204 print "Doing redirects...\n";
205 $fname = 'DumpHTML::doRedirects';
206 $this->setupGlobals();
207 $dbr =& wfGetDB( DB_SLAVE );
208
209 $res = $dbr->select( 'page', array( 'page_namespace', 'page_title' ),
210 array( 'page_is_redirect' => 1 ), $fname );
211 $num = $dbr->numRows( $res );
212 print "$num redirects to do...\n";
213 $i = 0;
214 while ( $row = $dbr->fetchObject( $res ) ) {
215 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
216 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
217 print "Done $i of $num\n";
218 }
219 $this->doArticle( $title );
220 }
221 }
222
223 /** Write an article specified by title */
224 function doArticle( $title ) {
225 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
226 global $wgUploadDirectory;
227
228 $this->rawPages = array();
229 $text = $this->getArticleHTML( $title );
230
231 if ( $text === false ) {
232 return;
233 }
234
235 # Parse the XHTML to find the images
236 $images = $this->findImages( $text );
237 $this->copyImages( $images );
238
239 # Write to file
240 $this->writeArticle( $title, $text );
241
242 # Do raw pages
243 wfMkdirParents( "{$this->dest}/raw", 0755 );
244 foreach( $this->rawPages as $record ) {
245 list( $file, $title, $params ) = $record;
246
247 $path = "{$this->dest}/raw/$file";
248 if ( !file_exists( $path ) ) {
249 $article = new Article( $title );
250 $request = new FauxRequest( $params );
251 $rp = new RawPage( $article, $request );
252 $text = $rp->getRawText();
253
254 print "Writing $file\n";
255 $file = fopen( $path, 'w' );
256 if ( !$file ) {
257 print("Can't open file $fullName for writing\n");
258 continue;
259 }
260 fwrite( $file, $text );
261 fclose( $file );
262 }
263 }
264 }
265
266 /** Write the given text to the file identified by the given title object */
267 function writeArticle( &$title, $text ) {
268 $filename = $this->getHashedFilename( $title );
269 $fullName = "{$this->dest}/$filename";
270 $fullDir = dirname( $fullName );
271
272 wfMkdirParents( $fullDir, 0755 );
273
274 wfSuppressWarnings();
275 $file = fopen( $fullName, 'w' );
276 wfRestoreWarnings();
277
278 if ( !$file ) {
279 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
280 return;
281 }
282
283 fwrite( $file, $text );
284 fclose( $file );
285 }
286
287 /** Set up globals required for parsing */
288 function setupGlobals( $currentDepth = NULL ) {
289 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
290 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
291 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
292 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
293 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon;
294
295 static $oldLogo = NULL;
296
297 if ( !$this->setupDone ) {
298 $wgHooks['GetLocalURL'][] =& $this;
299 $wgHooks['GetFullURL'][] =& $this;
300 $this->oldArticlePath = $wgServer . $wgArticlePath;
301 }
302
303 if ( is_null( $currentDepth ) ) {
304 $currentDepth = $this->depth;
305 }
306
307 if ( $this->alternateScriptPath ) {
308 if ( $currentDepth == 0 ) {
309 $wgScriptPath = '.';
310 } else {
311 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
312 }
313 } else {
314 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
315 }
316
317 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
318
319 # Logo image
320 # Allow for repeated setup
321 if ( !is_null( $oldLogo ) ) {
322 $wgLogo = $oldLogo;
323 } else {
324 $oldLogo = $wgLogo;
325 }
326
327 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
328 # If it's in the upload directory, rewrite it to the new upload directory
329 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
330 } elseif ( $wgLogo{0} == '/' ) {
331 # This is basically heuristic
332 # Rewrite an absolute logo path to one relative to the the script path
333 $wgLogo = $wgScriptPath . $wgLogo;
334 }
335
336 # Another ugly hack
337 if ( !$this->setupDone ) {
338 $this->oldCopyrightIcon = $wgCopyrightIcon;
339 }
340 $wgCopyrightIcon = str_replace( 'src="/images',
341 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
342
343 $wgStylePath = "$wgScriptPath/skins";
344 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
345 $wgSharedUploadPath = "$wgUploadPath/shared";
346 $wgMaxCredits = -1;
347 $wgHideInterlanguageLinks = !$this->interwiki;
348 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
349 $wgEnableParserCache = false;
350 $wgMathPath = "$wgScriptPath/math";
351
352 if ( !empty( $wgRightsText ) ) {
353 $wgRightsUrl = "$wgScriptPath/COPYING.html";
354 }
355
356 $wgUser = new User;
357 $wgUser->setOption( 'skin', $this->skin );
358 $wgUser->setOption( 'editsection', 0 );
359
360 if ( $this->makeSnapshot ) {
361 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
362 if ( realpath( $this->destUploadDirectory == $wgUploadDirectory ) ) {
363 $this->makeSnapshot = false;
364 }
365 }
366
367 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
368
369 $this->setupDone = true;
370 }
371
372 /** Reads the content of a title object, executes the skin and captures the result */
373 function getArticleHTML( &$title ) {
374 global $wgOut, $wgTitle, $wgArticle, $wgUser;
375
376 $linkCache =& LinkCache::singleton();
377 $linkCache->clear();
378 $wgTitle = $title;
379 if ( is_null( $wgTitle ) ) {
380 return false;
381 }
382
383 $ns = $wgTitle->getNamespace();
384 if ( $ns == NS_SPECIAL ) {
385 $wgOut = new OutputPage;
386 $wgOut->setParserOptions( new ParserOptions );
387 SpecialPage::executePath( $wgTitle );
388 } else {
389 /** @todo merge with Wiki.php code */
390 if ( $ns == NS_IMAGE ) {
391 $wgArticle = new ImagePage( $wgTitle );
392 } elseif ( $ns == NS_CATEGORY ) {
393 $wgArticle = new CategoryPage( $wgTitle );
394 } else {
395 $wgArticle = new Article( $wgTitle );
396 }
397 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
398 if ( $rt != NULL ) {
399 return $this->getRedirect( $rt );
400 } else {
401 $wgOut = new OutputPage;
402 $wgOut->setParserOptions( new ParserOptions );
403
404 $wgArticle->view();
405 }
406 }
407
408 $sk =& $wgUser->getSkin();
409 ob_start();
410 $sk->outputPage( $wgOut );
411 $text = ob_get_contents();
412 ob_end_clean();
413
414 return $text;
415 }
416
417 function getRedirect( $rt ) {
418 $url = $rt->escapeLocalURL();
419 $text = $rt->getPrefixedText();
420 return <<<ENDTEXT
421 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
422 <html xmlns="http://www.w3.org/1999/xhtml">
423 <head>
424 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
425 <meta http-equiv="Refresh" content="0;url=$url" />
426 </head>
427 <body>
428 <p>Redirecting to <a href="$url">$text</a></p>
429 </body>
430 </html>
431 ENDTEXT;
432 }
433
434 /** Returns image paths used in an XHTML document */
435 function findImages( $text ) {
436 global $wgOutputEncoding, $wgDumpImages;
437 $parser = xml_parser_create( $wgOutputEncoding );
438 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
439
440 $wgDumpImages = array();
441 xml_parse( $parser, $text );
442 xml_parser_free( $parser );
443
444 return $wgDumpImages;
445 }
446
447 /**
448 * Copy a file specified by a URL to a given directory
449 *
450 * @param string $srcPath The source URL
451 * @param string $srcPathBase The base directory of the source URL
452 * @param string $srcDirBase The base filesystem directory of the source URL
453 * @param string $destDirBase The base filesystem directory of the destination URL
454 */
455 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
456 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
457 $sourceLoc = "$srcDirBase/$rel";
458 $destLoc = "$destDirBase/$rel";
459 #print "Copying $sourceLoc to $staticLoc\n";
460 if ( !file_exists( $destLoc ) ) {
461 wfMkdirParents( dirname( $destLoc ), 0755 );
462 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
463 symlink( $sourceLoc, $destLoc );
464 } else {
465 copy( $sourceLoc, $destLoc );
466 }
467 }
468 }
469
470 /**
471 * Copy an image, and if it is a thumbnail, copy its parent image too
472 */
473 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
474 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
475 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
476 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
477 # The image was a thumbnail
478 # Copy the source image as well
479 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
480 $parts = explode( '/', $rel );
481 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
482 $newSrc = "$srcPathBase/$rel";
483 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
484 }
485 }
486
487 /**
488 * Copy images (or create symlinks) from commons to a static directory.
489 * This is necessary even if you intend to distribute all of commons, because
490 * the directory contents is used to work out which image description pages
491 * are needed.
492 *
493 * Also copies math images, and full-sized images if the makeSnapshot option
494 * is specified.
495 *
496 */
497 function copyImages( $images ) {
498 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
499 $wgMathPath, $wgMathDirectory;
500 # Find shared uploads and copy them into the static directory
501 $sharedPathLength = strlen( $wgSharedUploadPath );
502 $mathPathLength = strlen( $wgMathPath );
503 $uploadPathLength = strlen( $wgUploadPath );
504 foreach ( $images as $escapedImage => $dummy ) {
505 $image = urldecode( $escapedImage );
506
507 if ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
508 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
509 } elseif ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
510 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
511 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
512 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
513 }
514 }
515 }
516
517 function onGetFullURL( &$title, &$url, $query ) {
518 global $wgContLang, $wgArticlePath;
519
520 $iw = $title->getInterwiki();
521 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
522 if ( $title->getDBkey() == '' ) {
523 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
524 } else {
525 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
526 $wgArticlePath );
527 }
528 return false;
529 } else {
530 return true;
531 }
532 }
533
534 function onGetLocalURL( &$title, &$url, $query ) {
535 global $wgArticlePath;
536
537 if ( $title->isExternal() ) {
538 # Default is fine for interwiki
539 return true;
540 }
541
542 $url = false;
543 if ( $query != '' ) {
544 parse_str( $query, $params );
545 if ( isset($params['action']) && $params['action'] == 'raw' ) {
546 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
547 $file = 'gen.' . $params['gen'];
548 } else {
549 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
550 // Clean up Monobook.css etc.
551 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
552 $file = $matches[1] . '.' . $matches[2];
553 }
554 }
555 $this->rawPages[$file] = array( $file, $title, $params );
556 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
557 }
558 }
559 if ( $url === false ) {
560 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
561 }
562
563 return false;
564 }
565
566 function getHashedFilename( &$title ) {
567 if ( '' != $title->mInterwiki ) {
568 $dbkey = $title->getDBkey();
569 } else {
570 $dbkey = $title->getPrefixedDBkey();
571 }
572
573 $mainPage = Title::newMainPage();
574 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
575 return 'index.html';
576 }
577
578 return $this->getHashedDirectory( $title ) . '/' .
579 $this->getFriendlyName( $dbkey ) . '.html';
580 }
581
582 function getFriendlyName( $name ) {
583 global $wgLang;
584 # Replace illegal characters for Windows paths with underscores
585 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
586
587 # Work out lower case form. We assume we're on a system with case-insensitive
588 # filenames, so unless the case is of a special form, we have to disambiguate
589 if ( function_exists( 'mb_strtolower' ) ) {
590 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
591 } else {
592 $lowerCase = ucfirst( strtolower( $name ) );
593 }
594
595 # Make it mostly unique
596 if ( $lowerCase != $friendlyName ) {
597 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
598 }
599 # Handle colon specially by replacing it with tilde
600 # Thus we reduce the number of paths with hashes appended
601 $friendlyName = str_replace( ':', '~', $friendlyName );
602
603 return $friendlyName;
604 }
605
606 /**
607 * Get a relative directory for putting a title into
608 */
609 function getHashedDirectory( &$title ) {
610 if ( '' != $title->getInterwiki() ) {
611 $pdbk = $title->getDBkey();
612 } else {
613 $pdbk = $title->getPrefixedDBkey();
614 }
615
616 # Find the first colon if there is one, use characters after it
617 $p = strpos( $pdbk, ':' );
618 if ( $p !== false ) {
619 $dbk = substr( $pdbk, $p + 1 );
620 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
621 } else {
622 $dbk = $pdbk;
623 }
624
625 # Split into characters
626 preg_match_all( '/./us', $dbk, $m );
627
628 $chars = $m[0];
629 $length = count( $chars );
630 $dir = '';
631
632 for ( $i = 0; $i < $this->depth; $i++ ) {
633 if ( $i ) {
634 $dir .= '/';
635 }
636 if ( $i >= $length ) {
637 $dir .= '_';
638 } else {
639 $c = $chars[$i];
640 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
641 if ( function_exists( 'mb_strtolower' ) ) {
642 $dir .= mb_strtolower( $c );
643 } else {
644 $dir .= strtolower( $c );
645 }
646 } else {
647 $dir .= sprintf( "%02X", ord( $c ) );
648 }
649 }
650 }
651 return $dir;
652 }
653
654 }
655
656 /** XML parser callback */
657 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
658 global $wgDumpImages;
659
660 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
661 $wgDumpImages[$attribs['SRC']] = true;
662 }
663 }
664
665 /** XML parser callback */
666 function wfDumpEndTagHandler( $parser, $name ) {}
667
668 # vim: syn=php
669 ?>