Revision::getTitle produce rev_id IS NULL query
authorumherirrender <umherirrender_de.wp@web.de>
Sun, 27 May 2012 14:39:04 +0000 (16:39 +0200)
committerumherirrender <umherirrender_de.wp@web.de>
Sun, 27 May 2012 14:39:04 +0000 (16:39 +0200)
Seen on Special:NewPages: Linker::revComment is calling
Revision::getTitle, but the revision object has no id set.
This is another way to avoid a query per non-empty comment on
Special:NewPages, see gerrit 9003

Change-Id: I1786a4c13000f574c0f34fb59759bb2fc4117bcd

includes/Revision.php

index 6928eb9..fa2e7b3 100644 (file)
@@ -493,7 +493,7 @@ class Revision {
        /**
         * Get revision ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getId() {
                return $this->mId;
@@ -512,7 +512,7 @@ class Revision {
        /**
         * Get text row ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getTextId() {
                return $this->mTextId;
@@ -530,7 +530,7 @@ class Revision {
        /**
         * Returns the length of the text in this revision, or null if unknown.
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getSize() {
                return $this->mSize;
@@ -539,30 +539,34 @@ class Revision {
        /**
         * Returns the base36 sha1 of the text in this revision, or null if unknown.
         *
-        * @return String
+        * @return String|null
         */
        public function getSha1() {
                return $this->mSha1;
        }
 
        /**
-        * Returns the title of the page associated with this entry.
+        * Returns the title of the page associated with this entry or null.
         *
-        * @return Title
+        * Will do a query, when title is not set and id is given.
+        *
+        * @return Title|null
         */
        public function getTitle() {
                if( isset( $this->mTitle ) ) {
                        return $this->mTitle;
                }
-               $dbr = wfGetDB( DB_SLAVE );
-               $row = $dbr->selectRow(
-                       array( 'page', 'revision' ),
-                       self::selectPageFields(),
-                       array( 'page_id=rev_page',
-                                  'rev_id' => $this->mId ),
-                       __METHOD__ );
-               if ( $row ) {
-                       $this->mTitle = Title::newFromRow( $row );
+               if( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $row = $dbr->selectRow(
+                               array( 'page', 'revision' ),
+                               self::selectPageFields(),
+                               array( 'page_id=rev_page',
+                                          'rev_id' => $this->mId ),
+                               __METHOD__ );
+                       if ( $row ) {
+                               $this->mTitle = Title::newFromRow( $row );
+                       }
                }
                return $this->mTitle;
        }
@@ -579,7 +583,7 @@ class Revision {
        /**
         * Get the page ID
         *
-        * @return Integer
+        * @return Integer|null
         */
        public function getPage() {
                return $this->mPage;