ApiQueryBase::titleToKey and ApiQueryBase::keyToTitle;
authorDaniel Friesen <dantman@users.mediawiki.org>
Mon, 25 Aug 2008 06:57:15 +0000 (06:57 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Mon, 25 Aug 2008 06:57:15 +0000 (06:57 +0000)
Don't bother constructing a title object when the $title/$key is ''. (Why were we doing this check after creating the title object, there's no point in even creating a big object if we're just going to check the old string we already had to see if it's empty)
And as a bonus, use trim() so that user input such as ' ' does not dish out an error (There really isn't much difference between "&title=" and "&title= " if one shouldn't output an error, why should the other?).

includes/api/ApiQueryBase.php

index b1e6752..fc1e7dc 100644 (file)
@@ -324,14 +324,10 @@ abstract class ApiQueryBase extends ApiBase {
         * @return string Page title with underscores
         */
        public function titleToKey($title) {
+               # Don't throw an error if we got an empty string
+               if(trim($title) == '') return '';
                $t = Title::newFromText($title);
-               if(!$t)
-               {
-                       # Don't throw an error if we got an empty string
-                       if($title == '')
-                               return '';
-                       $this->dieUsageMsg(array('invalidtitle', $title));
-               }
+               if(!$t) $this->dieUsageMsg(array('invalidtitle', $title));
                return $t->getDbKey();
        }
 
@@ -341,15 +337,11 @@ abstract class ApiQueryBase extends ApiBase {
         * @return string Page title with spaces
         */
        public function keyToTitle($key) {
+               # Don't throw an error if we got an empty string
+               if(trim($key) == '') return '';
                $t = Title::newFromDbKey($key);
                # This really shouldn't happen but we gotta check anyway
-               if(!$t)
-               {
-                       # Don't throw an error if we got an empty string
-                       if($key == '')
-                               return '';
-                       $this->dieUsageMsg(array('invalidtitle', $key));
-               }
+               if(!$t) $this->dieUsageMsg(array('invalidtitle', $key));
                return $t->getPrefixedText();
        }