r80305 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80304‎ | r80305 | r80306 >
Date:19:34, 14 January 2011
Author:btongminh
Status:deferred
Tags:license-work 
Comment:
Function documentation; change a few constructors.
Modified paths:
  • /branches/license-work/phase3/includes/filerepo/FileProperties.php (modified) (history)

Diff [purge]

Index: branches/license-work/phase3/includes/filerepo/FileProperties.php
@@ -1,6 +1,13 @@
22 <?php
33
44 class FileProperties {
 5+ /**
 6+ * Constructor for the FileProperties class that represents proeprties
 7+ * associated with a file.
 8+ *
 9+ * @param $file File
 10+ * @param $revision int Revision id
 11+ */
512 public function __construct( $file, $revision = null ) {
613 $this->file = file;
714 $this->revId = $revision;
@@ -11,6 +18,9 @@
1219 $this->load();
1320 }
1421
 22+ /**
 23+ * Load the properties for this file from the DB
 24+ */
1525 public function load() {
1626 if ( !$this->revId ) {
1727 $this->revId = $this->file->getTitle()->getLatestRevID();
@@ -24,7 +34,11 @@
2535 $this->loadFromResult( $res );
2636 }
2737
28 - public function loadFromResult( $res ) {
 38+ /**
 39+ * Construct FileAuthor and FileProperties objects from a database result
 40+ */
 41+ protected function loadFromResult( $res ) {
 42+ $repo = $this->file->getRepo();
2943 $result = array();
3044
3145 foreach ( $res as $row ) {
@@ -33,7 +47,7 @@
3448 $this->authors[] = FileAuthor::newFromRow( $row );
3549 break;
3650 case 'license':
37 - $this->licenses[] = FileLicense::newFromRow( $row );
 51+ $this->licenses[] = FileLicense::newFromRow( $repo, $row );
3852 break;
3953 }
4054 }
@@ -43,13 +57,25 @@
4458 }
4559 }
4660
 61+ /**
 62+ * Get all licenses associated with this file
 63+ */
4764 public function getLicenses() {
4865 return $this->licenses;
4966 }
 67+ /**
 68+ * Get all authors associated with this file
 69+ */
5070 public function getAuthors() {
5171 return $this->authors;
5272 }
5373
 74+ /**
 75+ * Save the licenses and authors to the database
 76+ *
 77+ * @param $comment string Edit summary
 78+ * @param $minor bool Minor edit
 79+ */
5480 public function save( $comment, $minor = false ) {
5581 $dbw = $this->file->getRepo()->getMasterDB();
5682
@@ -68,7 +94,7 @@
6995 'fp_value_int' => $author->getId()
7096 );
7197
72 - $text = $author->getRawText();
 98+ $text = $author->getText();
7399 if ( $text ) {
74100 $a['fp_value_text'] = $text;
75101 }
@@ -88,35 +114,80 @@
89115 }
90116
91117 class FileAuthor {
 118+ /**
 119+ * Construct a FileAuthor object from a database row
 120+ *
 121+ * @param $row Stdclass
 122+ * @return FileAuthor
 123+ */
92124 public static function newFromRow( $row ) {
93125 return new self( $row->fp_value_int, $row->fp_value_text );
94126 }
95 -
 127+ /**
 128+ * Constructor for a FileAuthor object
 129+ *
 130+ * @param $id int User id if any
 131+ * @param $text string User text
 132+ */
96133 public function __construct( $id, $text ) {
97134 $this->id = $id;
98135 $this->text = $text;
99136 }
 137+ /**
 138+ * Get the user id if any, 0 or null otherwise
 139+ *
 140+ * @return int
 141+ */
100142 public function getUserId() {
101143 return $this->id;
102144 }
 145+ /**
 146+ * Get the user text
 147+ *
 148+ * @return string
 149+ */
103150 public function getText() {
104 - if ( $this->text ) {
105 - return $this->text;
106 - }
107 - return User::newFromId( $this->id )->getName();
108 - }
109 - public function getRawText() {
110151 return $this->text;
111152 }
112153 }
113154
114155 class FileLicense {
115 - public static function newFromRow( $row ) {
116 - return new self( $row->fp_value_int );
 156+ /**
 157+ * Initialize a license object from a row
 158+ *
 159+ * @param $repo FileRepo
 160+ * @param $row Stdclass
 161+ * @return FileLicense
 162+ */
 163+ public static function newFromRow( $repo, $row ) {
 164+ $license = new self( $repo );
 165+ $license->id = $row->fp_value_int;
 166+ return $license;
117167 }
 168+ /**
 169+ * Initialize a license object from a name
 170+ *
 171+ * @param $repo FileRepo
 172+ * @param $name string
 173+ * @return FileLicense
 174+ */
 175+ public static function newFromName( $repo, $name ) {
 176+ $license = new self( $repo );
 177+ $license->name = $name;
 178+ return $license;
 179+ }
118180
119 - public function __construct( $id ) {
120 - $this->id = $id;
 181+ /**
 182+ * Constructor for the FileLicense
 183+ *
 184+ * @param $repo FileRepo
 185+ */
 186+ public function __construct( $repo ) {
 187+ $this->repo = $repo;
 188+ $this->id = null;
 189+ $this->name = null;
 190+ $this->url = null;
 191+ $this->count = 0;
121192 }
122193 public function getId() {
123194 return $this->id;
@@ -124,29 +195,57 @@
125196 public function getName() {
126197 return $this->name;
127198 }
 199+ public function setName( $name ) {
 200+ $this->name = $name;
 201+ }
128202 public function getUrl() {
129203 return $this->url;
130204 }
 205+ public function setUrl( $url ) {
 206+ $this->url = $url;
 207+ }
131208 public function getCount() {
132209 return $this->count;
133210 }
134211
135 - public static function loadArray( &$licenses ) {
 212+ /**
 213+ * Initializes an uninitialized array of FileLicense objects from the db.
 214+ * Removes non-existent licenses from the array. Allows loading from id or
 215+ * name.
 216+ *
 217+ * @param &$licenses array Array of FileLicense objects
 218+ * @param $loadFrom string id|name depending on from which field to load
 219+ */
 220+ public static function loadArray( &$licenses, $loadFrom = 'id' ) {
136221 $licenseIds = array();
137222 foreach ( $licenses as $license ) {
138 - $licenseIds[] = $license->getId();
 223+ switch ( $loadFrom ) {
 224+ case 'id':
 225+ $licenseIds[] = $license->getId();
 226+ break;
 227+ case 'name':
 228+ $licenseIds[] = $license->getName();
 229+ break;
 230+ }
 231+
139232 }
 233+ if ( !$licenseIds ) {
 234+ $licenses = array();
 235+ return;
 236+ }
140237
141 - $dbr = wfGetDB( DB_SLAVE );
 238+ $dbr = $this->repo->getSlaveDb();
142239 $res = $dbr->select( 'licenses',
143240 array( 'lic_id', 'lic_name', 'lic_url', 'lic_count' ),
144 - array( 'lic_id' => $licenseIds ),
 241+ array( "lic_{$loadFrom}" => $licenseIds ),
145242 __METHOD__ );
146 -
 243+
 244+ // Create an id<>row map
147245 $licenseData = array();
148246 foreach ( $res as $row ) {
149247 $licenseData[$row->lic_id] = $row;
150248 }
 249+ // Initializes the licenses from the rows, removes non-existent licenses
151250 foreach ( $licenses as $key => $license ) {
152251 if ( isset( $licenseData[$license->getId()] ) ) {
153252 $license->loadFromLicenseRow( $licenseData[$license->getId()] );
@@ -156,11 +255,50 @@
157256 }
158257 }
159258
160 -
 259+ /**
 260+ * Initializes a license from a row.
 261+ *
 262+ * @param $row Stdclass
 263+ */
161264 public function loadFromLicenseRow( $row ) {
162265 $this->name = $row->lic_name;
163266 $this->url = $row->lic_url;
164267 $this->count = $row->lic_count;
165268 }
166269
 270+ /**
 271+ * Inserts a new license into the database.
 272+ */
 273+ public function insert() {
 274+ $dbw = $this->repo->getMasterDb();
 275+ $dbw->insert( 'licenses', array(
 276+ 'lic_name' => $this->getName(),
 277+ 'lic_url' => $this->getUrl(),
 278+ 'lic_count' => 0 ),
 279+ __METHOD__ );
 280+ $this->id = $dbw->insertId();
 281+ }
 282+ /**
 283+ * Updates information for a license.
 284+ */
 285+ public function update() {
 286+ $dbw = $this->repo->getMasterDb();
 287+ $dbw->update( 'licenses', array(
 288+ 'lic_name' => $this->getName(),
 289+ 'lic_url' => $this->getUrl(),
 290+ 'lic_count' => $this->count + 1 ),
 291+ array( 'lic_id' => $this->id ),
 292+ __METHOD__ );
 293+ }
 294+ /**
 295+ * Inserts a new license into the database if this license does not have
 296+ * an id, updates it otherwise
 297+ */
 298+ public function save() {
 299+ if ( $this->id ) {
 300+ $this->update();
 301+ } else {
 302+ $this->insert();
 303+ }
 304+ }
167305 }

Status & tagging log