4 * @subpackage Maintenance
7 define( 'REPORTING_INTERVAL', 10 );
9 require_once( 'includes/ImagePage.php' );
10 require_once( 'includes/CategoryPage.php' );
11 require_once( 'includes/RawPage.php' );
14 # Destination directory
18 var $noOverwrite = false;
20 # Show interlanguage links?
21 var $interwiki = true;
23 # Depth of HTML directory tree
26 # Directory that commons images are copied into
27 var $sharedStaticDirectory;
29 # Directory that the images are in, after copying
30 var $destUploadDirectory;
32 # Relative path to image directory
33 var $imageRel = 'upload';
35 # Copy commons images instead of symlinking
36 var $forceCopy = false;
38 # Make a copy of all images encountered
39 var $makeSnapshot = false;
41 # Don't image description pages in doEverything()
42 var $noSharedDesc = false;
44 # Make links assuming the script path is in the same directory as
46 var $alternateScriptPath = false;
48 # Original values of various globals
49 var $oldArticlePath = false, $oldCopyrightIcon = false;
51 # Has setupGlobals been called?
52 var $setupDone = false;
54 # Has to compress html pages
55 var $compress = false;
57 # List of raw pages used in the current article
61 var $skin = 'htmldump';
64 var $checkpointFile = false, $checkpoints = false;
66 var $startID = 1, $endID = false;
68 var $sliceNumerator = 1, $sliceDenominator = 1;
70 # Max page ID, lazy initialised
71 var $maxPageID = false;
74 var $udpProfile, $udpProfileCounter = 0, $udpProfileInit = false;
76 function DumpHTML( $settings = array() ) {
77 foreach ( $settings as $var => $value ) {
82 function loadCheckpoints() {
83 if ( $this->checkpoints !== false ) {
85 } elseif ( !$this->checkpointFile ) {
88 $lines = @file( $this->checkpointFile );
89 if ( $lines === false ) {
90 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
91 $this->checkpoints = array();
93 $lines = array_map( 'trim', $lines );
94 $this->checkpoints = array();
95 foreach ( $lines as $line ) {
96 list( $name, $value ) = explode( '=', $line, 2 );
97 $this->checkpoints[$name] = $value;
104 function getCheckpoint( $type, $defValue = false ) {
105 if ( !$this->loadCheckpoints() ) {
108 if ( !isset( $this->checkpoints[$type] ) ) {
111 return $this->checkpoints[$type];
115 function setCheckpoint( $type, $value ) {
116 if ( !$this->checkpointFile ) {
119 $this->checkpoints[$type] = $value;
121 foreach ( $this->checkpoints as $type => $value ) {
122 $blob .= "$type=$value\n";
124 file_put_contents( $this->checkpointFile, $blob );
127 function doEverything() {
128 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
129 print "Checkpoint says everything is already done\n";
133 $this->doCategories();
134 $this->doRedirects();
135 if ( $this->sliceNumerator == 1 ) {
138 $this->doLocalImageDescriptions();
140 if ( !$this->noSharedDesc ) {
141 $this->doSharedImageDescriptions();
144 $this->setCheckpoint( 'everything', 'done' );
148 * Write a set of articles specified by start and end page_id
149 * Skip categories and images, they will be done separately
151 function doArticles() {
152 if ( $this->endID === false ) {
153 $end = $this->getMaxPageID();
157 $start = $this->startID;
159 # Start from the checkpoint
160 $cp = $this->getCheckpoint( 'article' );
161 if ( $cp == 'done' ) {
162 print "Articles already done\n";
164 } elseif ( $cp !== false ) {
166 print "Resuming article dump from checkpoint at page_id $start of $end\n";
168 print "Starting from page_id $start of $end\n";
171 # Move the start point to the correct slice if it isn't there already
172 $start = $this->modSliceStart( $start );
174 $this->setupGlobals();
176 $mainPageObj = Title::newMainPage();
177 $mainPage = $mainPageObj->getPrefixedDBkey();
179 for ( $id = $start, $i = 0; $id <= $end; $id += $this->sliceDenominator, $i++ ) {
180 wfWaitForSlaves( 20 );
181 if ( !( $i % REPORTING_INTERVAL) ) {
182 print "Processing ID: $id\r";
183 $this->setCheckpoint( 'article', $id );
185 if ( !($i % (REPORTING_INTERVAL*10) ) ) {
188 $title = Title::newFromID( $id );
190 $ns = $title->getNamespace() ;
191 if ( $ns != NS_CATEGORY && $ns != NS_MEDIAWIKI &&
192 $title->getPrefixedDBkey() != $mainPage ) {
193 $this->doArticle( $title );
197 $this->setCheckpoint( 'article', 'done' );
201 function doSpecials() {
204 $this->setupGlobals();
205 print "Special:Categories...";
206 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
210 /** Write the main page as index.html */
211 function doMainPage() {
213 print "Making index.html ";
215 // Set up globals with no ../../.. in the link URLs
216 $this->setupGlobals( 0 );
218 $title = Title::newMainPage();
219 $text = $this->getArticleHTML( $title );
221 # Parse the XHTML to find the images
222 $images = $this->findImages( $text );
223 $this->copyImages( $images );
225 $file = fopen( "{$this->dest}/index.html", "w" );
227 print "\nCan't open index.html for writing\n";
230 fwrite( $file, $text );
235 function doImageDescriptions() {
236 $this->doLocalImageDescriptions();
237 if ( !$this->noSharedDesc ) {
238 $this->doSharedImageDescriptions();
243 * Dump image description pages that don't have an associated article, but do
246 function doLocalImageDescriptions() {
247 global $wgSharedUploadDirectory;
250 $dbr =& wfGetDB( DB_SLAVE );
252 $cp = $this->getCheckpoint( 'local image' );
253 if ( $cp == 'done' ) {
254 print "Local image descriptions already done\n";
256 } elseif ( $cp !== false ) {
257 print "Writing image description pages starting from $cp\n";
258 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
260 print "Writing image description pages for local images\n";
264 $this->setupGlobals();
268 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
269 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
270 $numRows = $dbr->numRows( $res );
272 while ( $row = $dbr->fetchObject( $res ) ) {
273 # Update conds for the next chunk query
274 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
276 // Slice the result set with a filter
277 if ( !$this->sliceFilter( $row->img_name ) ) {
281 wfWaitForSlaves( 10 );
282 if ( !( ++$i % REPORTING_INTERVAL ) ) {
283 print "{$row->img_name}\n";
284 if ( $row->img_name !== 'done' ) {
285 $this->setCheckpoint( 'local image', $row->img_name );
288 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
289 if ( $title->getArticleID() ) {
290 // Already done by dumpHTML
293 $this->doArticle( $title );
295 $dbr->freeResult( $res );
296 } while ( $numRows );
298 $this->setCheckpoint( 'local image', 'done' );
303 * Dump images which only have a real description page on commons
305 function doSharedImageDescriptions() {
306 list( $start, $end ) = $this->sliceRange( 0, 255 );
308 $cp = $this->getCheckpoint( 'shared image' );
309 if ( $cp == 'done' ) {
310 print "Shared description pages already done\n";
312 } elseif ( $cp !== false ) {
313 print "Writing description pages for commons images starting from directory $cp/255\n";
316 print "Writing description pages for commons images\n";
319 $this->setupGlobals();
321 for ( $hash = $start; $hash <= $end; $hash++ ) {
322 $this->setCheckpoint( 'shared image', $hash );
324 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
325 intval( $hash / 16 ), $hash );
326 $handle = @opendir( $dir );
327 while ( $handle && $file = readdir( $handle ) ) {
328 if ( $file[0] == '.' ) {
331 if ( !(++$i % REPORTING_INTERVAL ) ) {
335 $title = Title::makeTitleSafe( NS_IMAGE, $file );
336 $this->doArticle( $title );
342 $this->setCheckpoint( 'shared image', 'done' );
346 function doCategories() {
349 $this->setupGlobals();
350 $dbr =& wfGetDB( DB_SLAVE );
352 $cp = $this->getCheckpoint( 'category' );
353 if ( $cp == 'done' ) {
354 print "Category pages already done\n";
356 } elseif ( $cp !== false ) {
357 print "Resuming category page dump from $cp\n";
358 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
360 print "Starting category pages\n";
366 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
367 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
368 $numRows = $dbr->numRows( $res );
370 while ( $row = $dbr->fetchObject( $res ) ) {
371 // Set conditions for next chunk
372 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
374 // Filter pages from other slices
375 if ( !$this->sliceFilter( $row->cl_to ) ) {
379 wfWaitForSlaves( 10 );
380 if ( !(++$i % REPORTING_INTERVAL ) ) {
381 print "{$row->cl_to}\n";
382 if ( $row->cl_to != 'done' ) {
383 $this->setCheckpoint( 'category', $row->cl_to );
386 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
387 $this->doArticle( $title );
389 $dbr->freeResult( $res );
390 } while ( $numRows );
392 $this->setCheckpoint( 'category', 'done' );
396 function doRedirects() {
397 print "Doing redirects...\n";
400 $end = $this->getMaxPageID();
401 $cp = $this->getCheckpoint( 'redirect' );
402 if ( $cp == 'done' ) {
403 print "Redirects already done\n";
405 } elseif ( $cp !== false ) {
406 print "Resuming redirect generation from page_id $cp\n";
407 $start = intval( $cp );
412 $this->setupGlobals();
413 $dbr =& wfGetDB( DB_SLAVE );
416 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
417 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
419 'page_is_redirect' => 1,
420 "page_id BETWEEN $chunkStart AND $chunkEnd"
422 # Modulo slicing in SQL
423 if ( $this->sliceDenominator != 1 ) {
424 $n = intval( $this->sliceNumerator );
425 $m = intval( $this->sliceDenominator );
426 $conds[] = "page_id % $m = $n";
428 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
429 $conds, __METHOD__ );
431 while ( $row = $dbr->fetchObject( $res ) ) {
432 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
433 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
434 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
435 $this->setCheckpoint( 'redirect', $row->page_id );
437 $this->doArticle( $title );
439 $dbr->freeResult( $res );
441 $this->setCheckpoint( 'redirect', 'done' );
444 /** Write an article specified by title */
445 function doArticle( $title ) {
446 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
447 global $wgUploadDirectory;
449 if ( $this->noOverwrite ) {
450 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
451 if ( file_exists( $fileName ) ) {
458 $this->rawPages = array();
459 $text = $this->getArticleHTML( $title );
461 if ( $text === false ) {
465 # Parse the XHTML to find the images
466 $images = $this->findImages( $text );
467 $this->copyImages( $images );
470 $this->writeArticle( $title, $text );
473 wfMkdirParents( "{$this->dest}/raw", 0755 );
474 foreach( $this->rawPages as $record ) {
475 list( $file, $title, $params ) = $record;
477 $path = "{$this->dest}/raw/$file";
478 if ( !file_exists( $path ) ) {
479 $article = new Article( $title );
480 $request = new FauxRequest( $params );
481 $rp = new RawPage( $article, $request );
482 $text = $rp->getRawText();
484 print "Writing $file\n";
485 $file = fopen( $path, 'w' );
487 print("Can't open file $fullName for writing\n");
490 fwrite( $file, $text );
495 wfIncrStats( 'dumphtml_article' );
498 /** Write the given text to the file identified by the given title object */
499 function writeArticle( &$title, $text ) {
500 $filename = $this->getHashedFilename( $title );
502 # Temporary hack for current dump, this should be moved to
503 # getFriendlyName() at the earliest opportunity.
505 # Limit filename length to 255 characters, so it works on ext3.
506 # Titles are in fact limited to 255 characters, but dumpHTML
507 # adds a suffix which may put them over the limit.
508 $length = strlen( $filename );
509 if ( $length > 255 ) {
510 print "Warning: Filename too long ($length bytes). Skipping.\n";
514 $fullName = "{$this->dest}/$filename";
515 $fullDir = dirname( $fullName );
517 if ( $this->compress ) {
519 $text = gzencode( $text, 9 );
522 wfMkdirParents( $fullDir, 0755 );
524 wfSuppressWarnings();
525 $file = fopen( $fullName, 'w' );
529 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
533 fwrite( $file, $text );
537 /** Set up globals required for parsing */
538 function setupGlobals( $currentDepth = NULL ) {
539 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
540 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
541 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
542 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
543 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
544 global $wgGenerateThumbnailOnParse;
546 static $oldLogo = NULL;
548 if ( !$this->setupDone ) {
549 $wgHooks['GetLocalURL'][] =& $this;
550 $wgHooks['GetFullURL'][] =& $this;
551 $wgHooks['SiteNoticeBefore'][] =& $this;
552 $wgHooks['SiteNoticeAfter'][] =& $this;
553 $this->oldArticlePath = $wgServer . $wgArticlePath;
556 if ( is_null( $currentDepth ) ) {
557 $currentDepth = $this->depth;
560 if ( $this->alternateScriptPath ) {
561 if ( $currentDepth == 0 ) {
564 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
567 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
570 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
573 # Allow for repeated setup
574 if ( !is_null( $oldLogo ) ) {
580 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
581 # If it's in the upload directory, rewrite it to the new upload directory
582 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
583 } elseif ( $wgLogo{0} == '/' ) {
584 # This is basically heuristic
585 # Rewrite an absolute logo path to one relative to the the script path
586 $wgLogo = $wgScriptPath . $wgLogo;
590 if ( !$this->setupDone ) {
591 $this->oldCopyrightIcon = $wgCopyrightIcon;
593 $wgCopyrightIcon = str_replace( 'src="/images',
594 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
596 $wgStylePath = "$wgScriptPath/skins";
597 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
598 $wgSharedUploadPath = "$wgUploadPath/shared";
600 $wgHideInterlanguageLinks = !$this->interwiki;
601 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
602 $wgEnableParserCache = false;
603 $wgMathPath = "$wgScriptPath/math";
604 $wgEnableSidebarCache = false;
605 $wgGenerateThumbnailOnParse = true;
607 if ( !empty( $wgRightsText ) ) {
608 $wgRightsUrl = "$wgScriptPath/COPYING.html";
612 $wgUser->setOption( 'skin', $this->skin );
613 $wgUser->setOption( 'editsection', 0 );
615 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
616 if ( realpath( $this->destUploadDirectory ) == realpath( $wgUploadDirectory ) ) {
617 print "Disabling image snapshot because the destination is the same as the source\n";
618 $this->makeSnapshot = false;
620 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
622 $this->setupDone = true;
625 /** Reads the content of a title object, executes the skin and captures the result */
626 function getArticleHTML( &$title ) {
627 global $wgOut, $wgTitle, $wgArticle, $wgUser;
629 $linkCache =& LinkCache::singleton();
632 if ( is_null( $wgTitle ) ) {
636 $ns = $wgTitle->getNamespace();
637 if ( $ns == NS_SPECIAL ) {
638 $wgOut = new OutputPage;
639 $wgOut->setParserOptions( new ParserOptions );
640 SpecialPage::executePath( $wgTitle );
642 /** @todo merge with Wiki.php code */
643 if ( $ns == NS_IMAGE ) {
644 $wgArticle = new ImagePage( $wgTitle );
645 } elseif ( $ns == NS_CATEGORY ) {
646 $wgArticle = new CategoryPage( $wgTitle );
648 $wgArticle = new Article( $wgTitle );
650 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
652 return $this->getRedirect( $rt );
654 $wgOut = new OutputPage;
655 $wgOut->setParserOptions( new ParserOptions );
662 $sk =& $wgUser->getSkin();
664 $sk->outputPage( $wgOut );
665 $text = ob_get_contents();
671 function getRedirect( $rt ) {
672 $url = $rt->escapeLocalURL();
673 $text = $rt->getPrefixedText();
675 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
676 <html xmlns="http://www.w3.org/1999/xhtml">
678 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
679 <meta http-equiv="Refresh" content="0;url=$url" />
682 <p>Redirecting to <a href="$url">$text</a></p>
688 /** Returns image paths used in an XHTML document */
689 function findImages( $text ) {
690 global $wgOutputEncoding, $wgDumpImages;
691 $parser = xml_parser_create( $wgOutputEncoding );
692 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
694 $wgDumpImages = array();
695 xml_parse( $parser, $text );
696 xml_parser_free( $parser );
698 return $wgDumpImages;
702 * Copy a file specified by a URL to a given directory
704 * @param string $srcPath The source URL
705 * @param string $srcPathBase The base directory of the source URL
706 * @param string $srcDirBase The base filesystem directory of the source URL
707 * @param string $destDirBase The base filesystem directory of the destination URL
709 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
710 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
711 $sourceLoc = "$srcDirBase/$rel";
712 $destLoc = "$destDirBase/$rel";
713 #print "Copying $sourceLoc to $destLoc\n";
714 if ( !file_exists( $destLoc ) ) {
715 wfMkdirParents( dirname( $destLoc ), 0755 );
716 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
717 if ( !symlink( $sourceLoc, $destLoc ) ) {
718 print "Warning: unable to create symlink at $destLoc\n";
721 if ( !copy( $sourceLoc, $destLoc ) ) {
722 print "Warning: unable to copy $sourceLoc to $destLoc\n";
729 * Copy an image, and if it is a thumbnail, copy its parent image too
731 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
732 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
733 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
734 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
735 # The image was a thumbnail
736 # Copy the source image as well
737 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
738 $parts = explode( '/', $rel );
739 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
740 $newSrc = "$srcPathBase/$rel";
741 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
746 * Copy images (or create symlinks) from commons to a static directory.
747 * This is necessary even if you intend to distribute all of commons, because
748 * the directory contents is used to work out which image description pages
751 * Also copies math images, and full-sized images if the makeSnapshot option
755 function copyImages( $images ) {
756 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
757 $wgMathPath, $wgMathDirectory;
758 # Find shared uploads and copy them into the static directory
759 $sharedPathLength = strlen( $wgSharedUploadPath );
760 $mathPathLength = strlen( $wgMathPath );
761 $uploadPathLength = strlen( $wgUploadPath );
762 foreach ( $images as $escapedImage => $dummy ) {
763 $image = urldecode( $escapedImage );
765 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
766 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
767 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
768 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
769 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
770 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
775 function onGetFullURL( &$title, &$url, $query ) {
776 global $wgContLang, $wgArticlePath;
778 $iw = $title->getInterwiki();
779 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
780 if ( $title->getDBkey() == '' ) {
781 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
783 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
786 $url .= $this->compress ? ".gz" : "";
793 function onGetLocalURL( &$title, &$url, $query ) {
794 global $wgArticlePath;
796 if ( $title->isExternal() ) {
797 # Default is fine for interwiki
802 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.
810 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
811 $file = $matches[1] . '.' . $matches[2];
814 $this->rawPages[$file] = array( $file, $title, $params );
815 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
818 if ( $url === false ) {
819 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
821 $url .= $this->compress ? ".gz" : "";
825 function getHashedFilename( &$title ) {
826 if ( '' != $title->mInterwiki ) {
827 $dbkey = $title->getDBkey();
829 $dbkey = $title->getPrefixedDBkey();
832 $mainPage = Title::newMainPage();
833 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
837 return $this->getHashedDirectory( $title ) . '/' .
838 $this->getFriendlyName( $dbkey ) . '.html';
841 function getFriendlyName( $name ) {
843 # Replace illegal characters for Windows paths with underscores
844 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
846 # Work out lower case form. We assume we're on a system with case-insensitive
847 # filenames, so unless the case is of a special form, we have to disambiguate
848 if ( function_exists( 'mb_strtolower' ) ) {
849 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
851 $lowerCase = ucfirst( strtolower( $name ) );
854 # Make it mostly unique
855 if ( $lowerCase != $friendlyName ) {
856 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
858 # Handle colon specially by replacing it with tilde
859 # Thus we reduce the number of paths with hashes appended
860 $friendlyName = str_replace( ':', '~', $friendlyName );
862 return $friendlyName;
866 * Get a relative directory for putting a title into
868 function getHashedDirectory( &$title ) {
869 if ( '' != $title->getInterwiki() ) {
870 $pdbk = $title->getDBkey();
872 $pdbk = $title->getPrefixedDBkey();
875 # Find the first colon if there is one, use characters after it
876 $p = strpos( $pdbk, ':' );
877 if ( $p !== false ) {
878 $dbk = substr( $pdbk, $p + 1 );
879 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
884 # Split into characters
885 preg_match_all( '/./us', $dbk, $m );
888 $length = count( $chars );
891 for ( $i = 0; $i < $this->depth; $i++ ) {
895 if ( $i >= $length ) {
899 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
900 if ( function_exists( 'mb_strtolower' ) ) {
901 $dir .= mb_strtolower( $c );
903 $dir .= strtolower( $c );
906 $dir .= sprintf( "%02X", ord( $c ) );
914 * Calculate the start end end of a job based on the current slice
915 * @param integer $start
916 * @param integer $end
917 * @return array of integers
919 function sliceRange( $start, $end ) {
920 $count = $end - $start + 1;
921 $each = $count / $this->sliceDenominator;
922 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
923 if ( $this->sliceNumerator == $this->sliceDenominator ) {
926 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
928 return array( $sliceStart, $sliceEnd );
932 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
933 * @param integer $start
934 * @param integer $base The true start of the range; the minimum start
936 function modSliceStart( $start, $base = 1 ) {
937 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
941 * Determine whether a string belongs to the current slice, based on hash
943 function sliceFilter( $s ) {
944 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
950 function onSiteNoticeBefore( &$text ) {
954 function onSiteNoticeAfter( &$text ) {
959 function getMaxPageID() {
960 if ( $this->maxPageID === false ) {
961 $dbr =& wfGetDB( DB_SLAVE );
962 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
964 return $this->maxPageID;
970 if ( !$this->udpProfile ) {
973 if ( !$this->udpProfileInit ) {
974 $this->udpProfileInit = true;
975 } elseif ( $this->udpProfileCounter == 1 % $this->udpProfile ) {
976 $wgProfiler->getFunctionReport();
977 $wgProfiler = new DumpHTML_ProfilerStub;
979 if ( $this->udpProfileCounter == 0 ) {
980 $wgProfiler = new ProfilerSimpleUDP;
981 $wgProfiler->setProfileID( 'dumpHTML' );
983 $this->udpProfileCounter = ( $this->udpProfileCounter + 1 ) % $this->udpProfile;
987 class DumpHTML_ProfilerStub {
988 function profileIn() {}
989 function profileOut() {}
990 function getOutput() {}
992 function getFunctionReport() {}
995 /** XML parser callback */
996 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
997 global $wgDumpImages;
999 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
1000 $wgDumpImages[$attribs['SRC']] = true;
1004 /** XML parser callback */
1005 function wfDumpEndTagHandler( $parser, $name ) {}