3 * @addtogroup Maintenance
6 define( 'REPORTING_INTERVAL', 10 );
8 require_once( 'includes/ImagePage.php' );
9 require_once( 'includes/CategoryPage.php' );
10 require_once( 'includes/RawPage.php' );
13 # Destination directory
17 var $noOverwrite = false;
19 # Show interlanguage links?
20 var $interwiki = true;
22 # Depth of HTML directory tree
25 # Directory that commons images are copied into
26 var $sharedStaticDirectory;
28 # Directory that the images are in, after copying
29 var $destUploadDirectory;
31 # Relative path to image directory
32 var $imageRel = 'upload';
34 # Copy commons images instead of symlinking
35 var $forceCopy = false;
37 # Make a copy of all images encountered
38 var $makeSnapshot = false;
40 # Don't image description pages in doEverything()
41 var $noSharedDesc = false;
43 # Make links assuming the script path is in the same directory as
45 var $alternateScriptPath = false;
47 # Original values of various globals
48 var $oldArticlePath = false, $oldCopyrightIcon = false;
50 # Has setupGlobals been called?
51 var $setupDone = false;
53 # Has to compress html pages
54 var $compress = false;
56 # List of raw pages used in the current article
60 var $skin = 'htmldump';
63 var $checkpointFile = false, $checkpoints = false;
65 var $startID = 1, $endID = false;
67 var $sliceNumerator = 1, $sliceDenominator = 1;
69 # Max page ID, lazy initialised
70 var $maxPageID = false;
73 var $udpProfile, $udpProfileCounter = 0, $udpProfileInit = false;
75 function DumpHTML( $settings = array() ) {
76 foreach ( $settings as $var => $value ) {
81 function loadCheckpoints() {
82 if ( $this->checkpoints !== false ) {
84 } elseif ( !$this->checkpointFile ) {
87 $lines = @file( $this->checkpointFile );
88 if ( $lines === false ) {
89 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
90 $this->checkpoints = array();
92 $lines = array_map( 'trim', $lines );
93 $this->checkpoints = array();
94 foreach ( $lines as $line ) {
95 list( $name, $value ) = explode( '=', $line, 2 );
96 $this->checkpoints[$name] = $value;
103 function getCheckpoint( $type, $defValue = false ) {
104 if ( !$this->loadCheckpoints() ) {
107 if ( !isset( $this->checkpoints[$type] ) ) {
110 return $this->checkpoints[$type];
114 function setCheckpoint( $type, $value ) {
115 if ( !$this->checkpointFile ) {
118 $this->checkpoints[$type] = $value;
120 foreach ( $this->checkpoints as $type => $value ) {
121 $blob .= "$type=$value\n";
123 file_put_contents( $this->checkpointFile, $blob );
126 function doEverything() {
127 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
128 print "Checkpoint says everything is already done\n";
132 $this->doCategories();
133 $this->doRedirects();
134 if ( $this->sliceNumerator == 1 ) {
137 $this->doLocalImageDescriptions();
139 if ( !$this->noSharedDesc ) {
140 $this->doSharedImageDescriptions();
143 $this->setCheckpoint( 'everything', 'done' );
147 * Write a set of articles specified by start and end page_id
148 * Skip categories and images, they will be done separately
150 function doArticles() {
151 if ( $this->endID === false ) {
152 $end = $this->getMaxPageID();
156 $start = $this->startID;
158 # Start from the checkpoint
159 $cp = $this->getCheckpoint( 'article' );
160 if ( $cp == 'done' ) {
161 print "Articles already done\n";
163 } elseif ( $cp !== false ) {
165 print "Resuming article dump from checkpoint at page_id $start of $end\n";
167 print "Starting from page_id $start of $end\n";
170 # Move the start point to the correct slice if it isn't there already
171 $start = $this->modSliceStart( $start );
173 $this->setupGlobals();
175 $mainPageObj = Title::newMainPage();
176 $mainPage = $mainPageObj->getPrefixedDBkey();
178 for ( $id = $start, $i = 0; $id <= $end; $id += $this->sliceDenominator, $i++ ) {
179 wfWaitForSlaves( 20 );
180 if ( !( $i % REPORTING_INTERVAL) ) {
181 print "Processing ID: $id\r";
182 $this->setCheckpoint( 'article', $id );
184 if ( !($i % (REPORTING_INTERVAL*10) ) ) {
187 $title = Title::newFromID( $id );
189 $ns = $title->getNamespace() ;
190 if ( $ns != NS_CATEGORY && $ns != NS_MEDIAWIKI &&
191 $title->getPrefixedDBkey() != $mainPage ) {
192 $this->doArticle( $title );
196 $this->setCheckpoint( 'article', 'done' );
200 function doSpecials() {
203 $this->setupGlobals();
204 print "Special:Categories...";
205 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
209 /** Write the main page as index.html */
210 function doMainPage() {
212 print "Making index.html ";
214 // Set up globals with no ../../.. in the link URLs
215 $this->setupGlobals( 0 );
217 $title = Title::newMainPage();
218 $text = $this->getArticleHTML( $title );
220 # Parse the XHTML to find the images
221 $images = $this->findImages( $text );
222 $this->copyImages( $images );
224 $file = fopen( "{$this->dest}/index.html", "w" );
226 print "\nCan't open index.html for writing\n";
229 fwrite( $file, $text );
234 function doImageDescriptions() {
235 $this->doLocalImageDescriptions();
236 if ( !$this->noSharedDesc ) {
237 $this->doSharedImageDescriptions();
242 * Dump image description pages that don't have an associated article, but do
245 function doLocalImageDescriptions() {
248 $dbr = wfGetDB( DB_SLAVE );
250 $cp = $this->getCheckpoint( 'local image' );
251 if ( $cp == 'done' ) {
252 print "Local image descriptions already done\n";
254 } elseif ( $cp !== false ) {
255 print "Writing image description pages starting from $cp\n";
256 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
258 print "Writing image description pages for local images\n";
262 $this->setupGlobals();
266 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
267 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
268 $numRows = $dbr->numRows( $res );
270 while ( $row = $dbr->fetchObject( $res ) ) {
271 # Update conds for the next chunk query
272 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
274 // Slice the result set with a filter
275 if ( !$this->sliceFilter( $row->img_name ) ) {
279 wfWaitForSlaves( 10 );
280 if ( !( ++$i % REPORTING_INTERVAL ) ) {
281 print "{$row->img_name}\n";
282 if ( $row->img_name !== 'done' ) {
283 $this->setCheckpoint( 'local image', $row->img_name );
286 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
287 if ( $title->getArticleID() ) {
288 // Already done by dumpHTML
291 $this->doArticle( $title );
293 $dbr->freeResult( $res );
294 } while ( $numRows );
296 $this->setCheckpoint( 'local image', 'done' );
301 * Dump images which only have a real description page on commons
303 function doSharedImageDescriptions() {
304 list( $start, $end ) = $this->sliceRange( 0, 255 );
306 $cp = $this->getCheckpoint( 'shared image' );
307 if ( $cp == 'done' ) {
308 print "Shared description pages already done\n";
310 } elseif ( $cp !== false ) {
311 print "Writing description pages for commons images starting from directory $cp/255\n";
314 print "Writing description pages for commons images\n";
317 $this->setupGlobals();
319 for ( $hash = $start; $hash <= $end; $hash++ ) {
320 $this->setCheckpoint( 'shared image', $hash );
322 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
323 intval( $hash / 16 ), $hash );
324 $handle = @opendir( $dir );
325 while ( $handle && $file = readdir( $handle ) ) {
326 if ( $file[0] == '.' ) {
329 if ( !(++$i % REPORTING_INTERVAL ) ) {
333 $title = Title::makeTitleSafe( NS_IMAGE, $file );
334 $this->doArticle( $title );
340 $this->setCheckpoint( 'shared image', 'done' );
344 function doCategories() {
347 $this->setupGlobals();
348 $dbr = wfGetDB( DB_SLAVE );
350 $cp = $this->getCheckpoint( 'category' );
351 if ( $cp == 'done' ) {
352 print "Category pages already done\n";
354 } elseif ( $cp !== false ) {
355 print "Resuming category page dump from $cp\n";
356 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
358 print "Starting category pages\n";
364 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
365 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
366 $numRows = $dbr->numRows( $res );
368 while ( $row = $dbr->fetchObject( $res ) ) {
369 // Set conditions for next chunk
370 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
372 // Filter pages from other slices
373 if ( !$this->sliceFilter( $row->cl_to ) ) {
377 wfWaitForSlaves( 10 );
378 if ( !(++$i % REPORTING_INTERVAL ) ) {
379 print "{$row->cl_to}\n";
380 if ( $row->cl_to != 'done' ) {
381 $this->setCheckpoint( 'category', $row->cl_to );
384 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
385 $this->doArticle( $title );
387 $dbr->freeResult( $res );
388 } while ( $numRows );
390 $this->setCheckpoint( 'category', 'done' );
394 function doRedirects() {
395 print "Doing redirects...\n";
398 $end = $this->getMaxPageID();
399 $cp = $this->getCheckpoint( 'redirect' );
400 if ( $cp == 'done' ) {
401 print "Redirects already done\n";
403 } elseif ( $cp !== false ) {
404 print "Resuming redirect generation from page_id $cp\n";
405 $start = intval( $cp );
410 $this->setupGlobals();
411 $dbr = wfGetDB( DB_SLAVE );
414 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
415 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
417 'page_is_redirect' => 1,
418 "page_id BETWEEN $chunkStart AND $chunkEnd"
420 # Modulo slicing in SQL
421 if ( $this->sliceDenominator != 1 ) {
422 $n = intval( $this->sliceNumerator );
423 $m = intval( $this->sliceDenominator );
424 $conds[] = "page_id % $m = $n";
426 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
427 $conds, __METHOD__ );
429 while ( $row = $dbr->fetchObject( $res ) ) {
430 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
431 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
432 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
433 $this->setCheckpoint( 'redirect', $row->page_id );
435 $this->doArticle( $title );
437 $dbr->freeResult( $res );
439 $this->setCheckpoint( 'redirect', 'done' );
442 /** Write an article specified by title */
443 function doArticle( $title ) {
444 if ( $this->noOverwrite ) {
445 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
446 if ( file_exists( $fileName ) ) {
453 $this->rawPages = array();
454 $text = $this->getArticleHTML( $title );
456 if ( $text === false ) {
460 # Parse the XHTML to find the images
461 $images = $this->findImages( $text );
462 $this->copyImages( $images );
465 $this->writeArticle( $title, $text );
468 wfMkdirParents( "{$this->dest}/raw", 0755 );
469 foreach( $this->rawPages as $record ) {
470 list( $file, $title, $params ) = $record;
472 $path = "{$this->dest}/raw/$file";
473 if ( !file_exists( $path ) ) {
474 $article = new Article( $title );
475 $request = new FauxRequest( $params );
476 $rp = new RawPage( $article, $request );
477 $text = $rp->getRawText();
479 print "Writing $file\n";
480 $file = fopen( $path, 'w' );
482 print("Can't open file $path for writing\n");
485 fwrite( $file, $text );
490 wfIncrStats( 'dumphtml_article' );
493 /** Write the given text to the file identified by the given title object */
494 function writeArticle( $title, $text ) {
495 $filename = $this->getHashedFilename( $title );
497 # Temporary hack for current dump, this should be moved to
498 # getFriendlyName() at the earliest opportunity.
500 # Limit filename length to 255 characters, so it works on ext3.
501 # Titles are in fact limited to 255 characters, but dumpHTML
502 # adds a suffix which may put them over the limit.
503 $length = strlen( $filename );
504 if ( $length > 255 ) {
505 print "Warning: Filename too long ($length bytes). Skipping.\n";
509 $fullName = "{$this->dest}/$filename";
510 $fullDir = dirname( $fullName );
512 if ( $this->compress ) {
514 $text = gzencode( $text, 9 );
517 wfMkdirParents( $fullDir, 0755 );
519 wfSuppressWarnings();
520 $file = fopen( $fullName, 'w' );
524 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
528 fwrite( $file, $text );
532 /** Set up globals required for parsing */
533 function setupGlobals( $currentDepth = NULL ) {
534 global $wgUser, $wgStylePath, $wgArticlePath, $wgMathPath;
535 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
536 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
537 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
538 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
539 global $wgGenerateThumbnailOnParse;
541 static $oldLogo = NULL;
543 if ( !$this->setupDone ) {
544 $wgHooks['GetLocalURL'][] =& $this;
545 $wgHooks['GetFullURL'][] =& $this;
546 $wgHooks['SiteNoticeBefore'][] =& $this;
547 $wgHooks['SiteNoticeAfter'][] =& $this;
548 $this->oldArticlePath = $wgServer . $wgArticlePath;
551 if ( is_null( $currentDepth ) ) {
552 $currentDepth = $this->depth;
555 if ( $this->alternateScriptPath ) {
556 if ( $currentDepth == 0 ) {
559 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
562 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
565 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
568 # Allow for repeated setup
569 if ( !is_null( $oldLogo ) ) {
575 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
576 # If it's in the upload directory, rewrite it to the new upload directory
577 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
578 } elseif ( $wgLogo{0} == '/' ) {
579 # This is basically heuristic
580 # Rewrite an absolute logo path to one relative to the the script path
581 $wgLogo = $wgScriptPath . $wgLogo;
585 if ( !$this->setupDone ) {
586 $this->oldCopyrightIcon = $wgCopyrightIcon;
588 $wgCopyrightIcon = str_replace( 'src="/images',
589 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
591 $wgStylePath = "$wgScriptPath/skins";
592 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
593 $wgSharedUploadPath = "$wgUploadPath/shared";
595 $wgHideInterlanguageLinks = !$this->interwiki;
596 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
597 $wgEnableParserCache = false;
598 $wgMathPath = "$wgScriptPath/math";
599 $wgEnableSidebarCache = false;
600 $wgGenerateThumbnailOnParse = true;
602 if ( !empty( $wgRightsText ) ) {
603 $wgRightsUrl = "$wgScriptPath/COPYING.html";
607 $wgUser->setOption( 'skin', $this->skin );
608 $wgUser->setOption( 'editsection', 0 );
610 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
611 if ( realpath( $this->destUploadDirectory ) == realpath( $wgUploadDirectory ) ) {
612 print "Disabling image snapshot because the destination is the same as the source\n";
613 $this->makeSnapshot = false;
615 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
617 $this->setupDone = true;
620 /** Reads the content of a title object, executes the skin and captures the result */
621 function getArticleHTML( $title ) {
622 global $wgOut, $wgTitle, $wgArticle, $wgUser;
624 $linkCache =& LinkCache::singleton();
627 if ( is_null( $wgTitle ) ) {
631 $ns = $wgTitle->getNamespace();
632 if ( $ns == NS_SPECIAL ) {
633 $wgOut = new OutputPage;
634 $wgOut->setParserOptions( new ParserOptions );
635 SpecialPage::executePath( $wgTitle );
637 /** @todo merge with Wiki.php code */
638 if ( $ns == NS_IMAGE ) {
639 $wgArticle = new ImagePage( $wgTitle );
640 } elseif ( $ns == NS_CATEGORY ) {
641 $wgArticle = new CategoryPage( $wgTitle );
643 $wgArticle = new Article( $wgTitle );
645 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
647 return $this->getRedirect( $rt );
649 $wgOut = new OutputPage;
650 $wgOut->setParserOptions( new ParserOptions );
657 $sk =& $wgUser->getSkin();
659 $sk->outputPage( $wgOut );
660 $text = ob_get_contents();
666 function getRedirect( $rt ) {
667 $url = $rt->escapeLocalURL();
668 $text = $rt->getPrefixedText();
670 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
671 <html xmlns="http://www.w3.org/1999/xhtml">
673 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
674 <meta http-equiv="Refresh" content="0;url=$url" />
677 <p>Redirecting to <a href="$url">$text</a></p>
683 /** Returns image paths used in an XHTML document */
684 function findImages( $text ) {
685 global $wgOutputEncoding, $wgDumpImages;
686 $parser = xml_parser_create( $wgOutputEncoding );
687 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
689 $wgDumpImages = array();
690 xml_parse( $parser, $text );
691 xml_parser_free( $parser );
693 return $wgDumpImages;
697 * Copy a file specified by a URL to a given directory
699 * @param string $srcPath The source URL
700 * @param string $srcPathBase The base directory of the source URL
701 * @param string $srcDirBase The base filesystem directory of the source URL
702 * @param string $destDirBase The base filesystem directory of the destination URL
704 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
705 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
706 $sourceLoc = "$srcDirBase/$rel";
707 $destLoc = "$destDirBase/$rel";
708 #print "Copying $sourceLoc to $destLoc\n";
709 if ( !file_exists( $destLoc ) ) {
710 wfMkdirParents( dirname( $destLoc ), 0755 );
711 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
712 if ( !symlink( $sourceLoc, $destLoc ) ) {
713 print "Warning: unable to create symlink at $destLoc\n";
716 if ( !copy( $sourceLoc, $destLoc ) ) {
717 print "Warning: unable to copy $sourceLoc to $destLoc\n";
724 * Copy an image, and if it is a thumbnail, copy its parent image too
726 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
727 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
728 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
729 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
730 # The image was a thumbnail
731 # Copy the source image as well
732 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
733 $parts = explode( '/', $rel );
734 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
735 $newSrc = "$srcPathBase/$rel";
736 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
741 * Copy images (or create symlinks) from commons to a static directory.
742 * This is necessary even if you intend to distribute all of commons, because
743 * the directory contents is used to work out which image description pages
746 * Also copies math images, and full-sized images if the makeSnapshot option
750 function copyImages( $images ) {
751 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
752 $wgMathPath, $wgMathDirectory;
753 # Find shared uploads and copy them into the static directory
754 $sharedPathLength = strlen( $wgSharedUploadPath );
755 $mathPathLength = strlen( $wgMathPath );
756 $uploadPathLength = strlen( $wgUploadPath );
757 foreach ( $images as $escapedImage => $dummy ) {
758 $image = urldecode( $escapedImage );
760 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
761 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
762 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
763 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
764 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
765 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
770 function onGetFullURL( &$title, &$url, $query ) {
771 global $wgContLang, $wgArticlePath;
773 $iw = $title->getInterwiki();
774 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
775 if ( $title->getDBkey() == '' ) {
776 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
778 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
781 $url .= $this->compress ? ".gz" : "";
788 function onGetLocalURL( &$title, &$url, $query ) {
789 global $wgArticlePath;
791 if ( $title->isExternal() ) {
792 # Default is fine for interwiki
797 if ( $query != '' ) {
799 parse_str( $query, $params );
800 if ( isset($params['action']) && $params['action'] == 'raw' ) {
801 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
802 $file = 'gen.' . $params['gen'];
804 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
805 // Clean up Monobook.css etc.
807 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
808 $file = $matches[1] . '.' . $matches[2];
811 $this->rawPages[$file] = array( $file, $title, $params );
812 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
815 if ( $url === false ) {
816 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
818 $url .= $this->compress ? ".gz" : "";
822 function getHashedFilename( &$title ) {
823 if ( '' != $title->mInterwiki ) {
824 $dbkey = $title->getDBkey();
826 $dbkey = $title->getPrefixedDBkey();
829 $mainPage = Title::newMainPage();
830 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
834 return $this->getHashedDirectory( $title ) . '/' .
835 $this->getFriendlyName( $dbkey ) . '.html';
838 function getFriendlyName( $name ) {
840 # Replace illegal characters for Windows paths with underscores
841 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
843 # Work out lower case form. We assume we're on a system with case-insensitive
844 # filenames, so unless the case is of a special form, we have to disambiguate
845 if ( function_exists( 'mb_strtolower' ) ) {
846 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
848 $lowerCase = ucfirst( strtolower( $name ) );
851 # Make it mostly unique
852 if ( $lowerCase != $friendlyName ) {
853 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
855 # Handle colon specially by replacing it with tilde
856 # Thus we reduce the number of paths with hashes appended
857 $friendlyName = str_replace( ':', '~', $friendlyName );
859 return $friendlyName;
863 * Get a relative directory for putting a title into
865 function getHashedDirectory( &$title ) {
866 if ( '' != $title->getInterwiki() ) {
867 $pdbk = $title->getDBkey();
869 $pdbk = $title->getPrefixedDBkey();
872 # Find the first colon if there is one, use characters after it
873 $p = strpos( $pdbk, ':' );
874 if ( $p !== false ) {
875 $dbk = substr( $pdbk, $p + 1 );
876 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
881 # Split into characters
883 preg_match_all( '/./us', $dbk, $m );
886 $length = count( $chars );
889 for ( $i = 0; $i < $this->depth; $i++ ) {
893 if ( $i >= $length ) {
897 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
898 if ( function_exists( 'mb_strtolower' ) ) {
899 $dir .= mb_strtolower( $c );
901 $dir .= strtolower( $c );
904 $dir .= sprintf( "%02X", ord( $c ) );
912 * Calculate the start end end of a job based on the current slice
913 * @param integer $start
914 * @param integer $end
915 * @return array of integers
917 function sliceRange( $start, $end ) {
918 $count = $end - $start + 1;
919 $each = $count / $this->sliceDenominator;
920 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
921 if ( $this->sliceNumerator == $this->sliceDenominator ) {
924 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
926 return array( $sliceStart, $sliceEnd );
930 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
931 * @param integer $start
932 * @param integer $base The true start of the range; the minimum start
934 function modSliceStart( $start, $base = 1 ) {
935 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
939 * Determine whether a string belongs to the current slice, based on hash
941 function sliceFilter( $s ) {
942 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
948 function onSiteNoticeBefore( &$text ) {
952 function onSiteNoticeAfter( &$text ) {
957 function getMaxPageID() {
958 if ( $this->maxPageID === false ) {
959 $dbr = wfGetDB( DB_SLAVE );
960 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
962 return $this->maxPageID;
968 if ( !$this->udpProfile ) {
971 if ( !$this->udpProfileInit ) {
972 $this->udpProfileInit = true;
973 } elseif ( $this->udpProfileCounter == 1 % $this->udpProfile ) {
974 $wgProfiler->getFunctionReport();
975 $wgProfiler = new DumpHTML_ProfilerStub;
977 if ( $this->udpProfileCounter == 0 ) {
978 $wgProfiler = new ProfilerSimpleUDP;
979 $wgProfiler->setProfileID( 'dumpHTML' );
981 $this->udpProfileCounter = ( $this->udpProfileCounter + 1 ) % $this->udpProfile;
985 class DumpHTML_ProfilerStub {
986 function profileIn() {}
987 function profileOut() {}
988 function getOutput() {}
990 function getFunctionReport() {}
993 /** XML parser callback */
994 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
995 global $wgDumpImages;
997 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
998 $wgDumpImages[$attribs['SRC']] = true;
1002 /** XML parser callback */
1003 function wfDumpEndTagHandler( $parser, $name ) {}