Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php |
— | — | @@ -822,6 +822,26 @@ |
823 | 823 | } |
824 | 824 | return $sql_conds; |
825 | 825 | } |
| 826 | + |
| 827 | + /** |
| 828 | + * Find out if the given page is a redirect and determine its target. |
| 829 | + * Return the target or the page itself if it is not redirect. |
| 830 | + */ |
| 831 | + protected function getRedirectTarget($page, &$db) { |
| 832 | + $options = array('LIMIT' => '1'); |
| 833 | + $id = $page->getArticleID(); |
| 834 | + if ($id == 0) { // page not existing, return |
| 835 | + return $page; |
| 836 | + } |
| 837 | + $res = $db->select($db->tableName('redirect'), 'rd_namespace, rd_title', 'rd_from=' . $id, 'SMW::getRedirectTarget', $options); |
| 838 | + if ($row = $db->fetchObject($res)) { |
| 839 | + $result = Title::newFromText($row->rd_title, $row->rd_namespace); |
| 840 | + if ($result !== NULL) { |
| 841 | + return $result; |
| 842 | + } |
| 843 | + } |
| 844 | + return $page; |
| 845 | + } |
826 | 846 | |
827 | 847 | /** |
828 | 848 | * Add the table $tablename to the $from condition via an inner join, |
— | — | @@ -865,6 +885,12 @@ |
866 | 886 | $from .= ' INNER JOIN ' . $db->tableName('smw_longstrings') . ' AS ' . $curtables['TEXT'] . ' ON ' . $curtables['TEXT'] . '.subject_id=' . $curtables['PAGE'] . '.page_id'; |
867 | 887 | return true; |
868 | 888 | } |
| 889 | + } elseif ($tablename == 'REDIRECT') { |
| 890 | + if ($this->addInnerJoin('PAGE', $from, $db, $curtables)) { // try to add PAGE |
| 891 | + $curtables['REDIRECT'] = 'rd' . $this->m_tablenum++; |
| 892 | + $from .= ' INNER JOIN ' . $db->tableName('redirect') . ' AS ' . $curtables['REDIRECT'] . ' ON ' . $curtables['REDIRECT'] . '.rd_from=' . $curtables['PAGE'] . '.page_id'; |
| 893 | + return true; |
| 894 | + } |
869 | 895 | } |
870 | 896 | return false; |
871 | 897 | } |
— | — | @@ -912,16 +938,34 @@ |
913 | 939 | $where .= $curtables['PAGE'] . '.page_namespace=' . $db->addQuotes($description->getNamespace()); |
914 | 940 | } |
915 | 941 | } elseif ($description instanceof SMWNominalDescription) { |
| 942 | + global $smwgIQRedirectNormalization; |
| 943 | + if ($smwgIQRedirectNormalization) { |
| 944 | + $page = $this->getRedirectTarget($description->getIndividual(), $db); |
| 945 | + } else { |
| 946 | + $page = $description->getIndividual(); |
| 947 | + } |
916 | 948 | if (array_key_exists('PREVREL', $curtables)) { |
917 | | - $where .= $curtables['PREVREL'] . '.object_title=' . |
918 | | - $db->addQuotes($description->getIndividual()->getDBKey()) . ' AND ' . |
919 | | - $curtables['PREVREL'] . '.object_namespace=' . |
920 | | - $description->getIndividual()->getNamespace(); |
| 949 | + $cond = $curtables['PREVREL'] . '.object_title=' . |
| 950 | + $db->addQuotes($page->getDBKey()) . ' AND ' . |
| 951 | + $curtables['PREVREL'] . '.object_namespace=' . |
| 952 | + $page->getNamespace(); |
| 953 | + if ( $smwgIQRedirectNormalization && ($this->addInnerJoin('PAGE', $from, $db, $curtables)) ) { |
| 954 | + $rdtable = 'rd' . $this->m_tablenum++; |
| 955 | + $from .= ' INNER JOIN ' . $db->tableName('redirect') . ' AS ' . $rdtable; |
| 956 | + $cond = '(' . $cond . ') OR (' . |
| 957 | + $rdtable . '.rd_from=' . |
| 958 | + $curtables['PAGE'] . '.page_id AND ' . |
| 959 | + $rdtable . '.rd_title=' . |
| 960 | + $db->addQuotes($page->getDBKey()) . ' AND ' . |
| 961 | + $rdtable . '.rd_namespace=' . |
| 962 | + $page->getNamespace() . ')'; |
| 963 | + } |
| 964 | + $where .= $cond; |
921 | 965 | } elseif ($this->addInnerJoin('PAGE', $from, $db, $curtables)) { |
922 | 966 | $where .= $curtables['PAGE'] . '.page_title=' . |
923 | | - $db->addQuotes($description->getIndividual()->getDBKey()) . ' AND ' . |
| 967 | + $db->addQuotes($page->getDBKey()) . ' AND ' . |
924 | 968 | $curtables['PAGE'] . '.page_namespace=' . |
925 | | - $description->getIndividual()->getNamespace(); |
| 969 | + $page->getNamespace(); |
926 | 970 | } |
927 | 971 | } elseif ($description instanceof SMWValueDescription) { |
928 | 972 | switch ($description->getDatavalue()->getTypeID()) { |