API:
authorRoan Kattouw <catrope@users.mediawiki.org>
Wed, 27 Aug 2008 16:48:30 +0000 (16:48 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Wed, 27 Aug 2008 16:48:30 +0000 (16:48 +0000)
* Add titlePartToKey() and keyPartToTitle() which use the substr() hack to preserve trailing spaces
* Migrate function calls where needed. ??continue parameters still use titleToKey() because they're generated using keyToTitle() and therefore can't contain trailing spaces

includes/api/ApiQueryAllCategories.php
includes/api/ApiQueryAllLinks.php
includes/api/ApiQueryAllimages.php
includes/api/ApiQueryAllpages.php
includes/api/ApiQueryBase.php

index fe33eae..47ad79f 100644 (file)
@@ -57,9 +57,9 @@ class ApiQueryAllCategories extends ApiQueryGeneratorBase {
                $this->addFields('cat_title');
 
                if (!is_null($params['from']))
-                       $this->addWhere('cat_title>=' . $db->addQuotes($this->titleToKey($params['from'])));
+                       $this->addWhere('cat_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
                if (isset ($params['prefix']))
-                       $this->addWhere("cat_title LIKE '" . $db->escapeLike($this->titleToKey($params['prefix'])) . "%'");
+                       $this->addWhere("cat_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
 
                $this->addOption('LIMIT', $params['limit']+1);
                $this->addOption('ORDER BY', 'cat_title' . ($params['dir'] == 'descending' ? ' DESC' : ''));
index 32ef16d..e81e69f 100644 (file)
@@ -80,9 +80,9 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase {
                }               
 
                if (!is_null($params['from']))
-                       $this->addWhere('pl_title>=' . $db->addQuotes($this->titleToKey($params['from'])));
+                       $this->addWhere('pl_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
                if (isset ($params['prefix']))
-                       $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titleToKey($params['prefix'])) . "%'");
+                       $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
 
                $this->addFields(array (
                        'pl_namespace',
index 89a89e7..2754c25 100644 (file)
@@ -62,9 +62,9 @@ class ApiQueryAllimages extends ApiQueryGeneratorBase {
 
                // Image filters
                if (!is_null($params['from']))
-                       $this->addWhere('img_name>=' . $db->addQuotes($this->titleToKey($params['from'])));
+                       $this->addWhere('img_name>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
                if (isset ($params['prefix']))
-                       $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titleToKey($params['prefix'])) . "%'");
+                       $this->addWhere("img_name LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
 
                if (isset ($params['minsize'])) {
                        $this->addWhere('img_size>=' . intval($params['minsize']));
index c4ac486..97f1c39 100644 (file)
@@ -62,10 +62,10 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                        $this->addWhereIf('page_is_redirect = 0', $params['filterredir'] === 'nonredirects');
                $this->addWhereFld('page_namespace', $params['namespace']);
                $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
-               $from = (is_null($params['from']) ? null : $this->titleToKey($params['from']));
+               $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
                $this->addWhereRange('page_title', $dir, $from, null);
                if (isset ($params['prefix']))
-                       $this->addWhere("page_title LIKE '" . $db->escapeLike($this->titleToKey($params['prefix'])) . "%'");
+                       $this->addWhere("page_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
 
                $forceNameTitleIndex = true;
                if (isset ($params['minsize'])) {
index fc1e7dc..6d5d1ec 100644 (file)
@@ -325,9 +325,11 @@ abstract class ApiQueryBase extends ApiBase {
         */
        public function titleToKey($title) {
                # Don't throw an error if we got an empty string
-               if(trim($title) == '') return '';
+               if(trim($title) == '')
+                       return '';
                $t = Title::newFromText($title);
-               if(!$t) $this->dieUsageMsg(array('invalidtitle', $title));
+               if(!$t)
+                       $this->dieUsageMsg(array('invalidtitle', $title));
                return $t->getDbKey();
        }
 
@@ -338,12 +340,32 @@ abstract class ApiQueryBase extends ApiBase {
         */
        public function keyToTitle($key) {
                # Don't throw an error if we got an empty string
-               if(trim($key) == '') return '';
+               if(trim($key) == '')
+                       return '';
                $t = Title::newFromDbKey($key);
                # This really shouldn't happen but we gotta check anyway
-               if(!$t) $this->dieUsageMsg(array('invalidtitle', $key));
+               if(!$t)
+                       $this->dieUsageMsg(array('invalidtitle', $key));
                return $t->getPrefixedText();
        }
+       
+       /**
+        * An alternative to titleToKey() that doesn't trim trailing spaces
+        * @param string $titlePart Title part with spaces
+        * @return string Title part with underscores
+        */
+       public function titlePartToKey($titlePart) {
+               return substr($this->titleToKey($titlePart . '.'), 0, -1);
+       }
+       
+       /**
+        * An alternative to keyToTitle() that doesn't trim trailing spaces
+        * @param string $keyPart Key part with spaces
+        * @return string Key part with underscores
+        */
+       public function keyPartToTitle($keyPart) {
+               return substr($this->keyToTitle($keyPart . '.'), 0, -1);
+       }
 
        /**
         * Get version string for use in the API help output