Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Command to convert contains_ to tags

  • Loading branch information...
commit 3a1f194088b521cb4f8262a83727417f973d4110 1 parent c097d12
@shama shama authored
View
47 Console/Command/PackagesShell.php
@@ -1,5 +1,6 @@
<?php
App::uses('PackageExistsJob', 'Lib/Job');
+App::uses('CakeSession', 'Model/Datasource');
class PackagesShell extends AppShell {
@@ -227,4 +228,50 @@ public function characterize() {
$this->out(sprintf(__('* Checked %s of %s repositories'), $count, count($packages)));
}
+/**
+ * Converts the existing `contains_` fields into tags.
+ *
+ * @return void
+ */
+ public function convertContainFields() {
+ $count = 0;
+ $this->Package->Behaviors->unload('CakePackagesTaggable');
+ $packages = $this->Package->find('all', array(
+ 'conditions' => array(
+ 'OR' => array(
+ 'Package.tags IS NULL',
+ 'Package.tags' => '',
+ ),
+ ),
+ 'contain' => array(),
+ ));
+ $convert = array();
+ foreach ($packages as $key => $package) {
+ $tags = array();
+ foreach ($package['Package'] as $field => $val) {
+ if (substr($field, 0, 9) == 'contains_' && !empty($val)) {
+ $tags[] = substr($field, 9);
+ }
+ }
+ if (!empty($tags)) {
+ $packages[$key]['Package']['contains'] = $tags;
+ } else {
+ unset($packages[$key]);
+ }
+ }
+ CakeSession::write('Auth', array(
+ 'User' => array('is_admin' => true),
+ ));
+ $this->Package->Behaviors->load('CakePackagesTaggable');
+ reset($packages);
+ foreach ($packages as $package) {
+ $this->Package->create();
+ $this->Package->save($package, false);
+ $this->out(sprintf('[CONVERT] %s', $package['Package']['name']));
+ $count++;
+ }
+ $this->out(sprintf(__('* Converted %s packages'), $count++));
+ CakeSession::delete('Auth');
+ }
+
}
View
50 Test/Case/Console/Command/PackagesShellTest.php
@@ -0,0 +1,50 @@
+<?php
+App::uses('PackagesShell', 'Console/Command');
+
+class PackagesShellTest extends CakeTestCase {
+
+/**
+ * Fixtures
+ *
+ * @var array
+ */
+ public $fixtures = array(
+ 'app.package',
+ 'app.tag',
+ 'app.tagged',
+ );
+
+/**
+ * setUp method
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ $this->Shell = new PackagesShell();
+ $this->Shell->initialize();
+ }
+
+/**
+ * tearDown method
+ *
+ * @return void
+ */
+ public function tearDown() {
+ parent::tearDown();
+ }
+
+/**
+ * testConvertContainFields
+ *
+ * @return void
+ */
+ public function testConvertContainFields() {
+ $this->Shell->convertContainFields();
+ $this->Shell->Package->contain(array('Tag'));
+ $result = $this->Shell->Package->findById(3);
+ $this->assertTrue($result['Package']['contains_model']);
+ $this->assertTrue($result['Package']['contains_theme']);
+ }
+
+}
View
36 Test/Fixture/PackageFixture.php
@@ -127,5 +127,41 @@ class PackageFixture extends CakeTestFixture {
'contains_app' => 0,
'deleted' => 0,
),
+ array(
+ 'id' => 3,
+ 'maintainer_id' => 2,
+ 'name' => 'previouspackages',
+ 'repository_url' => 'http://github.com/shama/junk',
+ 'bakery_article' => '',
+ 'homepage' => 'http://shama.github.com/junk/',
+ 'description' => 'Lorem ipsum dolor sit amet',
+ 'tags' => '',
+ 'category_id' => 2,
+ 'open_issues' => 1,
+ 'forks' => 1,
+ 'watchers' => 1,
+ 'collaborators' => 1,
+ 'contributors' => 1,
+ 'created_at' => '2012-02-24 04:42:44',
+ 'last_pushed_at' => '2012-02-24 04:42:44',
+ 'created' => '2012-02-24 04:42:44',
+ 'modified' => '2012-02-24 04:42:44',
+ 'contains_model' => 1,
+ 'contains_view' => 0,
+ 'contains_controller' => 0,
+ 'contains_behavior' => 0,
+ 'contains_helper' => 0,
+ 'contains_component' => 0,
+ 'contains_shell' => 0,
+ 'contains_theme' => 1,
+ 'contains_datasource' => 0,
+ 'contains_vendor' => 0,
+ 'contains_test' => 0,
+ 'contains_lib' => 0,
+ 'contains_resource' => 0,
+ 'contains_config' => 0,
+ 'contains_app' => 0,
+ 'deleted' => 0,
+ ),
);
}
Please sign in to comment.
Something went wrong with that request. Please try again.