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' );
12 # Explicitly disable article view counter (bug 6823)
13 global $wgDisableCounters;
14 $wgDisableCounters = true;
17 # Destination directory
21 var $noOverwrite = false;
23 # Show interlanguage links?
24 var $interwiki = true;
26 # Depth of HTML directory tree
29 # Directory that commons images are copied into
30 var $sharedStaticDirectory;
32 # Directory that the images are in, after copying
33 var $destUploadDirectory;
35 # Relative path to image directory
36 var $imageRel = 'upload';
38 # Copy commons images instead of symlinking
39 var $forceCopy = false;
41 # Make a copy of all images encountered
42 var $makeSnapshot = false;
44 # Don't image description pages in doEverything()
45 var $noSharedDesc = false;
47 # Make links assuming the script path is in the same directory as
49 var $alternateScriptPath = false;
51 # Original values of various globals
52 var $oldArticlePath = false, $oldCopyrightIcon = false;
54 # Has setupGlobals been called?
55 var $setupDone = false;
57 # Has to compress html pages
58 var $compress = false;
60 # List of raw pages used in the current article
64 var $skin = 'htmldump';
67 var $checkpointFile = false, $checkpoints = false;
69 var $startID = 1, $endID = false;
71 var $sliceNumerator = 1, $sliceDenominator = 1;
73 # Max page ID, lazy initialised
74 var $maxPageID = false;
77 var $udpProfile, $udpProfileCounter = 0, $udpProfileInit = false;
79 function DumpHTML( $settings = array() ) {
80 foreach ( $settings as $var => $value ) {
85 function loadCheckpoints() {
86 if ( $this->checkpoints !== false ) {
88 } elseif ( !$this->checkpointFile ) {
91 $lines = @file( $this->checkpointFile );
92 if ( $lines === false ) {
93 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
94 $this->checkpoints = array();
96 $lines = array_map( 'trim', $lines );
97 $this->checkpoints = array();
98 foreach ( $lines as $line ) {
99 list( $name, $value ) = explode( '=', $line, 2 );
100 $this->checkpoints[$name] = $value;
107 function getCheckpoint( $type, $defValue = false ) {
108 if ( !$this->loadCheckpoints() ) {
111 if ( !isset( $this->checkpoints[$type] ) ) {
114 return $this->checkpoints[$type];
118 function setCheckpoint( $type, $value ) {
119 if ( !$this->checkpointFile ) {
122 $this->checkpoints[$type] = $value;
124 foreach ( $this->checkpoints as $type => $value ) {
125 $blob .= "$type=$value\n";
127 file_put_contents( $this->checkpointFile, $blob );
130 function doEverything() {
131 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
132 print "Checkpoint says everything is already done\n";
136 $this->doCategories();
137 $this->doRedirects();
138 if ( $this->sliceNumerator == 1 ) {
141 $this->doLocalImageDescriptions();
143 if ( !$this->noSharedDesc ) {
144 $this->doSharedImageDescriptions();
147 $this->setCheckpoint( 'everything', 'done' );
151 * Write a set of articles specified by start and end page_id
152 * Skip categories and images, they will be done separately
154 function doArticles() {
155 if ( $this->endID === false ) {
156 $end = $this->getMaxPageID();
160 $start = $this->startID;
162 # Start from the checkpoint
163 $cp = $this->getCheckpoint( 'article' );
164 if ( $cp == 'done' ) {
165 print "Articles already done\n";
167 } elseif ( $cp !== false ) {
169 print "Resuming article dump from checkpoint at page_id $start of $end\n";
171 print "Starting from page_id $start of $end\n";
174 # Move the start point to the correct slice if it isn't there already
175 $start = $this->modSliceStart( $start );
177 $this->setupGlobals();
179 $mainPageObj = Title::newMainPage();
180 $mainPage = $mainPageObj->getPrefixedDBkey();
182 for ( $id = $start, $i = 0; $id <= $end; $id += $this->sliceDenominator, $i++ ) {
183 wfWaitForSlaves( 20 );
184 if ( !( $i % REPORTING_INTERVAL) ) {
185 print "Processing ID: $id\r";
186 $this->setCheckpoint( 'article', $id );
188 if ( !($i % (REPORTING_INTERVAL*10) ) ) {
191 $title = Title::newFromID( $id );
193 $ns = $title->getNamespace() ;
194 if ( $ns != NS_CATEGORY && $ns != NS_MEDIAWIKI &&
195 $title->getPrefixedDBkey() != $mainPage ) {
196 $this->doArticle( $title );
200 $this->setCheckpoint( 'article', 'done' );
204 function doSpecials() {
207 $this->setupGlobals();
208 print "Special:Categories...";
209 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
213 /** Write the main page as index.html */
214 function doMainPage() {
216 print "Making index.html ";
218 // Set up globals with no ../../.. in the link URLs
219 $this->setupGlobals( 0 );
221 $title = Title::newMainPage();
222 $text = $this->getArticleHTML( $title );
224 # Parse the XHTML to find the images
225 $images = $this->findImages( $text );
226 $this->copyImages( $images );
228 $file = fopen( "{$this->dest}/index.html", "w" );
230 print "\nCan't open index.html for writing\n";
233 fwrite( $file, $text );
238 function doImageDescriptions() {
239 $this->doLocalImageDescriptions();
240 if ( !$this->noSharedDesc ) {
241 $this->doSharedImageDescriptions();
246 * Dump image description pages that don't have an associated article, but do
249 function doLocalImageDescriptions() {
252 $dbr = wfGetDB( DB_SLAVE );
254 $cp = $this->getCheckpoint( 'local image' );
255 if ( $cp == 'done' ) {
256 print "Local image descriptions already done\n";
258 } elseif ( $cp !== false ) {
259 print "Writing image description pages starting from $cp\n";
260 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
262 print "Writing image description pages for local images\n";
266 $this->setupGlobals();
270 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
271 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
272 $numRows = $dbr->numRows( $res );
274 while ( $row = $dbr->fetchObject( $res ) ) {
275 # Update conds for the next chunk query
276 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
278 // Slice the result set with a filter
279 if ( !$this->sliceFilter( $row->img_name ) ) {
283 wfWaitForSlaves( 10 );
284 if ( !( ++$i % REPORTING_INTERVAL ) ) {
285 print "{$row->img_name}\n";
286 if ( $row->img_name !== 'done' ) {
287 $this->setCheckpoint( 'local image', $row->img_name );
290 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
291 if ( $title->getArticleID() ) {
292 // Already done by dumpHTML
295 $this->doArticle( $title );
297 $dbr->freeResult( $res );
298 } while ( $numRows );
300 $this->setCheckpoint( 'local image', 'done' );
305 * Dump images which only have a real description page on commons
307 function doSharedImageDescriptions() {
308 list( $start, $end ) = $this->sliceRange( 0, 255 );
310 $cp = $this->getCheckpoint( 'shared image' );
311 if ( $cp == 'done' ) {
312 print "Shared description pages already done\n";
314 } elseif ( $cp !== false ) {
315 print "Writing description pages for commons images starting from directory $cp/255\n";
318 print "Writing description pages for commons images\n";
321 $this->setupGlobals();
323 for ( $hash = $start; $hash <= $end; $hash++ ) {
324 $this->setCheckpoint( 'shared image', $hash );
326 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
327 intval( $hash / 16 ), $hash );
328 $handle = @opendir( $dir );
329 while ( $handle && $file = readdir( $handle ) ) {
330 if ( $file[0] == '.' ) {
333 if ( !(++$i % REPORTING_INTERVAL ) ) {
337 $title = Title::makeTitleSafe( NS_IMAGE, $file );
338 $this->doArticle( $title );
344 $this->setCheckpoint( 'shared image', 'done' );
348 function doCategories() {
351 $this->setupGlobals();
352 $dbr = wfGetDB( DB_SLAVE );
354 $cp = $this->getCheckpoint( 'category' );
355 if ( $cp == 'done' ) {
356 print "Category pages already done\n";
358 } elseif ( $cp !== false ) {
359 print "Resuming category page dump from $cp\n";
360 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
362 print "Starting category pages\n";
368 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
369 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
370 $numRows = $dbr->numRows( $res );
372 while ( $row = $dbr->fetchObject( $res ) ) {
373 // Set conditions for next chunk
374 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
376 // Filter pages from other slices
377 if ( !$this->sliceFilter( $row->cl_to ) ) {
381 wfWaitForSlaves( 10 );
382 if ( !(++$i % REPORTING_INTERVAL ) ) {
383 print "{$row->cl_to}\n";
384 if ( $row->cl_to != 'done' ) {
385 $this->setCheckpoint( 'category', $row->cl_to );
388 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
389 $this->doArticle( $title );
391 $dbr->freeResult( $res );
392 } while ( $numRows );
394 $this->setCheckpoint( 'category', 'done' );
398 function doRedirects() {
399 print "Doing redirects...\n";
402 $end = $this->getMaxPageID();
403 $cp = $this->getCheckpoint( 'redirect' );
404 if ( $cp == 'done' ) {
405 print "Redirects already done\n";
407 } elseif ( $cp !== false ) {
408 print "Resuming redirect generation from page_id $cp\n";
409 $start = intval( $cp );
414 $this->setupGlobals();
415 $dbr = wfGetDB( DB_SLAVE );
418 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
419 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
421 'page_is_redirect' => 1,
422 "page_id BETWEEN $chunkStart AND $chunkEnd"
424 # Modulo slicing in SQL
425 if ( $this->sliceDenominator != 1 ) {
426 $n = intval( $this->sliceNumerator );
427 $m = intval( $this->sliceDenominator );
428 $conds[] = "page_id % $m = $n";
430 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
431 $conds, __METHOD__ );
433 while ( $row = $dbr->fetchObject( $res ) ) {
434 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
435 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
436 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
437 $this->setCheckpoint( 'redirect', $row->page_id );
439 $this->doArticle( $title );
441 $dbr->freeResult( $res );
443 $this->setCheckpoint( 'redirect', 'done' );
446 /** Write an article specified by title */
447 function doArticle( $title ) {
448 if ( $this->noOverwrite ) {
449 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
450 if ( file_exists( $fileName ) ) {
457 $this->rawPages = array();
458 $text = $this->getArticleHTML( $title );
460 if ( $text === false ) {
464 # Parse the XHTML to find the images
465 $images = $this->findImages( $text );
466 $this->copyImages( $images );
469 $this->writeArticle( $title, $text );
472 wfMkdirParents( "{$this->dest}/raw", 0755 );
473 foreach( $this->rawPages as $record ) {
474 list( $file, $title, $params ) = $record;
476 $path = "{$this->dest}/raw/$file";
477 if ( !file_exists( $path ) ) {
478 $article = new Article( $title );
479 $request = new FauxRequest( $params );
480 $rp = new RawPage( $article, $request );
481 $text = $rp->getRawText();
483 print "Writing $file\n";
484 $file = fopen( $path, 'w' );
486 print("Can't open file $path for writing\n");
489 fwrite( $file, $text );
494 wfIncrStats( 'dumphtml_article' );
497 /** Write the given text to the file identified by the given title object */
498 function writeArticle( $title, $text ) {
499 $filename = $this->getHashedFilename( $title );
501 # Temporary hack for current dump, this should be moved to
502 # getFriendlyName() at the earliest opportunity.
504 # Limit filename length to 255 characters, so it works on ext3.
505 # Titles are in fact limited to 255 characters, but dumpHTML
506 # adds a suffix which may put them over the limit.
507 $length = strlen( $filename );
508 if ( $length > 255 ) {
509 print "Warning: Filename too long ($length bytes). Skipping.\n";
513 $fullName = "{$this->dest}/$filename";
514 $fullDir = dirname( $fullName );
516 if ( $this->compress ) {
518 $text = gzencode( $text, 9 );
521 wfMkdirParents( $fullDir, 0755 );
523 wfSuppressWarnings();
524 $file = fopen( $fullName, 'w' );
528 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
532 fwrite( $file, $text );
536 /** Set up globals required for parsing */
537 function setupGlobals( $currentDepth = NULL ) {
538 global $wgUser, $wgStylePath, $wgArticlePath, $wgMathPath;
539 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
540 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
541 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
542 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
543 global $wgGenerateThumbnailOnParse;
545 static $oldLogo = NULL;
547 if ( !$this->setupDone ) {
548 $wgHooks['GetLocalURL'][] =& $this;
549 $wgHooks['GetFullURL'][] =& $this;
550 $wgHooks['SiteNoticeBefore'][] =& $this;
551 $wgHooks['SiteNoticeAfter'][] =& $this;
552 $this->oldArticlePath = $wgServer . $wgArticlePath;
555 if ( is_null( $currentDepth ) ) {
556 $currentDepth = $this->depth;
559 if ( $this->alternateScriptPath ) {
560 if ( $currentDepth == 0 ) {
563 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
566 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
569 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
572 # Allow for repeated setup
573 if ( !is_null( $oldLogo ) ) {
579 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
580 # If it's in the upload directory, rewrite it to the new upload directory
581 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
582 } elseif ( $wgLogo{0} == '/' ) {
583 # This is basically heuristic
584 # Rewrite an absolute logo path to one relative to the the script path
585 $wgLogo = $wgScriptPath . $wgLogo;
589 if ( !$this->setupDone ) {
590 $this->oldCopyrightIcon = $wgCopyrightIcon;
592 $wgCopyrightIcon = str_replace( 'src="/images',
593 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
595 $wgStylePath = "$wgScriptPath/skins";
596 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
597 $wgSharedUploadPath = "$wgUploadPath/shared";
599 $wgHideInterlanguageLinks = !$this->interwiki;
600 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
601 $wgEnableParserCache = false;
602 $wgMathPath = "$wgScriptPath/math";
603 $wgEnableSidebarCache = false;
604 $wgGenerateThumbnailOnParse = true;
606 if ( !empty( $wgRightsText ) ) {
607 $wgRightsUrl = "$wgScriptPath/COPYING.html";
611 $wgUser->setOption( 'skin', $this->skin );
612 $wgUser->setOption( 'editsection', 0 );
614 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
615 if ( realpath( $this->destUploadDirectory ) == realpath( $wgUploadDirectory ) ) {
616 print "Disabling image snapshot because the destination is the same as the source\n";
617 $this->makeSnapshot = false;
619 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
621 $this->setupDone = true;
624 /** Reads the content of a title object, executes the skin and captures the result */
625 function getArticleHTML( $title ) {
626 global $wgOut, $wgTitle, $wgArticle, $wgUser;
628 $linkCache =& LinkCache::singleton();
631 if ( is_null( $wgTitle ) ) {
635 $ns = $wgTitle->getNamespace();
636 if ( $ns == NS_SPECIAL ) {
637 $wgOut = new OutputPage;
638 $wgOut->setParserOptions( new ParserOptions );
639 SpecialPage::executePath( $wgTitle );
641 /** @todo merge with Wiki.php code */
642 if ( $ns == NS_IMAGE ) {
643 $wgArticle = new ImagePage( $wgTitle );
644 } elseif ( $ns == NS_CATEGORY ) {
645 $wgArticle = new CategoryPage( $wgTitle );
647 $wgArticle = new Article( $wgTitle );
649 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
651 return $this->getRedirect( $rt );
653 $wgOut = new OutputPage;
654 $wgOut->setParserOptions( new ParserOptions );
661 $sk =& $wgUser->getSkin();
663 $sk->outputPage( $wgOut );
664 $text = ob_get_contents();
670 function getRedirect( $rt ) {
671 $url = $rt->escapeLocalURL();
672 $text = $rt->getPrefixedText();
674 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
675 <html xmlns="http://www.w3.org/1999/xhtml">
677 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
678 <meta http-equiv="Refresh" content="0;url=$url" />
681 <p>Redirecting to <a href="$url">$text</a></p>
687 /** Returns image paths used in an XHTML document */
688 function findImages( $text ) {
689 global $wgOutputEncoding, $wgDumpImages;
690 $parser = xml_parser_create( $wgOutputEncoding );
691 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
693 $wgDumpImages = array();
694 xml_parse( $parser, $text );
695 xml_parser_free( $parser );
697 return $wgDumpImages;
701 * Copy a file specified by a URL to a given directory
703 * @param string $srcPath The source URL
704 * @param string $srcPathBase The base directory of the source URL
705 * @param string $srcDirBase The base filesystem directory of the source URL
706 * @param string $destDirBase The base filesystem directory of the destination URL
708 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
709 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
710 $sourceLoc = "$srcDirBase/$rel";
711 $destLoc = "$destDirBase/$rel";
712 #print "Copying $sourceLoc to $destLoc\n";
713 if ( !file_exists( $destLoc ) ) {
714 wfMkdirParents( dirname( $destLoc ), 0755 );
715 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
716 if ( !symlink( $sourceLoc, $destLoc ) ) {
717 print "Warning: unable to create symlink at $destLoc\n";
720 if ( !copy( $sourceLoc, $destLoc ) ) {
721 print "Warning: unable to copy $sourceLoc to $destLoc\n";
728 * Copy an image, and if it is a thumbnail, copy its parent image too
730 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
731 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
732 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
733 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
734 # The image was a thumbnail
735 # Copy the source image as well
736 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
737 $parts = explode( '/', $rel );
738 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
739 $newSrc = "$srcPathBase/$rel";
740 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
745 * Copy images (or create symlinks) from commons to a static directory.
746 * This is necessary even if you intend to distribute all of commons, because
747 * the directory contents is used to work out which image description pages
750 * Also copies math images, and full-sized images if the makeSnapshot option
754 function copyImages( $images ) {
755 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
756 $wgMathPath, $wgMathDirectory;
757 # Find shared uploads and copy them into the static directory
758 $sharedPathLength = strlen( $wgSharedUploadPath );
759 $mathPathLength = strlen( $wgMathPath );
760 $uploadPathLength = strlen( $wgUploadPath );
761 foreach ( $images as $escapedImage => $dummy ) {
762 $image = urldecode( $escapedImage );
764 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
765 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
766 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
767 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
768 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
769 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
774 function onGetFullURL( &$title, &$url, $query ) {
775 global $wgContLang, $wgArticlePath;
777 $iw = $title->getInterwiki();
778 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
779 if ( $title->getDBkey() == '' ) {
780 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
782 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
785 $url .= $this->compress ? ".gz" : "";
792 function onGetLocalURL( &$title, &$url, $query ) {
793 global $wgArticlePath;
795 if ( $title->isExternal() ) {
796 # Default is fine for interwiki
801 if ( $query != '' ) {
803 parse_str( $query, $params );
804 if ( isset($params['action']) && $params['action'] == 'raw' ) {
805 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
806 $file = 'gen.' . $params['gen'];
808 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
809 // Clean up Monobook.css etc.
811 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
812 $file = $matches[1] . '.' . $matches[2];
815 $this->rawPages[$file] = array( $file, $title, $params );
816 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
819 if ( $url === false ) {
820 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
822 $url .= $this->compress ? ".gz" : "";
826 function getHashedFilename( &$title ) {
827 if ( '' != $title->mInterwiki ) {
828 $dbkey = $title->getDBkey();
830 $dbkey = $title->getPrefixedDBkey();
833 $mainPage = Title::newMainPage();
834 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
838 return $this->getHashedDirectory( $title ) . '/' .
839 $this->getFriendlyName( $dbkey ) . '.html';
842 function getFriendlyName( $name ) {
844 # Replace illegal characters for Windows paths with underscores
845 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
847 # Work out lower case form. We assume we're on a system with case-insensitive
848 # filenames, so unless the case is of a special form, we have to disambiguate
849 if ( function_exists( 'mb_strtolower' ) ) {
850 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
852 $lowerCase = ucfirst( strtolower( $name ) );
855 # Make it mostly unique
856 if ( $lowerCase != $friendlyName ) {
857 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
859 # Handle colon specially by replacing it with tilde
860 # Thus we reduce the number of paths with hashes appended
861 $friendlyName = str_replace( ':', '~', $friendlyName );
863 return $friendlyName;
867 * Get a relative directory for putting a title into
869 function getHashedDirectory( &$title ) {
870 if ( '' != $title->getInterwiki() ) {
871 $pdbk = $title->getDBkey();
873 $pdbk = $title->getPrefixedDBkey();
876 # Find the first colon if there is one, use characters after it
877 $p = strpos( $pdbk, ':' );
878 if ( $p !== false ) {
879 $dbk = substr( $pdbk, $p + 1 );
880 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
885 # Split into characters
887 preg_match_all( '/./us', $dbk, $m );
890 $length = count( $chars );
893 for ( $i = 0; $i < $this->depth; $i++ ) {
897 if ( $i >= $length ) {
901 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
902 if ( function_exists( 'mb_strtolower' ) ) {
903 $dir .= mb_strtolower( $c );
905 $dir .= strtolower( $c );
908 $dir .= sprintf( "%02X", ord( $c ) );
916 * Calculate the start end end of a job based on the current slice
917 * @param integer $start
918 * @param integer $end
919 * @return array of integers
921 function sliceRange( $start, $end ) {
922 $count = $end - $start + 1;
923 $each = $count / $this->sliceDenominator;
924 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
925 if ( $this->sliceNumerator == $this->sliceDenominator ) {
928 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
930 return array( $sliceStart, $sliceEnd );
934 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
935 * @param integer $start
936 * @param integer $base The true start of the range; the minimum start
938 function modSliceStart( $start, $base = 1 ) {
939 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
943 * Determine whether a string belongs to the current slice, based on hash
945 function sliceFilter( $s ) {
946 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
952 function onSiteNoticeBefore( &$text ) {
956 function onSiteNoticeAfter( &$text ) {
961 function getMaxPageID() {
962 if ( $this->maxPageID === false ) {
963 $dbr = wfGetDB( DB_SLAVE );
964 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
966 return $this->maxPageID;
972 if ( !$this->udpProfile ) {
975 if ( !$this->udpProfileInit ) {
976 $this->udpProfileInit = true;
977 } elseif ( $this->udpProfileCounter == 1 % $this->udpProfile ) {
978 $wgProfiler->getFunctionReport();
979 $wgProfiler = new DumpHTML_ProfilerStub;
981 if ( $this->udpProfileCounter == 0 ) {
982 $wgProfiler = new ProfilerSimpleUDP;
983 $wgProfiler->setProfileID( 'dumpHTML' );
985 $this->udpProfileCounter = ( $this->udpProfileCounter + 1 ) % $this->udpProfile;
989 class DumpHTML_ProfilerStub {
990 function profileIn() {}
991 function profileOut() {}
992 function getOutput() {}
994 function getFunctionReport() {}
997 /** XML parser callback */
998 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
999 global $wgDumpImages;
1001 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
1002 $wgDumpImages[$attribs['SRC']] = true;
1006 /** XML parser callback */
1007 function wfDumpEndTagHandler( $parser, $name ) {}