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 # Make links assuming the script path is in the same directory as
43 var $alternateScriptPath = false;
45 # Original values of various globals
46 var $oldArticlePath = false, $oldCopyrightIcon = false;
48 # Has setupGlobals been called?
49 var $setupDone = false;
51 # Has to compress html pages
52 var $compress = false;
54 # List of raw pages used in the current article
58 var $skin = 'htmldump';
61 var $checkpointFile = false, $checkpoints = false;
63 var $startID = 1, $endID = false;
65 var $sliceNumerator = 1, $sliceDenominator = 1;
67 # Max page ID, lazy initialised
68 var $maxPageID = false;
70 function DumpHTML( $settings = array() ) {
71 foreach ( $settings as $var => $value ) {
76 function loadCheckpoints() {
77 if ( $this->checkpoints !== false ) {
79 } elseif ( !$this->checkpointFile ) {
82 $lines = @file( $this->checkpointFile );
83 if ( $lines === false ) {
84 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
85 $this->checkpoints = array();
87 $lines = array_map( 'trim', $lines );
88 $this->checkpoints = array();
89 foreach ( $lines as $line ) {
90 list( $name, $value ) = explode( '=', $line, 2 );
91 $this->checkpoints[$name] = $value;
98 function getCheckpoint( $type, $defValue = false ) {
99 if ( !$this->loadCheckpoints() ) {
102 if ( !isset( $this->checkpoints[$type] ) ) {
105 return $this->checkpoints[$type];
109 function setCheckpoint( $type, $value ) {
110 if ( !$this->checkpointFile ) {
113 $this->checkpoints[$type] = $value;
115 foreach ( $this->checkpoints as $type => $value ) {
116 $blob .= "$type=$value\n";
118 file_put_contents( $this->checkpointFile, $blob );
121 function doEverything() {
122 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
123 print "Checkpoint says everything is already done\n";
127 $this->doCategories();
128 $this->doRedirects();
129 if ( $this->sliceNumerator == 1 ) {
132 $this->doLocalImageDescriptions();
133 $this->doSharedImageDescriptions();
135 $this->setCheckpoint( 'everything', 'done' );
139 * Write a set of articles specified by start and end page_id
140 * Skip categories and images, they will be done separately
142 function doArticles() {
143 if ( $this->endID === false ) {
144 $end = $this->getMaxPageID();
148 $start = $this->startID;
150 # Start from the checkpoint
151 $cp = $this->getCheckpoint( 'article' );
152 if ( $cp == 'done' ) {
153 print "Articles already done\n";
155 } elseif ( $cp !== false ) {
157 print "Resuming article dump from checkpoint at page_id $start of $end\n";
159 print "Starting from page_id $start of $end\n";
162 # Move the start point to the correct slice if it isn't there already
163 $start = $this->modSliceStart( $start );
165 $this->setupGlobals();
167 $mainPageObj = Title::newMainPage();
168 $mainPage = $mainPageObj->getPrefixedDBkey();
170 for ( $id = $start, $i = 0; $id <= $end; $id += $this->sliceDenominator, $i++ ) {
171 wfWaitForSlaves( 20 );
172 if ( !( $i % REPORTING_INTERVAL) ) {
173 print "Processing ID: $id\r";
174 $this->setCheckpoint( 'article', $id );
176 if ( !($i % (REPORTING_INTERVAL*10) ) ) {
179 $title = Title::newFromID( $id );
181 $ns = $title->getNamespace() ;
182 if ( $ns != NS_CATEGORY && $title->getPrefixedDBkey() != $mainPage ) {
183 $this->doArticle( $title );
187 $this->setCheckpoint( 'article', 'done' );
191 function doSpecials() {
194 $this->setupGlobals();
195 print "Special:Categories...";
196 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
200 /** Write the main page as index.html */
201 function doMainPage() {
203 print "Making index.html ";
205 // Set up globals with no ../../.. in the link URLs
206 $this->setupGlobals( 0 );
208 $title = Title::newMainPage();
209 $text = $this->getArticleHTML( $title );
211 # Parse the XHTML to find the images
212 $images = $this->findImages( $text );
213 $this->copyImages( $images );
215 $file = fopen( "{$this->dest}/index.html", "w" );
217 print "\nCan't open index.html for writing\n";
220 fwrite( $file, $text );
225 function doImageDescriptions() {
226 $this->doLocalImageDescriptions();
227 $this->doSharedImageDescriptions();
231 * Dump image description pages that don't have an associated article, but do
234 function doLocalImageDescriptions() {
235 global $wgSharedUploadDirectory;
238 $dbr =& wfGetDB( DB_SLAVE );
240 $cp = $this->getCheckpoint( 'local image' );
241 if ( $cp == 'done' ) {
242 print "Local image descriptions already done\n";
244 } elseif ( $cp !== false ) {
245 print "Writing image description pages starting from $cp\n";
246 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
248 print "Writing image description pages for local images\n";
252 $this->setupGlobals();
256 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
257 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
258 $numRows = $dbr->numRows( $res );
260 while ( $row = $dbr->fetchObject( $res ) ) {
261 # Update conds for the next chunk query
262 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
264 // Slice the result set with a filter
265 if ( !$this->sliceFilter( $row->img_name ) ) {
269 wfWaitForSlaves( 10 );
270 if ( !( ++$i % REPORTING_INTERVAL ) ) {
271 print "{$row->img_name}\n";
272 if ( $row->img_name !== 'done' ) {
273 $this->setCheckpoint( 'local image', $row->img_name );
276 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
277 if ( $title->getArticleID() ) {
278 // Already done by dumpHTML
281 $this->doArticle( $title );
283 $dbr->freeResult( $res );
284 } while ( $numRows );
286 $this->setCheckpoint( 'local image', 'done' );
291 * Dump images which only have a real description page on commons
293 function doSharedImageDescriptions() {
294 list( $start, $end ) = $this->sliceRange( 0, 255 );
296 $cp = $this->getCheckpoint( 'shared image' );
297 if ( $cp == 'done' ) {
298 print "Shared description pages already done\n";
300 } elseif ( $cp !== false ) {
301 print "Writing description pages for commons images starting from directory $cp/255\n";
304 print "Writing description pages for commons images\n";
307 $this->setupGlobals();
309 for ( $hash = $start; $hash <= $end; $hash++ ) {
310 $this->setCheckpoint( 'shared image', $hash );
312 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
313 intval( $hash / 16 ), $hash );
314 $handle = @opendir( $dir );
315 while ( $handle && $file = readdir( $handle ) ) {
316 if ( $file[0] == '.' ) {
319 if ( !(++$i % REPORTING_INTERVAL ) ) {
323 $title = Title::makeTitleSafe( NS_IMAGE, $file );
324 $this->doArticle( $title );
330 $this->setCheckpoint( 'shared image', 'done' );
334 function doCategories() {
337 $this->setupGlobals();
338 $dbr =& wfGetDB( DB_SLAVE );
340 $cp = $this->getCheckpoint( 'category' );
341 if ( $cp == 'done' ) {
342 print "Category pages already done\n";
344 } elseif ( $cp !== false ) {
345 print "Resuming category page dump from $cp\n";
346 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
348 print "Starting category pages\n";
354 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
355 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
356 $numRows = $dbr->numRows( $res );
358 while ( $row = $dbr->fetchObject( $res ) ) {
359 // Set conditions for next chunk
360 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
362 // Filter pages from other slices
363 if ( !$this->sliceFilter( $row->cl_to ) ) {
367 wfWaitForSlaves( 10 );
368 if ( !(++$i % REPORTING_INTERVAL ) ) {
369 print "{$row->cl_to}\n";
370 if ( $row->cl_to != 'done' ) {
371 $this->setCheckpoint( 'category', $row->cl_to );
374 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
375 $this->doArticle( $title );
377 $dbr->freeResult( $res );
378 } while ( $numRows );
380 $this->setCheckpoint( 'category', 'done' );
384 function doRedirects() {
385 print "Doing redirects...\n";
388 $end = $this->getMaxPageID();
389 $cp = $this->getCheckpoint( 'redirect' );
390 if ( $cp == 'done' ) {
391 print "Redirects already done\n";
393 } elseif ( $cp !== false ) {
394 print "Resuming redirect generation from page_id $cp\n";
395 $start = intval( $cp );
400 $this->setupGlobals();
401 $dbr =& wfGetDB( DB_SLAVE );
404 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
405 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
407 'page_is_redirect' => 1,
408 "page_id BETWEEN $chunkStart AND $chunkEnd"
410 # Modulo slicing in SQL
411 if ( $this->sliceDenominator != 1 ) {
412 $n = intval( $this->sliceNumerator );
413 $m = intval( $this->sliceDenominator );
414 $conds[] = "page_id % $m = $n";
416 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
417 $conds, __METHOD__ );
419 while ( $row = $dbr->fetchObject( $res ) ) {
420 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
421 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
422 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
423 $this->setCheckpoint( 'redirect', $row->page_id );
425 $this->doArticle( $title );
427 $dbr->freeResult( $res );
429 $this->setCheckpoint( 'redirect', 'done' );
432 /** Write an article specified by title */
433 function doArticle( $title ) {
434 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
435 global $wgUploadDirectory;
437 if ( $this->noOverwrite ) {
438 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
439 if ( file_exists( $fileName ) ) {
444 $this->rawPages = array();
445 $text = $this->getArticleHTML( $title );
447 if ( $text === false ) {
451 # Parse the XHTML to find the images
452 $images = $this->findImages( $text );
453 $this->copyImages( $images );
456 $this->writeArticle( $title, $text );
459 wfMkdirParents( "{$this->dest}/raw", 0755 );
460 foreach( $this->rawPages as $record ) {
461 list( $file, $title, $params ) = $record;
463 $path = "{$this->dest}/raw/$file";
464 if ( !file_exists( $path ) ) {
465 $article = new Article( $title );
466 $request = new FauxRequest( $params );
467 $rp = new RawPage( $article, $request );
468 $text = $rp->getRawText();
470 print "Writing $file\n";
471 $file = fopen( $path, 'w' );
473 print("Can't open file $fullName for writing\n");
476 fwrite( $file, $text );
482 /** Write the given text to the file identified by the given title object */
483 function writeArticle( &$title, $text ) {
484 $filename = $this->getHashedFilename( $title );
485 $fullName = "{$this->dest}/$filename";
486 $fullDir = dirname( $fullName );
488 if ( $this->compress ) {
490 $text = gzencode( $text, 9 );
493 wfMkdirParents( $fullDir, 0755 );
495 wfSuppressWarnings();
496 $file = fopen( $fullName, 'w' );
500 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
504 fwrite( $file, $text );
508 /** Set up globals required for parsing */
509 function setupGlobals( $currentDepth = NULL ) {
510 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
511 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
512 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
513 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
514 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
515 global $wgGenerateThumbnailOnParse;
517 static $oldLogo = NULL;
519 if ( !$this->setupDone ) {
520 $wgHooks['GetLocalURL'][] =& $this;
521 $wgHooks['GetFullURL'][] =& $this;
522 $wgHooks['SiteNoticeBefore'][] =& $this;
523 $wgHooks['SiteNoticeAfter'][] =& $this;
524 $this->oldArticlePath = $wgServer . $wgArticlePath;
527 if ( is_null( $currentDepth ) ) {
528 $currentDepth = $this->depth;
531 if ( $this->alternateScriptPath ) {
532 if ( $currentDepth == 0 ) {
535 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
538 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
541 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
544 # Allow for repeated setup
545 if ( !is_null( $oldLogo ) ) {
551 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
552 # If it's in the upload directory, rewrite it to the new upload directory
553 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
554 } elseif ( $wgLogo{0} == '/' ) {
555 # This is basically heuristic
556 # Rewrite an absolute logo path to one relative to the the script path
557 $wgLogo = $wgScriptPath . $wgLogo;
561 if ( !$this->setupDone ) {
562 $this->oldCopyrightIcon = $wgCopyrightIcon;
564 $wgCopyrightIcon = str_replace( 'src="/images',
565 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
567 $wgStylePath = "$wgScriptPath/skins";
568 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
569 $wgSharedUploadPath = "$wgUploadPath/shared";
571 $wgHideInterlanguageLinks = !$this->interwiki;
572 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
573 $wgEnableParserCache = false;
574 $wgMathPath = "$wgScriptPath/math";
575 $wgEnableSidebarCache = false;
576 $wgGenerateThumbnailOnParse = true;
578 if ( !empty( $wgRightsText ) ) {
579 $wgRightsUrl = "$wgScriptPath/COPYING.html";
583 $wgUser->setOption( 'skin', $this->skin );
584 $wgUser->setOption( 'editsection', 0 );
586 if ( $this->makeSnapshot ) {
587 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
588 if ( realpath( $this->destUploadDirectory == $wgUploadDirectory ) ) {
589 $this->makeSnapshot = false;
593 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
595 $this->setupDone = true;
598 /** Reads the content of a title object, executes the skin and captures the result */
599 function getArticleHTML( &$title ) {
600 global $wgOut, $wgTitle, $wgArticle, $wgUser;
602 $linkCache =& LinkCache::singleton();
605 if ( is_null( $wgTitle ) ) {
609 $ns = $wgTitle->getNamespace();
610 if ( $ns == NS_SPECIAL ) {
611 $wgOut = new OutputPage;
612 $wgOut->setParserOptions( new ParserOptions );
613 SpecialPage::executePath( $wgTitle );
615 /** @todo merge with Wiki.php code */
616 if ( $ns == NS_IMAGE ) {
617 $wgArticle = new ImagePage( $wgTitle );
618 } elseif ( $ns == NS_CATEGORY ) {
619 $wgArticle = new CategoryPage( $wgTitle );
621 $wgArticle = new Article( $wgTitle );
623 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
625 return $this->getRedirect( $rt );
627 $wgOut = new OutputPage;
628 $wgOut->setParserOptions( new ParserOptions );
635 $sk =& $wgUser->getSkin();
637 $sk->outputPage( $wgOut );
638 $text = ob_get_contents();
644 function getRedirect( $rt ) {
645 $url = $rt->escapeLocalURL();
646 $text = $rt->getPrefixedText();
648 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
649 <html xmlns="http://www.w3.org/1999/xhtml">
651 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
652 <meta http-equiv="Refresh" content="0;url=$url" />
655 <p>Redirecting to <a href="$url">$text</a></p>
661 /** Returns image paths used in an XHTML document */
662 function findImages( $text ) {
663 global $wgOutputEncoding, $wgDumpImages;
664 $parser = xml_parser_create( $wgOutputEncoding );
665 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
667 $wgDumpImages = array();
668 xml_parse( $parser, $text );
669 xml_parser_free( $parser );
671 return $wgDumpImages;
675 * Copy a file specified by a URL to a given directory
677 * @param string $srcPath The source URL
678 * @param string $srcPathBase The base directory of the source URL
679 * @param string $srcDirBase The base filesystem directory of the source URL
680 * @param string $destDirBase The base filesystem directory of the destination URL
682 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
683 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
684 $sourceLoc = "$srcDirBase/$rel";
685 $destLoc = "$destDirBase/$rel";
686 #print "Copying $sourceLoc to $destLoc\n";
687 if ( !file_exists( $destLoc ) ) {
688 wfMkdirParents( dirname( $destLoc ), 0755 );
689 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
690 symlink( $sourceLoc, $destLoc );
692 copy( $sourceLoc, $destLoc );
698 * Copy an image, and if it is a thumbnail, copy its parent image too
700 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
701 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
702 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
703 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
704 # The image was a thumbnail
705 # Copy the source image as well
706 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
707 $parts = explode( '/', $rel );
708 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
709 $newSrc = "$srcPathBase/$rel";
710 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
715 * Copy images (or create symlinks) from commons to a static directory.
716 * This is necessary even if you intend to distribute all of commons, because
717 * the directory contents is used to work out which image description pages
720 * Also copies math images, and full-sized images if the makeSnapshot option
724 function copyImages( $images ) {
725 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
726 $wgMathPath, $wgMathDirectory;
727 # Find shared uploads and copy them into the static directory
728 $sharedPathLength = strlen( $wgSharedUploadPath );
729 $mathPathLength = strlen( $wgMathPath );
730 $uploadPathLength = strlen( $wgUploadPath );
731 foreach ( $images as $escapedImage => $dummy ) {
732 $image = urldecode( $escapedImage );
734 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
735 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
736 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
737 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
738 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
739 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
744 function onGetFullURL( &$title, &$url, $query ) {
745 global $wgContLang, $wgArticlePath;
747 $iw = $title->getInterwiki();
748 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
749 if ( $title->getDBkey() == '' ) {
750 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
752 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
755 $url .= $this->compress ? ".gz" : "";
762 function onGetLocalURL( &$title, &$url, $query ) {
763 global $wgArticlePath;
765 if ( $title->isExternal() ) {
766 # Default is fine for interwiki
771 if ( $query != '' ) {
772 parse_str( $query, $params );
773 if ( isset($params['action']) && $params['action'] == 'raw' ) {
774 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
775 $file = 'gen.' . $params['gen'];
777 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
778 // Clean up Monobook.css etc.
779 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
780 $file = $matches[1] . '.' . $matches[2];
783 $this->rawPages[$file] = array( $file, $title, $params );
784 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
787 if ( $url === false ) {
788 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
790 $url .= $this->compress ? ".gz" : "";
794 function getHashedFilename( &$title ) {
795 if ( '' != $title->mInterwiki ) {
796 $dbkey = $title->getDBkey();
798 $dbkey = $title->getPrefixedDBkey();
801 $mainPage = Title::newMainPage();
802 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
806 return $this->getHashedDirectory( $title ) . '/' .
807 $this->getFriendlyName( $dbkey ) . '.html';
810 function getFriendlyName( $name ) {
812 # Replace illegal characters for Windows paths with underscores
813 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
815 # Work out lower case form. We assume we're on a system with case-insensitive
816 # filenames, so unless the case is of a special form, we have to disambiguate
817 if ( function_exists( 'mb_strtolower' ) ) {
818 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
820 $lowerCase = ucfirst( strtolower( $name ) );
823 # Make it mostly unique
824 if ( $lowerCase != $friendlyName ) {
825 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
827 # Handle colon specially by replacing it with tilde
828 # Thus we reduce the number of paths with hashes appended
829 $friendlyName = str_replace( ':', '~', $friendlyName );
831 return $friendlyName;
835 * Get a relative directory for putting a title into
837 function getHashedDirectory( &$title ) {
838 if ( '' != $title->getInterwiki() ) {
839 $pdbk = $title->getDBkey();
841 $pdbk = $title->getPrefixedDBkey();
844 # Find the first colon if there is one, use characters after it
845 $p = strpos( $pdbk, ':' );
846 if ( $p !== false ) {
847 $dbk = substr( $pdbk, $p + 1 );
848 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
853 # Split into characters
854 preg_match_all( '/./us', $dbk, $m );
857 $length = count( $chars );
860 for ( $i = 0; $i < $this->depth; $i++ ) {
864 if ( $i >= $length ) {
868 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
869 if ( function_exists( 'mb_strtolower' ) ) {
870 $dir .= mb_strtolower( $c );
872 $dir .= strtolower( $c );
875 $dir .= sprintf( "%02X", ord( $c ) );
883 * Calculate the start end end of a job based on the current slice
884 * @param integer $start
885 * @param integer $end
886 * @return array of integers
888 function sliceRange( $start, $end ) {
889 $count = $end - $start + 1;
890 $each = $count / $this->sliceDenominator;
891 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
892 if ( $this->sliceNumerator == $this->sliceDenominator ) {
895 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
897 return array( $sliceStart, $sliceEnd );
901 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
902 * @param integer $start
903 * @param integer $base The true start of the range; the minimum start
905 function modSliceStart( $start, $base = 1 ) {
906 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
910 * Determine whether a string belongs to the current slice, based on hash
912 function sliceFilter( $s ) {
913 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
919 function onSiteNoticeBefore( &$text ) {
923 function onSiteNoticeAfter( &$text ) {
928 function getMaxPageID() {
929 if ( $this->maxPageID === false ) {
930 $dbr =& wfGetDB( DB_SLAVE );
931 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
933 return $this->maxPageID;
938 /** XML parser callback */
939 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
940 global $wgDumpImages;
942 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
943 $wgDumpImages[$attribs['SRC']] = true;
947 /** XML parser callback */
948 function wfDumpEndTagHandler( $parser, $name ) {}