what is the source for the jewish custom that a mourner should get an aliyah or maftir if he is in the synagogue
This custom dates back thousands of years. It is based on the Medrash which tells of Rebbi Akiva who met a deceased soul who was suffering from Gehinom. The sould instructed Rebbi Akiva that his salvation would be if he had a son who would learn and daven, and specifically say “Barchu es Hashem Hamevorach”, which is understood to mean an aliyah to Torah. The main custom is to receive an aliyah the week before the yortzeit or on the yortzeit itself, but many try to receive throughout the year of aveilus.
The general idea is that any additional Tefilla, Torah, Tzedaka or any othe rmitzvah performed by the offspring brings great merit to the deceased in Heaven.
See Menoras Hameor [Ner 1 Klal 1 Part 2 Chapter 1] in the name of the Medrash Tanchuma in Parshas Noach. See Gesher Hachaim Chapter 30, Leket Yosher pg. 99.