Page MenuHomePhabricator

shortcutRedirects.patch

Authored By
bzimport
Nov 21 2014, 9:22 PM
Size
4 KB
Referenced Files
None
Subscribers
None

shortcutRedirects.patch

Index: dumpHTML.inc
===================================================================
--- dumpHTML.inc (révision 16653)
+++ dumpHTML.inc (copie de travail)
@@ -57,6 +57,9 @@
# Checkpoint stuff
var $checkpointFile = false, $checkpoints = false;
+ # Redirect shortcut stuff
+ var $redirectHash = array();
+
var $startID = 1, $endID = false;
var $sliceNumerator = 1, $sliceDenominator = 1;
@@ -124,7 +127,9 @@
$this->doLocalImageDescriptions();
$this->doSharedImageDescriptions();
$this->doCategories();
- $this->doRedirects();
+ if (!$this->shortcutRedirects) {
+ $this->doRedirects();
+ }
if ( $this->sliceNumerator == 1 ) {
$this->doSpecials();
}
@@ -371,6 +376,69 @@
print "\n";
}
+ function buildRedirectHash() {
+ global $wgTitle, $wgArticle, $wgUser, $redirectHash;
+
+ print "Build redirect hash...\n";
+ $fname = 'DumpHTML::buldRedirectHash';
+
+ $dbr =& wfGetDB( DB_SLAVE );
+ $end = $dbr->selectField( 'page', 'max(page_id)', false, $fname );
+ list( $start, $end ) = $this->sliceRange( 1, $end );
+
+ $conds = array(
+ 'page_is_redirect' => 1,
+ "page_id BETWEEN $start AND $end"
+ );
+
+ $this->setupGlobals();
+ $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
+ $conds, $fname );
+ $num = $dbr->numRows( $res );
+ print "$num redirects to shortcut...\n";
+
+ $i = 0;
+ while ( $row = $dbr->fetchObject( $res ) ) {
+ // load the redirect article
+ $wgTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
+ $wgArticle = new Article( $wgTitle );
+
+ // cache the article namespace and title
+ $sourceNamespace = $wgTitle->getNamespace();
+ $sourceTitle = $wgTitle->getDbKey();
+
+ // initialize the path
+ $path = array();
+ $path[$wgTitle->getNamespace().$wgTitle->getDbKey()] = true;
+
+ // load the destination title
+ $recursiveRedirect = false;
+ do {
+ $wgTitle = Title::newFromRedirect( $wgArticle->fetchContent() );
+ if ($wgTitle) {
+ if ($path[$wgTitle->getNamespace().$wgTitle->getDbKey()]) {
+ $recursiveRedirect = true;
+ break;
+ } else {
+ $path[$wgTitle->getNamespace().$wgTitle->getDbKey()] = true;
+ $wgArticle = new Article( $wgTitle );
+ }
+ }
+ } while ($wgArticle && $wgArticle->isRedirect());
+
+ if ($wgTitle) {
+ if ($recursiveRedirect) {
+ $redirectHash[$sourceNamespace.$sourceTitle] = 42;
+ } else {
+ $redirectHash[$sourceNamespace.$sourceTitle] = $wgTitle->escapeLocalURL();
+ }
+ echo $sourceNamespace.":".$sourceTitle." -> ".$wgTitle->escapeLocalURL()."\n";
+ }
+ $i++;
+ if (($i % 100) == 0) { echo $i." redirects resolved.\n"; };
+ }
+ }
+
function doRedirects() {
print "Doing redirects...\n";
@@ -741,8 +809,18 @@
}
function onGetLocalURL( &$title, &$url, $query ) {
- global $wgArticlePath;
+ global $wgArticlePath, $redirectHash;
+ if($redirectUrl = $redirectHash[$title->getNamespace().$title->getDBKey()]) {
+ if ($redirectUrl == 42) {
+ $url = "";
+ return false;
+ } else {
+ $url = $redirectUrl;
+ return true;
+ }
+ }
+
if ( $title->isExternal() ) {
# Default is fine for interwiki
return true;
Index: dumpHTML.php
===================================================================
--- dumpHTML.php (révision 16653)
+++ dumpHTML.php (copie de travail)
@@ -23,9 +23,9 @@
* --force-copy copy commons instead of symlink, needed for Wikimedia
* --interlang allow interlanguage links
* --image-snapshot copy all images used to the destination directory
+ * --shorcut-redirects write the final destination diretly
*/
-
$optionsWithArgs = array( 's', 'd', 'e', 'k', 'checkpoint', 'slice' );
$profiling = false;
@@ -77,7 +77,6 @@
} else {
$sliceNumerator = $sliceDenominator = 1;
}
-
$wgHTMLDump = new DumpHTML( array(
'dest' => $dest,
'forceCopy' => $options['force-copy'],
@@ -90,9 +89,13 @@
'endID' => $end,
'sliceNumerator' => $sliceNumerator,
'sliceDenominator' => $sliceDenominator,
- 'noOverwrite' => $options['no-overwrite'],
+ 'shortcutRedirects' => $options['shortcut-redirects'],
+ 'noOverwrite' => $options['no-overwrite']
));
+if ( $options['shortcut-redirects']) {
+ $wgHTMLDump->buildRedirectHash();
+}
if ( $options['special'] ) {
$wgHTMLDump->doSpecials();

File Metadata

Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2721
Default Alt Text
shortcutRedirects.patch (4 KB)

Event Timeline