Page MenuHomePhabricator

category_redirect script of Pywikibot is not working as it's documented
Open, MediumPublicBUG REPORT

Description

This category_redirect script is an amazing script of Pywikibot, but I just realized that it is not working as it's documented. Issues are:

  • After running the script, it's saving pages immediately without prompt the user to allow or skip the page.
  • While pressing Ctrl+C and interrupting the script, we get Traceback (most recent call last):... instead of the execution, read and write calculations.
  • Typo: recentyrecently
  • This is the doc of the script and indicates that it will do some good functions, but it is just doing those parts I have bolded here: The bot will look for categories that are marked with a category redirect template, take the first parameter of the template as the target of the redirect, and move all pages and subcategories of the category there. It also changes hard redirects into soft redirects, and fixes double redirects. A log is written under <userpage>/category_redirect_log. Only category pages that haven't been edited for a certain cooldown period (currently 7 days) are taken into account.
  • I have localized more than 10 messages of this script into ckb a while ago, but they are still printed in English. Please update the i18n.

In the doc point I haven't tested everything, but the thing I'm sure is that the script doesn't moving pages/subcategories of to the redirected page. All I have seen from the script is just like this edit. That is all I have noticed. Thanks!

Event Timeline

Xqt triaged this task as Medium priority.Dec 3 2023, 10:02 AM
Xqt subscribed.
This comment was removed by Xqt.

Change 979673 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [doc] Fix typo in category_redirect.py

https://gerrit.wikimedia.org/r/979673

Change 979673 merged by jenkins-bot:

[pywikibot/core@master] [doc] Fix typo in category_redirect.py

https://gerrit.wikimedia.org/r/979673

I have localized more than 10 messages of this script into ckb a while ago, but they are still printed in English.

@Aram: Could you please give an example.

@Aram: Could you please give an example.

Sure. If you mean an edit summary sample, I provided this one already here. And If you mean the translations, see here; actually, all messages are translated.

@Aram: Cannot reproduce the i18n issue. After adding print statements I get:

D:\pwb\GIT\core>pwb -simulate category_redirect -lang:ckb
Checking hard-redirect category pages.
بۆت: زیادکردنی داڕێژەی ڕەوانەکەری پۆل بۆ چاکسازی
@@ -1 +1 @@
- #ڕەوانەکەر [[:پۆل:ئابووریی بەرھەمھێنان]]
+ {{ڕەوانەکەری پۆل|ئابووریی بەرھەمھێنان}}

SIMULATION: edit action blocked.
Page [[پۆل:ئابووریی بەرهەمهێنان]] saved

Please ensure that you updated submodule if you are using git or svn repository.

@Aram: Cannot reproduce the i18n issue. After adding print statements I get:

D:\pwb\GIT\core>pwb -simulate category_redirect -lang:ckb
Checking hard-redirect category pages.
بۆت: زیادکردنی داڕێژەی ڕەوانەکەری پۆل بۆ چاکسازی
@@ -1 +1 @@
- #ڕەوانەکەر [[:پۆل:ئابووریی بەرھەمھێنان]]
+ {{ڕەوانەکەری پۆل|ئابووریی بەرھەمھێنان}}

SIMULATION: edit action blocked.
Page [[پۆل:ئابووریی بەرهەمهێنان]] saved

Please ensure that you updated submodule if you are using git or svn repository.

@Xqt, The code you showed me is correct. I tried updating my Pywikibot directory (which is stable version) via git and it was successful, but the i18n submodule wouldn't update, anyway, I updated it manually and here's an example.

@Xqt, The code you showed me is correct. I tried updating my Pywikibot directory (which is stable version) via git and it was successful, but the i18n submodule wouldn't update, anyway, I updated it manually and here's an example.

I think submodule has to be updated additional to the Pywikibot branch or in one command like git pull --recurse-submodules. But I am not familiar enough with git, sorry.

@Xqt, The code you showed me is correct. I tried updating my Pywikibot directory (which is stable version) via git and it was successful, but the i18n submodule wouldn't update, anyway, I updated it manually and here's an example.

I think submodule has to be updated additional to the Pywikibot branch or in one command like git pull --recurse-submodules. But I am not familiar enough with git, sorry.

You are right. Even if I run that command, it says:

Fetching submodule scripts/i18n
Already up to date.

But it is not so. I don't know why. Let other Pywikibot users try to update. If this file does not update for them, then there is obviously a problem on Pywikibot side. If it's updated, then the problem is on my side. Thank you for your concerns about me!

Change 979900 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [IMPR] Split CategoryRedirectBot.run()

https://gerrit.wikimedia.org/r/979900

Pages are moved if the user has this rights:

