Merge "Introducing pp_sortkey."
[lhc/web/wiklou.git] / includes / deferred / LinksUpdate.php
index 01555ff..0789b21 100644 (file)
@@ -256,7 +256,7 @@ class LinksUpdate extends SqlDataUpdate {
                        $job = new RefreshLinksJob(
                                $title,
                                array(
-                                       'table'     => $table,
+                                       'table' => $table,
                                        'recursive' => true,
                                ) + Job::newRootJobParams( // "overall" refresh links job info
                                        "refreshlinks:{$table}:{$title->getPrefixedText()}"
@@ -269,7 +269,7 @@ class LinksUpdate extends SqlDataUpdate {
        }
 
        /**
-        * @param $cats
+        * @param array $cats
         */
        function invalidateCategories( $cats ) {
                $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
@@ -288,7 +288,7 @@ class LinksUpdate extends SqlDataUpdate {
        }
 
        /**
-        * @param $images
+        * @param array $images
         */
        function invalidateImageDescriptions( $images ) {
                $this->invalidatePages( NS_FILE, array_keys( $images ) );
@@ -329,7 +329,7 @@ class LinksUpdate extends SqlDataUpdate {
                                $toField = $prefix . '_to';
                        }
                        if ( count( $deletions ) ) {
-                               $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
+                               $where[$toField] = array_keys( $deletions );
                        } else {
                                $where = false;
                        }
@@ -432,7 +432,7 @@ class LinksUpdate extends SqlDataUpdate {
        /**
         * Get an array of category insertions
         *
-        * @param array $existing mapping existing category names to sort keys. If both
+        * @param array $existing Mapping existing category names to sort keys. If both
         * match a link in $this, the link will be omitted from the output
         *
         * @return array
@@ -477,7 +477,7 @@ class LinksUpdate extends SqlDataUpdate {
        /**
         * Get an array of interlanguage link insertions
         *
-        * @param array $existing mapping existing language codes to titles
+        * @param array $existing Mapping existing language codes to titles
         *
         * @return array
         */
@@ -502,18 +502,69 @@ class LinksUpdate extends SqlDataUpdate {
         */
        function getPropertyInsertions( $existing = array() ) {
                $diffs = array_diff_assoc( $this->mProperties, $existing );
+
                $arr = array();
-               foreach ( $diffs as $name => $value ) {
-                       $arr[] = array(
-                               'pp_page' => $this->mId,
-                               'pp_propname' => $name,
-                               'pp_value' => $value,
-                       );
+               foreach ( array_keys( $diffs ) as $name ) {
+                       $arr[] = $this->getPagePropRowData( $name );
                }
 
                return $arr;
        }
 
+       /**
+        * Returns an associative array to be used for inserting a row into
+        * the page_props table. Besides the given property name, this will
+        * include the page id from $this->mId and any property value from
+        * $this->mProperties.
+        *
+        * The array returned will include the pp_sortkey field if this
+        * is present in the database (as indicated by $wgPagePropsHaveSortkey).
+        * The sortkey value is currently determined by getPropertySortKeyValue().
+        *
+        * @note: this assumes that $this->mProperties[$prop] is defined.
+        *
+        * @param string $prop The name of the property.
+        *
+        * @return array
+        */
+       private function getPagePropRowData( $prop ) {
+               global $wgPagePropsHaveSortkey;
+
+               $value = $this->mProperties[$prop];
+
+               $row = array(
+                       'pp_page' => $this->mId,
+                       'pp_propname' => $prop,
+                       'pp_value' => $value,
+               );
+
+               if ( $wgPagePropsHaveSortkey ) {
+                       $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
+               }
+
+               return $row;
+       }
+
+       /**
+        * Determines the sort key for the given property value.
+        * This will return $value if it is a float or int,
+        * 1 or resp. 0 if it is a bool, and null otherwise.
+        *
+        * @note: In the future, we may allow the sortkey to be specified explicitly
+        *        in ParserOutput::setProperty.
+        *
+        * @param mixed $value
+        *
+        * @return float|null
+        */
+       private function getPropertySortKeyValue( $value ) {
+               if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
+                       return floatval( $value );
+               }
+
+               return null;
+       }
+
        /**
         * Get an array of interwiki insertions for passing to the DB
         * Skips the titles specified by the 2-D array $existing
@@ -769,7 +820,7 @@ class LinksUpdate extends SqlDataUpdate {
        /**
         * Get an array of existing categories, with the name in the key and sort key in the value.
         *
-        * @return array of property names and values
+        * @return array Array of property names and values
         */
        private function getExistingProperties() {
                $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
@@ -831,7 +882,7 @@ class LinksUpdate extends SqlDataUpdate {
        /**
         * Fetch page links added by this LinksUpdate.  Only available after the update is complete.
         * @since 1.22
-        * @return null|array of Titles
+        * @return null|array Array of Titles
         */
        public function getAddedLinks() {
                if ( $this->linkInsertions === null ) {
@@ -848,7 +899,7 @@ class LinksUpdate extends SqlDataUpdate {
        /**
         * Fetch page links removed by this LinksUpdate.  Only available after the update is complete.
         * @since 1.22
-        * @return null|array of Titles
+        * @return null|array Array of Titles
         */
        public function getRemovedLinks() {
                if ( $this->linkDeletions === null ) {