Support Title::GAID_FOR_UPDATE for Title->exists
authorMatthew Flaschen <mflaschen@wikimedia.org>
Thu, 30 Apr 2015 18:55:23 +0000 (14:55 -0400)
committerOri.livneh <ori@wikimedia.org>
Tue, 5 May 2015 03:13:26 +0000 (03:13 +0000)
Without this, you have to call:

getArticleID( Title::GAID_FOR_UPDATE )

then either use that directly (bypassing the TitleExists hook) or
call exists afterwards.

Change-Id: Ieec5579e4de2a289795364e7001028932535e435

includes/Title.php
tests/phpunit/includes/TitleTest.php

index fe989b5..509fc27 100644 (file)
@@ -4235,10 +4235,12 @@ class Title {
         * If you want to know if a title can be meaningfully viewed, you should
         * probably call the isKnown() method instead.
         *
+        * @param int $flags An optional bit field; may be Title::GAID_FOR_UPDATE to check
+        *   from master/for update
         * @return bool
         */
-       public function exists() {
-               $exists = $this->getArticleID() != 0;
+       public function exists( $flags = 0 ) {
+               $exists = $this->getArticleID( $flags ) != 0;
                Hooks::run( 'TitleExists', array( $this, &$exists ) );
                return $exists;
        }
index 74a741a..a8cffd1 100644 (file)
@@ -638,4 +638,26 @@ class TitleTest extends MediaWikiTestCase {
                $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
                $this->assertTrue( $title->isAlwaysKnown() );
        }
+
+       /**
+        * @covers Title::exists
+        */
+       public function testExists() {
+               $title = Title::makeTitle( NS_PROJECT, 'New page' );
+               $linkCache = LinkCache::singleton();
+
+               $article = new Article( $title );
+               $page = $article->getPage();
+               $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
+
+               // Tell Title it doesn't know whether it exists
+               $title->mArticleID = -1;
+
+               // Tell the link cache it doesn't exists when it really does
+               $linkCache->clearLink( $title );
+               $linkCache->addBadLinkObj( $title );
+
+               $this->assertEquals( false, $title->exists(), 'exists() should rely on link cache unless GAID_FOR_UPDATE is used' );
+               $this->assertEquals( true, $title->exists( Title::GAID_FOR_UPDATE ), 'exists() should re-query database when GAID_FOR_UPDATE is used' );
+       }
 }