>>> p?l:y'abwwr l? 1936 <<<
SIMULATION: edit action blocked.
Page [[p?l:k?mpanyakan? dam?zrany 1936]] saved
[[ckb:p?l:y'abwwr l? 1936]]: 1 found, 1 moved
SIMULATION: edit action blocked.
Page [[p?l:y'abwwr l? 1936]] saved


>>> p?l:y'abwwry y'a?manya <<<
SIMULATION: edit action blocked.
Page [[p?l:zanst w t?kn?l?zhya l? y'??manya]] saved
SIMULATION: edit action blocked.
Page [[p?l:k?mpanyakany y'??manya]] saved
[[ckb:p?l:y'abwwry y'a?manya]]: 2 found, 2 moved
SIMULATION: edit action blocked.
Page [[p?l:y'abwwry y'a?manya]] saved

With the last patch a log will be written to <userpage>/category_edit_requests if moving fails.

The log is also writen:

>>> p?l:khwly yan? pa??wan?kany y'?wrwwpa <<<

KeyboardInterrupt during CategoryRedirectBot bot run...
SIMULATION: edit action blocked.
Page [[b?kar??n?r:Xqtest/category redirect log]] saved

Double redirects are also solved. If not there may be a problem with the category redirect template which is not registered as such.
@Aram: Can You give an example where double soft category redirects are not solved?

@Aram: Are you able to test the patch?

@Xqt, After updating scripts/category_redirect.py with your last edit and running the script, looks like it is now modifying the old categories to new ones as you can see in this edit. The diff, edit summary and prompt were showed. But the script firstly, Checking hard-redirect category pages and after interrupting the script, it says:

User quit CategoryRedirectBot bot run...
Continue with soft category redirects ([y]es, [n]o):

I said yes, and this is the next lines:

Checking 52 category redirect pages
Page [[پۆل:ئافرەتان لە ئاسیا]] saved

Moving pages out of 22 redirected categories.
Retrieving 22 pages from wikipedia:ckb.


>>> پۆل:ئابوور بە پێی وڵات <<<
Sleeping for 6.7 seconds, 2023-12-08 19:08:13
Page [[تی دی کەنەدا ترەست]] saved
Sleeping for 8.6 seconds, 2023-12-08 19:08:21
Page [[بەکارھێنەر:AramBot/category redirect log]] saved

59 read operations
1 write operation
1 touch operation
Execution time: 269 seconds
Read operation time: 4.6 seconds
Write operation time: 269.0 seconds
Touch operation time: 269.0 seconds
Script terminated by exception:

ERROR: 'CategoryRedirectBot' object has no attribute 'Counter' (AttributeError)
Traceback (most recent call last):
  File "C:\Pywikibot master\pwb.py", line 39, in <module>
    sys.exit(main())
  File "C:\Pywikibot master\pwb.py", line 35, in main
    runpy.run_path(str(path), run_name='__main__')
  File "C:\Users\Aram\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\Aram\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\Aram\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 515, in <module>
    main()
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 499, in main
    if not execute():
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 486, in execute
    run_python_file(filename, script_args, module)
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 147, in run_python_file
    exec(compile(source, filename, 'exec', dont_inherit=True),
  File "C:\Pywikibot master\scripts\category_redirect.py", line 570, in <module>
    main()
  File "C:\Pywikibot master\scripts\category_redirect.py", line 566, in main
    bot.run()
  File "C:\Pywikibot master\scripts\category_redirect.py", line 513, in run
    super().run()
  File "C:\Pywikibot master\pywikibot\bot.py", line 1653, in run
    self.treat(page)
  File "C:\Pywikibot master\pywikibot\bot.py", line 1906, in treat
    self.treat_page()
  File "C:\Pywikibot master\scripts\category_redirect.py", line 470, in check_soft_redirect
    found, moved = self.move_contents(
  File "C:\Pywikibot master\scripts\category_redirect.py", line 174, in move_contents
    self.Counter['move'] += moved
AttributeError: 'CategoryRedirectBot' object has no attribute 'Counter'. Did you mean: 'counter'?
CRITICAL: Exiting due to uncaught exception AttributeError: 'CategoryRedirectBot' object has no attribute 'Counter'

This is the log till now. It added the template here, but didn't moved subcategories. Also logged the template added to other categories, but I can't see the template at at all. Till now, I have never faced double redirects. I hope my comment was useful, not annoying you.

@Aram: Thank you for reviewing, I fixed that bug above and it should work now. Could you please check?

@Xqt, Thank you! Much closer to perfect this time. The bot has made some good edits and this is the log revision, but in the code/log we have symbol , but this is for LTR languages and it is better if we print for RTL languages (if applicable).

The script is now working, but if we want to make it more better and dig more this ticket, I have noticed two more things. As you can see in below and if we press Ctrl+C after both questions (specifically the second question), we get these lines.

Do you want to accept these changes? ([y]es, [N]o, [a]ll, [q]uit):
User quit CategoryRedirectBot bot run...
Continue with soft category redirects ([y]es, [n]o): Traceback (most recent call last):
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_base.py", line 411, in _input_reraise_cntl_c
    text = self._raw_input()
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_win32.py", line 68, in _raw_input
    data = self.stdin.readline()
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Pywikibot master\pwb.py", line 39, in <module>
    sys.exit(main())
  File "C:\Pywikibot master\pwb.py", line 35, in main
    runpy.run_path(str(path), run_name='__main__')
  File "C:\Users\Aram\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\Aram\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\Aram\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 515, in <module>
    main()
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 499, in main
    if not execute():
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 486, in execute
    run_python_file(filename, script_args, module)
  File "C:\Pywikibot master\pywikibot\scripts\wrapper.py", line 147, in run_python_file
    exec(compile(source, filename, 'exec', dont_inherit=True),
  File "C:\Pywikibot master\scripts\category_redirect.py", line 490, in <module>
    main()
  File "C:\Pywikibot master\scripts\category_redirect.py", line 488, in main
    bot.run()
  File "C:\Pywikibot master\scripts\category_redirect.py", line 432, in run
    or not pywikibot.input_yn(
  File "C:\Pywikibot master\pywikibot\bot.py", line 614, in input_yn
    return input_choice(question, [('Yes', 'y'), ('No', 'n')],
  File "C:\Pywikibot master\pywikibot\bot.py", line 536, in wrapper
    return function(*args, **kwargs)
  File "C:\Pywikibot master\pywikibot\bot.py", line 584, in input_choice
    return ui.input_choice(question, answers, default, return_shortcut,
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_base.py", line 489, in input_choice
    answer = self.input(output) or default
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_base.py", line 391, in input
    text = self._input_reraise_cntl_c(password)
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_base.py", line 413, in _input_reraise_cntl_c
    raise QuitKeyboardInterrupt()
pywikibot.bot_choice.QuitKeyboardInterrupt
CRITICAL: Exiting due to uncaught exception QuitKeyboardInterrupt:

And if press q for the first question and n for the second one, (while the bot has no made any changes), it is still add an empty log as you can see here, but I'm not sure if this action is necessary.

I have three more questions:

  1. Why the bot moves old categories to new ones in multiple edits? Can't we moving all categories in a specific page using one edit only? For example, here, the bot fixed two categories in two edits.
  2. If we run the bot on Toolforge and used -always parameter, what happens in the second question Continue with soft category redirects ([y]es, [n]o):
  3. Why page generators are not allowed in this script? Sometimes, the operator may need to run the script just for one page or category.

I haven't faced double redirects yet.

but in the code/log we have symbol , but this is for LTR languages and it is better if we print for RTL languages (if applicable)

I haven't found any in the code but in the i18n at translatewiki and this should be changed there.

if we press Ctrl+C after both questions (specifically the second question), we get these lines.

Do you want to accept these changes? ([y]es, [N]o, [a]ll, [q]uit):
User quit CategoryRedirectBot bot run...
Continue with soft category redirects ([y]es, [n]o): Traceback (most recent call last):
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_base.py", line 411, in _input_reraise_cntl_c
    text = self._raw_input()
  File "C:\Pywikibot master\pywikibot\userinterfaces\terminal_interface_win32.py", line 68, in _raw_input
    data = self.stdin.readline()
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

This is intentional because you pressed Ctrl-C instead of choosing N for the question Continue with soft category redirects ([y]es, [n]o)

if press q for the first question and n for the second one, (while the bot has no made any changes), it is still add an empty log

This is a good catch and I am working on it.

  1. Why the bot moves old categories to new ones in multiple edits

We currently have no other implementation except change_category. Maybe it it a feature request but it is not trivial to implement it.

  1. what happens in the second question:

With -always option hard and soft category checks are processed without any questions. If hard category check is interrupted (Ctrl-C) the following soft redirect check is not done.

  1. Why page generators are not allowed in this script.

I think this is a feature request and can be done in a separate patch (T353282)

I haven't found any in the code but in the i18n at translatewiki and this should be changed there.

No, the symbol is in the category_redirect.py and not related to translatewiki.net. It is in self.newredirs.append('*# {} → {}'.format(.

This is intentional because you pressed Ctrl-C instead of choosing N for the question Continue with soft category redirects ([y]es, [n]o)

But in case of working on hard redirects and Ctrl-C for soft redirects, we need to print the execution, read, write and skip statistics rather than just quitting the script.

This is a good catch and I am working on it.

Thanks! This is now solved.

We currently have no other implementation except change_category. Maybe it it a feature request but it is not trivial to implement it.

If we collect all modifications and publish all in just one edit per page, we save the history pages from expanding.

With -always option hard and soft category checks are processed without any questions. If hard category check is interrupted (Ctrl-C) the following soft redirect check is not done.

I'm not sure about this, but I think we have to let the operator to just work on the soft redirects only and skip the hard redirects if they wanted.

I think this is a feature request and can be done in a separate patch (T353282)

I used the new option -category and it didn't work. But wait, I meant that the operator should be able to access the Page Generators parameters, not a special option. What if we wanted to work on a list of categories? The Page Generators options are the best and in my opinion there is no need for separate parameters.

Change 979900 merged by jenkins-bot:

[pywikibot/core@master] [IMPR] improvements for CategoryRedirectBot

https://gerrit.wikimedia.org/r/979900