Merge "Add missing @return to function docs"
[lhc/web/wiklou.git] / includes / deferred / LinksUpdate.php
index abd44fd..45d2664 100644 (file)
@@ -31,7 +31,7 @@ class LinksUpdate extends SqlDataUpdate {
        /** @var int Page ID of the article linked from */
        public $mId;
 
-       /** @var Title object of the article linked from */
+       /** @var Title Title object of the article linked from */
        public $mTitle;
 
        /** @var ParserOutput */
@@ -52,7 +52,7 @@ class LinksUpdate extends SqlDataUpdate {
        /** @var array Map of category names to sort keys */
        public $mCategories;
 
-       /** @var array ap of language codes to titles */
+       /** @var array Map of language codes to titles */
        public $mInterlangs;
 
        /** @var array Map of arbitrary name to value */
@@ -358,6 +358,7 @@ class LinksUpdate extends SqlDataUpdate {
                        foreach ( $diffs as $dbk => $id ) {
                                $arr[] = array(
                                        'pl_from' => $this->mId,
+                                       'pl_from_namespace' => $this->mTitle->getNamespace(),
                                        'pl_namespace' => $ns,
                                        'pl_title' => $dbk
                                );
@@ -379,6 +380,7 @@ class LinksUpdate extends SqlDataUpdate {
                        foreach ( $diffs as $dbk => $id ) {
                                $arr[] = array(
                                        'tl_from' => $this->mId,
+                                       'tl_from_namespace' => $this->mTitle->getNamespace(),
                                        'tl_namespace' => $ns,
                                        'tl_title' => $dbk
                                );
@@ -400,6 +402,7 @@ class LinksUpdate extends SqlDataUpdate {
                foreach ( $diffs as $iname => $dummy ) {
                        $arr[] = array(
                                'il_from' => $this->mId,
+                               'il_from_namespace' => $this->mTitle->getNamespace(),
                                'il_to' => $iname
                        );
                }
@@ -502,18 +505,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