From fb84c15c1e2b325c1d600b2112c7f5b3226d8366 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Wed, 27 Aug 2008 16:48:30 +0000 Subject: [PATCH] API: * 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 | 4 ++-- includes/api/ApiQueryAllLinks.php | 4 ++-- includes/api/ApiQueryAllimages.php | 4 ++-- includes/api/ApiQueryAllpages.php | 4 ++-- includes/api/ApiQueryBase.php | 30 ++++++++++++++++++++++---- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/includes/api/ApiQueryAllCategories.php b/includes/api/ApiQueryAllCategories.php index fe33eae05c..47ad79fc51 100644 --- a/includes/api/ApiQueryAllCategories.php +++ b/includes/api/ApiQueryAllCategories.php @@ -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' : '')); diff --git a/includes/api/ApiQueryAllLinks.php b/includes/api/ApiQueryAllLinks.php index 32ef16d76a..e81e69fa66 100644 --- a/includes/api/ApiQueryAllLinks.php +++ b/includes/api/ApiQueryAllLinks.php @@ -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', diff --git a/includes/api/ApiQueryAllimages.php b/includes/api/ApiQueryAllimages.php index 89a89e7e28..2754c25862 100644 --- a/includes/api/ApiQueryAllimages.php +++ b/includes/api/ApiQueryAllimages.php @@ -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'])); diff --git a/includes/api/ApiQueryAllpages.php b/includes/api/ApiQueryAllpages.php index c4ac486cf6..97f1c39e0a 100644 --- a/includes/api/ApiQueryAllpages.php +++ b/includes/api/ApiQueryAllpages.php @@ -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'])) { diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index fc1e7dc02a..6d5d1eca40 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -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 -- 2.20.1