(bug 14012) prop=templates filesorts under some conditions
authorRoan Kattouw <catrope@users.mediawiki.org>
Tue, 13 May 2008 10:39:01 +0000 (10:39 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Tue, 13 May 2008 10:39:01 +0000 (10:39 +0000)
includes/api/ApiQueryLinks.php

index 5a2cf07..b27be25 100644 (file)
@@ -84,7 +84,18 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                $this->addTables($this->table);
                $this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
                $this->addWhereFld($this->prefix . '_namespace', $params['namespace']);
-               $this->addOption('ORDER BY', str_replace('pl_', $this->prefix . '_', 'pl_from, pl_namespace, pl_title'));
+               
+               # Here's some MySQL craziness going on: if you use WHERE foo='bar'
+               # and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
+               # but instead goes and filesorts, because the index for foo was used
+               # already. To work around this, we drop constant fields in the WHERE
+               # clause from the ORDER BY clause
+               $order = array();
+               if(count($this->getPageSet()->getGoodTitles()) != 1)
+                       $order[] = "{$this->prefix}_from";
+               // pl_namespace is always constant
+               $order[] = "{$this->prefix}_title";
+               $this->addOption('ORDER BY', implode(", ", $order));
 
                $db = $this->getDB();
                $res = $this->select(__METHOD__);