| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 168 |
| Lines | 72 |
| Ratio | 42.86 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 18 | use XoopsModules\News; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @return bool |
||
| 22 | */ |
||
| 23 | function xoops_module_update_news() |
||
| 24 | { |
||
| 25 | global $xoopsDB; |
||
| 26 | $errors = 0; |
||
| 27 | |||
| 28 | //0) Rename all tables |
||
| 29 | |||
| 30 | if (News\Utility::existTable($xoopsDB->prefix('stories_files'))) { |
||
| 31 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files')); |
||
| 32 | $result = $xoopsDB->queryF($sql); |
||
| 33 | if (!$result) { |
||
| 34 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2; |
||
| 35 | ++$errors; |
||
| 36 | } |
||
| 37 | } elseif (!News\Utility::existTable($xoopsDB->prefix('news_stories_files'))) { |
||
| 38 | // 1) Create, if it does not exists, the stories_files table |
||
| 39 | $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_files') . " ( |
||
| 40 | fileid INT(8) UNSIGNED NOT NULL AUTO_INCREMENT, |
||
| 41 | filerealname VARCHAR(255) NOT NULL DEFAULT '', |
||
| 42 | storyid INT(8) UNSIGNED NOT NULL DEFAULT '0', |
||
| 43 | date INT(10) NOT NULL DEFAULT '0', |
||
| 44 | mimetype VARCHAR(64) NOT NULL DEFAULT '', |
||
| 45 | downloadname VARCHAR(255) NOT NULL DEFAULT '', |
||
| 46 | counter INT(8) UNSIGNED NOT NULL DEFAULT '0', |
||
| 47 | PRIMARY KEY (fileid), |
||
| 48 | KEY storyid (storyid) |
||
| 49 | ) ENGINE=MyISAM;"; |
||
| 50 | if (!$xoopsDB->queryF($sql)) { |
||
| 51 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED1; |
||
| 52 | ++$errors; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if (News\Utility::existTable($xoopsDB->prefix('stories'))) { |
||
| 57 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories') . ' RENAME ' . $xoopsDB->prefix('news_stories')); |
||
| 58 | $result = $xoopsDB->queryF($sql); |
||
| 59 | if (!$result) { |
||
| 60 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2; |
||
| 61 | ++$errors; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | if (News\Utility::existTable($xoopsDB->prefix('topics'))) { |
||
| 66 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('topics') . ' RENAME ' . $xoopsDB->prefix('news_topics')); |
||
| 67 | $result = $xoopsDB->queryF($sql); |
||
| 68 | if (!$result) { |
||
| 69 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2; |
||
| 70 | ++$errors; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | if (News\Utility::existTable($xoopsDB->prefix('stories_files'))) { |
||
| 75 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files')); |
||
| 76 | $result = $xoopsDB->queryF($sql); |
||
| 77 | if (!$result) { |
||
| 78 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2; |
||
| 79 | ++$errors; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | // 2) Change the topic title's length, in the topics table |
||
| 84 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' CHANGE topic_title topic_title VARCHAR( 255 ) NOT NULL;'); |
||
| 85 | $result = $xoopsDB->queryF($sql); |
||
| 86 | if (!$result) { |
||
| 87 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2; |
||
| 88 | ++$errors; |
||
| 89 | } |
||
| 90 | |||
| 91 | // 2.1) Add the new fields to the topic table |
||
| 92 | if (!News\Utility::existField('menu', $xoopsDB->prefix('news_topics'))) { |
||
| 93 | News\Utility::addField("menu TINYINT( 1 ) DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_topics')); |
||
| 94 | } |
||
| 95 | if (!News\Utility::existField('topic_frontpage', $xoopsDB->prefix('news_topics'))) { |
||
| 96 | News\Utility::addField("topic_frontpage TINYINT( 1 ) DEFAULT '1' NOT NULL", $xoopsDB->prefix('news_topics')); |
||
| 97 | } |
||
| 98 | if (!News\Utility::existField('topic_rssurl', $xoopsDB->prefix('news_topics'))) { |
||
| 99 | News\Utility::addField('topic_rssurl VARCHAR( 255 ) NOT NULL', $xoopsDB->prefix('news_topics')); |
||
| 100 | } |
||
| 101 | if (!News\Utility::existField('topic_description', $xoopsDB->prefix('news_topics'))) { |
||
| 102 | News\Utility::addField('topic_description TEXT NOT NULL', $xoopsDB->prefix('news_topics')); |
||
| 103 | } |
||
| 104 | if (!News\Utility::existField('topic_color', $xoopsDB->prefix('news_topics'))) { |
||
| 105 | News\Utility::addField("topic_color varchar(6) NOT NULL default '000000'", $xoopsDB->prefix('news_topics')); |
||
| 106 | } |
||
| 107 | |||
| 108 | // 3) If it does not exists, create the table stories_votedata |
||
| 109 | if (!News\Utility::existTable($xoopsDB->prefix('news_stories_votedata'))) { |
||
| 110 | $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_votedata') . " ( |
||
| 111 | ratingid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
||
| 112 | storyid INT(8) UNSIGNED NOT NULL DEFAULT '0', |
||
| 113 | ratinguser INT(11) NOT NULL DEFAULT '0', |
||
| 114 | rating TINYINT(3) UNSIGNED NOT NULL DEFAULT '0', |
||
| 115 | ratinghostname VARCHAR(60) NOT NULL DEFAULT '', |
||
| 116 | ratingtimestamp INT(10) NOT NULL DEFAULT '0', |
||
| 117 | PRIMARY KEY (ratingid), |
||
| 118 | KEY ratinguser (ratinguser), |
||
| 119 | KEY ratinghostname (ratinghostname), |
||
| 120 | KEY storyid (storyid) |
||
| 121 | ) ENGINE=MyISAM;"; |
||
| 122 | if (!$xoopsDB->queryF($sql)) { |
||
| 123 | echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED3; |
||
| 124 | ++$errors; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // 4) Create the four new fields for the votes in the story table |
||
| 129 | if (!News\Utility::existField('rating', $xoopsDB->prefix('news_stories'))) { |
||
| 130 | News\Utility::addField("rating DOUBLE( 6, 4 ) DEFAULT '0.0000' NOT NULL", $xoopsDB->prefix('news_stories')); |
||
| 131 | } |
||
| 132 | if (!News\Utility::existField('votes', $xoopsDB->prefix('news_stories'))) { |
||
| 133 | News\Utility::addField("votes INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_stories')); |
||
| 134 | } |
||
| 135 | if (!News\Utility::existField('keywords', $xoopsDB->prefix('news_stories'))) { |
||
| 136 | News\Utility::addField('keywords VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories')); |
||
| 137 | } |
||
| 138 | if (!News\Utility::existField('description', $xoopsDB->prefix('news_stories'))) { |
||
| 139 | News\Utility::addField('description VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories')); |
||
| 140 | } |
||
| 141 | if (!News\Utility::existField('pictureinfo', $xoopsDB->prefix('news_stories'))) { |
||
| 142 | News\Utility::addField('pictureinfo VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories')); |
||
| 143 | } |
||
| 144 | if (!News\Utility::existField('subtitle', $xoopsDB->prefix('news_stories'))) { |
||
| 145 | News\Utility::addField('subtitle VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories')); |
||
| 146 | } |
||
| 147 | |||
| 148 | // 5) Add some indexes to the topics table |
||
| 149 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `topic_title` );'); |
||
| 150 | $result = $xoopsDB->queryF($sql); |
||
|
|
|||
| 151 | $sql = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `menu` );'); |
||
| 152 | $result = $xoopsDB->queryF($sql); |
||
| 153 | |||
| 154 | // 6) Make files and folders |
||
| 155 | $dir = XOOPS_ROOT_PATH . '/uploads/news'; |
||
| 156 | if (!@mkdir($dir) && !is_dir($dir)) { |
||
| 157 | throw new \RuntimeException('The directory ' . $dir . ' could not be created.'); |
||
| 158 | } |
||
| 159 | if (!is_writable($dir)) { |
||
| 160 | chmod($dir, 0777); |
||
| 161 | } |
||
| 162 | |||
| 163 | $dir = XOOPS_ROOT_PATH . '/uploads/news/file'; |
||
| 164 | if (!@mkdir($dir) && !is_dir($dir)) { |
||
| 165 | throw new \RuntimeException('The directory ' . $dir . ' could not be created.'); |
||
| 166 | } |
||
| 167 | if (!is_writable($dir)) { |
||
| 168 | chmod($dir, 0777); |
||
| 169 | } |
||
| 170 | |||
| 171 | $dir = XOOPS_ROOT_PATH . '/uploads/news/image'; |
||
| 172 | if (!@mkdir($dir) && !is_dir($dir)) { |
||
| 173 | throw new \RuntimeException('The directory ' . $dir . ' could not be created.'); |
||
| 174 | } |
||
| 175 | if (!is_writable($dir)) { |
||
| 176 | chmod($dir, 0777); |
||
| 177 | } |
||
| 178 | |||
| 179 | // Copy index.html files on uploads folders |
||
| 180 | $indexFile = XOOPS_ROOT_PATH . '/modules/news/include/index.html'; |
||
| 181 | copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/index.html'); |
||
| 182 | copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/file/index.html'); |
||
| 183 | copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/image/index.html'); |
||
| 184 | |||
| 185 | return true; |
||
| 186 | } |
||
| 187 |