Migrate remaining usages of Title::userCan() to PermissionManager
[lhc/web/wiklou.git] / includes / specials / pagers / ImageListPager.php
index 3ddbe08..8adef98 100644 (file)
@@ -253,28 +253,26 @@ class ImageListPager extends TablePager {
         * @return array Query info
         */
        protected function getQueryInfoReal( $table ) {
+               $dbr = wfGetDB( DB_REPLICA );
                $prefix = $table === 'oldimage' ? 'oi' : 'img';
 
                $tables = [ $table ];
-               $fields = $this->getFieldNames();
+               $fields = array_keys( $this->getFieldNames() );
+               $fields = array_combine( $fields, $fields );
                unset( $fields['img_description'] );
                unset( $fields['img_user_text'] );
-               $fields = array_keys( $fields );
 
                if ( $table === 'oldimage' ) {
-                       foreach ( $fields as $id => &$field ) {
-                               if ( substr( $field, 0, 4 ) !== 'img_' ) {
-                                       continue;
+                       foreach ( $fields as $id => $field ) {
+                               if ( substr( $id, 0, 4 ) === 'img_' ) {
+                                       $fields[$id] = $prefix . substr( $field, 3 );
                                }
-                               $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
-                       }
-                       $fields[array_search( 'top', $fields )] = "'no' AS top";
-               } else {
-                       if ( $this->mShowAll ) {
-                               $fields[array_search( 'top', $fields )] = "'yes' AS top";
                        }
+                       $fields['top'] = $dbr->addQuotes( 'no' );
+               } elseif ( $this->mShowAll ) {
+                       $fields['top'] = $dbr->addQuotes( 'yes' );
                }
-               $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
+               $fields['thumb'] = $prefix . '_name';
 
                $options = $join_conds = [];
 
@@ -283,7 +281,7 @@ class ImageListPager extends TablePager {
                $tables += $commentQuery['tables'];
                $fields += $commentQuery['fields'];
                $join_conds += $commentQuery['joins'];
-               $fields['description_field'] = "'{$prefix}_description'";
+               $fields['description_field'] = $dbr->addQuotes( "{$prefix}_description" );
 
                # User fields
                $actorQuery = ActorMigration::newMigration()->getJoin( $prefix . '_user' );
@@ -295,20 +293,13 @@ class ImageListPager extends TablePager {
 
                # Depends on $wgMiserMode
                # Will also not happen if mShowAll is true.
-               if ( isset( $this->mFieldNames['count'] ) ) {
-                       $tables[] = 'oldimage';
-
-                       # Need to rewrite this one
-                       foreach ( $fields as &$field ) {
-                               if ( $field == 'count' ) {
-                                       $field = 'COUNT(oi_archive_name) AS count';
-                               }
-                       }
-                       unset( $field );
-
-                       $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
-                       $options = [ 'GROUP BY' => array_merge( [ $fields['img_user'] ], $columnlist ) ];
-                       $join_conds['oldimage'] = [ 'LEFT JOIN', 'oi_name = img_name' ];
+               if ( isset( $fields['count'] ) ) {
+                       $fields['count'] = $dbr->buildSelectSubquery(
+                               'oldimage',
+                               'COUNT(oi_archive_name)',
+                               'oi_name = img_name',
+                               __METHOD__
+                       );
                }
 
                return [
@@ -328,15 +319,15 @@ class ImageListPager extends TablePager {
         *   is descending, so I renamed it to $asc here.
         * @param int $offset
         * @param int $limit
-        * @param bool $asc
-        * @return array
+        * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
+        * @return FakeResultWrapper
         * @throws MWException
         */
-       function reallyDoQuery( $offset, $limit, $asc ) {
+       function reallyDoQuery( $offset, $limit, $order ) {
                $prevTableName = $this->mTableName;
                $this->mTableName = 'image';
                list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
-                       $this->buildQueryInfo( $offset, $limit, $asc );
+                       $this->buildQueryInfo( $offset, $limit, $order );
                $imageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
                $this->mTableName = $prevTableName;
 
@@ -354,13 +345,13 @@ class ImageListPager extends TablePager {
                $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
 
                list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
-                       $this->buildQueryInfo( $offset, $limit, $asc );
+                       $this->buildQueryInfo( $offset, $limit, $order );
                $oldimageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
 
                $this->mTableName = $prevTableName;
                $this->mIndexField = $oldIndex;
 
-               return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
+               return $this->combineResult( $imageRes, $oldimageRes, $limit, $order );
        }
 
        /**
@@ -389,14 +380,12 @@ class ImageListPager extends TablePager {
                                        $resultArray[] = $topRes2;
                                        $topRes2 = $res2->next();
                                }
+                       } elseif ( !$ascending ) {
+                               $resultArray[] = $topRes2;
+                               $topRes2 = $res2->next();
                        } else {
-                               if ( !$ascending ) {
-                                       $resultArray[] = $topRes2;
-                                       $topRes2 = $res2->next();
-                               } else {
-                                       $resultArray[] = $topRes1;
-                                       $topRes1 = $res1->next();
-                               }
+                               $resultArray[] = $topRes1;
+                               $topRes1 = $res1->next();
                        }
                }
 
@@ -487,7 +476,9 @@ class ImageListPager extends TablePager {
 
                                        // Add delete links if allowed
                                        // From https://github.com/Wikia/app/pull/3859
-                                       if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
+                                       $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
+
+                                       if ( $permissionManager->userCan( 'delete', $this->getUser(), $filePage ) ) {
                                                $deleteMsg = $this->msg( 'listfiles-delete' )->text();
 
                                                $delete = $linkRenderer->makeKnownLink(
@@ -524,7 +515,7 @@ class ImageListPager extends TablePager {
                                return $this->getLanguage()->formatNum( intval( $value ) + 1 );
                        case 'top':
                                // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
-                               return $this->msg( 'listfiles-latestversion-' . $value );
+                               return $this->msg( 'listfiles-latestversion-' . $value )->escaped();
                        default:
                                throw new MWException( "Unknown field '$field'" );
                }