Add profiling for database interaction
authorSiebrand Mazeland <s.mazeland@xs4all.nl>
Mon, 8 Oct 2012 13:44:44 +0000 (15:44 +0200)
committerSiebrand Mazeland <s.mazeland@xs4all.nl>
Thu, 8 Nov 2012 11:35:12 +0000 (12:35 +0100)
Also reformat some code.

Change-Id: Iee18ffeab422bdd2581aab7cab0eab482aec3a80

includes/Category.php
includes/CategoryViewer.php
includes/Categoryfinder.php

index ffd7bb8..6a5eac7 100644 (file)
@@ -58,6 +58,9 @@ class Category {
                        # Already initialized
                        return true;
                }
+
+               wfProfileIn( __METHOD__ );
+
                $dbr = wfGetDB( DB_SLAVE );
                $row = $dbr->selectRow(
                        'category',
@@ -66,6 +69,8 @@ class Category {
                        __METHOD__
                );
 
+               wfProfileOut( __METHOD__ );
+
                if ( !$row ) {
                        # Okay, there were no contents.  Nothing to initialize.
                        if ( $this->mTitle ) {
@@ -190,25 +195,37 @@ class Category {
        }
 
        /** @return mixed DB key name, or false on failure */
-       public function getName() { return $this->getX( 'mName' ); }
+       public function getName() {
+               return $this->getX( 'mName' );
+       }
 
        /** @return mixed Category ID, or false on failure */
-       public function getID() { return $this->getX( 'mID' ); }
+       public function getID() {
+               return $this->getX( 'mID' );
+       }
 
        /** @return mixed Total number of member pages, or false on failure */
-       public function getPageCount() { return $this->getX( 'mPages' ); }
+       public function getPageCount() {
+               return $this->getX( 'mPages' );
+       }
 
        /** @return mixed Number of subcategories, or false on failure */
-       public function getSubcatCount() { return $this->getX( 'mSubcats' ); }
+       public function getSubcatCount() {
+               return $this->getX( 'mSubcats' );
+       }
 
        /** @return mixed Number of member files, or false on failure */
-       public function getFileCount() { return $this->getX( 'mFiles' ); }
+       public function getFileCount() {
+               return $this->getX( 'mFiles' );
+       }
 
        /**
         * @return Title|bool Title for this category, or false on failure.
         */
        public function getTitle() {
-               if ( $this->mTitle ) return $this->mTitle;
+               if ( $this->mTitle ) {
+                       return $this->mTitle;
+               }
 
                if ( !$this->initialize() ) {
                        return false;
@@ -226,20 +243,22 @@ class Category {
         * @return TitleArray object for category members.
         */
        public function getMembers( $limit = false, $offset = '' ) {
+               wfProfileIn( __METHOD__ );
+
                $dbr = wfGetDB( DB_SLAVE );
 
                $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
                $options = array( 'ORDER BY' => 'cl_sortkey' );
 
                if ( $limit ) {
-                       $options[ 'LIMIT' ] = $limit;
+                       $options['LIMIT'] = $limit;
                }
 
                if ( $offset !== '' ) {
                        $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
                }
 
-               return TitleArray::newFromResult(
+               $result = TitleArray::newFromResult(
                        $dbr->select(
                                array( 'page', 'categorylinks' ),
                                array( 'page_id', 'page_namespace', 'page_title', 'page_len',
@@ -249,6 +268,10 @@ class Category {
                                $options
                        )
                );
+
+               wfProfileOut( __METHOD__ );
+
+               return $result;
        }
 
        /**
@@ -259,7 +282,7 @@ class Category {
                if ( !$this->initialize() ) {
                        return false;
                }
-               return $this-> { $key } ;
+               return $this->{$key};
        }
 
        /**
@@ -279,8 +302,10 @@ class Category {
                        }
                }
 
+               wfProfileIn( __METHOD__ );
+
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->begin( __METHOD__  );
+               $dbw->begin( __METHOD__ );
 
                # Insert the row if it doesn't exist yet (e.g., this is being run via
                # update.php from a pre-1.16 schema).  TODO: This will cause lots and
@@ -303,8 +328,8 @@ class Category {
                $result = $dbw->selectRow(
                        array( 'categorylinks', 'page' ),
                        array( 'pages' => 'COUNT(*)',
-                                  'subcats' => "COUNT($cond1)",
-                                  'files' => "COUNT($cond2)"
+                               'subcats' => "COUNT($cond1)",
+                               'files' => "COUNT($cond2)"
                        ),
                        array( 'cl_to' => $this->mName, 'page_id = cl_from' ),
                        __METHOD__,
@@ -322,6 +347,8 @@ class Category {
                );
                $dbw->commit( __METHOD__ );
 
+               wfProfileOut( __METHOD__ );
+
                # Now we should update our local counts.
                $this->mPages   = $result->pages;
                $this->mSubcats = $result->subcats;
index 3d66b74..b59b4e1 100644 (file)
@@ -173,7 +173,7 @@ class CategoryViewer extends ContextSource {
 
        /**
         * Add a subcategory to the internal lists, using a title object
-        * @deprecated since 1.17 kept for compatibility, please use addSubcategoryObject instead
+        * @deprecated since 1.17 kept for compatibility, use addSubcategoryObject instead
         */
        function addSubcategory( Title $title, $sortkey, $pageLength ) {
                wfDeprecated( __METHOD__, '1.17' );
@@ -181,14 +181,14 @@ class CategoryViewer extends ContextSource {
        }
 
        /**
-       * Get the character to be used for sorting subcategories.
-       * If there's a link from Category:A to Category:B, the sortkey of the resulting
-       * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
-       * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
-       * else use sortkey...
-       *
-       * @param Title $title
-       * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
+        * Get the character to be used for sorting subcategories.
+        * If there's a link from Category:A to Category:B, the sortkey of the resulting
+        * entry in the categorylinks table is Category:A, not A, which it SHOULD be.
+        * Workaround: If sortkey == "Category:".$title, than use $title for sorting,
+        * else use sortkey...
+        *
+        * @param Title $title
+        * @param string $sortkey The human-readable sortkey (before transforming to icu or whatever).
         * @return string
         */
        function getSubcategorySortChar( $title, $sortkey ) {
@@ -259,15 +259,15 @@ class CategoryViewer extends ContextSource {
 
        function finaliseCategoryState() {
                if ( $this->flip['subcat'] ) {
-                       $this->children            = array_reverse( $this->children );
+                       $this->children = array_reverse( $this->children );
                        $this->children_start_char = array_reverse( $this->children_start_char );
                }
                if ( $this->flip['page'] ) {
-                       $this->articles            = array_reverse( $this->articles );
+                       $this->articles = array_reverse( $this->articles );
                        $this->articles_start_char = array_reverse( $this->articles_start_char );
                }
                if ( !$this->showGallery && $this->flip['file'] ) {
-                       $this->imgsNoGallery            = array_reverse( $this->imgsNoGallery );
+                       $this->imgsNoGallery = array_reverse( $this->imgsNoGallery );
                        $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char );
                }
        }
@@ -302,7 +302,7 @@ class CategoryViewer extends ContextSource {
                                        'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
                                        'cat_subcats', 'cat_pages', 'cat_files',
                                        'cl_sortkey_prefix', 'cl_collation' ),
-                               array_merge( array( 'cl_to' => $this->title->getDBkey() ),  $extraConds ),
+                               array_merge( array( 'cl_to' => $this->title->getDBkey() ), $extraConds ),
                                __METHOD__,
                                array(
                                        'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
@@ -310,7 +310,7 @@ class CategoryViewer extends ContextSource {
                                        'ORDER BY' => $this->flip[$type] ? 'cl_sortkey DESC' : 'cl_sortkey',
                                ),
                                array(
-                                       'categorylinks'  => array( 'INNER JOIN', 'cl_from = page_id' ),
+                                       'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
                                        'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY )
                                )
                        );
@@ -469,7 +469,7 @@ class CategoryViewer extends ContextSource {
         */
        function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
                $list = '';
-               if ( count ( $articles ) > $cutoff ) {
+               if ( count( $articles ) > $cutoff ) {
                        $list = self::columnList( $articles, $articles_start_char );
                } elseif ( count( $articles ) > 0 ) {
                        // for short lists of articles in categories.
@@ -478,7 +478,7 @@ class CategoryViewer extends ContextSource {
 
                $pageLang = $this->title->getPageLanguage();
                $attribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
-                       'class' => 'mw-content-'.$pageLang->getDir() );
+                       'class' => 'mw-content-' . $pageLang->getDir() );
                $list = Html::rawElement( 'div', $attribs, $list );
 
                return $list;
@@ -604,7 +604,7 @@ class CategoryViewer extends ContextSource {
                        );
                }
 
-               return $this->msg('categoryviewer-pagedlinks')->rawParams($prevLink, $nextLink)->escaped();
+               return $this->msg( 'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped();
        }
 
        /**
@@ -635,6 +635,7 @@ class CategoryViewer extends ContextSource {
                return Title::makeTitle( $title->getNamespace(),
                        $title->getDBkey(), $fragment );
        }
+
        /**
         * What to do if the category table conflicts with the number of results
         * returned?  This function says what. Each type is considered independently
@@ -676,8 +677,9 @@ class CategoryViewer extends ContextSource {
                        $fromOrUntil = true;
                }
 
-               if ( $dbcnt == $rescnt || ( ( $rescnt == $this->limit || $fromOrUntil )
-                       && $dbcnt > $rescnt ) ) {
+               if ( $dbcnt == $rescnt ||
+                       ( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt )
+               ) {
                        # Case 1: seems sane.
                        $totalcnt = $dbcnt;
                } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
index e2b6a0c..589950f 100644 (file)
  *
  * Example use :
  * <code>
- *     # Determines whether the article with the page_id 12345 is in both
- *     # "Category 1" and "Category 2" or their subcategories, respectively
+ *     # Determines whether the article with the page_id 12345 is in both
+ *     # "Category 1" and "Category 2" or their subcategories, respectively
  *
- *     $cf = new Categoryfinder;
- *     $cf->seed(
- *             array( 12345 ),
- *             array( 'Category 1', 'Category 2' ),
- *             'AND'
- *     );
- *     $a = $cf->run();
- *     print implode( ',' , $a );
+ *     $cf = new Categoryfinder;
+ *     $cf->seed(
+ *         array( 12345 ),
+ *         array( 'Category 1', 'Category 2' ),
+ *         'AND'
+ *     );
+ *     $a = $cf->run();
+ *     print implode( ',' , $a );
  * </code>
  *
  */
@@ -135,7 +135,7 @@ class Categoryfinder {
 
                # iterate through the parents
                foreach ( $this->parents[$id] as $p ) {
-                       $pname = $p->cl_to ;
+                       $pname = $p->cl_to;
 
                        # Is this a condition?
                        if ( isset( $conds[$pname] ) ) {
@@ -172,6 +172,8 @@ class Categoryfinder {
         * Scans a "parent layer" of the articles/categories in $this->next
         */
        function scan_next_layer() {
+               wfProfileIn( __METHOD__ );
+
                # Find all parents of the article currently in $this->next
                $layer = array();
                $res = $this->dbr->select(
@@ -225,6 +227,7 @@ class Categoryfinder {
                foreach ( $layer as $v ) {
                        $this->deadend[$v] = $v;
                }
-       }
 
+               wfProfileOut( __METHOD__ );
+       }
 }