r17529 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17528‎ | r17529 | r17530 >
Date:05:41, 11 November 2006
Author:mkroetzsch
Status:old
Tags:
Comment:
Fixed bugs in redirect normalisation
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php
@@ -488,17 +488,23 @@
489489 // using a simple SQL query instead)
490490 // Also, redirects are not taken into account for sub-queries
491491 // anymore now.
492 - foreach ($values as $idx => $v) {
493 - $values[$idx] = smwfNormalTitleDBKey($v);
494 - }
495 - $values = $this->normalizeRedirects($values);
496 - // search for values
 492+ $vtitles = array();
497493 foreach ($values as $v) {
498494 $vtitle = Title::newFromText($v);
499 - if (NULL != $vtitle) {
500 - $or_conditions[] = "$curtable.object_title=" . $this->dbr->addQuotes($vtitle->getDBKey()) . " AND $curtable.object_namespace=" . $vtitle->getNamespace();
 495+ if (NULL != $vtitle) {
 496+ $id = $vtitle->getArticleID(); // create index for title
 497+ if (0 == $id) $id = $vtitle->getPrefixedText();
 498+ $vtitles[$vtitle->getArticleID()] = $vtitle; //convert values to titles
501499 }
502500 }
 501+ $vtitles = $this->normalizeRedirects($vtitles);
 502+
 503+ // search for values
 504+ foreach ($vtitles as $vtitle) {
 505+ //if (NULL != $vtitle) {
 506+ $or_conditions[] = "$curtable.object_title=" . $this->dbr->addQuotes($vtitle->getDBKey()) . " AND $curtable.object_namespace=" . $vtitle->getNamespace();
 507+ //}
 508+ }
503509 }
504510 if ($relation == $this->mSort) {
505511 $result->mOrderBy = "$curtable.object_title";
@@ -601,7 +607,7 @@
602608 }
603609
604610 /**
605 - * Turns an array of articles into an array of all these articles and
 611+ * Turns an array of article titles into an array of all these articles and
606612 * the transitive closure of all redirects from and to this articles.
607613 * Or, simply said: it gets all aliases of what you put in.
608614 *
@@ -610,61 +616,77 @@
611617 * plugable SQL-query to compute one-step back-and-forth redirects without any
612618 * materialisation.
613619 */
614 - private function normalizeRedirects(&$articles) {
 620+ private function normalizeRedirects(&$titles) {
615621 global $smwgIQRedirectNormalization;
616622 if (!$smwgIQRedirectNormalization) {
617 - return $articles;
 623+ return $titles;
618624 }
619 - $stable = FALSE;
620 - $check_articles = array_diff( $articles , array() ); // Copies the array
621 - while (!$stable) {
622 - $new_articles = array();
623 - foreach ( $check_articles as $article ) {
 625+
 626+ print "STARTING NORMALIZATION FOR:<br />\n";
 627+ foreach ($titles as $title) print "+ " . $title->getPrefixedText();
 628+ print "<br />\n";
 629+
 630+ $stable = 0;
 631+ $check_titles = array_diff( $titles , array() ); // Copies the array
 632+ while ($stable<30) { // emergency stop after 30 iterations
 633+ $stable++;
 634+ $new_titles = array();
 635+ foreach ( $check_titles as $title ) {
624636 // there...
625 - $res = $this->dbr->select(
626 - array( 'page' , 'pagelinks' ),
627 - array( 'pl_title' ),
628 - array( 'page_title = ' . $this->dbr->addQuotes( $article ),
 637+
 638+ print "1 ";
 639+ if ( 0 != $title->getArticleID() ) {
 640+ $res = $this->dbr->select(
 641+ array( 'page' , 'pagelinks' ),
 642+ array( 'pl_title', 'pl_namespace'),
 643+ array( 'page_id = ' . $title->getArticleID(),
629644 'page_is_redirect = 1',
630645 'page_id = pl_from' ) ,
631646 "SMW::NormalizeRedirects" );
632 - if ( $res ) {
633647 while ( $res && $row = $this->dbr->fetchRow( $res )) {
634 - if ( array_key_exists( 'pl_title', $row) ) {
635 - $new_article = $row[ 'pl_title' ];
636 - if (!in_array( $new_article , $articles)) {
637 - $new_articles[] = $new_article;
 648+ $new_title = Title::newFromText($row['pl_title'], $row['pl_namespace']);
 649+ if (NULL != $new_title) {
 650+ $id = $new_title->getArticleID();
 651+ if (0 == $id) $id = $new_title->getPrefixedText();
 652+ if (!array_key_exists( $id , $titles)) {
 653+ $titles[$id] = $new_title;
 654+ $new_titles[] = $new_title;
 655+ print "+ " . $new_title->getPrefixedText() . "(" . $new_title->getArticleID() . ") ";
638656 }
639657 }
640658 }
 659+ $this->dbr->freeResult( $res );
641660 }
642 - $this->dbr->freeResult( $res );
643 -
 661+
 662+ print "<br />\n";
 663+ print "2 ";
644664 // ... and back again
645665 $res = $this->dbr->select(
646666 array( 'page' , 'pagelinks' ),
647 - array( 'page_title' ),
648 - array( 'pl_title = '. $this->dbr->addQuotes( $article ),
649 - 'page_is_redirect = 1',
650 - 'page_id = pl_from' ) ,
651 - "SMW::NormalizeRedirects" );
652 - if ( $res ) {
653 - while ( $res && $row = $this->dbr->fetchRow( $res )) {
654 - if ( array_key_exists( 'page_title', $row) ) {
655 - $new_article = $row[ 'page_title' ];
656 - if (!in_array( $new_article , $articles)) {
657 - $new_articles[] = $new_article;
658 - }
659 - }
 667+ array( 'page_id' ),
 668+ array( 'pl_title = ' . $this->dbr->addQuotes( $title->getDBkey() ),
 669+ 'pl_namespace = ' . $this->dbr->addQuotes( $title->getNamespace() ),
 670+ 'page_is_redirect = 1',
 671+ 'page_id = pl_from' ) ,
 672+ "SMW::NormalizeRedirects" );
 673+ while ( $res && $row = $this->dbr->fetchRow( $res )) {
 674+ $new_title = Title::newFromID( $row['page_id'] );
 675+ if (!array_key_exists( $row['page_id'] , $titles)) {
 676+ $titles[$row['page_id']] = $new_title;
 677+ $new_titles[] = $new_title;
 678+ print "+ " . $new_title->getPrefixedText() . "(" . $new_title->getArticleID() . ") ";
660679 }
661680 }
662681 $this->dbr->freeResult( $res );
663682 }
664 - $stable = (count($new_articles)==0);
665 - $articles = array_merge( $articles, $new_articles );
666 - $check_articles = array_diff( $new_articles , array() );
 683+ if (count($new_titles)==0)
 684+ $stable= 500; // stop
 685+ else
 686+ $check_titles = array_diff( $new_titles , array() );
 687+// foreach ($new_titles as $title) print "+ " . $title->getPrefixedText();
 688+ print "<br />\n";
667689 }
668 - return $articles;
 690+ return $titles;
669691 }
670692
671693 /**

Status & tagging log