Add a function that returns a list of categories the page is a member of
authorKunal Mehta <legoktm@gmail.com>
Fri, 9 Aug 2013 04:59:27 +0000 (12:59 +0800)
committerKunal Mehta <legoktm@gmail.com>
Thu, 15 Aug 2013 04:07:38 +0000 (21:07 -0700)
Main reason is so internal code does not have to recreate the database query
each time. This will also be useful for extensions.

Change-Id: Ia80c051ff0691087564710f2864ed617d5b9ee96

includes/WikiPage.php

index 9d61abc..ce26fb9 100644 (file)
@@ -2978,6 +2978,29 @@ class WikiPage implements Page, IDBAccessObject {
 
        /**#@-*/
 
+       /**
+        * Returns a list of categories this page is a member of.
+        * Results will include hidden categories
+        *
+        * @return TitleArray
+        */
+       public function getCategories() {
+               $id = $this->getId();
+               if ( $id == 0 ) {
+                       return TitleArray::newFromResult( new FakeResultWrapper( array() ) );
+               }
+
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select( 'categorylinks',
+                       array( 'cl_to AS page_title, ' . NS_CATEGORY . ' AS page_namespace' ),
+                       // Have to do that since DatabaseBase::fieldNamesWithAlias treats numeric indexes
+                       // as not being aliases, and NS_CATEGORY is numeric
+                       array( 'cl_from' => $id ),
+                       __METHOD__ );
+
+               return TitleArray::newFromResult( $res );
+       }
+
        /**
         * Returns a list of hidden categories this page is a member of.
         * Uses the page_props and categorylinks tables.