| 1 |
#!/usr/bin/php
|
| 2 |
<?php
|
| 3 |
// $Id: makenode.php,v 1.1 2005/11/21 03:30:07 jjeff Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* This shell script allows Drupal nodes to be created from the command line.
|
| 7 |
* Place it in the scripts directory for it to work.
|
| 8 |
*
|
| 9 |
* @example
|
| 10 |
* Example use:
|
| 11 |
* ./makenode.php title="Example Title" body="This is the body." file=myPodcast.mp3
|
| 12 |
*
|
| 13 |
* @param
|
| 14 |
* Script arguments are converted into an $edit array, so the
|
| 15 |
* usual node keys apply.
|
| 16 |
*
|
| 17 |
* There is also a 'file' argument which is the path to an
|
| 18 |
* existing file relative to the 'files' directory. If present,
|
| 19 |
* this file will get attached to the node.
|
| 20 |
*
|
| 21 |
*/
|
| 22 |
|
| 23 |
define ('FORM_VALIDATION_ERROR',1);
|
| 24 |
define ('FILE_NOT_FOUND',2);
|
| 25 |
|
| 26 |
chdir(realpath(dirname($_SERVER['PHP_SELF'])));
|
| 27 |
chdir('../');
|
| 28 |
include_once './includes/bootstrap.inc';
|
| 29 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
|
| 30 |
|
| 31 |
// default arguments
|
| 32 |
$defaults = array(
|
| 33 |
'title' => "",
|
| 34 |
'body' => "",
|
| 35 |
'format' => FILTER_FORMAT_DEFAULT,
|
| 36 |
'status' => 1,
|
| 37 |
'promote' => 1,
|
| 38 |
'type' => 'blog',
|
| 39 |
'comment' => variable_get('comment_'. $edit['type'], 2),
|
| 40 |
'date' => 'now'
|
| 41 |
);
|
| 42 |
|
| 43 |
// parse command line arguments and create $edit array
|
| 44 |
for ($i=1; $i < $argc; $i++) {
|
| 45 |
parse_str($argv[$i], $thisarg);
|
| 46 |
$arguments = array_merge($arguments, $thisarg);
|
| 47 |
}
|
| 48 |
$edit = array_merge($defaults, $arguments);
|
| 49 |
|
| 50 |
var_dump($edit);
|
| 51 |
|
| 52 |
// if there's a file, break it down
|
| 53 |
if ($edit['file']) {
|
| 54 |
$filesdir = variable_get('file_directory_path', 'files') .'/';
|
| 55 |
$filerelpath = $filesdir . $edit['file'];
|
| 56 |
if (file_exists($filerelpath)){
|
| 57 |
$parts = pathinfo($filerelpath);
|
| 58 |
|
| 59 |
$suffix = $parts['extension'];
|
| 60 |
$filebasename = basename($parts['basename'], ".". $suffix);
|
| 61 |
$filename = $parts['basename'];
|
| 62 |
|
| 63 |
// set stuff up for the database
|
| 64 |
$filepath = $edit['file'];
|
| 65 |
$filesize = filesize($filesdir . $filepath);
|
| 66 |
// sniff out the mime-type based on the file's extension
|
| 67 |
$mimes = makenode_mimes();
|
| 68 |
$filemime = $mimes[$suffix];
|
| 69 |
|
| 70 |
// set flag to save to db
|
| 71 |
$filesave = TRUE;
|
| 72 |
}
|
| 73 |
else {
|
| 74 |
fwrite(STDERR, "File not found\n");
|
| 75 |
exit(FILE_NOT_FOUND);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
// if there's no title, but there is a valid file, use the filename as title
|
| 80 |
$edit['title'] = (empty($edit['title']) && $filesave) ? str_replace('_', ' ', $filebasename) : $edit['title'];
|
| 81 |
|
| 82 |
// make a global $user variable for this session
|
| 83 |
global $user;
|
| 84 |
$user = user_load(array('uid' => 1));
|
| 85 |
|
| 86 |
// load the user info for the author
|
| 87 |
$uarray = isset($edit['name']) ? array('name' => $edit['name']) : array('uid' => 1);
|
| 88 |
$account = user_load($uarray);
|
| 89 |
|
| 90 |
// attribute this post to this user
|
| 91 |
$edit['uid'] = $account->uid;
|
| 92 |
$edit['name'] = $account->name;
|
| 93 |
|
| 94 |
//validate for both 4.6 and 4.7
|
| 95 |
if (function_exists('node_execute')){
|
| 96 |
$node = node_execute($edit);
|
| 97 |
node_validate($node);
|
| 98 |
}
|
| 99 |
else {
|
| 100 |
$node = node_validate($edit);
|
| 101 |
}
|
| 102 |
|
| 103 |
if ($errors = form_get_errors()){
|
| 104 |
// I don't know if this is working
|
| 105 |
fwrite(STDERR, strip_tags(implode("\n", $errors))."\n");
|
| 106 |
exit(FORM_VALIDATION_ERROR);
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
//save it
|
| 110 |
node_save($node);
|
| 111 |
fwrite(STDOUT, "node $node->nid created\n");
|
| 112 |
|
| 113 |
if ($filesave){
|
| 114 |
// put it in the database
|
| 115 |
$fid = db_next_id('{files}_fid');
|
| 116 |
db_query("INSERT INTO {files} (fid, vid, nid, filename, filepath, filemime, filesize, list) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, %d)", $fid, $node->nid, $node->vid, $filename, $filepath, $filemime, $filesize, 1);
|
| 117 |
fwrite(STDOUT, "file attached\n");
|
| 118 |
}
|
| 119 |
exit(0);
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* Returns an array of mime types
|
| 124 |
*/
|
| 125 |
|
| 126 |
function makenode_mimes() {
|
| 127 |
return array(
|
| 128 |
'ez' => 'application/andrew-inset',
|
| 129 |
'hqx' => 'application/mac-binhex40',
|
| 130 |
'cpt' => 'application/mac-compactpro',
|
| 131 |
'doc' => 'application/msword',
|
| 132 |
'bin' => 'application/octet-stream',
|
| 133 |
'dms' => 'application/octet-stream',
|
| 134 |
'lha' => 'application/octet-stream',
|
| 135 |
'lzh' => 'application/octet-stream',
|
| 136 |
'exe' => 'application/octet-stream',
|
| 137 |
'class' => 'application/octet-stream',
|
| 138 |
'so' => 'application/octet-stream',
|
| 139 |
'dll' => 'application/octet-stream',
|
| 140 |
'oda' => 'application/oda',
|
| 141 |
'pdf' => 'application/pdf',
|
| 142 |
'ai' => 'application/postscript',
|
| 143 |
'eps' => 'application/postscript',
|
| 144 |
'ps' => 'application/postscript',
|
| 145 |
'smi' => 'application/smil',
|
| 146 |
'smil' => 'application/smil',
|
| 147 |
'mif' => 'application/vnd.mif',
|
| 148 |
'xls' => 'application/vnd.ms-excel',
|
| 149 |
'ppt' => 'application/vnd.ms-powerpoint',
|
| 150 |
'wbxml' => 'application/vnd.wap.wbxml',
|
| 151 |
'wmlc' => 'application/vnd.wap.wmlc',
|
| 152 |
'wmlsc' => 'application/vnd.wap.wmlscriptc',
|
| 153 |
'bcpio' => 'application/x-bcpio',
|
| 154 |
'vcd' => 'application/x-cdlink',
|
| 155 |
'pgn' => 'application/x-chess-pgn',
|
| 156 |
'cpio' => 'application/x-cpio',
|
| 157 |
'csh' => 'application/x-csh',
|
| 158 |
'dcr' => 'application/x-director',
|
| 159 |
'dir' => 'application/x-director',
|
| 160 |
'dxr' => 'application/x-director',
|
| 161 |
'dvi' => 'application/x-dvi',
|
| 162 |
'spl' => 'application/x-futuresplash',
|
| 163 |
'gtar' => 'application/x-gtar',
|
| 164 |
'hdf' => 'application/x-hdf',
|
| 165 |
'js' => 'application/x-javascript',
|
| 166 |
'skp' => 'application/x-koan',
|
| 167 |
'skd' => 'application/x-koan',
|
| 168 |
'skt' => 'application/x-koan',
|
| 169 |
'skm' => 'application/x-koan',
|
| 170 |
'latex' => 'application/x-latex',
|
| 171 |
'nc' => 'application/x-netcdf',
|
| 172 |
'cdf' => 'application/x-netcdf',
|
| 173 |
'sh' => 'application/x-sh',
|
| 174 |
'shar' => 'application/x-shar',
|
| 175 |
'swf' => 'application/x-shockwave-flash',
|
| 176 |
'sit' => 'application/x-stuffit',
|
| 177 |
'sv4cpio' => 'application/x-sv4cpio',
|
| 178 |
'sv4crc' => 'application/x-sv4crc',
|
| 179 |
'tar' => 'application/x-tar',
|
| 180 |
'tcl' => 'application/x-tcl',
|
| 181 |
'tex' => 'application/x-tex',
|
| 182 |
'texinfo' => 'application/x-texinfo',
|
| 183 |
'texi' => 'application/x-texinfo',
|
| 184 |
't' => 'application/x-troff',
|
| 185 |
'tr' => 'application/x-troff',
|
| 186 |
'roff' => 'application/x-troff',
|
| 187 |
'man' => 'application/x-troff-man',
|
| 188 |
'me' => 'application/x-troff-me',
|
| 189 |
'ms' => 'application/x-troff-ms',
|
| 190 |
'ustar' => 'application/x-ustar',
|
| 191 |
'src' => 'application/x-wais-source',
|
| 192 |
'xhtml' => 'application/xhtml+xml',
|
| 193 |
'xht' => 'application/xhtml+xml',
|
| 194 |
'zip' => 'application/zip',
|
| 195 |
'au' => 'audio/basic',
|
| 196 |
'snd' => 'audio/basic',
|
| 197 |
'mid' => 'audio/midi',
|
| 198 |
'midi' => 'audio/midi',
|
| 199 |
'kar' => 'audio/midi',
|
| 200 |
'mpga' => 'audio/mpeg',
|
| 201 |
'mp2' => 'audio/mpeg',
|
| 202 |
'mp3' => 'audio/mpeg',
|
| 203 |
'aif' => 'audio/x-aiff',
|
| 204 |
'aiff' => 'audio/x-aiff',
|
| 205 |
'aifc' => 'audio/x-aiff',
|
| 206 |
'm3u' => 'audio/x-mpegurl',
|
| 207 |
'ram' => 'audio/x-pn-realaudio',
|
| 208 |
'rm' => 'audio/x-pn-realaudio',
|
| 209 |
'rpm' => 'audio/x-pn-realaudio-plugin',
|
| 210 |
'ra' => 'audio/x-realaudio',
|
| 211 |
'wav' => 'audio/x-wav',
|
| 212 |
'pdb' => 'chemical/x-pdb',
|
| 213 |
'xyz' => 'chemical/x-xyz',
|
| 214 |
'bmp' => 'image/bmp',
|
| 215 |
'gif' => 'image/gif',
|
| 216 |
'ief' => 'image/ief',
|
| 217 |
'jpeg' => 'image/jpeg',
|
| 218 |
'jpg' => 'image/jpeg',
|
| 219 |
'jpe' => 'image/jpeg',
|
| 220 |
'png' => 'image/png',
|
| 221 |
'tiff' => 'image/tiff',
|
| 222 |
'tif' => 'image/tiff',
|
| 223 |
'djvu' => 'image/vnd.djvu',
|
| 224 |
'djv' => 'image/vnd.djvu',
|
| 225 |
'wbmp' => 'image/vnd.wap.wbmp',
|
| 226 |
'ras' => 'image/x-cmu-raster',
|
| 227 |
'pnm' => 'image/x-portable-anymap',
|
| 228 |
'pbm' => 'image/x-portable-bitmap',
|
| 229 |
'pgm' => 'image/x-portable-graymap',
|
| 230 |
'ppm' => 'image/x-portable-pixmap',
|
| 231 |
'rgb' => 'image/x-rgb',
|
| 232 |
'xbm' => 'image/x-xbitmap',
|
| 233 |
'xpm' => 'image/x-xpixmap',
|
| 234 |
'xwd' => 'image/x-xwindowdump',
|
| 235 |
'igs' => 'model/iges',
|
| 236 |
'iges' => 'model/iges',
|
| 237 |
'msh' => 'model/mesh',
|
| 238 |
'mesh' => 'model/mesh',
|
| 239 |
'silo' => 'model/mesh',
|
| 240 |
'wrl' => 'model/vrml',
|
| 241 |
'vrml' => 'model/vrml',
|
| 242 |
'css' => 'text/css',
|
| 243 |
'html' => 'text/html',
|
| 244 |
'htm' => 'text/html',
|
| 245 |
'asc' => 'text/plain',
|
| 246 |
'txt' => 'text/plain',
|
| 247 |
'rtx' => 'text/richtext',
|
| 248 |
'rtf' => 'text/rtf',
|
| 249 |
'sgml' => 'text/sgml',
|
| 250 |
'sgm' => 'text/sgml',
|
| 251 |
'tsv' => 'text/tab-separated-values',
|
| 252 |
'wml' => 'text/vnd.wap.wml',
|
| 253 |
'wmls' => 'text/vnd.wap.wmlscript',
|
| 254 |
'etx' => 'text/x-setext',
|
| 255 |
'xsl' => 'text/xml',
|
| 256 |
'xml' => 'text/xml',
|
| 257 |
'mpeg' => 'video/mpeg',
|
| 258 |
'mpg' => 'video/mpeg',
|
| 259 |
'mpe' => 'video/mpeg',
|
| 260 |
'qt' => 'video/quicktime',
|
| 261 |
'mov' => 'video/quicktime',
|
| 262 |
'mxu' => 'video/vnd.mpegurl',
|
| 263 |
'avi' => 'video/x-msvideo',
|
| 264 |
'movie' => 'video/x-sgi-movie',
|
| 265 |
'ice' => 'x-conference/x-cooltalk',
|
| 266 |
'asf' => 'video/x-ms-asf',
|
| 267 |
'asx' => 'video/x-ms-asf',
|
| 268 |
'wma' => 'audio/x-ms-wma',
|
| 269 |
'wax' => 'audio/x-ms-wax',
|
| 270 |
'wmv' => 'video/x-ms-wmv',
|
| 271 |
'wvx' => 'video/x-ms-wvx',
|
| 272 |
'wm' => 'video/x-ms-wm',
|
| 273 |
'wmx' => 'video/x-ms-wmx',
|
| 274 |
'wmz' => 'application/x-ms-wmz',
|
| 275 |
'wmd' => 'application/x-ms-wmd',
|
| 276 |
'm4a' => 'audio/x-m4a',
|
| 277 |
'm4p' => 'audio/x-m4p',
|
| 278 |
'mp4' => 'video/mpeg'
|
| 279 |
);
|
| 280 |
}
|
| 281 |
|
| 282 |
|
| 283 |
?>
|