Clean up array() syntax in docs, part III
[lhc/web/wiklou.git] / includes / pager / IndexPager.php
index 1d93c27..a96ca87 100644 (file)
  * @ingroup Pager
  */
 abstract class IndexPager extends ContextSource implements Pager {
+       /**
+        * Constants for the $mDefaultDirection field.
+        *
+        * These are boolean for historical reasons and should stay boolean for backwards-compatibility.
+        */
+       const DIR_ASCENDING = false;
+       const DIR_DESCENDING = true;
+
        public $mRequest;
-       public $mLimitsShown = array( 20, 50, 100, 250, 500 );
+       public $mLimitsShown = [ 20, 50, 100, 250, 500 ];
        public $mDefaultLimit = 50;
        public $mOffset, $mLimit;
        public $mQueryDone = false;
@@ -87,7 +95,7 @@ abstract class IndexPager extends ContextSource implements Pager {
        protected $mOrderType;
        /**
         * $mDefaultDirection gives the direction to use when sorting results:
-        * false for ascending, true for descending.  If $mIsBackwards is set, we
+        * DIR_ASCENDING or DIR_DESCENDING.  If $mIsBackwards is set, we
         * start from the opposite end, but we still sort the page itself according
         * to $mDefaultDirection.  E.g., if $mDefaultDirection is false but we're
         * going backwards, we'll display the last page of results, but the last
@@ -148,14 +156,14 @@ abstract class IndexPager extends ContextSource implements Pager {
                        $this->mIndexField = $index[$order];
                        $this->mExtraSortFields = isset( $extraSort[$order] )
                                ? (array)$extraSort[$order]
-                               : array();
+                               : [];
                } elseif ( is_array( $index ) ) {
                        # First element is the default
                        reset( $index );
                        list( $this->mOrderType, $this->mIndexField ) = each( $index );
                        $this->mExtraSortFields = isset( $extraSort[$this->mOrderType] )
                                ? (array)$extraSort[$this->mOrderType]
-                               : array();
+                               : [];
                } else {
                        # $index is not an array
                        $this->mOrderType = null;
@@ -174,7 +182,7 @@ abstract class IndexPager extends ContextSource implements Pager {
        /**
         * Get the Database object in use
         *
-        * @return DatabaseBase
+        * @return IDatabase
         */
        public function getDatabase() {
                return $this->mDb;
@@ -188,8 +196,9 @@ abstract class IndexPager extends ContextSource implements Pager {
        public function doQuery() {
                # Use the child class name for profiling
                $fname = __METHOD__ . ' (' . get_class( $this ) . ')';
-               wfProfileIn( $fname );
+               $section = Profiler::instance()->scopedProfileIn( $fname );
 
+               // @todo This should probably compare to DIR_DESCENDING and DIR_ASCENDING constants
                $descending = ( $this->mIsBackwards == $this->mDefaultDirection );
                # Plus an extra row so that we can tell the "next" link should be shown
                $queryLimit = $this->mLimit + 1;
@@ -217,8 +226,6 @@ abstract class IndexPager extends ContextSource implements Pager {
 
                $this->preprocessResults( $this->mResult );
                $this->mResult->rewind(); // Paranoia
-
-               wfProfileOut( $fname );
        }
 
        /**
@@ -371,15 +378,15 @@ abstract class IndexPager extends ContextSource implements Pager {
                $info = $this->getQueryInfo();
                $tables = $info['tables'];
                $fields = $info['fields'];
-               $conds = isset( $info['conds'] ) ? $info['conds'] : array();
-               $options = isset( $info['options'] ) ? $info['options'] : array();
-               $join_conds = isset( $info['join_conds'] ) ? $info['join_conds'] : array();
-               $sortColumns = array_merge( array( $this->mIndexField ), $this->mExtraSortFields );
+               $conds = isset( $info['conds'] ) ? $info['conds'] : [];
+               $options = isset( $info['options'] ) ? $info['options'] : [];
+               $join_conds = isset( $info['join_conds'] ) ? $info['join_conds'] : [];
+               $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
                if ( $descending ) {
                        $options['ORDER BY'] = $sortColumns;
                        $operator = $this->mIncludeOffset ? '>=' : '>';
                } else {
-                       $orderBy = array();
+                       $orderBy = [];
                        foreach ( $sortColumns as $col ) {
                                $orderBy[] = $col . ' DESC';
                        }
@@ -390,7 +397,7 @@ abstract class IndexPager extends ContextSource implements Pager {
                        $conds[] = $this->mIndexField . $operator . $this->mDb->addQuotes( $offset );
                }
                $options['LIMIT'] = intval( $limit );
-               return array( $tables, $fields, $conds, $fname, $options, $join_conds );
+               return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
        }
 
        /**
@@ -447,8 +454,8 @@ abstract class IndexPager extends ContextSource implements Pager {
         *
         * @param string $text Text displayed on the link
         * @param array $query Associative array of parameter to be in the query string
-        * @param string $type Value of the "rel" attribute
-        *
+        * @param string $type Link type used to create additional attributes, like "rel", "class" or
+        *  "title". Valid values (non-exhaustive list): 'first', 'last', 'prev', 'next', 'asc', 'desc'.
         * @return string HTML fragment
         */
        function makeLink( $text, array $query = null, $type = null ) {
@@ -456,12 +463,15 @@ abstract class IndexPager extends ContextSource implements Pager {
                        return $text;
                }
 
-               $attrs = array();
-               if ( in_array( $type, array( 'first', 'prev', 'next', 'last' ) ) ) {
-                       # HTML5 rel attributes
+               $attrs = [];
+               if ( in_array( $type, [ 'prev', 'next' ] ) ) {
                        $attrs['rel'] = $type;
                }
 
+               if ( in_array( $type, [ 'asc', 'desc' ] ) ) {
+                       $attrs['title'] = wfMessage( $type == 'asc' ? 'sort-ascending' : 'sort-descending' )->text();
+               }
+
                if ( $type ) {
                        $attrs['class'] = "mw-{$type}link";
                }
@@ -563,26 +573,26 @@ abstract class IndexPager extends ContextSource implements Pager {
                        $prev = false;
                        $first = false;
                } else {
-                       $prev = array(
+                       $prev = [
                                'dir' => 'prev',
                                'offset' => $this->mFirstShown,
                                'limit' => $urlLimit
-                       );
-                       $first = array( 'limit' => $urlLimit );
+                       ];
+                       $first = [ 'limit' => $urlLimit ];
                }
                if ( $this->mIsLast ) {
                        $next = false;
                        $last = false;
                } else {
-                       $next = array( 'offset' => $this->mLastShown, 'limit' => $urlLimit );
-                       $last = array( 'dir' => 'prev', 'limit' => $urlLimit );
+                       $next = [ 'offset' => $this->mLastShown, 'limit' => $urlLimit ];
+                       $last = [ 'dir' => 'prev', 'limit' => $urlLimit ];
                }
-               return array(
+               return [
                        'prev' => $prev,
                        'next' => $next,
                        'first' => $first,
                        'last' => $last
-               );
+               ];
        }
 
        /**
@@ -608,9 +618,9 @@ abstract class IndexPager extends ContextSource implements Pager {
         * @param array $disabledTexts
         * @return array
         */
-       function getPagingLinks( $linkTexts, $disabledTexts = array() ) {
+       function getPagingLinks( $linkTexts, $disabledTexts = [] ) {
                $queries = $this->getPagingQueries();
-               $links = array();
+               $links = [];
 
                foreach ( $queries as $type => $query ) {
                        if ( $query !== false ) {
@@ -630,7 +640,7 @@ abstract class IndexPager extends ContextSource implements Pager {
        }
 
        function getLimitLinks() {
-               $links = array();
+               $links = [];
                if ( $this->mIsBackwards ) {
                        $offset = $this->mPastTheEndIndex;
                } else {
@@ -639,7 +649,7 @@ abstract class IndexPager extends ContextSource implements Pager {
                foreach ( $this->mLimitsShown as $limit ) {
                        $links[] = $this->makeLink(
                                $this->getLanguage()->formatNum( $limit ),
-                               array( 'offset' => $offset, 'limit' => $limit ),
+                               [ 'offset' => $offset, 'limit' => $limit ],
                                'num'
                        );
                }
@@ -690,8 +700,8 @@ abstract class IndexPager extends ContextSource implements Pager {
         * not be used in the pager offset or in any links for users.
         *
         * If getIndexField() returns an array of 'querykey' => 'indexfield' pairs then
-        * this must return a corresponding array of 'querykey' => array( fields...) pairs
-        * in order for a request with &count=querykey to use array( fields...) to sort.
+        * this must return a corresponding array of 'querykey' => [ fields... ] pairs
+        * in order for a request with &count=querykey to use [ fields... ] to sort.
         *
         * This is useful for pagers that GROUP BY a unique column (say page_id)
         * and ORDER BY another (say page_len). Using GROUP BY and ORDER BY both on
@@ -701,12 +711,12 @@ abstract class IndexPager extends ContextSource implements Pager {
         * @return array
         */
        protected function getExtraSortFields() {
-               return array();
+               return [];
        }
 
        /**
-        * Return the default sorting direction: false for ascending, true for
-        * descending.  You can also have an associative array of ordertype => dir,
+        * Return the default sorting direction: DIR_ASCENDING or DIR_DESCENDING.
+        * You can also have an associative array of ordertype => dir,
         * if multiple order types are supported.  In this case getIndexField()
         * must return an array, and the keys of that must exactly match the keys
         * of this.
@@ -724,6 +734,6 @@ abstract class IndexPager extends ContextSource implements Pager {
         * @return bool
         */
        protected function getDefaultDirections() {
-               return false;
+               return IndexPager::DIR_ASCENDING;
        }
 }