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->doLocalImageDescriptions();
128 $this->doSharedImageDescriptions();
129 $this->doCategories();
130 $this->doRedirects();
131 if ( $this->sliceNumerator == 1 ) {
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( Title::makeTitle( NS_SPECIAL, '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( "%01x/%02x", intval( $hash / 16 ), $hash );
313 $paths = array_merge( glob( "{$this->sharedStaticDirectory}/$dir/*" ),
314 glob( "{$this->sharedStaticDirectory}/thumb/$dir/*" ) );
316 foreach ( $paths as $path ) {
317 $file = wfBaseName( $path );
318 if ( !(++$i % REPORTING_INTERVAL ) ) {
322 $title = Title::makeTitle( NS_IMAGE, $file );
323 $this->doArticle( $title );
326 $this->setCheckpoint( 'shared image', 'done' );
330 function doCategories() {
333 $this->setupGlobals();
334 $dbr =& wfGetDB( DB_SLAVE );
336 $cp = $this->getCheckpoint( 'category' );
337 if ( $cp == 'done' ) {
338 print "Category pages already done\n";
340 } elseif ( $cp !== false ) {
341 print "Resuming category page dump from $cp\n";
342 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
344 print "Starting category pages\n";
350 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
351 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
352 $numRows = $dbr->numRows( $res );
354 while ( $row = $dbr->fetchObject( $res ) ) {
355 // Set conditions for next chunk
356 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
358 // Filter pages from other slices
359 if ( !$this->sliceFilter( $row->cl_to ) ) {
363 wfWaitForSlaves( 10 );
364 if ( !(++$i % REPORTING_INTERVAL ) ) {
365 print "{$row->cl_to}\n";
366 if ( $row->cl_to != 'done' ) {
367 $this->setCheckpoint( 'category', $row->cl_to );
370 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
371 $this->doArticle( $title );
373 $dbr->freeResult( $res );
374 } while ( $numRows );
376 $this->setCheckpoint( 'category', 'done' );
380 function doRedirects() {
381 print "Doing redirects...\n";
384 $end = $this->getMaxPageID();
385 $cp = $this->getCheckpoint( 'redirect' );
386 if ( $cp == 'done' ) {
387 print "Redirects already done\n";
389 } elseif ( $cp !== false ) {
390 print "Resuming redirect generation from page_id $cp\n";
391 $start = intval( $cp );
396 $this->setupGlobals();
397 $dbr =& wfGetDB( DB_SLAVE );
400 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
401 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
403 'page_is_redirect' => 1,
404 "page_id BETWEEN $chunkStart AND $chunkEnd"
406 # Modulo slicing in SQL
407 if ( $this->sliceDenominator != 1 ) {
408 $n = intval( $this->sliceNumerator );
409 $m = intval( $this->sliceDenominator );
410 $conds[] = "page_id % $m = $n";
412 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
413 $conds, __METHOD__ );
415 while ( $row = $dbr->fetchObject( $res ) ) {
416 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
417 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
418 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
419 $this->setCheckpoint( 'redirect', $row->page_id );
421 $this->doArticle( $title );
423 $dbr->freeResult( $res );
425 $this->setCheckpoint( 'redirect', 'done' );
428 /** Write an article specified by title */
429 function doArticle( $title ) {
430 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
431 global $wgUploadDirectory;
433 if ( $this->noOverwrite ) {
434 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
435 if ( file_exists( $fileName ) ) {
440 $this->rawPages = array();
441 $text = $this->getArticleHTML( $title );
443 if ( $text === false ) {
447 # Parse the XHTML to find the images
448 $images = $this->findImages( $text );
449 $this->copyImages( $images );
452 $this->writeArticle( $title, $text );
455 wfMkdirParents( "{$this->dest}/raw", 0755 );
456 foreach( $this->rawPages as $record ) {
457 list( $file, $title, $params ) = $record;
459 $path = "{$this->dest}/raw/$file";
460 if ( !file_exists( $path ) ) {
461 $article = new Article( $title );
462 $request = new FauxRequest( $params );
463 $rp = new RawPage( $article, $request );
464 $text = $rp->getRawText();
466 print "Writing $file\n";
467 $file = fopen( $path, 'w' );
469 print("Can't open file $fullName for writing\n");
472 fwrite( $file, $text );
478 /** Write the given text to the file identified by the given title object */
479 function writeArticle( &$title, $text ) {
480 $filename = $this->getHashedFilename( $title );
481 $fullName = "{$this->dest}/$filename";
482 $fullDir = dirname( $fullName );
484 if ( $this->compress ) {
486 $text = gzencode( $text, 9 );
489 wfMkdirParents( $fullDir, 0755 );
491 wfSuppressWarnings();
492 $file = fopen( $fullName, 'w' );
496 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
500 fwrite( $file, $text );
504 /** Set up globals required for parsing */
505 function setupGlobals( $currentDepth = NULL ) {
506 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
507 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
508 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
509 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
510 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
511 global $wgGenerateThumbnailOnParse;
513 static $oldLogo = NULL;
515 if ( !$this->setupDone ) {
516 $wgHooks['GetLocalURL'][] =& $this;
517 $wgHooks['GetFullURL'][] =& $this;
518 $wgHooks['SiteNoticeBefore'][] =& $this;
519 $wgHooks['SiteNoticeAfter'][] =& $this;
520 $this->oldArticlePath = $wgServer . $wgArticlePath;
523 if ( is_null( $currentDepth ) ) {
524 $currentDepth = $this->depth;
527 if ( $this->alternateScriptPath ) {
528 if ( $currentDepth == 0 ) {
531 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
534 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
537 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
540 # Allow for repeated setup
541 if ( !is_null( $oldLogo ) ) {
547 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
548 # If it's in the upload directory, rewrite it to the new upload directory
549 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
550 } elseif ( $wgLogo{0} == '/' ) {
551 # This is basically heuristic
552 # Rewrite an absolute logo path to one relative to the the script path
553 $wgLogo = $wgScriptPath . $wgLogo;
557 if ( !$this->setupDone ) {
558 $this->oldCopyrightIcon = $wgCopyrightIcon;
560 $wgCopyrightIcon = str_replace( 'src="/images',
561 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
563 $wgStylePath = "$wgScriptPath/skins";
564 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
565 $wgSharedUploadPath = "$wgUploadPath/shared";
567 $wgHideInterlanguageLinks = !$this->interwiki;
568 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
569 $wgEnableParserCache = false;
570 $wgMathPath = "$wgScriptPath/math";
571 $wgEnableSidebarCache = false;
572 $wgGenerateThumbnailOnParse = true;
574 if ( !empty( $wgRightsText ) ) {
575 $wgRightsUrl = "$wgScriptPath/COPYING.html";
579 $wgUser->setOption( 'skin', $this->skin );
580 $wgUser->setOption( 'editsection', 0 );
582 if ( $this->makeSnapshot ) {
583 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
584 if ( realpath( $this->destUploadDirectory == $wgUploadDirectory ) ) {
585 $this->makeSnapshot = false;
589 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
591 $this->setupDone = true;
594 /** Reads the content of a title object, executes the skin and captures the result */
595 function getArticleHTML( &$title ) {
596 global $wgOut, $wgTitle, $wgArticle, $wgUser;
598 $linkCache =& LinkCache::singleton();
601 if ( is_null( $wgTitle ) ) {
605 $ns = $wgTitle->getNamespace();
606 if ( $ns == NS_SPECIAL ) {
607 $wgOut = new OutputPage;
608 $wgOut->setParserOptions( new ParserOptions );
609 SpecialPage::executePath( $wgTitle );
611 /** @todo merge with Wiki.php code */
612 if ( $ns == NS_IMAGE ) {
613 $wgArticle = new ImagePage( $wgTitle );
614 } elseif ( $ns == NS_CATEGORY ) {
615 $wgArticle = new CategoryPage( $wgTitle );
617 $wgArticle = new Article( $wgTitle );
619 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
621 return $this->getRedirect( $rt );
623 $wgOut = new OutputPage;
624 $wgOut->setParserOptions( new ParserOptions );
631 $sk =& $wgUser->getSkin();
633 $sk->outputPage( $wgOut );
634 $text = ob_get_contents();
640 function getRedirect( $rt ) {
641 $url = $rt->escapeLocalURL();
642 $text = $rt->getPrefixedText();
644 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
645 <html xmlns="http://www.w3.org/1999/xhtml">
647 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
648 <meta http-equiv="Refresh" content="0;url=$url" />
651 <p>Redirecting to <a href="$url">$text</a></p>
657 /** Returns image paths used in an XHTML document */
658 function findImages( $text ) {
659 global $wgOutputEncoding, $wgDumpImages;
660 $parser = xml_parser_create( $wgOutputEncoding );
661 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
663 $wgDumpImages = array();
664 xml_parse( $parser, $text );
665 xml_parser_free( $parser );
667 return $wgDumpImages;
671 * Copy a file specified by a URL to a given directory
673 * @param string $srcPath The source URL
674 * @param string $srcPathBase The base directory of the source URL
675 * @param string $srcDirBase The base filesystem directory of the source URL
676 * @param string $destDirBase The base filesystem directory of the destination URL
678 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
679 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
680 $sourceLoc = "$srcDirBase/$rel";
681 $destLoc = "$destDirBase/$rel";
682 #print "Copying $sourceLoc to $destLoc\n";
683 if ( !file_exists( $destLoc ) ) {
684 wfMkdirParents( dirname( $destLoc ), 0755 );
685 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
686 symlink( $sourceLoc, $destLoc );
688 copy( $sourceLoc, $destLoc );
694 * Copy an image, and if it is a thumbnail, copy its parent image too
696 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
697 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
698 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
699 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
700 # The image was a thumbnail
701 # Copy the source image as well
702 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
703 $parts = explode( '/', $rel );
704 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
705 $newSrc = "$srcPathBase/$rel";
706 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
711 * Copy images (or create symlinks) from commons to a static directory.
712 * This is necessary even if you intend to distribute all of commons, because
713 * the directory contents is used to work out which image description pages
716 * Also copies math images, and full-sized images if the makeSnapshot option
720 function copyImages( $images ) {
721 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
722 $wgMathPath, $wgMathDirectory;
723 # Find shared uploads and copy them into the static directory
724 $sharedPathLength = strlen( $wgSharedUploadPath );
725 $mathPathLength = strlen( $wgMathPath );
726 $uploadPathLength = strlen( $wgUploadPath );
727 foreach ( $images as $escapedImage => $dummy ) {
728 $image = urldecode( $escapedImage );
730 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
731 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
732 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
733 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
734 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
735 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
740 function onGetFullURL( &$title, &$url, $query ) {
741 global $wgContLang, $wgArticlePath;
743 $iw = $title->getInterwiki();
744 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
745 if ( $title->getDBkey() == '' ) {
746 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
748 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
751 $url .= $this->compress ? ".gz" : "";
758 function onGetLocalURL( &$title, &$url, $query ) {
759 global $wgArticlePath;
761 if ( $title->isExternal() ) {
762 # Default is fine for interwiki
767 if ( $query != '' ) {
768 parse_str( $query, $params );
769 if ( isset($params['action']) && $params['action'] == 'raw' ) {
770 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
771 $file = 'gen.' . $params['gen'];
773 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
774 // Clean up Monobook.css etc.
775 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
776 $file = $matches[1] . '.' . $matches[2];
779 $this->rawPages[$file] = array( $file, $title, $params );
780 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
783 if ( $url === false ) {
784 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
786 $url .= $this->compress ? ".gz" : "";
790 function getHashedFilename( &$title ) {
791 if ( '' != $title->mInterwiki ) {
792 $dbkey = $title->getDBkey();
794 $dbkey = $title->getPrefixedDBkey();
797 $mainPage = Title::newMainPage();
798 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
802 return $this->getHashedDirectory( $title ) . '/' .
803 $this->getFriendlyName( $dbkey ) . '.html';
806 function getFriendlyName( $name ) {
808 # Replace illegal characters for Windows paths with underscores
809 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
811 # Work out lower case form. We assume we're on a system with case-insensitive
812 # filenames, so unless the case is of a special form, we have to disambiguate
813 if ( function_exists( 'mb_strtolower' ) ) {
814 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
816 $lowerCase = ucfirst( strtolower( $name ) );
819 # Make it mostly unique
820 if ( $lowerCase != $friendlyName ) {
821 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
823 # Handle colon specially by replacing it with tilde
824 # Thus we reduce the number of paths with hashes appended
825 $friendlyName = str_replace( ':', '~', $friendlyName );
827 return $friendlyName;
831 * Get a relative directory for putting a title into
833 function getHashedDirectory( &$title ) {
834 if ( '' != $title->getInterwiki() ) {
835 $pdbk = $title->getDBkey();
837 $pdbk = $title->getPrefixedDBkey();
840 # Find the first colon if there is one, use characters after it
841 $p = strpos( $pdbk, ':' );
842 if ( $p !== false ) {
843 $dbk = substr( $pdbk, $p + 1 );
844 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
849 # Split into characters
850 preg_match_all( '/./us', $dbk, $m );
853 $length = count( $chars );
856 for ( $i = 0; $i < $this->depth; $i++ ) {
860 if ( $i >= $length ) {
864 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
865 if ( function_exists( 'mb_strtolower' ) ) {
866 $dir .= mb_strtolower( $c );
868 $dir .= strtolower( $c );
871 $dir .= sprintf( "%02X", ord( $c ) );
879 * Calculate the start end end of a job based on the current slice
880 * @param integer $start
881 * @param integer $end
882 * @return array of integers
884 function sliceRange( $start, $end ) {
885 $count = $end - $start + 1;
886 $each = $count / $this->sliceDenominator;
887 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
888 if ( $this->sliceNumerator == $this->sliceDenominator ) {
891 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
893 return array( $sliceStart, $sliceEnd );
897 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
898 * @param integer $start
899 * @param integer $base The true start of the range; the minimum start
901 function modSliceStart( $start, $base = 1 ) {
902 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
906 * Determine whether a string belongs to the current slice, based on hash
908 function sliceFilter( $s ) {
909 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
915 function onSiteNoticeBefore( &$text ) {
919 function onSiteNoticeAfter( &$text ) {
924 function getMaxPageID() {
925 if ( $this->maxPageID === false ) {
926 $dbr =& wfGetDB( DB_SLAVE );
927 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
929 return $this->maxPageID;
934 /** XML parser callback */
935 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
936 global $wgDumpImages;
938 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
939 $wgDumpImages[$attribs['SRC']] = true;
943 /** XML parser callback */
944 function wfDumpEndTagHandler( $parser, $name ) {}