أنا جديد على Drush. كيف يمكنني تنفيذ هذا البرنامج النصي لإزالة تعليقات مستخدم معين؟
$uid = xx // the spam users id;
$query = db_query("SELECT cid FROM {comments} WHERE uid = %d", $uid);
while($cid = db_result($query)) {
comment_delete($cid);
}
سيكون من الرائع أيضًا أن تخبرني عن كيفية إكمال البرنامج النصي بحيث يأخذ اسم المستخدم بدلاً من $ uid.
شكر
يمكنك تحويل البرنامج النصي في Drush Shell script .
البرنامج النصي Shell drush هو أي ملف نصي لـ Unix Shell يحتوي على مجموعة بت "تنفيذ" (أي عبر
chmod +x myscript.drush
) وهذا يبدأ بخط معين:#!/usr/bin/env drush
أو
#!/full/path/to/drush
مخطوطات Drush أفضل من نصوص Bash للأسباب التالية:
ما يلي هو المثال الذي يمكنك أن تجده في helloword.script .
#!/usr/bin/env drush
//
// This example demonstrates how to write a drush
// "Shebang" script. These scripts start with the
// line "#!/usr/bin/env drush" or "#!/full/path/to/drush".
//
// See `drush topic docs-scripts` for more information.
//
drush_print("Hello world!");
drush_print();
drush_print("The arguments to this command were:");
//
// If called with --everything, use drush_get_arguments
// to print the commandline arguments. Note that this
// call will include 'php-script' (the drush command)
// and the path to this script.
//
if (drush_get_option('everything')) {
drush_print(" " . implode("\n ", drush_get_arguments()));
}
//
// If --everything is not included, then use
// drush_shift to pull off the arguments one at
// a time. drush_shift only returns the user
// commandline arguments, and does not include
// the drush command or the path to this script.
//
else {
while ($arg = drush_shift()) {
drush_print(' ' . $arg);
}
}
drush_print();
يمكنك جعل البرنامج النصي قابلاً للتنفيذ ، بحيث يمكنك تنفيذه باستخدام <script file> <parameters>
أين <script name>
هو اسم البرنامج النصي و <parameters>
هي المعلمات التي تم تمريرها إلى البرنامج النصي ؛ إذا كان النص غير قابل للتنفيذ ، يمكنك تسميته بـ drush <script name> <parameters>
.
باستخدام drush php-eval
، يمكنك تشغيل البرنامج النصي الخاص بك دون الحاجة إلى حفظه في ملف أولاً:
drush php-eval '
$uid = 1234;
$query = db_query("SELECT cid FROM {comments} WHERE uid = %d", $uid);
while($cid = db_result($query)) {
comment_delete($cid);
}
'
يستخدم هذا علامات الاقتباس المتداخلة ، لذا لمنع حدوث فوضى ، أوصي باستخدام علامات الاقتباس المزدوجة فقط "
داخل الرمز PHP.
أعتقد أنك تنظر إلى drush -d scr --uri=example.org sample_script.php
لتنفيذ sample_script.php.
يمكننا استخدام drush php-script script_name
لامتصاص ملف php في Drush.
للمساعدة المتعلقة بـ Drush لتنفيذ ملفات php ، اكتب Drush php-script --help
قائمة الأوامر
ملحوظة: لقد قمت بوضع php scirpt في المجلد الرئيسي لـ Drupal
يمكنك تشغيل سكربت php عن طريق drush scr ~/sample.php
.
في سطر الأوامر الخاص بك ، من أي مكان ، قم بتشغيل:
$ drush --root=/path/to/drupal-installation --uri=youdomain.com scr /path/to/your/script.php
في حال كنت بالفعل في/path/to/drupal-install فقط قم بتشغيل:
$ drush --uri=youdomain.com scr /path/to/your/script.php
في حال كنت متقدمًا على /path/to/drupal-installation/sites/youdomain.com أكثر من مجرد تشغيل:
$ drush scr /path/to/your/script.php
ملف script.php الخاص بك:
<?php
// Not always needed but sometimes you might have to first login as an administrator.
$admin_uid = 1;
$form_state = array('uid' => $admin_uid);
user_login_submit(array(), $form_state);
// Now the logged in user global $user object become available.
global $user;
print_r($user);
// Do whatever you want here.
يرجى ملاحظة أن db_result
تمت إزالته في Drupal 7. يمكن تغيير الرمز أعلاه إلى:
$result = db_query($query);
foreach($result as $cid) {
comment_delete($cid);
}
إذا كنت تريد استخدام اسم المستخدم بدلاً من uid ، فيمكنك الحصول على اسم المستخدم باستخدام هذا:
$username = user_load($uid)->name;